Skip to content

Commit 00d4456

Browse files
v.what.rast: Add test file (#6109)
* v.what.rast: add test file * fix qsort ordering by row and then col
1 parent 2401877 commit 00d4456

File tree

4 files changed

+123
-8
lines changed

4 files changed

+123
-8
lines changed

vector/v.what.rast/local_proto.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,6 @@ struct order {
1111
};
1212

1313
/* search.c */
14-
int by_row(const void *, const void *);
14+
int by_row_col(const void *, const void *);
1515
int by_cat(const void *, const void *);
1616
int srch_cat(const void *, const void *);

vector/v.what.rast/main.c

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -304,8 +304,8 @@ int main(int argc, char *argv[])
304304
if (nocat_cnt)
305305
G_warning(_("%d points without category were skipped"), nocat_cnt);
306306

307-
/* Sort cache by current region row */
308-
qsort(cache, point_cnt, sizeof(struct order), by_row);
307+
/* Sort cache by current region row and col */
308+
qsort(cache, point_cnt, sizeof(struct order), by_row_col);
309309

310310
/* Allocate space for raster row */
311311
if (out_type == CELL_TYPE)

vector/v.what.rast/search.c

Lines changed: 5 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,14 +1,14 @@
11
#include "local_proto.h"
22

3-
/* for qsort, order list by row */
4-
int by_row(const void *ii, const void *jj)
3+
/* for qsort, order list by row and col */
4+
int by_row_col(const void *ii, const void *jj)
55
{
66
const struct order *i = ii, *j = jj;
77

8-
if (i->row < j->row)
9-
return -1;
8+
if (i->row != j->row)
9+
return i->row - j->row;
1010

11-
return (i->row > j->row);
11+
return (i->col - j->col);
1212
}
1313

1414
/* for qsort, order list by cat */
Lines changed: 115 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,115 @@
1+
from grass.gunittest.case import TestCase
2+
from grass.gunittest.main import test
3+
from grass.script.core import read_command
4+
5+
6+
class TestVWhatRast(TestCase):
7+
"""Unit tests for the v.what.rast module"""
8+
9+
test_raster = "test_raster"
10+
float_raster = "float_raster"
11+
test_vector = "test_vector"
12+
13+
@classmethod
14+
def setUpClass(cls):
15+
"""Set up test environment"""
16+
cls.use_temp_region()
17+
cls.runModule("g.region", s=0, n=5, w=0, e=5, res=1)
18+
cls.runModule(
19+
"r.mapcalc",
20+
expression=(f"{cls.test_raster} = if(col() < 5, col(), null())"),
21+
)
22+
cls.runModule(
23+
"r.mapcalc",
24+
expression=(f"{cls.float_raster} = if(col() < 5, col() / 2., 4.5)"),
25+
)
26+
cls.runModule("v.mkgrid", map=f"{cls.test_vector}", grid=[5, 5], type="point")
27+
28+
@classmethod
29+
def tearDownClass(cls):
30+
"""Clean up test environment"""
31+
cls.del_temp_region()
32+
cls.runModule(
33+
"g.remove",
34+
flags="f",
35+
type="raster",
36+
name=[cls.test_raster, cls.float_raster],
37+
)
38+
cls.runModule(
39+
"g.remove",
40+
flags="f",
41+
type="vector",
42+
name=[cls.test_vector],
43+
)
44+
45+
def test_plain_output_int(self):
46+
"""Verify plain text output with integer map."""
47+
result = read_command(
48+
"v.what.rast", map=self.test_vector, raster=self.test_raster, flags="p"
49+
).splitlines()
50+
expected = [
51+
"21|1",
52+
"22|2",
53+
"23|3",
54+
"24|4",
55+
"25|*",
56+
"16|1",
57+
"17|2",
58+
"18|3",
59+
"19|4",
60+
"20|*",
61+
"11|1",
62+
"12|2",
63+
"13|3",
64+
"14|4",
65+
"15|*",
66+
"6|1",
67+
"7|2",
68+
"8|3",
69+
"9|4",
70+
"10|*",
71+
"1|1",
72+
"2|2",
73+
"3|3",
74+
"4|4",
75+
"5|*",
76+
]
77+
self.assertEqual(result, expected)
78+
79+
def test_plain_output_float(self):
80+
"""Verify plain text output with float map."""
81+
result = read_command(
82+
"v.what.rast", map=self.test_vector, raster=self.float_raster, flags="p"
83+
).splitlines()
84+
expected = [
85+
"21|0.5",
86+
"22|1",
87+
"23|1.5",
88+
"24|2",
89+
"25|4.5",
90+
"16|0.5",
91+
"17|1",
92+
"18|1.5",
93+
"19|2",
94+
"20|4.5",
95+
"11|0.5",
96+
"12|1",
97+
"13|1.5",
98+
"14|2",
99+
"15|4.5",
100+
"6|0.5",
101+
"7|1",
102+
"8|1.5",
103+
"9|2",
104+
"10|4.5",
105+
"1|0.5",
106+
"2|1",
107+
"3|1.5",
108+
"4|2",
109+
"5|4.5",
110+
]
111+
self.assertEqual(result, expected)
112+
113+
114+
if __name__ == "__main__":
115+
test()

0 commit comments

Comments
 (0)