PostGIS 3.7.0dev-r@@SVN_REVISION@@
Loading...
Searching...
No Matches
liblwgeom/cunit/cu_tester.h
Go to the documentation of this file.
1/**********************************************************************
2 *
3 * PostGIS - Spatial Types for PostgreSQL
4 * http://postgis.net
5 *
6 * Copyright (C) 2009 Paul Ramsey <pramsey@cleverelephant.ca>
7 *
8 * This is free software; you can redistribute and/or modify it under
9 * the terms of the GNU General Public Licence. See the COPYING file.
10 *
11 **********************************************************************/
12
13#ifndef _CU_TESTER_H
14#define _CU_TESTER_H 1
15
16#include "liblwgeom.h"
17#include <stdio.h>
18
19#define MAX_CUNIT_ERROR_LENGTH 512
20
21#define PG_ADD_TEST(suite, testfunc) CU_add_test(suite, #testfunc, testfunc)
22
23/* Contains the most recent error message generated by lwerror. */
24extern char cu_error_msg[];
25
26/* Resets cu_error_msg back to blank. */
27void cu_error_msg_reset(void);
28
29/* Our internal callback to register Suites with the main tester */
30typedef void (*PG_SuiteSetup)(void);
31
32#define ASSERT_DOUBLE_EQUAL(o,e) do { \
33 if ( o != e ) \
34 fprintf(stderr, "[%s:%d]\n Expected: %g\n Obtained: %g\n", __FILE__, __LINE__, (double)(e), (o)); \
35 CU_ASSERT_EQUAL(o,(double)e); \
36} while (0);
37
38#define ASSERT_DOUBLE_EQUAL_TOLERANCE(o,e,t) do { \
39 if ( fabs(o-e) > t ) \
40 fprintf(stderr, "[%s:%d]\n Expected: %g\n Obtained: %g\n Difference: %.15g\n Tolerated: %.15g\n", __FILE__, __LINE__, (double)(e), (o), fabs((o)-(e)), (t)); \
41 CU_ASSERT_DOUBLE_EQUAL(o,e,t); \
42} while (0);
43
44#define ASSERT_INT_EQUAL(o,e) do { \
45 if ( o != e ) \
46 fprintf(stderr, "[%s:%d]\n Expected: %d\n Obtained: %d\n", __FILE__, __LINE__, (e), (o)); \
47 CU_ASSERT_EQUAL(o,e); \
48} while (0);
49
50#define ASSERT_NORMALIZED_GEOM_SAME(gobt, gexp) \
51 do \
52 { \
53 char *obt, *exp; \
54 LWGEOM *ngobt, *ngexp; \
55 ngobt = lwgeom_normalize(gobt); \
56 ngexp = lwgeom_normalize(gexp); \
57 if (!lwgeom_same((ngobt), (ngexp))) \
58 { \
59 obt = lwgeom_to_wkt((ngobt), WKT_ISO, 8, NULL); \
60 exp = lwgeom_to_wkt((ngexp), WKT_ISO, 8, NULL); \
61 fprintf(stderr, "[%s:%d]\n Expected: %s\n Obtained: %s\n", __FILE__, __LINE__, exp, obt); \
62 free(obt); \
63 free(exp); \
64 lwgeom_free(ngobt); \
65 lwgeom_free(ngexp); \
66 CU_ASSERT(0); \
67 } \
68 else \
69 { \
70 lwgeom_free(ngobt); \
71 lwgeom_free(ngexp); \
72 CU_ASSERT(1); \
73 } \
74 } while (0)
75
76static inline void
77assert_string_equal_impl(const char *obtained, const char *expected, const char *file, int line)
78{
79 CU_BOOL error = (!obtained && expected) || (obtained && !expected) || (strcmp(obtained, expected) != 0);
80 char *msg = NULL;
81 if (error)
82 {
83 msg = lwalloc(60 + (obtained ? strlen(obtained) : 4) + (expected ? strlen(expected) : 4));
84 sprintf(msg,
85 "ASSERT_STRING_EQUAL\n\t* Expected: %s\n\t* Obtained: %s",
86 expected ? expected : "NULL",
87 obtained ? obtained : "NULL");
88 }
89 CU_assertImplementation(!error, line, msg, file, NULL, CU_FALSE);
90 if (msg)
91 lwfree(msg);
92}
93
94#define ASSERT_STRING_EQUAL(o, e) assert_string_equal_impl(o, e, __FILE__, __LINE__)
95
96#define ASSERT_VARLENA_EQUAL(v, s) \
97 do \
98 { \
99 if (strncmp(v->data, s, LWSIZE_GET(v->size) - LWVARHDRSZ) != 0) \
100 { \
101 fprintf( \
102 stderr, "[%s:%d]\n Expected: %s\n Obtained: %s\n", __FILE__, __LINE__, (s), (v->data)); \
103 CU_FAIL(); \
104 } \
105 else \
106 CU_PASS(); \
107 } while (0);
108
109#define ASSERT_LWGEOM_EQUAL(o, e) do { \
110 if ( !lwgeom_same(o, e) ) { \
111 char* wkt_o = lwgeom_to_ewkt(o); \
112 char* wkt_e = lwgeom_to_ewkt(e); \
113 fprintf(stderr, "[%s:%d]\n Expected: %s\n Obtained: %s\n", __FILE__, __LINE__, (wkt_o), (wkt_e)); \
114 lwfree(wkt_o); \
115 lwfree(wkt_e); \
116 } \
117 CU_ASSERT_TRUE(lwgeom_same(o, e)); \
118} while(0);
119
120#define ASSERT_INTARRAY_EQUAL(o, e, n) do { \
121 size_t i = 0; \
122 for (i = 0; i < n; i++) { \
123 if (o[i] != e[i]) { \
124 fprintf(stderr, "[%s:%d]", __FILE__, __LINE__); \
125 fprintf(stderr, "\nExpected: ["); \
126 for (i = 0; i < n; i++) \
127 fprintf(stderr, " %d", e[i]); \
128 fprintf(stderr, " ]\nObtained: ["); \
129 for (i = 0; i < n; i++) \
130 fprintf(stderr, " %d", o[i]); \
131 fprintf(stderr, " ]\n"); \
132 CU_FAIL(); \
133 break; \
134 } \
135 } \
136 CU_PASS(); \
137} while(0);
138
139#define ASSERT_POINT2D_EQUAL(o, e, eps) do { \
140 CU_ASSERT_DOUBLE_EQUAL(o.x, e.x, eps); \
141 CU_ASSERT_DOUBLE_EQUAL(o.y, e.y, eps); \
142} while(0);
143
144#define ASSERT_POINT4D_EQUAL(o, e, eps) do { \
145 CU_ASSERT_DOUBLE_EQUAL(o.x, e.x, eps); \
146 CU_ASSERT_DOUBLE_EQUAL(o.y, e.y, eps); \
147 CU_ASSERT_DOUBLE_EQUAL(o.z, e.z, eps); \
148 CU_ASSERT_DOUBLE_EQUAL(o.m, e.m, eps); \
149} while(0);
150
151/* Utility functions */
152void do_fn_test(LWGEOM* (*transfn)(LWGEOM*), char *input_wkt, char *expected_wkt);
153
154#endif /* _CU_TESTER_H */
void(* PG_SuiteSetup)(void)
void do_fn_test(LWGEOM *(*transfn)(LWGEOM *), char *input_wkt, char *expected_wkt)
static void assert_string_equal_impl(const char *obtained, const char *expected, const char *file, int line)
void cu_error_msg_reset(void)
char cu_error_msg[]
void * lwalloc(size_t size)
Definition lwutil.c:227
void lwfree(void *mem)
Definition lwutil.c:248
This library is the generic geometry handling section of PostGIS.