@@ -48,6 +48,9 @@ icu_test_simple(pg_wchar code)
48
48
}
49
49
}
50
50
51
+ /*
52
+ * Exhaustively compare case mappings with the results from ICU.
53
+ */
51
54
static void
52
55
test_icu (void )
53
56
{
@@ -82,9 +85,100 @@ test_icu(void)
82
85
}
83
86
#endif
84
87
85
- /*
86
- * Exhaustively compare case mappings with the results from libc and ICU.
87
- */
88
+ static void
89
+ test_strlower (const char * test_string , const char * expected )
90
+ {
91
+ size_t src1len = strlen (test_string );
92
+ size_t src2len = -1 ; /* NUL-terminated */
93
+ size_t dst1len = strlen (expected );
94
+ size_t dst2len = strlen (expected ) + 1 ; /* NUL-terminated */
95
+ char * src1 = malloc (src1len );
96
+ char * dst1 = malloc (dst1len );
97
+ char * src2 = strdup (test_string );
98
+ char * dst2 = malloc (dst2len );
99
+ size_t needed ;
100
+
101
+ memcpy (src1 , test_string , src1len ); /* not NUL-terminated */
102
+
103
+ /* neither source nor destination are NUL-terminated */
104
+ memset (dst1 , 0x7F , dst1len );
105
+ needed = unicode_strlower (dst1 , dst1len , src1 , src1len );
106
+ if (needed != strlen (expected ))
107
+ {
108
+ printf ("case_test: convert_case test1 FAILURE: needed %zu\n" , needed );
109
+ exit (1 );
110
+ }
111
+ if (memcmp (dst1 , expected , dst1len ) != 0 )
112
+ {
113
+ printf ("case_test: convert_case test1 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n" ,
114
+ test_string , (int ) dst1len , dst1 , expected );
115
+ exit (1 );
116
+ }
117
+
118
+ /* destination is NUL-terminated and source is not */
119
+ memset (dst2 , 0x7F , dst2len );
120
+ needed = unicode_strlower (dst2 , dst2len , src1 , src1len );
121
+ if (needed != strlen (expected ))
122
+ {
123
+ printf ("case_test: convert_case test2 FAILURE: needed %zu\n" , needed );
124
+ exit (1 );
125
+ }
126
+ if (strcmp (dst2 , expected ) != 0 )
127
+ {
128
+ printf ("case_test: convert_case test2 FAILURE: test: '%s' result: '%s' expected: '%s'\n" ,
129
+ test_string , dst2 , expected );
130
+ exit (1 );
131
+ }
132
+
133
+ /* source is NUL-terminated and destination is not */
134
+ memset (dst1 , 0x7F , dst1len );
135
+ needed = unicode_strlower (dst1 , dst1len , src2 , src2len );
136
+ if (needed != strlen (expected ))
137
+ {
138
+ printf ("case_test: convert_case test3 FAILURE: needed %zu\n" , needed );
139
+ exit (1 );
140
+ }
141
+ if (memcmp (dst1 , expected , dst1len ) != 0 )
142
+ {
143
+ printf ("case_test: convert_case test3 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n" ,
144
+ test_string , (int ) dst1len , dst1 , expected );
145
+ exit (1 );
146
+ }
147
+
148
+ /* both source and destination are NUL-terminated */
149
+ memset (dst2 , 0x7F , dst2len );
150
+ needed = unicode_strlower (dst2 , dst2len , src2 , src2len );
151
+ if (needed != strlen (expected ))
152
+ {
153
+ printf ("case_test: convert_case test4 FAILURE: needed %zu\n" , needed );
154
+ exit (1 );
155
+ }
156
+ if (strcmp (dst2 , expected ) != 0 )
157
+ {
158
+ printf ("case_test: convert_case test4 FAILURE: test: '%s' result: '%s' expected: '%s'\n" ,
159
+ test_string , dst2 , expected );
160
+ exit (1 );
161
+ }
162
+
163
+ free (src1 );
164
+ free (dst1 );
165
+ free (src2 );
166
+ free (dst2 );
167
+ }
168
+
169
+ static void
170
+ test_convert_case ()
171
+ {
172
+ /* test string with no case changes */
173
+ test_strlower ("√∞" , "√∞" );
174
+ /* test string with case changes */
175
+ test_strlower ("ABC" , "abc" );
176
+ /* test string with case changes and byte length changes */
177
+ test_strlower ("ȺȺȺ" , "ⱥⱥⱥ" );
178
+
179
+ printf ("case_test: convert_case: success\n" );
180
+ }
181
+
88
182
int
89
183
main (int argc , char * * argv )
90
184
{
@@ -96,5 +190,6 @@ main(int argc, char **argv)
96
190
printf ("case_test: ICU not available; skipping\n" );
97
191
#endif
98
192
193
+ test_convert_case ();
99
194
exit (0 );
100
195
}
0 commit comments