1 /********************************************************************
2 * Copyright (c) 2015, International Business Machines Corporation
3 * and others. All Rights Reserved.
4 ********************************************************************/
5 /* C API TEST for UListFormatter */
7 #include "unicode/utypes.h"
9 #if !UCONFIG_NO_FORMATTING
11 #include "unicode/ustring.h"
12 #include "unicode/ulistformatter.h"
17 static void TestUListFmt(void);
19 void addUListFmtTest(TestNode
** root
);
21 #define TESTCASE(x) addTest(root, &x, "tsformat/ulistfmttest/" #x)
23 void addUListFmtTest(TestNode
** root
)
25 TESTCASE(TestUListFmt
);
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 };
40 const char *expectedResult
; /* invariant chars + escaped Unicode */
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 */
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
));
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
));
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
));
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
));
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
));
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
));
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
));
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
);
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
);
122 ulistfmt_close(listfmt
);
128 #endif /* #if !UCONFIG_NO_FORMATTING */