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