1 //
2 // Test Suite for C-API GEOSIntersects
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_capigeosintersects_data {
20     GEOSGeometry* geom1_;
21     GEOSGeometry* geom2_;
22 
23     static void
noticetut::test_capigeosintersects_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_capigeosintersects_datatut::test_capigeosintersects_data36     test_capigeosintersects_data()
37         : geom1_(nullptr), geom2_(nullptr)
38     {
39         initGEOS(notice, notice);
40     }
41 
~test_capigeosintersects_datatut::test_capigeosintersects_data42     ~test_capigeosintersects_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_capigeosintersects_data> group;
54 typedef group::object object;
55 
56 group test_capigeosintersects_group("capi::GEOSIntersects");
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 = GEOSIntersects(geom1_, geom2_);
74 
75     ensure_equals(r1, 0);
76 
77     char const r2 = GEOSIntersects(geom2_, geom1_);
78 
79     ensure_equals(r2, 0);
80 }
81 
82 template<>
83 template<>
test()84 void object::test<2>
85 ()
86 {
87     geom1_ = GEOSGeomFromWKT("POLYGON((1 1,1 5,5 5,5 1,1 1))");
88     geom2_ = GEOSGeomFromWKT("POINT(2 2)");
89 
90     ensure(nullptr != geom1_);
91     ensure(nullptr != geom2_);
92 
93     char const r1 = GEOSIntersects(geom1_, geom2_);
94 
95     ensure_equals(int(r1), 1);
96 
97     char const r2 = GEOSIntersects(geom2_, geom1_);
98 
99     ensure_equals(int(r2), 1);
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((1 1,1 2,2 2,2 1,1 1))");
109 
110     ensure(nullptr != geom1_);
111     ensure(nullptr != geom2_);
112 
113     char const r1 = GEOSIntersects(geom1_, geom2_);
114 
115     ensure_equals(int(r1), 1);
116 
117     char const r2 = GEOSIntersects(geom2_, geom1_);
118 
119     ensure_equals(int(r2), 1);
120 }
121 
122 // This is a test for bug #357 (GEOSIntersects 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 = GEOSIntersects(geom1_, geom1_);
144 
145     ensure_equals(int(r1), 2);
146 
147 }
148 
149 // This is a test for bug #357 (GEOSIntersects 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 = GEOSIntersects(geom1_, geom1_);
163 
164     ensure_equals(int(r1), 2);
165 
166 }
167 
168 // Test for #782 (collection with empty components)
169 template<>
170 template<>
test()171 void object::test<6>
172 ()
173 {
174     geom1_ = GEOSGeomFromWKT("LINESTRING(10 0, 0 0, 0 10)");
175     geom2_ = GEOSGeomFromWKT("MULTILINESTRING((10 -1,-1 10),EMPTY)");
176 
177     char r1 = GEOSIntersects(geom1_, geom2_);
178 
179     ensure_equals(r1, 1);
180 }
181 
182 } // namespace tut
183 
184