PostGIS  3.1.6dev-r@@SVN_REVISION@@
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. */
24 extern char cu_error_msg[];
25 
26 /* Resets cu_error_msg back to blank. */
27 void cu_error_msg_reset(void);
28 
29 /* Our internal callback to register Suites with the main tester */
30 typedef 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_INT_EQUAL(o,e) do { \
39  if ( o != e ) \
40  fprintf(stderr, "[%s:%d]\n Expected: %d\n Obtained: %d\n", __FILE__, __LINE__, (e), (o)); \
41  CU_ASSERT_EQUAL(o,e); \
42 } while (0);
43 
44 static inline void
45 assert_string_equal_impl(const char *obtained, const char *expected, const char *file, int line)
46 {
47  CU_BOOL error = (!obtained && expected) || (obtained && !expected) || (strcmp(obtained, expected) != 0);
48  char *msg = NULL;
49  if (error)
50  {
51  msg = lwalloc(60 + (obtained ? strlen(obtained) : 4) + (expected ? strlen(expected) : 4));
52  sprintf(msg,
53  "ASSERT_STRING_EQUAL\n\t* Expected: %s\n\t* Obtained: %s",
54  expected ? expected : "NULL",
55  obtained ? obtained : "NULL");
56  }
57  CU_assertImplementation(!error, line, msg, file, NULL, CU_FALSE);
58  if (msg)
59  lwfree(msg);
60 }
61 
62 #define ASSERT_STRING_EQUAL(o, e) assert_string_equal_impl(o, e, __FILE__, __LINE__)
63 
64 #define ASSERT_VARLENA_EQUAL(v, s) \
65  do \
66  { \
67  if (strncmp(v->data, s, LWSIZE_GET(v->size) - LWVARHDRSZ) != 0) \
68  { \
69  fprintf( \
70  stderr, "[%s:%d]\n Expected: %s\n Obtained: %s\n", __FILE__, __LINE__, (s), (v->data)); \
71  CU_FAIL(); \
72  } \
73  else \
74  CU_PASS(); \
75  } while (0);
76 
77 #define ASSERT_LWGEOM_EQUAL(o, e) do { \
78  if ( !lwgeom_same(o, e) ) { \
79  char* wkt_o = lwgeom_to_ewkt(o); \
80  char* wkt_e = lwgeom_to_ewkt(e); \
81  fprintf(stderr, "[%s:%d]\n Expected: %s\n Obtained: %s\n", __FILE__, __LINE__, (wkt_o), (wkt_e)); \
82  lwfree(wkt_o); \
83  lwfree(wkt_e); \
84  } \
85  CU_ASSERT_TRUE(lwgeom_same(o, e)); \
86 } while(0);
87 
88 #define ASSERT_INTARRAY_EQUAL(o, e, n) do { \
89  size_t i = 0; \
90  for (i = 0; i < n; i++) { \
91  if (o[i] != e[i]) { \
92  fprintf(stderr, "[%s:%d]", __FILE__, __LINE__); \
93  fprintf(stderr, "\nExpected: ["); \
94  for (i = 0; i < n; i++) \
95  fprintf(stderr, " %d", e[i]); \
96  fprintf(stderr, " ]\nObtained: ["); \
97  for (i = 0; i < n; i++) \
98  fprintf(stderr, " %d", o[i]); \
99  fprintf(stderr, " ]\n"); \
100  CU_FAIL(); \
101  break; \
102  } \
103  } \
104  CU_PASS(); \
105 } while(0);
106 
107 #define ASSERT_POINT2D_EQUAL(o, e, eps) do { \
108  CU_ASSERT_DOUBLE_EQUAL(o.x, e.x, eps); \
109  CU_ASSERT_DOUBLE_EQUAL(o.y, e.y, eps); \
110 } while(0);
111 
112 #define ASSERT_POINT4D_EQUAL(o, e, eps) do { \
113  CU_ASSERT_DOUBLE_EQUAL(o.x, e.x, eps); \
114  CU_ASSERT_DOUBLE_EQUAL(o.y, e.y, eps); \
115  CU_ASSERT_DOUBLE_EQUAL(o.z, e.z, eps); \
116  CU_ASSERT_DOUBLE_EQUAL(o.m, e.m, eps); \
117 } while(0);
118 
119 /* Utility functions */
120 void do_fn_test(LWGEOM* (*transfn)(LWGEOM*), char *input_wkt, char *expected_wkt);
121 
122 #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 lwfree(void *mem)
Definition: lwutil.c:242
void * lwalloc(size_t size)
Definition: lwutil.c:227
This library is the generic geometry handling section of PostGIS.