PostgreSQL Source Code git master
case_test.c File Reference
#include "postgres_fe.h"
#include <locale.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wctype.h>
#include "common/unicode_case.h"
#include "common/unicode_category.h"
#include "common/unicode_version.h"
Include dependency graph for case_test.c:

Go to the source code of this file.

Data Structures

struct  WordBoundaryState
 

Macros

#define BUFSZ   256
 

Typedefs

typedef size_t(* TestFunc) (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 

Functions

static size_t initcap_wbnext (void *state)
 
static void test_convert (TestFunc tfunc, const char *test_string, const char *expected)
 
static size_t tfunc_lower (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static size_t tfunc_title (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static size_t tfunc_upper (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static size_t tfunc_fold (char *dst, size_t dstsize, const char *src, ssize_t srclen)
 
static void test_convert_case ()
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ BUFSZ

#define BUFSZ   256

Definition at line 29 of file case_test.c.

Typedef Documentation

◆ TestFunc

typedef size_t(* TestFunc) (char *dst, size_t dstsize, const char *src, ssize_t srclen)

Definition at line 35 of file case_test.c.

Function Documentation

◆ initcap_wbnext()

static size_t initcap_wbnext ( void *  state)
static

Definition at line 50 of file case_test.c.

51{
52 struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
53
54 while (wbstate->offset < wbstate->len &&
55 wbstate->str[wbstate->offset] != '\0')
56 {
57 pg_wchar u = utf8_to_unicode((unsigned char *) wbstate->str +
58 wbstate->offset);
59 bool curr_alnum = pg_u_isalnum(u, wbstate->posix);
60
61 if (!wbstate->init || curr_alnum != wbstate->prev_alnum)
62 {
63 size_t prev_offset = wbstate->offset;
64
65 wbstate->init = true;
66 wbstate->offset += unicode_utf8len(u);
67 wbstate->prev_alnum = curr_alnum;
68 return prev_offset;
69 }
70
71 wbstate->offset += unicode_utf8len(u);
72 }
73
74 return wbstate->len;
75}
static pg_wchar utf8_to_unicode(const unsigned char *c)
Definition: mbprint.c:53
unsigned int pg_wchar
Definition: mbprint.c:31
static int unicode_utf8len(pg_wchar c)
Definition: pg_wchar.h:607
Definition: regguts.h:323
bool pg_u_isalnum(pg_wchar code, bool posix)

References WordBoundaryState::init, WordBoundaryState::len, WordBoundaryState::offset, pg_u_isalnum(), WordBoundaryState::posix, WordBoundaryState::prev_alnum, WordBoundaryState::str, unicode_utf8len(), and utf8_to_unicode().

Referenced by tfunc_title().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 376 of file case_test.c.

377{
378#ifdef USE_ICU
379 UErrorCode status = U_ZERO_ERROR;
380
381 /*
382 * Disable ICU's word break adjustment for titlecase to match the expected
383 * behavior of unicode_strtitle().
384 */
385 casemap = ucasemap_open("und", U_TITLECASE_NO_BREAK_ADJUSTMENT, &status);
386 if (U_FAILURE(status))
387 {
388 printf("case_test: failure opening UCaseMap: %s\n",
389 u_errorName(status));
390 exit(1);
391 }
392#endif
393
394 printf("case_test: Postgres Unicode version:\t%s\n", PG_UNICODE_VERSION);
395#ifdef USE_ICU
396 printf("case_test: ICU Unicode version:\t\t%s\n", U_UNICODE_VERSION);
397 test_icu();
398#else
399 printf("case_test: ICU not available; skipping\n");
400#endif
401
403
404#ifdef USE_ICU
405 ucasemap_close(casemap);
406#endif
407 exit(0);
408}
static void test_convert_case()
Definition: case_test.c:331
#define printf(...)
Definition: port.h:245
static enum CaseMapResult casemap(pg_wchar u1, CaseKind casekind, bool full, const char *src, size_t srclen, size_t srcoff, pg_wchar *simple, const pg_wchar **special)
Definition: unicode_case.c:397
#define PG_UNICODE_VERSION

References casemap(), PG_UNICODE_VERSION, printf, and test_convert_case().

◆ test_convert()

static void test_convert ( TestFunc  tfunc,
const char *  test_string,
const char *  expected 
)
static

Definition at line 208 of file case_test.c.

209{
210 size_t src1len = strlen(test_string);
211 size_t src2len = -1; /* NUL-terminated */
212 size_t dst1len = strlen(expected);
213 size_t dst2len = strlen(expected) + 1; /* NUL-terminated */
214 char *src1 = malloc(src1len);
215 char *dst1 = malloc(dst1len);
216 char *src2 = strdup(test_string);
217 char *dst2 = malloc(dst2len);
218 size_t needed;
219
220 memcpy(src1, test_string, src1len); /* not NUL-terminated */
221
222 /* neither source nor destination are NUL-terminated */
223 memset(dst1, 0x7F, dst1len);
224 needed = tfunc(dst1, dst1len, src1, src1len);
225 if (needed != strlen(expected))
226 {
227 printf("case_test: convert_case test1 FAILURE: '%s' needed %zu expected %zu\n",
228 test_string, needed, strlen(expected));
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",
234 test_string, (int) dst1len, dst1, expected);
235 exit(1);
236 }
237
238 /* destination is NUL-terminated and source is not */
239 memset(dst2, 0x7F, dst2len);
240 needed = tfunc(dst2, dst2len, src1, src1len);
241 if (needed != strlen(expected))
242 {
243 printf("case_test: convert_case test2 FAILURE: '%s' needed %zu expected %zu\n",
244 test_string, needed, strlen(expected));
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",
250 test_string, dst2, expected);
251 exit(1);
252 }
253
254 /* source is NUL-terminated and destination is not */
255 memset(dst1, 0x7F, dst1len);
256 needed = tfunc(dst1, dst1len, src2, src2len);
257 if (needed != strlen(expected))
258 {
259 printf("case_test: convert_case test3 FAILURE: '%s' needed %zu expected %zu\n",
260 test_string, needed, strlen(expected));
261 printf("case_test: convert_case test3 FAILURE: needed %zu\n", needed);
262 exit(1);
263 }
264 if (memcmp(dst1, expected, dst1len) != 0)
265 {
266 printf("case_test: convert_case test3 FAILURE: test: '%s' result: '%.*s' expected: '%s'\n",
267 test_string, (int) dst1len, dst1, expected);
268 exit(1);
269 }
270
271 /* both source and destination are NUL-terminated */
272 memset(dst2, 0x7F, dst2len);
273 needed = tfunc(dst2, dst2len, src2, src2len);
274 if (needed != strlen(expected))
275 {
276 printf("case_test: convert_case test4 FAILURE: '%s' needed %zu expected %zu\n",
277 test_string, needed, strlen(expected));
278 exit(1);
279 }
280 if (strcmp(dst2, expected) != 0)
281 {
282 printf("case_test: convert_case test4 FAILURE: test: '%s' result: '%s' expected: '%s'\n",
283 test_string, dst2, expected);
284 exit(1);
285 }
286
287 free(src1);
288 free(dst1);
289 free(src2);
290 free(dst2);
291}
#define free(a)
Definition: header.h:65
#define malloc(a)
Definition: header.h:50

References free, malloc, and printf.

Referenced by test_convert_case().

◆ test_convert_case()

static void test_convert_case ( )
static

Definition at line 331 of file case_test.c.

332{
333 /* test string with no case changes */
334 test_convert(tfunc_lower, "√∞", "√∞");
335 /* test adjust-to-cased behavior */
336 test_convert(tfunc_title, "abc 123xyz", "Abc 123xyz");
337 /* test string with case changes */
338 test_convert(tfunc_upper, "abc", "ABC");
339 /* test string with case changes and byte length changes */
340 test_convert(tfunc_lower, "ȺȺȺ", "ⱥⱥⱥ");
341 /* test special case conversions */
342 test_convert(tfunc_upper, "ß", "SS");
343 test_convert(tfunc_lower, "ıiIİ", "ıiii\u0307");
344 test_convert(tfunc_upper, "ıiIİ", "IIIİ");
345 test_convert(tfunc_fold, "ıiIİ", "ıiii\u0307");
346 /* test final sigma */
347 test_convert(tfunc_lower, "σςΣ ΣΣΣ", "σςς σσς");
348 test_convert(tfunc_lower, "σς'Σ' ΣΣ'Σ'", "σς'ς' σσ'ς'");
349 test_convert(tfunc_title, "σςΣ ΣΣΣ", "Σςς Σσς");
350 test_convert(tfunc_fold, "σςΣ ΣΣΣ", "σσσ σσσ");
351 /* test that alphanumerics are word characters */
352 test_convert(tfunc_title, "λλ", "Λλ");
353 test_convert(tfunc_title, "1a", "1a");
354 /* U+FF11 FULLWIDTH ONE is alphanumeric for full case mapping */
355 test_convert(tfunc_title, "\uFF11a", "\uFF11a");
356
357
358#ifdef USE_ICU
359 icu_test_full("");
360 icu_test_full("ȺȺȺ");
361 icu_test_full("ßßß");
362 icu_test_full("√∞");
363 icu_test_full("a b");
364 icu_test_full("abc 123xyz");
365 icu_test_full("σςΣ ΣΣΣ");
366 icu_test_full("ıiIİ");
367 icu_test_full("\uFF11a");
368 /* test <alpha><iota_subscript><acute> */
369 icu_test_full("\u0391\u0345\u0301");
370#endif
371
372 printf("case_test: convert_case: success\n");
373}
static void test_convert(TestFunc tfunc, const char *test_string, const char *expected)
Definition: case_test.c:208
static size_t tfunc_lower(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: case_test.c:294
static size_t tfunc_title(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: case_test.c:301
static size_t tfunc_upper(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: case_test.c:317
static size_t tfunc_fold(char *dst, size_t dstsize, const char *src, ssize_t srclen)
Definition: case_test.c:324

References printf, test_convert(), tfunc_fold(), tfunc_lower(), tfunc_title(), and tfunc_upper().

Referenced by main().

◆ tfunc_fold()

static size_t tfunc_fold ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 324 of file case_test.c.

326{
327 return unicode_strfold(dst, dstsize, src, srclen, true);
328}
size_t unicode_strfold(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:189

References unicode_strfold().

Referenced by test_convert_case().

◆ tfunc_lower()

static size_t tfunc_lower ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 294 of file case_test.c.

296{
297 return unicode_strlower(dst, dstsize, src, srclen, true);
298}
size_t unicode_strlower(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:101

References unicode_strlower().

Referenced by test_convert_case().

◆ tfunc_title()

static size_t tfunc_title ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 301 of file case_test.c.

303{
304 struct WordBoundaryState wbstate = {
305 .str = src,
306 .len = srclen,
307 .offset = 0,
308 .init = false,
309 .prev_alnum = false,
310 };
311
312 return unicode_strtitle(dst, dstsize, src, srclen, true, initcap_wbnext,
313 &wbstate);
314}
static size_t initcap_wbnext(void *state)
Definition: case_test.c:50
size_t unicode_strtitle(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)
Definition: unicode_case.c:138

References initcap_wbnext(), WordBoundaryState::str, and unicode_strtitle().

Referenced by test_convert_case().

◆ tfunc_upper()

static size_t tfunc_upper ( char *  dst,
size_t  dstsize,
const char *  src,
ssize_t  srclen 
)
static

Definition at line 317 of file case_test.c.

319{
320 return unicode_strupper(dst, dstsize, src, srclen, true);
321}
size_t unicode_strupper(char *dst, size_t dstsize, const char *src, ssize_t srclen, bool full)
Definition: unicode_case.c:165

References unicode_strupper().

Referenced by test_convert_case().