PostGIS  2.2.8dev-r@@SVN_REVISION@@
cu_print.c
Go to the documentation of this file.
1 /**********************************************************************
2  *
3  * PostGIS - Spatial Types for PostgreSQL
4  * http://postgis.net
5  * Copyright 2008 Paul Ramsey
6  *
7  * This is free software; you can redistribute and/or modify it under
8  * the terms of the GNU General Public Licence. See the COPYING file.
9  *
10  **********************************************************************/
11 
12 #include <stdio.h>
13 #include <stdlib.h>
14 #include <string.h>
15 #include "CUnit/Basic.h"
16 
17 #include "liblwgeom_internal.h"
18 #include "cu_tester.h"
19 
20 static void test_lwprint_assert_format(char * point_wkt, const char * format, const char * expected)
21 {
22  LWPOINT * test_point = (LWPOINT*)lwgeom_from_wkt(point_wkt, LW_PARSER_CHECK_NONE);
23  int num_old_failures, num_new_failures;
24  char * actual;
26  actual = lwpoint_to_latlon(test_point, format);
27  if (0 != strlen(cu_error_msg))
28  {
29  printf("\nAssert failed:\n\tFormat [%s] generated an error: %s\n", format, cu_error_msg);
30  CU_FAIL();
31  }
32  num_old_failures = CU_get_number_of_failures();
33  CU_ASSERT_STRING_EQUAL(actual, expected);
34  num_new_failures = CU_get_number_of_failures();
35  if (num_new_failures > num_old_failures)
36  {
37  printf("\nAssert failed:\n\t%s\t(actual)\n\t%s\t(expected)\n", actual, expected);
38  }
39  lwfree(actual);
40  lwpoint_free(test_point);
41 }
42 static void test_lwprint_assert_error(char * point_wkt, const char * format)
43 {
44  LWPOINT * test_point = (LWPOINT*)lwgeom_from_wkt(point_wkt, LW_PARSER_CHECK_NONE);
46  char* tmp = lwpoint_to_latlon(test_point, format);
47  lwfree(tmp);
48  if (0 == strlen(cu_error_msg))
49  {
50  printf("\nAssert failed:\n\tFormat [%s] did not generate an error.\n", format);
51  CU_FAIL();
52  }
53  else
54  {
56  }
57  lwpoint_free(test_point);
58 }
59 
60 /*
61 ** Test points around the globe using the default format. Null and empty string both mean use the default.
62 */
63 static void test_lwprint_default_format(void)
64 {
65  test_lwprint_assert_format("POINT(0 0)", NULL, "0\xC2\xB0""0'0.000\"N 0\xC2\xB0""0'0.000\"E");
66  test_lwprint_assert_format("POINT(45.4545 12.34567)", "" , "12\xC2\xB0""20'44.412\"N 45\xC2\xB0""27'16.200\"E");
67  test_lwprint_assert_format("POINT(180 90)", NULL, "90\xC2\xB0""0'0.000\"N 180\xC2\xB0""0'0.000\"E");
68  test_lwprint_assert_format("POINT(181 91)", "" , "89\xC2\xB0""0'0.000\"N 1\xC2\xB0""0'0.000\"E");
69  test_lwprint_assert_format("POINT(180.0001 90.0001)", NULL, "89\xC2\xB0""59'59.640\"N 0\xC2\xB0""0'0.360\"E");
70  test_lwprint_assert_format("POINT(45.4545 -12.34567)", "" , "12\xC2\xB0""20'44.412\"S 45\xC2\xB0""27'16.200\"E");
71  test_lwprint_assert_format("POINT(180 -90)", NULL, "90\xC2\xB0""0'0.000\"S 180\xC2\xB0""0'0.000\"E");
72  test_lwprint_assert_format("POINT(181 -91)", "" , "89\xC2\xB0""0'0.000\"S 1\xC2\xB0""0'0.000\"E");
73  test_lwprint_assert_format("POINT(180.0001 -90.0001)", NULL, "89\xC2\xB0""59'59.640\"S 0\xC2\xB0""0'0.360\"E");
74  test_lwprint_assert_format("POINT(-45.4545 12.34567)", "" , "12\xC2\xB0""20'44.412\"N 45\xC2\xB0""27'16.200\"W");
75  test_lwprint_assert_format("POINT(-180 90)", NULL, "90\xC2\xB0""0'0.000\"N 180\xC2\xB0""0'0.000\"W");
76  test_lwprint_assert_format("POINT(-181 91)", "" , "89\xC2\xB0""0'0.000\"N 1\xC2\xB0""0'0.000\"W");
77  test_lwprint_assert_format("POINT(-180.0001 90.0001)", NULL, "89\xC2\xB0""59'59.640\"N 0\xC2\xB0""0'0.360\"W");
78  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "" , "12\xC2\xB0""20'44.412\"S 45\xC2\xB0""27'16.200\"W");
79  test_lwprint_assert_format("POINT(-180 -90)", NULL, "90\xC2\xB0""0'0.000\"S 180\xC2\xB0""0'0.000\"W");
80  test_lwprint_assert_format("POINT(-181 -91)", "" , "89\xC2\xB0""0'0.000\"S 1\xC2\xB0""0'0.000\"W");
81  test_lwprint_assert_format("POINT(-180.0001 -90.0001)", NULL, "89\xC2\xB0""59'59.640\"S 0\xC2\xB0""0'0.360\"W");
82  test_lwprint_assert_format("POINT(-2348982391.123456 -238749827.34879)", "" , "12\xC2\xB0""39'4.356\"N 31\xC2\xB0""7'24.442\"W");
83 }
84 
85 /*
86  * Test all possible combinations of the orders of the parameters.
87  */
88 static void test_lwprint_format_orders(void)
89 {
90  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C DD MM SS", "S 12 20 44 W 45 27 16");
91  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C DD SS MM", "S 12 44 20 W 45 16 27");
92  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C MM DD SS", "S 20 12 44 W 27 45 16");
93  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C MM SS DD", "S 20 44 12 W 27 16 45");
94  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C SS DD MM", "S 44 12 20 W 16 45 27");
95  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "C SS MM DD", "S 44 20 12 W 16 27 45");
96 
97  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD C MM SS", "12 S 20 44 45 W 27 16");
98  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD C SS MM", "12 S 44 20 45 W 16 27");
99  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM C DD SS", "20 S 12 44 27 W 45 16");
100  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM C SS DD", "20 S 44 12 27 W 16 45");
101  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS C DD MM", "44 S 12 20 16 W 45 27");
102  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS C MM DD", "44 S 20 12 16 W 27 45");
103 
104  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD MM C SS", "12 20 S 44 45 27 W 16");
105  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD SS C MM", "12 44 S 20 45 16 W 27");
106  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM DD C SS", "20 12 S 44 27 45 W 16");
107  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM SS C DD", "20 44 S 12 27 16 W 45");
108  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS DD C MM", "44 12 S 20 16 45 W 27");
109  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS MM C DD", "44 20 S 12 16 27 W 45");
110 
111  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD MM SS C", "12 20 44 S 45 27 16 W");
112  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD SS MM C", "12 44 20 S 45 16 27 W");
113  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM DD SS C", "20 12 44 S 27 45 16 W");
114  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "MM SS DD C", "20 44 12 S 27 16 45 W");
115  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS DD MM C", "44 12 20 S 16 45 27 W");
116  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "SS MM DD C", "44 20 12 S 16 27 45 W");
117 }
118 
119 /*
120  * Test with and without the optional parameters.
121  */
123 {
124  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD", "-12.346 -45.455");
125  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD C", "12.346 S 45.455 W");
126  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD MM.MMM", "-12.000 20.740 -45.000 27.270");
127  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD MM.MMM C", "12.000 20.740 S 45.000 27.270 W");
128  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD MM.MMM SS.SSS", "-12.000 20.000 44.412 -45.000 27.000 16.200");
129  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDD MM.MMM SS.SSS C", "12.000 20.000 44.412 S 45.000 27.000 16.200 W");
130 }
131 
133 {
134  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.DDDMM.MMMSS.SSSC", "12.00020.00044.412S 45.00027.00016.200W");
135  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DDMM.MMM", "-1220.740 -4527.270");
136  /* "##." will be printed as "##" */
137  test_lwprint_assert_format("POINT(-45.4545 -12.34567)", "DD.MM.MMM", "-1220.740 -4527.270");
138 }
139 
140 /*
141  * Test using formats that should produce errors.
142  */
143 static void test_lwprint_bad_formats(void)
144 {
145  test_lwprint_assert_error("POINT(1.23456 7.89012)", "DD.DDD SS.SSS");
146  test_lwprint_assert_error("POINT(1.23456 7.89012)", "MM.MMM SS.SSS");
147  test_lwprint_assert_error("POINT(1.23456 7.89012)", "DD.DDD SS.SSS DD");
148  test_lwprint_assert_error("POINT(1.23456 7.89012)", "DD MM SS MM");
149  test_lwprint_assert_error("POINT(1.23456 7.89012)", "DD MM SS SS");
150  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C DD.DDD C");
151  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C \xC2""DD.DDD");
152  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C DD.DDD \xC2");
153  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C DD\x80""MM ");
154  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C DD \xFF""MM");
155  test_lwprint_assert_error("POINT(1.23456 7.89012)", "C DD \xB0""MM");
156  test_lwprint_assert_error("POINT(1.23456 7.89012)", "DD.DDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDDD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jjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjjj");
158 }
159 
160 /*
161 ** Callback used by the test harness to register the tests in this file.
162 */
163 void print_suite_setup(void);
165 {
166  CU_pSuite suite = CU_add_suite("printing", NULL, NULL);
172 }
173 
static void test_lwprint_oddball_formats(void)
Definition: cu_print.c:132
void lwfree(void *mem)
Definition: lwutil.c:214
char * lwpoint_to_latlon(const LWPOINT *p, const char *format)
Definition: lwprint.c:393
void lwpoint_free(LWPOINT *pt)
Definition: lwpoint.c:182
static void test_lwprint_assert_format(char *point_wkt, const char *format, const char *expected)
Definition: cu_print.c:20
LWGEOM * lwgeom_from_wkt(const char *wkt, const char check)
Definition: lwin_wkt.c:890
void print_suite_setup(void)
Definition: cu_print.c:164
#define LW_PARSER_CHECK_NONE
Definition: liblwgeom.h:1869
void cu_error_msg_reset()
#define PG_ADD_TEST(suite, testfunc)
static void test_lwprint_bad_formats(void)
Definition: cu_print.c:143
static void test_lwprint_assert_error(char *point_wkt, const char *format)
Definition: cu_print.c:42
static void test_lwprint_format_orders(void)
Definition: cu_print.c:88
static void test_lwprint_optional_format(void)
Definition: cu_print.c:122
static void test_lwprint_default_format(void)
Definition: cu_print.c:63
char cu_error_msg[MAX_CUNIT_ERROR_LENGTH+1]