PostgreSQL Source Code git master
Loading...
Searching...
No Matches
case_test.c
Go to the documentation of this file.
1/*-------------------------------------------------------------------------
2 * case_test.c
3 * Program to test Unicode case mapping functions.
4 *
5 * Portions Copyright (c) 2017-2026, PostgreSQL Global Development Group
6 *
7 * IDENTIFICATION
8 * src/common/unicode/case_test.c
9 *
10 *-------------------------------------------------------------------------
11 */
12#include "postgres_fe.h"
13
14#include <locale.h>
15#include <stdio.h>
16#include <stdlib.h>
17#include <string.h>
18#include <wctype.h>
19
20#ifdef USE_ICU
21#include <unicode/ucasemap.h>
22#include <unicode/uchar.h>
23#endif
24#include "common/unicode_case.h"
27#include "mb/pg_wchar.h"
28
29/* enough to hold largest source or result string, including NUL */
30#define BUFSZ 256
31
32#ifdef USE_ICU
33static UCaseMap *casemap = NULL;
34#endif
35
36typedef size_t (*TestFunc) (char *dst, size_t dstsize, const char *src,
37 size_t srclen);
38
39/* simple boundary iterator copied from pg_locale_builtin.c */
41{
42 const char *str;
43 size_t len;
44 size_t offset;
45 bool posix;
46 bool init;
47 bool prev_alnum;
48};
49
50static size_t
52{
54
55 while (wbstate->offset < wbstate->len &&
56 wbstate->str[wbstate->offset] != '\0')
57 {
58 char32_t u = utf8_to_unicode((const unsigned char *) wbstate->str +
59 wbstate->offset);
60 bool curr_alnum = pg_u_isalnum(u, wbstate->posix);
61
62 if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
63 {
64 size_t prev_offset = wbstate->offset;
65
66 wbstate->init = true;
67 wbstate->offset += unicode_utf8len(u);
68 wbstate->prev_alnum = curr_alnum;
69 return prev_offset;
70 }
71
72 wbstate->offset += unicode_utf8len(u);
73 }
74
75 return wbstate->len;
76}
77
78#ifdef USE_ICU
79
80static void
81icu_test_simple(char32_t code)
82{
83 char32_t lower = unicode_lowercase_simple(code);
84 char32_t title = unicode_titlecase_simple(code);
85 char32_t upper = unicode_uppercase_simple(code);
86 char32_t fold = unicode_casefold_simple(code);
87 char32_t iculower = u_tolower(code);
88 char32_t icutitle = u_totitle(code);
89 char32_t icuupper = u_toupper(code);
90 char32_t icufold = u_foldCase(code, U_FOLD_CASE_DEFAULT);
91
92 if (lower != iculower || title != icutitle || upper != icuupper ||
93 fold != icufold)
94 {
95 printf("case_test: FAILURE for codepoint 0x%06x\n", code);
96 printf("case_test: Postgres lower/title/upper/fold: 0x%06x/0x%06x/0x%06x/0x%06x\n",
97 lower, title, upper, fold);
98 printf("case_test: ICU lower/title/upper/fold: 0x%06x/0x%06x/0x%06x/0x%06x\n",
100 printf("\n");
101 exit(1);
102 }
103}
104
105static void
106icu_test_full(char *str)
107{
108 char lower[BUFSZ];
109 char title[BUFSZ];
110 char upper[BUFSZ];
111 char fold[BUFSZ];
112 char icu_lower[BUFSZ];
113 char icu_title[BUFSZ];
114 char icu_upper[BUFSZ];
115 char icu_fold[BUFSZ];
116 UErrorCode status;
117 size_t len = strlen(str);
118 size_t consumed;
119
120 /* full case mapping doesn't use posix semantics */
121 struct WordBoundaryState wbstate = {
122 .str = str,
123 .len = strlen(str),
124 .offset = 0,
125 .posix = false,
126 .init = false,
127 .prev_alnum = false,
128 };
129
134 status = U_ZERO_ERROR;
136 status = U_ZERO_ERROR;
138 status = U_ZERO_ERROR;
140 status = U_ZERO_ERROR;
142
143 if (strcmp(lower, icu_lower) != 0)
144 {
145 printf("case_test: str='%s' lower='%s' icu_lower='%s'\n", str, lower,
146 icu_lower);
147 exit(1);
148 }
149 if (strcmp(title, icu_title) != 0)
150 {
151 printf("case_test: str='%s' title='%s' icu_title='%s'\n", str, title,
152 icu_title);
153 exit(1);
154 }
155 if (strcmp(upper, icu_upper) != 0)
156 {
157 printf("case_test: str='%s' upper='%s' icu_upper='%s'\n", str, upper,
158 icu_upper);
159 exit(1);
160 }
161 if (strcmp(fold, icu_fold) != 0)
162 {
163 printf("case_test: str='%s' fold='%s' icu_fold='%s'\n", str, fold,
164 icu_fold);
165 exit(1);
166 }
167}
168
169/*
170 * Exhaustively compare case mappings with the results from ICU.
171 */
172static void
173test_icu(void)
174{
175 int successful = 0;
176 int skipped_mismatch = 0;
177
178 for (char32_t code = 0; code <= 0x10ffff; code++)
179 {
180 pg_unicode_category category = unicode_category(code);
181
182 if (category != PG_U_UNASSIGNED)
183 {
185 char code_str[5] = {0};
186
188 {
190 continue;
191 }
192
193 icu_test_simple(code);
194 unicode_to_utf8(code, (unsigned char *) code_str);
196
197 successful++;
198 }
199 }
200
201 if (skipped_mismatch > 0)
202 printf("case_test: skipped %d codepoints unassigned in ICU due to Unicode version mismatch\n",
204
205 printf("case_test: ICU simple mapping test: %d codepoints successful\n",
206 successful);
207}
208#endif
209
210static void
212{
213 size_t src1len = strlen(test_string);
214 size_t dst1len = strlen(expected);
215 size_t dst2len = strlen(expected) + 1; /* NUL-terminated */
216 char *src1 = malloc(src1len);
217 char *dst1 = malloc(dst1len);
218 char *dst2 = malloc(dst2len);
219 size_t needed;
220
221 memcpy(src1, test_string, src1len); /* not NUL-terminated */
222
223 /* destination is not NUL-terminated */
224 memset(dst1, 0x7F, dst1len);
226 if (needed != strlen(expected))
227 {
228 printf("case_test: convert_case test1 FAILURE: '%s' needed %zu expected %zu\n",
230 exit(1);
231 }
232 if (memcmp(dst1, expected, dst1len) != 0)
233 {
234 printf("case_test: convert_case test1 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n",
236 exit(1);
237 }
238
239 /* destination is NUL-terminated */
240 memset(dst2, 0x7F, dst2len);
242 if (needed != strlen(expected))
243 {
244 printf("case_test: convert_case test2 FAILURE: '%s' needed %zu expected %zu\n",
246 exit(1);
247 }
248 if (strcmp(dst2, expected) != 0)
249 {
250 printf("case_test: convert_case test2 FAILURE: test: '%s' result: '%s' expected: '%s'\n",
252 exit(1);
253 }
254
255 free(src1);
256 free(dst1);
257 free(dst2);
258}
259
260static size_t
261tfunc_lower(char *dst, size_t dstsize, const char *src,
262 size_t srclen)
263{
264 size_t consumed;
265
266 return unicode_strlower(dst, dstsize, src, srclen, &consumed, true);
267}
268
269static size_t
270tfunc_title(char *dst, size_t dstsize, const char *src,
271 size_t srclen)
272{
273 size_t consumed;
274 struct WordBoundaryState wbstate = {
275 .str = src,
276 .len = srclen,
277 .offset = 0,
278 .init = false,
279 .prev_alnum = false,
280 };
281
282 return unicode_strtitle(dst, dstsize, src, srclen, &consumed, true,
284}
285
286static size_t
287tfunc_upper(char *dst, size_t dstsize, const char *src,
288 size_t srclen)
289{
290 size_t consumed;
291
292 return unicode_strupper(dst, dstsize, src, srclen, &consumed, true);
293}
294
295static size_t
296tfunc_fold(char *dst, size_t dstsize, const char *src,
297 size_t srclen)
298{
299 size_t consumed;
300
301 return unicode_strfold(dst, dstsize, src, srclen, &consumed, true);
302}
303
304static void
306{
307 size_t needed;
308 size_t consumed;
309
310 /* test string with no case changes */
311 test_convert(tfunc_lower, "√∞", "√∞");
312 /* test adjust-to-cased behavior */
313 test_convert(tfunc_title, "abc 123xyz", "Abc 123xyz");
314 /* test string with case changes */
315 test_convert(tfunc_upper, "abc", "ABC");
316 /* test string with case changes and byte length changes */
317 test_convert(tfunc_lower, "ȺȺȺ", "ⱥⱥⱥ");
318 /* test special case conversions */
319 test_convert(tfunc_upper, "ß", "SS");
320 test_convert(tfunc_lower, "ıiIİ", "ıiii\u0307");
321 test_convert(tfunc_upper, "ıiIİ", "IIIİ");
322 test_convert(tfunc_fold, "ıiIİ", "ıiii\u0307");
323 /* test final sigma */
324 test_convert(tfunc_lower, "σςΣ ΣΣΣ", "σςς σσς");
325 test_convert(tfunc_lower, "σς'Σ' ΣΣ'Σ'", "σς'ς' σσ'ς'");
326 test_convert(tfunc_title, "σςΣ ΣΣΣ", "Σςς Σσς");
327 test_convert(tfunc_fold, "σςΣ ΣΣΣ", "σσσ σσσ");
328 /* test that alphanumerics are word characters */
329 test_convert(tfunc_title, "λλ", "Λλ");
330 test_convert(tfunc_title, "1a", "1a");
331 /* U+FF11 FULLWIDTH ONE is alphanumeric for full case mapping */
332 test_convert(tfunc_title, "\uFF11a", "\uFF11a");
333
334 /* invalid UTF8: truncated multibyte sequence */
335 needed = unicode_strfold(NULL, 0, "abc\xCE", 4, &consumed, false);
336 Assert(needed == 3 && consumed == 3);
337 /* invalid UTF8: leading byte invalid length */
338 needed = unicode_strfold(NULL, 0, "abc\xF8xyz", 7, &consumed, false);
339 Assert(needed == 3 && consumed == 3);
340
341#ifdef USE_ICU
342 icu_test_full("");
343 icu_test_full("ȺȺȺ");
344 icu_test_full("ßßß");
345 icu_test_full("√∞");
346 icu_test_full("a b");
347 icu_test_full("abc 123xyz");
348 icu_test_full("σςΣ ΣΣΣ");
349 icu_test_full("ıiIİ");
350 icu_test_full("\uFF11a");
351 /* test <alpha><iota_subscript><acute> */
352 icu_test_full("\u0391\u0345\u0301");
353#endif
354
355 printf("case_test: convert_case: success\n");
356}
357
358int
359main(int argc, char **argv)
360{
361#ifdef USE_ICU
362 UErrorCode status = U_ZERO_ERROR;
363
364 /*
365 * Disable ICU's word break adjustment for titlecase to match the expected
366 * behavior of unicode_strtitle().
367 */
369 if (U_FAILURE(status))
370 {
371 printf("case_test: failure opening UCaseMap: %s\n",
372 u_errorName(status));
373 exit(1);
374 }
375#endif
376
377 printf("case_test: Postgres Unicode version:\t%s\n", PG_UNICODE_VERSION);
378#ifdef USE_ICU
379 printf("case_test: ICU Unicode version:\t\t%s\n", U_UNICODE_VERSION);
380 test_icu();
381#else
382 printf("case_test: ICU not available; skipping\n");
383#endif
384
386
387#ifdef USE_ICU
389#endif
390 exit(0);
391}
#define Assert(condition)
Definition c.h:1002
static void test_convert(TestFunc tfunc, const char *test_string, const char *expected)
Definition case_test.c:211
static void test_convert_case(void)
Definition case_test.c:305
static size_t tfunc_lower(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:261
static size_t tfunc_fold(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:296
static size_t initcap_wbnext(void *state)
Definition case_test.c:51
static size_t tfunc_upper(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:287
size_t(* TestFunc)(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:36
static size_t tfunc_title(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:270
#define BUFSZ
Definition case_test.c:30
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
int main(void)
const char * str
static char32_t utf8_to_unicode(const unsigned char *c)
Definition mbprint.c:53
Datum lower(PG_FUNCTION_ARGS)
Datum upper(PG_FUNCTION_ARGS)
const void size_t len
static unsigned char * unicode_to_utf8(char32_t c, unsigned char *utf8string)
Definition pg_wchar.h:428
static int unicode_utf8len(char32_t c)
Definition pg_wchar.h:460
#define printf(...)
Definition port.h:267
static int fb(int x)
#define free(a)
#define malloc(a)
size_t unicode_strupper(char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
char32_t unicode_titlecase_simple(char32_t code)
size_t unicode_strfold(char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
char32_t unicode_casefold_simple(char32_t code)
char32_t unicode_lowercase_simple(char32_t code)
size_t unicode_strlower(char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full)
size_t unicode_strtitle(char *dst, size_t dstsize, const char *src, size_t srclen, size_t *pconsumed, bool full, WordBoundaryNext wbnext, void *wbstate)
static enum CaseMapResult casemap(char32_t u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, char32_t *simple, const char32_t **special)
char32_t unicode_uppercase_simple(char32_t code)
bool pg_u_isalnum(char32_t code, bool posix)
pg_unicode_category unicode_category(char32_t code)
pg_unicode_category
@ PG_U_UNASSIGNED
#define PG_UNICODE_VERSION