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 "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, 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 30 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 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{
53 struct WordBoundaryState *wbstate = (struct WordBoundaryState *) state;
54
55 while (wbstate->offset < wbstate->len &&
56 wbstate->str[wbstate->offset] != '\0')
57 {
58 char32_t u = utf8_to_unicode((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:607
Definition: regguts.h:323
bool pg_u_isalnum(char32_t 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 377 of file case_test.c.

378{
379#ifdef USE_ICU
380 UErrorCode status = U_ZERO_ERROR;
381
382 /*
383 * Disable ICU's word break adjustment for titlecase to match the expected
384 * behavior of unicode_strtitle().
385 */
386 casemap = ucasemap_open("und", U_TITLECASE_NO_BREAK_ADJUSTMENT, &status);
387 if (U_FAILURE(status))
388 {
389 printf("case_test: failure opening UCaseMap: %s\n",
390 u_errorName(status));
391 exit(1);
392 }
393#endif
394
395 printf("case_test: Postgres Unicode version:\t%s\n", PG_UNICODE_VERSION);
396#ifdef USE_ICU
397 printf("case_test: ICU Unicode version:\t\t%s\n", U_UNICODE_VERSION);
398 test_icu();
399#else
400 printf("case_test: ICU not available; skipping\n");
401#endif
402
404
405#ifdef USE_ICU
406 ucasemap_close(casemap);
407#endif
408 exit(0);
409}
static void test_convert_case()
Definition: case_test.c:332
#define printf(...)
Definition: port.h:266
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)
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 209 of file case_test.c.

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

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

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 325 of file case_test.c.

327{
328 return unicode_strfold(dst, dstsize, src, srclen, true);
329}
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 295 of file case_test.c.

297{
298 return unicode_strlower(dst, dstsize, src, srclen, true);
299}
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 302 of file case_test.c.

304{
305 struct WordBoundaryState wbstate = {
306 .str = src,
307 .len = srclen,
308 .offset = 0,
309 .init = false,
310 .prev_alnum = false,
311 };
312
313 return unicode_strtitle(dst, dstsize, src, srclen, true, initcap_wbnext,
314 &wbstate);
315}
static size_t initcap_wbnext(void *state)
Definition: case_test.c:51
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 318 of file case_test.c.

320{
321 return unicode_strupper(dst, dstsize, src, srclen, true);
322}
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().