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 */ 8 9 #include "unicode/utypes.h" 10 11 #if !UCONFIG_NO_FORMATTING 12 13 #include "unicode/ustring.h" 14 #include "unicode/ulistformatter.h" 15 #include "cintltst.h" 16 #include "cmemory.h" 17 #include "cstring.h" 18 19 static void TestUListFmt(void); 20 21 void addUListFmtTest(TestNode** root); 22 23 #define TESTCASE(x) addTest(root, &x, "tsformat/ulistfmttest/" #x) 24 25 void addUListFmtTest(TestNode** root) 26 { 27 TESTCASE(TestUListFmt); 28 } 29 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 }; 38 39 typedef struct { 40 const char * locale; 41 int32_t stringCount; 42 const char *expectedResult; /* invariant chars + escaped Unicode */ 43 } ListFmtTestEntry; 44 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 */ 56 }; 57 58 enum { 59 kUBufMax = 128, 60 kBBufMax = 256 61 }; 62 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)); 70 } else { 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)); 75 } else { 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)); 81 } 82 } 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)); 88 } else { 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)); 94 } 95 } 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)); 101 } else { 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)); 107 } 108 } 109 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); 116 } 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); 122 } 123 124 ulistfmt_close(listfmt); 125 } 126 } 127 } 128 129 130 #endif /* #if !UCONFIG_NO_FORMATTING */ 131