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
119 /* full case mapping doesn't use posix semantics */
120 struct WordBoundaryState wbstate = {
121 .str = str,
122 .len = strlen(str),
123 .offset = 0,
124 .posix = false,
125 .init = false,
126 .prev_alnum = false,
127 };
128
132 unicode_strfold(fold, BUFSZ, str, len, true);
133 status = U_ZERO_ERROR;
135 status = U_ZERO_ERROR;
137 status = U_ZERO_ERROR;
139 status = U_ZERO_ERROR;
141
142 if (strcmp(lower, icu_lower) != 0)
143 {
144 printf("case_test: str='%s' lower='%s' icu_lower='%s'\n", str, lower,
145 icu_lower);
146 exit(1);
147 }
148 if (strcmp(title, icu_title) != 0)
149 {
150 printf("case_test: str='%s' title='%s' icu_title='%s'\n", str, title,
151 icu_title);
152 exit(1);
153 }
154 if (strcmp(upper, icu_upper) != 0)
155 {
156 printf("case_test: str='%s' upper='%s' icu_upper='%s'\n", str, upper,
157 icu_upper);
158 exit(1);
159 }
160 if (strcmp(fold, icu_fold) != 0)
161 {
162 printf("case_test: str='%s' fold='%s' icu_fold='%s'\n", str, fold,
163 icu_fold);
164 exit(1);
165 }
166}
167
168/*
169 * Exhaustively compare case mappings with the results from ICU.
170 */
171static void
172test_icu(void)
173{
174 int successful = 0;
175 int skipped_mismatch = 0;
176
177 for (char32_t code = 0; code <= 0x10ffff; code++)
178 {
179 pg_unicode_category category = unicode_category(code);
180
181 if (category != PG_U_UNASSIGNED)
182 {
184 char code_str[5] = {0};
185
187 {
189 continue;
190 }
191
192 icu_test_simple(code);
193 unicode_to_utf8(code, (unsigned char *) code_str);
195
196 successful++;
197 }
198 }
199
200 if (skipped_mismatch > 0)
201 printf("case_test: skipped %d codepoints unassigned in ICU due to Unicode version mismatch\n",
203
204 printf("case_test: ICU simple mapping test: %d codepoints successful\n",
205 successful);
206}
207#endif
208
209static void
211{
212 size_t src1len = strlen(test_string);
213 size_t dst1len = strlen(expected);
214 size_t dst2len = strlen(expected) + 1; /* NUL-terminated */
215 char *src1 = malloc(src1len);
216 char *dst1 = malloc(dst1len);
217 char *dst2 = malloc(dst2len);
218 size_t needed;
219
220 memcpy(src1, test_string, src1len); /* not NUL-terminated */
221
222 /* destination is not NUL-terminated */
223 memset(dst1, 0x7F, dst1len);
225 if (needed != strlen(expected))
226 {
227 printf("case_test: convert_case test1 FAILURE: '%s' needed %zu expected %zu\n",
229 exit(1);
230 }
231 if (memcmp(dst1, expected, dst1len) != 0)
232 {
233 printf("case_test: convert_case test1 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n",
235 exit(1);
236 }
237
238 /* destination is NUL-terminated */
239 memset(dst2, 0x7F, dst2len);
241 if (needed != strlen(expected))
242 {
243 printf("case_test: convert_case test2 FAILURE: '%s' needed %zu expected %zu\n",
245 exit(1);
246 }
247 if (strcmp(dst2, expected) != 0)
248 {
249 printf("case_test: convert_case test2 FAILURE: test: '%s' result: '%s' expected: '%s'\n",
251 exit(1);
252 }
253
254 free(src1);
255 free(dst1);
256 free(dst2);
257}
258
259static size_t
260tfunc_lower(char *dst, size_t dstsize, const char *src,
261 size_t srclen)
262{
263 return unicode_strlower(dst, dstsize, src, srclen, true);
264}
265
266static size_t
267tfunc_title(char *dst, size_t dstsize, const char *src,
268 size_t srclen)
269{
270 struct WordBoundaryState wbstate = {
271 .str = src,
272 .len = srclen,
273 .offset = 0,
274 .init = false,
275 .prev_alnum = false,
276 };
277
278 return unicode_strtitle(dst, dstsize, src, srclen, true, initcap_wbnext,
279 &wbstate);
280}
281
282static size_t
283tfunc_upper(char *dst, size_t dstsize, const char *src,
284 size_t srclen)
285{
286 return unicode_strupper(dst, dstsize, src, srclen, true);
287}
288
289static size_t
290tfunc_fold(char *dst, size_t dstsize, const char *src,
291 size_t srclen)
292{
293 return unicode_strfold(dst, dstsize, src, srclen, true);
294}
295
296static void
298{
299 /* test string with no case changes */
300 test_convert(tfunc_lower, "√∞", "√∞");
301 /* test adjust-to-cased behavior */
302 test_convert(tfunc_title, "abc 123xyz", "Abc 123xyz");
303 /* test string with case changes */
304 test_convert(tfunc_upper, "abc", "ABC");
305 /* test string with case changes and byte length changes */
306 test_convert(tfunc_lower, "ȺȺȺ", "ⱥⱥⱥ");
307 /* test special case conversions */
308 test_convert(tfunc_upper, "ß", "SS");
309 test_convert(tfunc_lower, "ıiIİ", "ıiii\u0307");
310 test_convert(tfunc_upper, "ıiIİ", "IIIİ");
311 test_convert(tfunc_fold, "ıiIİ", "ıiii\u0307");
312 /* test final sigma */
313 test_convert(tfunc_lower, "σςΣ ΣΣΣ", "σςς σσς");
314 test_convert(tfunc_lower, "σς'Σ' ΣΣ'Σ'", "σς'ς' σσ'ς'");
315 test_convert(tfunc_title, "σςΣ ΣΣΣ", "Σςς Σσς");
316 test_convert(tfunc_fold, "σςΣ ΣΣΣ", "σσσ σσσ");
317 /* test that alphanumerics are word characters */
318 test_convert(tfunc_title, "λλ", "Λλ");
319 test_convert(tfunc_title, "1a", "1a");
320 /* U+FF11 FULLWIDTH ONE is alphanumeric for full case mapping */
321 test_convert(tfunc_title, "\uFF11a", "\uFF11a");
322
323
324#ifdef USE_ICU
325 icu_test_full("");
326 icu_test_full("ȺȺȺ");
327 icu_test_full("ßßß");
328 icu_test_full("√∞");
329 icu_test_full("a b");
330 icu_test_full("abc 123xyz");
331 icu_test_full("σςΣ ΣΣΣ");
332 icu_test_full("ıiIİ");
333 icu_test_full("\uFF11a");
334 /* test <alpha><iota_subscript><acute> */
335 icu_test_full("\u0391\u0345\u0301");
336#endif
337
338 printf("case_test: convert_case: success\n");
339}
340
341int
342main(int argc, char **argv)
343{
344#ifdef USE_ICU
345 UErrorCode status = U_ZERO_ERROR;
346
347 /*
348 * Disable ICU's word break adjustment for titlecase to match the expected
349 * behavior of unicode_strtitle().
350 */
352 if (U_FAILURE(status))
353 {
354 printf("case_test: failure opening UCaseMap: %s\n",
355 u_errorName(status));
356 exit(1);
357 }
358#endif
359
360 printf("case_test: Postgres Unicode version:\t%s\n", PG_UNICODE_VERSION);
361#ifdef USE_ICU
362 printf("case_test: ICU Unicode version:\t\t%s\n", U_UNICODE_VERSION);
363 test_icu();
364#else
365 printf("case_test: ICU not available; skipping\n");
366#endif
367
369
370#ifdef USE_ICU
372#endif
373 exit(0);
374}
static void test_convert(TestFunc tfunc, const char *test_string, const char *expected)
Definition case_test.c:210
static void test_convert_case(void)
Definition case_test.c:297
static size_t tfunc_lower(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:260
static size_t tfunc_fold(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:290
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:283
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:267
#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_strtitle(char *dst, size_t dstsize, const char *src, size_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)
size_t unicode_strfold(char *dst, size_t dstsize, const char *src, size_t srclen, bool full)
size_t unicode_strupper(char *dst, size_t dstsize, const char *src, size_t srclen, bool full)
char32_t unicode_titlecase_simple(char32_t code)
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, bool full)
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