PostgreSQL Source Code git master
Loading...
Searching...
No Matches
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 "mb/pg_wchar.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, size_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, size_t srclen)
 
static size_t tfunc_title (char *dst, size_t dstsize, const char *src, size_t srclen)
 
static size_t tfunc_upper (char *dst, size_t dstsize, const char *src, size_t srclen)
 
static size_t tfunc_fold (char *dst, size_t dstsize, const char *src, size_t srclen)
 
static void test_convert_case (void)
 
int main (int argc, char **argv)
 

Macro Definition Documentation

◆ BUFSZ

#define BUFSZ   256

Definition at line 30 of file case_test.c.

Typedef Documentation

◆ TestFunc

Definition at line 36 of file case_test.c.

Function Documentation

◆ initcap_wbnext()

static size_t initcap_wbnext ( void state)
static

Definition at line 51 of file case_test.c.

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}
static char32_t utf8_to_unicode(const unsigned char *c)
Definition mbprint.c:53
static int unicode_utf8len(char32_t c)
Definition pg_wchar.h:460
static int fb(int x)
bool pg_u_isalnum(char32_t code, bool posix)

References fb(), pg_u_isalnum(), unicode_utf8len(), and utf8_to_unicode().

Referenced by tfunc_title().

◆ main()

int main ( int  argc,
char **  argv 
)

Definition at line 342 of file case_test.c.

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_case(void)
Definition case_test.c:297
#define printf(...)
Definition port.h:267
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)
#define PG_UNICODE_VERSION

References casemap(), fb(), 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 210 of file case_test.c.

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}
memcpy(sums, checksumBaseOffsets, sizeof(checksumBaseOffsets))
#define free(a)
#define malloc(a)

References fb(), free, malloc, memcpy(), and printf.

Referenced by test_convert_case().

◆ test_convert_case()

static void test_convert_case ( void  )
static

Definition at line 297 of file case_test.c.

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}
static void test_convert(TestFunc tfunc, const char *test_string, const char *expected)
Definition case_test.c:210
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 tfunc_upper(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:283
static size_t tfunc_title(char *dst, size_t dstsize, const char *src, size_t srclen)
Definition case_test.c:267

References fb(), 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,
size_t  srclen 
)
static

Definition at line 290 of file case_test.c.

292{
293 return unicode_strfold(dst, dstsize, src, srclen, true);
294}
size_t unicode_strfold(char *dst, size_t dstsize, const char *src, size_t srclen, bool full)

References fb(), and unicode_strfold().

Referenced by test_convert_case().

◆ tfunc_lower()

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

Definition at line 260 of file case_test.c.

262{
263 return unicode_strlower(dst, dstsize, src, srclen, true);
264}
size_t unicode_strlower(char *dst, size_t dstsize, const char *src, size_t srclen, bool full)

References fb(), and unicode_strlower().

Referenced by test_convert_case().

◆ tfunc_title()

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

Definition at line 267 of file case_test.c.

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}
static size_t initcap_wbnext(void *state)
Definition case_test.c:51
size_t unicode_strtitle(char *dst, size_t dstsize, const char *src, size_t srclen, bool full, WordBoundaryNext wbnext, void *wbstate)

References fb(), 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,
size_t  srclen 
)
static

Definition at line 283 of file case_test.c.

285{
286 return unicode_strupper(dst, dstsize, src, srclen, true);
287}
size_t unicode_strupper(char *dst, size_t dstsize, const char *src, size_t srclen, bool full)

References fb(), and unicode_strupper().

Referenced by test_convert_case().