1 //
2 // Test Suite for C-API GEOSEquals
3
4 #include <tut/tut.hpp>
5 // geos
6 #include <geos_c.h>
7 // std
8 #include <cstdarg>
9 #include <cstdio>
10 #include <cstdlib>
11 #include <cstring>
12
13 namespace tut {
14 //
15 // Test Group
16 //
17
18 // Common data used in test cases.
19 struct test_capigeosequals_data {
20 GEOSGeometry* geom1_;
21 GEOSGeometry* geom2_;
22
23 static void
noticetut::test_capigeosequals_data24 notice(const char* fmt, ...)
25 {
26 std::fprintf(stdout, "NOTICE: ");
27
28 va_list ap;
29 va_start(ap, fmt);
30 std::vfprintf(stdout, fmt, ap);
31 va_end(ap);
32
33 std::fprintf(stdout, "\n");
34 }
35
test_capigeosequals_datatut::test_capigeosequals_data36 test_capigeosequals_data()
37 : geom1_(nullptr), geom2_(nullptr)
38 {
39 initGEOS(notice, notice);
40 }
41
~test_capigeosequals_datatut::test_capigeosequals_data42 ~test_capigeosequals_data()
43 {
44 GEOSGeom_destroy(geom1_);
45 GEOSGeom_destroy(geom2_);
46 geom1_ = nullptr;
47 geom2_ = nullptr;
48 finishGEOS();
49 }
50
51 };
52
53 typedef test_group<test_capigeosequals_data> group;
54 typedef group::object object;
55
56 group test_capigeosequals_group("capi::GEOSEquals");
57
58 //
59 // Test Cases
60 //
61
62 template<>
63 template<>
test()64 void object::test<1>
65 ()
66 {
67 geom1_ = GEOSGeomFromWKT("POLYGON EMPTY");
68 geom2_ = GEOSGeomFromWKT("POLYGON EMPTY");
69
70 ensure(nullptr != geom1_);
71 ensure(nullptr != geom2_);
72
73 char const r1 = GEOSEquals(geom1_, geom2_);
74
75 ensure_equals(r1, 1);
76
77 char const r2 = GEOSEquals(geom2_, geom1_);
78
79 ensure_equals(r2, 1);
80 }
81
82 template<>
83 template<>
test()84 void object::test<2>
85 ()
86 {
87 geom1_ = GEOSGeomFromWKT("POINT(2 3)");
88 geom2_ = GEOSGeomFromWKT("POINT(2 2)");
89
90 ensure(nullptr != geom1_);
91 ensure(nullptr != geom2_);
92
93 char const r1 = GEOSEquals(geom1_, geom2_);
94
95 ensure_equals(int(r1), 0);
96
97 char const r2 = GEOSEquals(geom2_, geom1_);
98
99 ensure_equals(int(r2), 0);
100 }
101
102 template<>
103 template<>
test()104 void object::test<3>
105 ()
106 {
107 geom1_ = GEOSGeomFromWKT("MULTIPOLYGON(((0 0,0 10,10 10,10 0,0 0)))");
108 geom2_ = GEOSGeomFromWKT("POLYGON((0 0,0 10,10 10,10 0,0 0))");
109
110 ensure(nullptr != geom1_);
111 ensure(nullptr != geom2_);
112
113 char const r1 = GEOSEquals(geom1_, geom2_);
114
115 ensure_equals(int(r1), 1);
116
117 char const r2 = GEOSEquals(geom2_, geom1_);
118
119 ensure_equals(int(r2), 1);
120 }
121
122 // This is a test for bug #357 (GEOSEquals with nan coords)
123 template<>
124 template<>
test()125 void object::test<4>
126 ()
127 {
128 GEOSCoordSequence* cs = GEOSCoordSeq_create(5, 2);
129
130 constexpr double nan = std::numeric_limits<double>::quiet_NaN();
131 GEOSCoordSeq_setX(cs, 0, 1);
132 GEOSCoordSeq_setY(cs, 0, 1);
133 for(unsigned int i = 1; i < 4; ++i) {
134 GEOSCoordSeq_setX(cs, i, nan);
135 GEOSCoordSeq_setY(cs, i, nan);
136 }
137 GEOSCoordSeq_setX(cs, 4, 1);
138 GEOSCoordSeq_setY(cs, 4, 1);
139
140 geom1_ = GEOSGeom_createPolygon(GEOSGeom_createLinearRing(cs),
141 nullptr, 0);
142
143 char const r1 = GEOSEquals(geom1_, geom1_);
144
145 ensure_equals(int(r1), 2);
146
147 }
148
149 // This is a test for bug #357 (GEOSEquals with inf coords)
150 template<>
151 template<>
test()152 void object::test<5>
153 ()
154 {
155 const char* hex =
156 "0103000020E61000000100000005000000737979F3DDCC2CC0F92154F9E7534540000000000000F07F000000000000F07F8F806E993F7E55C0304B29FFEA8554400634E8D1DD424540B5FEE6A37FCD4540737979F3DDCC2CC0F92154F9E7534540";
157
158 geom1_ = GEOSGeomFromHEX_buf((unsigned char*)hex, std::strlen(hex));
159
160 ensure(nullptr != geom1_);
161
162 char const r1 = GEOSEquals(geom1_, geom1_);
163
164 ensure_equals(int(r1), 2);
165
166 }
167
168 #if 0 // fails
169 // This is a test for bug #752 (GEOSEquals with collection)
170 template<>
171 template<>
172 void object::test<6>
173 ()
174 {
175 const char* wkt1 = "GEOMETRYCOLLECTION(POLYGON ("
176 "(0 0, 0 4, 4 4, 4 0, 0 0)"
177 "),"
178 "POLYGON ("
179 "(3 3, 3 4, 4 4, 4 3, 3 3)"
180 "))";
181
182 geom1_ = GEOSGeomFromWKT(wkt1);
183
184 ensure(0 != geom1_);
185
186 char const r1 = GEOSEquals(geom1_, geom1_);
187
188 ensure_equals(int(r1), 1);
189 }
190 #endif
191
192
193 } // namespace tut
194
195