21 #include "test_unicode.h"
40 #define BUFSIZE (size_t) 16
47 #define BUFSIZE_UTF8 (BUFSIZE * (size_t) 4 + (size_t) 2)
49 #include "uc_test_nfc.c"
69 void test_unicode_octets(
char* ob,
size_t len_ob,
const char* uc_string)
71 size_t len = strlen(uc_string);
74 if (0U == len_ob || 3U * len > len_ob)
82 api_posix_snprintf(&ob[3U * i], len_ob - 3U * i,
83 "%02X ", (
unsigned int)(
unsigned char)uc_string[i]);
107 static int test_unicode_conformance(
size_t record,
const char* src_utf8,
108 const char* nfc_utf8,
109 const char* nfd_utf8)
111 int res = API_POSIX_EXIT_SUCCESS;
112 const char* buf1 = NULL;
113 const char* buf2 = NULL;
114 const char* buf3 = NULL;
116 const char* input = NULL;
117 const char* err = NULL;
121 printf(
"================\nrecord: %u\n", (
unsigned int) record);
122 printf(
"src_utf8: \"%s\"\n", src_utf8);
123 printf(
"nfc_utf8: \"%s\"\n", nfc_utf8);
124 printf(
"nfd_utf8: \"%s\"\n", nfd_utf8);
125 printf(
"================\n"),
134 res = API_POSIX_EXIT_FAILURE;
136 else if(strcmp(nfc_utf8, buf1))
140 res = API_POSIX_EXIT_FAILURE;
144 if(API_POSIX_EXIT_SUCCESS == res)
151 res = API_POSIX_EXIT_FAILURE;
153 else if(strcmp(nfc_utf8, buf2))
157 res = API_POSIX_EXIT_FAILURE;
162 if(API_POSIX_EXIT_SUCCESS == res)
169 res = API_POSIX_EXIT_FAILURE;
171 else if(strcmp(nfc_utf8, buf3))
175 res = API_POSIX_EXIT_FAILURE;
180 if(API_POSIX_EXIT_SUCCESS != res && NULL != err)
182 #if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
183 loc = api_posix_setlocale(API_POSIX_LC_CTYPE,
"");
189 print_error(
"Setting locale for debug messages failed");
194 if(NULL == strstr(loc,
"UTF") && NULL == strstr(loc,
"utf"))
197 "Debug messages can't be printed with current locale");
201 #define TEST_BUFSIZE 60U
202 char ob[TEST_BUFSIZE];
204 fprintf(stderr,
TEST_TAB "Record number in test data file: %lu\n",
205 (
unsigned long int) record);
206 test_unicode_octets(ob, TEST_BUFSIZE, input);
207 fprintf(stderr,
TEST_TAB "Input data: \"%s\" (%s)\n", input, ob);
208 test_unicode_octets(ob, TEST_BUFSIZE, err);
209 fprintf(stderr,
TEST_TAB "Result is : \"%s\" (%s)\n", err, ob);
210 test_unicode_octets(ob, TEST_BUFSIZE, nfc_utf8);
211 fprintf(stderr,
TEST_TAB "Should be : \"%s\" (%s)\n", nfc_utf8, ob);
217 if(buf1 != src_utf8) {
enc_free((
void*) buf1); }
218 if(buf2 != nfc_utf8) {
enc_free((
void*) buf2); }
219 if(buf3 != nfd_utf8) {
enc_free((
void*) buf3); }
233 static int test_unicode_part2(
void)
235 int res = API_POSIX_EXIT_SUCCESS;
238 enum sm_state state = SM_SRC;
239 long int src[BUFSIZE];
240 long int nfc[BUFSIZE];
241 long int nfd[BUFSIZE];
245 char src_utf8[BUFSIZE_UTF8];
246 char nfc_utf8[BUFSIZE_UTF8];
247 char nfd_utf8[BUFSIZE_UTF8];
252 while(API_POSIX_EXIT_SUCCESS == res && -1 != (ucp = uc_test_nfc_table[i++]))
259 if(-2L == ucp) { state = SM_NFC; }
263 res = API_POSIX_EXIT_FAILURE;
270 res = API_POSIX_EXIT_FAILURE;
272 else { src[src_i++] = ucp; }
278 if(-2L == ucp) { state = SM_NFD; }
282 res = API_POSIX_EXIT_FAILURE;
289 res = API_POSIX_EXIT_FAILURE;
291 else { nfc[nfc_i++] = ucp; }
300 src_utf8[0] =
' '; utf8_i = 1;
302 src_utf8[utf8_i] = 0;
303 nfc_utf8[0] =
' '; utf8_i = 1;
305 nfc_utf8[utf8_i] = 0;
306 nfd_utf8[0] =
' '; utf8_i = 1;
308 nfd_utf8[utf8_i] = 0;
309 if(src_i || nfc_i || nfd_i)
312 res = API_POSIX_EXIT_FAILURE;
317 res = test_unicode_conformance(rec++,
318 src_utf8, nfc_utf8, nfd_utf8);
326 res = API_POSIX_EXIT_FAILURE;
333 res = API_POSIX_EXIT_FAILURE;
335 else { nfd[nfd_i++] = ucp; }
342 res = API_POSIX_EXIT_FAILURE;
388 #define TS_NUM (size_t) 11
389 #define TS_UTF7 (size_t) 8
390 static const char* ts[TS_NUM] =
393 "This is an ASCII string",
394 "This is an Unicode string: \xC3\xA4word",
395 "This is an Unicode string: \xE0\xAD\x9Cword",
396 "This is an Unicode string: a\xCC\x88word",
397 "This is an Unicode string: start\xCE\xB1\xCC\x94\xCC\x81\xCD\x85word",
398 "This is an Unicode string: start\xCE\xB1\xCC\x81\xCC\x94\xCD\x85word",
399 "This is an Unicode string: \xE2\x84\xA6word",
400 "Composition of hangul jamo: \xE1\x84\x91\xE1\x85\xB1\xE1\x86\xB6",
402 "This is an Unicode string: hundertf+AHUDCA-nfzig",
403 "This is an Unicode string: Violinschl+APw-ssel (+2DTdHg-)",
404 "Shift sequence terminated by SP or end-of-data: A+AMQ- O+ANY U+ANw"
406 static const char* rs[TS_NUM] = {
408 "This is an ASCII string",
409 "This is an Unicode string: \xC3\xA4word",
410 "This is an Unicode string: \xE0\xAC\xA1\xE0\xAC\xBCword",
411 "This is an Unicode string: \xC3\xA4word",
412 "This is an Unicode string: start\xE1\xBE\x85word",
413 "This is an Unicode string: start\xE1\xBE\xB4\xCC\x94word",
414 "This is an Unicode string: \xCE\xA9word",
415 "Composition of hangul jamo: \xED\x93\x9B",
417 "This is an Unicode string: hundertf\xC3\xBCnfzig",
418 "This is an Unicode string: Violinschl\xC3\xBCssel (\xF0\x9D\x84\x9E)",
419 "Shift sequence terminated by SP or end-of-data: A\xC3\x84 O\xC3\x96 U\xC3\x9C"
421 int res = API_POSIX_EXIT_SUCCESS;
427 for(i = 0; i < TS_NUM; ++i)
440 res = API_POSIX_EXIT_FAILURE;
443 if(strcmp(rs[i], buf))
447 #if CFG_USE_POSIX_API >= 200112 || CFG_USE_XSI
448 loc = api_posix_setlocale(API_POSIX_LC_CTYPE,
"");
454 print_error(
"Setting locale for debug messages failed");
459 if(NULL == strstr(loc,
"UTF") && NULL == strstr(loc,
"utf"))
462 "Debug messages can't be printed with current locale");
466 fprintf(stderr,
TEST_TAB "Input data: \"%s\"\n", ts[i]);
467 fprintf(stderr,
TEST_TAB "Result is : \"%s\"\n", buf);
468 fprintf(stderr,
TEST_TAB "Should be : \"%s\"\n", rs[i]);
471 res = API_POSIX_EXIT_FAILURE;
474 if(buf != ts[i]) {
enc_free((
void*) buf); }
478 if(API_POSIX_EXIT_SUCCESS == res)
480 res = test_unicode_part2();