Have UnaryUnion use BinaryOp for the simple case
[geos.git] / tests / unit / utility.h
blob82c3fbf9bda01aa07cc1fa8da7975672139db7cb
1 // $Id$
2 //
3 // GEOS Unit Test utilities, extension of TUT Framework namespace
4 //
5 #ifndef GEOS_TUT_UTILITY_H_INCLUDED
6 #define GEOS_TUT_UTILITY_H_INCLUDED
8 // tut
9 #include <tut.hpp>
10 // geos
11 #include <geos/geom/Geometry.h>
12 #include <geos/geom/GeometryCollection.h>
13 #include <geos/geom/Coordinate.h>
14 #include <geos/geom/CoordinateArraySequence.h>
15 #include <geos/geom/CoordinateArraySequenceFactory.h>
16 #include <geos/geom/CoordinateSequenceFactory.h>
17 #include <geos/geom/Dimension.h>
18 #include <geos/geom/Point.h>
19 #include <geos/geom/Polygon.h>
20 #include <geos/geom/LinearRing.h>
21 #include <geos/geom/LineString.h>
22 #include <geos/geom/MultiPoint.h>
23 #include <geos/geom/MultiLineString.h>
24 #include <geos/geom/MultiPolygon.h>
25 #include <geos/geom/PrecisionModel.h>
26 #include <geos/geom/GeometryFactory.h>
27 #include <geos/geom/prep/PreparedGeometry.h>
28 #include <geos/io/WKTReader.h>
29 // std
30 #include <memory>
31 #include <cstdlib>
32 #include <cassert>
33 #include <string>
34 #include <vector>
36 namespace tut
40 // Helper typedefs
42 typedef geos::geom::Coordinate* CoordinatePtr;
43 typedef geos::geom::Coordinate const* CoordinateCPtr;
45 typedef geos::geom::CoordinateSequence* CoordSeqPtr;
46 typedef geos::geom::CoordinateSequence const* CoordSeqCPtr;
48 typedef geos::geom::CoordinateArraySequence* CoordArrayPtr;
49 typedef geos::geom::CoordinateArraySequence const* CoordArrayCPtr;
51 typedef geos::geom::Geometry* GeometryPtr;
52 typedef geos::geom::Geometry const* GeometryCPtr;
54 typedef geos::geom::Point* PointPtr;
55 typedef geos::geom::Point const* PointCPtr;
56 typedef geos::geom::LinearRing* LinearRingPtr;
57 typedef geos::geom::LinearRing const* LinearRingCPtr;
58 typedef geos::geom::LineString* LineStringPtr;
59 typedef geos::geom::LineString const* LineStringCPtr;
60 typedef geos::geom::Polygon* PolygonPtr;
61 typedef geos::geom::Polygon const* PolygonCPtr;
63 typedef geos::geom::GeometryCollection* GeometryColPtr;
64 typedef geos::geom::GeometryCollection const* GeometryColCPtr;
66 typedef geos::geom::MultiPoint* MultiPointPtr;
67 typedef geos::geom::MultiPoint const* MultiPointCPtr;
68 typedef geos::geom::MultiLineString* MultiLineStringPtr;
69 typedef geos::geom::MultiLineString const* MultiLineStringCPtr;
70 typedef geos::geom::MultiPolygon* MultiPolygonPtr;
71 typedef geos::geom::MultiPolygon const* MultiPolygonCPtr;
73 // prepared geometries always returend as const
74 typedef geos::geom::prep::PreparedGeometry const* PreparedGeometryPtr;
77 // Type cast helper utilities
80 template<typename Type, typename InstanceType>
81 inline bool isInstanceOf(InstanceType const* instance)
83 assert(0 != instance);
84 return (0 != dynamic_cast<Type const*>(instance) );
87 template<typename Type, typename InstanceType>
88 inline Type const* instanceOf(InstanceType const* instance)
90 assert(0 != instance);
91 return dynamic_cast<Type const*>(instance);
94 template <typename Dst, typename Src>
95 std::auto_ptr<Dst> dynamic_cast_auto_ptr(std::auto_ptr<Src>& ptr)
97 Dst* res = dynamic_cast<Dst*>(ptr.get());
98 if (0 != res)
100 ptr.release();
102 return std::auto_ptr<Dst>(res);
105 template <typename Dst, typename Src>
106 std::auto_ptr<Dst> static_cast_auto_ptr(std::auto_ptr<Src>& ptr)
108 return std::auto_ptr<Dst>(static_cast<Dst*>(ptr.release()));
112 // Geometries structure comparators
115 template <typename T1, typename T2>
116 inline void ensure_equals_geometry(T1 const* lhs, T2 const* rhs)
118 assert(0 != lhs);
119 assert(0 != rhs);
121 assert(!"DIFFERENT TYPES ARE NOT OF THE SAME STRUCTURE");
124 template <typename T>
125 inline void ensure_equals_geometry(T const* lhs, T const* rhs)
127 assert(0 != lhs);
128 assert(0 != rhs);
130 using geos::geom::Polygon;
131 using geos::geom::GeometryCollection;
133 ensure_equals("is-valid do not match",
134 lhs->isValid(), rhs->isValid());
136 ensure_equals("is-empty do not match",
137 lhs->isEmpty(), rhs->isEmpty());
139 if (!isInstanceOf<GeometryCollection>(lhs)
140 && !isInstanceOf<GeometryCollection>(rhs))
142 ensure_equals("is-simple do not match",
143 lhs->isSimple(), rhs->isSimple());
146 ensure_equals("type do not match",
147 lhs->getGeometryType(), rhs->getGeometryType());
149 ensure_equals("type id do not match",
150 lhs->getGeometryTypeId(), rhs->getGeometryTypeId());
152 ensure_equals("dimension do not match",
153 lhs->getDimension(), rhs->getDimension());
155 ensure_equals("boundary dimension do not match",
156 lhs->getBoundaryDimension(), rhs->getBoundaryDimension());
158 // NOTE - mloskot: Intentionally disabled, so simplified geometry
159 // can be compared to its original
160 //ensure_equals("number of points do not match",
161 // lhs->getNumPoints(), rhs->getNumPoints());
163 // Dispatch to run more specific testes
164 if (isInstanceOf<Polygon>(lhs)
165 && isInstanceOf<Polygon>(rhs))
167 ensure_equals_geometry(instanceOf<Polygon>(lhs),
168 instanceOf<Polygon>(rhs));
170 else if (isInstanceOf<GeometryCollection>(lhs)
171 && isInstanceOf<GeometryCollection>(rhs))
173 ensure_equals_geometry(instanceOf<GeometryCollection>(lhs),
174 instanceOf<GeometryCollection>(rhs));
178 template <>
179 inline void ensure_equals_geometry(geos::geom::Polygon const* lhs,
180 geos::geom::Polygon const* rhs)
182 assert(0 != lhs);
183 assert(0 != rhs);
185 ensure_equals("number of interior ring do not match",
186 lhs->getNumInteriorRing(), rhs->getNumInteriorRing());
189 template <>
190 inline void ensure_equals_geometry(geos::geom::GeometryCollection const* lhs,
191 geos::geom::GeometryCollection const* rhs)
193 assert(0 != lhs);
194 assert(0 != rhs);
196 using geos::geom::Geometry;
198 ensure_equals("number of geometries do not match",
199 lhs->getNumGeometries(), rhs->getNumGeometries());
201 for (std::size_t i = 0, n = lhs->getNumGeometries(); i < n; ++i)
203 Geometry const* g1 = lhs->getGeometryN(i);
204 Geometry const* g2 = rhs->getGeometryN(i);
205 ensure_equals_geometry(g1, g2); // breaks on failure
209 template <>
210 inline void ensure_equals_geometry(geos::geom::Geometry const* lhs,
211 geos::geom::prep::PreparedGeometry const* rhs)
213 assert(0 != lhs);
214 assert(0 != rhs);
216 geos::geom::Geometry const& pg = rhs->getGeometry();
217 ensure_equals_geometry(lhs, &pg);
221 // Utility functions
224 // Decodes hex-encoded WKB/EWKB to raw binary.
225 struct wkb_hex_decoder
227 typedef std::vector<unsigned char> binary_type;
229 // bytes [out] - buffer for binary output
230 static void decode(std::string const& hexstr, binary_type& bytes)
232 bytes.clear();
233 for(std::string::size_type i = 0; i < hexstr.size() / 2; ++i)
235 std::istringstream iss(hexstr.substr(i * 2, 2));
236 unsigned int n;
237 iss >> std::hex >> n;
238 bytes.push_back(static_cast<unsigned char>(n));
244 } // namespace tut
246 #endif // #ifndef GEOS_TUT_UTILITY_H_INCLUDED