]>
Commit | Line | Data |
---|---|---|
b331163b A |
1 | /******************************************************************** |
2 | * Copyright (c) 2015, International Business Machines Corporation | |
3 | * and others. All Rights Reserved. | |
4 | ********************************************************************/ | |
5 | /* C API TEST for UListFormatter */ | |
6 | ||
7 | #include "unicode/utypes.h" | |
8 | ||
9 | #if !UCONFIG_NO_FORMATTING | |
10 | ||
11 | #include "unicode/ustring.h" | |
12 | #include "unicode/ulistformatter.h" | |
13 | #include "cintltst.h" | |
14 | #include "cmemory.h" | |
15 | #include "cstring.h" | |
16 | ||
17 | static void TestUListFmt(void); | |
18 | ||
19 | void addUListFmtTest(TestNode** root); | |
20 | ||
21 | #define TESTCASE(x) addTest(root, &x, "tsformat/ulistfmttest/" #x) | |
22 | ||
23 | void addUListFmtTest(TestNode** root) | |
24 | { | |
25 | TESTCASE(TestUListFmt); | |
26 | } | |
27 | ||
28 | static const UChar str0[] = { 0x41,0 }; /* "A" */ | |
29 | static const UChar str1[] = { 0x42,0x62,0 }; /* "Bb" */ | |
30 | static const UChar str2[] = { 0x43,0x63,0x63,0 }; /* "Ccc" */ | |
31 | static const UChar str3[] = { 0x44,0x64,0x64,0x64,0 }; /* "Dddd" */ | |
32 | static const UChar str4[] = { 0x45,0x65,0x65,0x65,0x65,0 }; /* "Eeeee" */ | |
33 | static const UChar* strings[] = { str0, str1, str2, str3, str4 }; | |
34 | static const int32_t stringLengths[] = { 1, 2, 3, 4, 5 }; | |
35 | static const int32_t stringLengthsNeg[] = { -1, -1, -1, -1, -1 }; | |
36 | ||
37 | typedef struct { | |
38 | const char * locale; | |
39 | int32_t stringCount; | |
40 | const char *expectedResult; /* invariant chars + escaped Unicode */ | |
41 | } ListFmtTestEntry; | |
42 | ||
43 | static ListFmtTestEntry listFmtTestEntries[] = { | |
44 | /* locale stringCount expectedResult */ | |
45 | { "en" , 5, "A, Bb, Ccc, Dddd, and Eeeee" }, | |
46 | { "en" , 2, "A and Bb" }, | |
47 | { "de" , 5, "A, Bb, Ccc, Dddd und Eeeee" }, | |
48 | { "de" , 2, "A und Bb" }, | |
49 | { "ja" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u3001Eeeee" }, | |
50 | { "ja" , 2, "A\\u3001Bb" }, | |
51 | { "zh" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u548CEeeee" }, | |
52 | { "zh" , 2, "A\\u548CBb" }, | |
53 | { NULL , 0, NULL } /* terminator */ | |
54 | }; | |
55 | ||
56 | enum { | |
57 | kUBufMax = 128, | |
58 | kBBufMax = 256 | |
59 | }; | |
60 | ||
61 | static void TestUListFmt() { | |
62 | const ListFmtTestEntry * lftep; | |
63 | for (lftep = listFmtTestEntries; lftep->locale != NULL ; lftep++ ) { | |
64 | UErrorCode status = U_ZERO_ERROR; | |
65 | UListFormatter *listfmt = ulistfmt_open(lftep->locale, &status); | |
66 | if ( U_FAILURE(status) ) { | |
67 | log_data_err("ERROR: ulistfmt_open fails for locale %s, status %s\n", lftep->locale, u_errorName(status)); | |
68 | } else { | |
69 | UChar ubufActual[kUBufMax]; | |
70 | int32_t ulenActual = ulistfmt_format(listfmt, strings, stringLengths, lftep->stringCount, ubufActual, kUBufMax, &status); | |
71 | if ( U_FAILURE(status) ) { | |
72 | log_err("ERROR: ulistfmt_format fails for locale %s count %d (real lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); | |
73 | } else { | |
74 | UChar ubufExpected[kUBufMax]; | |
75 | int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); | |
76 | if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { | |
77 | log_err("ERROR: ulistfmt_format for locale %s count %d (real lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, | |
78 | lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); | |
79 | } | |
80 | } | |
81 | /* try again with all lengths -1 */ | |
82 | status = U_ZERO_ERROR; | |
83 | ulenActual = ulistfmt_format(listfmt, strings, stringLengthsNeg, lftep->stringCount, ubufActual, kUBufMax, &status); | |
84 | if ( U_FAILURE(status) ) { | |
85 | log_err("ERROR: ulistfmt_format fails for locale %s count %d (-1 lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); | |
86 | } else { | |
87 | UChar ubufExpected[kUBufMax]; | |
88 | int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); | |
89 | if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { | |
90 | log_err("ERROR: ulistfmt_format for locale %s count %d (-1 lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, | |
91 | lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); | |
92 | } | |
93 | } | |
94 | /* try again with NULL lengths */ | |
95 | status = U_ZERO_ERROR; | |
96 | ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, ubufActual, kUBufMax, &status); | |
97 | if ( U_FAILURE(status) ) { | |
98 | log_err("ERROR: ulistfmt_format fails for locale %s count %d (NULL lengths), status %s\n", lftep->locale, lftep->stringCount, u_errorName(status)); | |
99 | } else { | |
100 | UChar ubufExpected[kUBufMax]; | |
101 | int32_t ulenExpected = u_unescape(lftep->expectedResult, ubufExpected, kUBufMax); | |
102 | if (ulenActual != ulenExpected || u_strncmp(ubufActual, ubufExpected, ulenExpected) != 0) { | |
103 | log_err("ERROR: ulistfmt_format for locale %s count %d (NULL lengths), actual \"%s\" != expected \"%s\"\n", lftep->locale, | |
104 | lftep->stringCount, aescstrdup(ubufActual, ulenActual), aescstrdup(ubufExpected, ulenExpected)); | |
105 | } | |
106 | } | |
107 | ||
108 | /* try calls that should return error */ | |
109 | status = U_ZERO_ERROR; | |
110 | ulenActual = ulistfmt_format(listfmt, NULL, NULL, lftep->stringCount, ubufActual, kUBufMax, &status); | |
111 | if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) { | |
112 | log_err("ERROR: ulistfmt_format for locale %s count %d with NULL strings, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale, | |
113 | lftep->stringCount, u_errorName(status), ulenActual); | |
114 | } | |
115 | status = U_ZERO_ERROR; | |
116 | ulenActual = ulistfmt_format(listfmt, strings, NULL, lftep->stringCount, NULL, kUBufMax, &status); | |
117 | if (status != U_ILLEGAL_ARGUMENT_ERROR || ulenActual > 0) { | |
118 | log_err("ERROR: ulistfmt_format for locale %s count %d with NULL result, expected U_ILLEGAL_ARGUMENT_ERROR, got %s, result %d\n", lftep->locale, | |
119 | lftep->stringCount, u_errorName(status), ulenActual); | |
120 | } | |
121 | ||
122 | ulistfmt_close(listfmt); | |
123 | } | |
124 | } | |
125 | } | |
126 | ||
127 | ||
128 | #endif /* #if !UCONFIG_NO_FORMATTING */ |