1 // © 2016 and later: Unicode, Inc. and others.
2 // License & terms of use: http://www.unicode.org/copyright.html
3 /********************************************************************
4 * Copyright (c) 2015, International Business Machines Corporation
5 * and others. All Rights Reserved.
6 ********************************************************************/
7 /* C API TEST for UListFormatter */
9 #include "unicode/utypes.h"
11 #if !UCONFIG_NO_FORMATTING
13 #include "unicode/ustring.h"
14 #include "unicode/ulistformatter.h"
19 static void TestUListFmt(void);
21 void addUListFmtTest(TestNode
** root
);
23 #define TESTCASE(x) addTest(root, &x, "tsformat/ulistfmttest/" #x)
25 void addUListFmtTest(TestNode
** root
)
27 TESTCASE(TestUListFmt
);
30 static const UChar str0
[] = { 0x41,0 }; /* "A" */
31 static const UChar str1
[] = { 0x42,0x62,0 }; /* "Bb" */
32 static const UChar str2
[] = { 0x43,0x63,0x63,0 }; /* "Ccc" */
33 static const UChar str3
[] = { 0x44,0x64,0x64,0x64,0 }; /* "Dddd" */
34 static const UChar str4
[] = { 0x45,0x65,0x65,0x65,0x65,0 }; /* "Eeeee" */
35 static const UChar
* strings
[] = { str0
, str1
, str2
, str3
, str4
};
36 static const int32_t stringLengths
[] = { 1, 2, 3, 4, 5 };
37 static const int32_t stringLengthsNeg
[] = { -1, -1, -1, -1, -1 };
42 const char *expectedResult
; /* invariant chars + escaped Unicode */
45 static ListFmtTestEntry listFmtTestEntries
[] = {
46 /* locale stringCount expectedResult */
47 { "en" , 5, "A, Bb, Ccc, Dddd, and Eeeee" },
48 { "en" , 2, "A and Bb" },
49 { "de" , 5, "A, Bb, Ccc, Dddd und Eeeee" },
50 { "de" , 2, "A und Bb" },
51 { "ja" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u3001Eeeee" },
52 { "ja" , 2, "A\\u3001Bb" },
53 { "zh" , 5, "A\\u3001Bb\\u3001Ccc\\u3001Dddd\\u548CEeeee" },
54 { "zh" , 2, "A\\u548CBb" },
55 { NULL
, 0, NULL
} /* terminator */
63 static void TestUListFmt() {
64 const ListFmtTestEntry
* lftep
;
65 for (lftep
= listFmtTestEntries
; lftep
->locale
!= NULL
; lftep
++ ) {
66 UErrorCode status
= U_ZERO_ERROR
;
67 UListFormatter
*listfmt
= ulistfmt_open(lftep
->locale
, &status
);
68 if ( U_FAILURE(status
) ) {
69 log_data_err("ERROR: ulistfmt_open fails for locale %s, status %s\n", lftep
->locale
, u_errorName(status
));
71 UChar ubufActual
[kUBufMax
];
72 int32_t ulenActual
= ulistfmt_format(listfmt
, strings
, stringLengths
, lftep
->stringCount
, ubufActual
, kUBufMax
, &status
);
73 if ( U_FAILURE(status
) ) {
74 log_err("ERROR: ulistfmt_format fails for locale %s count %d (real lengths), status %s\n", lftep
->locale
, lftep
->stringCount
, u_errorName(status
));
76 UChar ubufExpected
[kUBufMax
];
77 int32_t ulenExpected
= u_unescape(lftep
->expectedResult
, ubufExpected
, kUBufMax
);
78 if (ulenActual
!= ulenExpected
|| u_strncmp(ubufActual
, ubufExpected
, ulenExpected
) != 0) {
79 log_err("ERROR: ulistfmt_format for locale %s count %d (real lengths), actual \"%s\" != expected \"%s\"\n", lftep
->locale
,
80 lftep
->stringCount
, aescstrdup(ubufActual
, ulenActual
), aescstrdup(ubufExpected
, ulenExpected
));
83 /* try again with all lengths -1 */
84 status
= U_ZERO_ERROR
;
85 ulenActual
= ulistfmt_format(listfmt
, strings
, stringLengthsNeg
, lftep
->stringCount
, ubufActual
, kUBufMax
, &status
);
86 if ( U_FAILURE(status
) ) {
87 log_err("ERROR: ulistfmt_format fails for locale %s count %d (-1 lengths), status %s\n", lftep
->locale
, lftep
->stringCount
, u_errorName(status
));
89 UChar ubufExpected
[kUBufMax
];
90 int32_t ulenExpected
= u_unescape(lftep
->expectedResult
, ubufExpected
, kUBufMax
);
91 if (ulenActual
!= ulenExpected
|| u_strncmp(ubufActual
, ubufExpected
, ulenExpected
) != 0) {
92 log_err("ERROR: ulistfmt_format for locale %s count %d (-1 lengths), actual \"%s\" != expected \"%s\"\n", lftep
->locale
,
93 lftep
->stringCount
, aescstrdup(ubufActual
, ulenActual
), aescstrdup(ubufExpected
, ulenExpected
));
96 /* try again with NULL lengths */
97 status
= U_ZERO_ERROR
;
98 ulenActual
= ulistfmt_format(listfmt
, strings
, NULL
, lftep
->stringCount
, ubufActual
, kUBufMax
, &status
);
99 if ( U_FAILURE(status
) ) {
100 log_err("ERROR: ulistfmt_format fails for locale %s count %d (NULL lengths), status %s\n", lftep
->locale
, lftep
->stringCount
, u_errorName(status
));
102 UChar ubufExpected
[kUBufMax
];
103 int32_t ulenExpected
= u_unescape(lftep
->expectedResult
, ubufExpected
, kUBufMax
);
104 if (ulenActual
!= ulenExpected
|| u_strncmp(ubufActual
, ubufExpected
, ulenExpected
) != 0) {
105 log_err("ERROR: ulistfmt_format for locale %s count %d (NULL lengths), actual \"%s\" != expected \"%s\"\n", lftep
->locale
,
106 lftep
->stringCount
, aescstrdup(ubufActual
, ulenActual
), aescstrdup(ubufExpected
, ulenExpected
));
110 /* try calls that should return error */
111 status
= U_ZERO_ERROR
;
112 ulenActual
= ulistfmt_format(listfmt
, NULL
, NULL
, lftep
->stringCount
, ubufActual
, kUBufMax
, &status
);
113 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
|| ulenActual
> 0) {
114 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
,
115 lftep
->stringCount
, u_errorName(status
), ulenActual
);
117 status
= U_ZERO_ERROR
;
118 ulenActual
= ulistfmt_format(listfmt
, strings
, NULL
, lftep
->stringCount
, NULL
, kUBufMax
, &status
);
119 if (status
!= U_ILLEGAL_ARGUMENT_ERROR
|| ulenActual
> 0) {
120 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
,
121 lftep
->stringCount
, u_errorName(status
), ulenActual
);
124 ulistfmt_close(listfmt
);
130 #endif /* #if !UCONFIG_NO_FORMATTING */