| OLD | NEW |
| 1 /* | 1 /* |
| 2 ******************************************************************************* | 2 ******************************************************************************* |
| 3 * Copyright (C) 2014, International Business Machines Corporation and * | 3 * Copyright (C) 2014, International Business Machines Corporation and * |
| 4 * others. All Rights Reserved. * | 4 * others. All Rights Reserved. * |
| 5 ******************************************************************************* | 5 ******************************************************************************* |
| 6 * | 6 * |
| 7 * File SIMPLEPATTERNFORMATTERTEST.CPP | 7 * File SIMPLEPATTERNFORMATTERTEST.CPP |
| 8 * | 8 * |
| 9 ******************************************************************************** | 9 ******************************************************************************** |
| 10 */ | 10 */ |
| 11 #include "cstring.h" | 11 #include "cstring.h" |
| 12 #include "intltest.h" | 12 #include "intltest.h" |
| 13 #include "simplepatternformatter.h" | 13 #include "simplepatternformatter.h" |
| 14 | 14 |
| 15 class SimplePatternFormatterTest : public IntlTest { | 15 class SimplePatternFormatterTest : public IntlTest { |
| 16 public: | 16 public: |
| 17 SimplePatternFormatterTest() { | 17 SimplePatternFormatterTest() { |
| 18 } | 18 } |
| 19 void TestNoPlaceholders(); | 19 void TestNoPlaceholders(); |
| 20 void TestOnePlaceholder(); | 20 void TestOnePlaceholder(); |
| 21 void TestManyPlaceholders(); | 21 void TestManyPlaceholders(); |
| 22 void TestTooFewPlaceholderValues(); |
| 23 void TestBadArguments(); |
| 22 void TestGetPatternWithNoPlaceholders(); | 24 void TestGetPatternWithNoPlaceholders(); |
| 23 void TestOptimization(); | 25 void TestFormatReplaceNoOptimization(); |
| 26 void TestFormatReplaceNoOptimizationLeadingText(); |
| 27 void TestFormatReplaceOptimization(); |
| 28 void TestFormatReplaceNoOptimizationLeadingPlaceholderUsedTwice(); |
| 29 void TestFormatReplaceOptimizationNoOffsets(); |
| 30 void TestFormatReplaceNoOptimizationNoOffsets(); |
| 24 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=
0); | 31 void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=
0); |
| 25 private: | 32 private: |
| 33 void verifyOffsets( |
| 34 const int32_t *expected, |
| 35 const int32_t *actual, |
| 36 int32_t count); |
| 26 }; | 37 }; |
| 27 | 38 |
| 28 void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const
char* &name, char* /*par*/) { | 39 void SimplePatternFormatterTest::runIndexedTest(int32_t index, UBool exec, const
char* &name, char* /*par*/) { |
| 29 TESTCASE_AUTO_BEGIN; | 40 TESTCASE_AUTO_BEGIN; |
| 30 TESTCASE_AUTO(TestNoPlaceholders); | 41 TESTCASE_AUTO(TestNoPlaceholders); |
| 31 TESTCASE_AUTO(TestOnePlaceholder); | 42 TESTCASE_AUTO(TestOnePlaceholder); |
| 32 TESTCASE_AUTO(TestManyPlaceholders); | 43 TESTCASE_AUTO(TestManyPlaceholders); |
| 44 TESTCASE_AUTO(TestTooFewPlaceholderValues); |
| 45 TESTCASE_AUTO(TestBadArguments); |
| 33 TESTCASE_AUTO(TestGetPatternWithNoPlaceholders); | 46 TESTCASE_AUTO(TestGetPatternWithNoPlaceholders); |
| 34 TESTCASE_AUTO(TestOptimization); | 47 TESTCASE_AUTO(TestFormatReplaceNoOptimization); |
| 48 TESTCASE_AUTO(TestFormatReplaceNoOptimizationLeadingText); |
| 49 TESTCASE_AUTO(TestFormatReplaceOptimization); |
| 50 TESTCASE_AUTO(TestFormatReplaceNoOptimizationLeadingPlaceholderUsedTwice); |
| 51 TESTCASE_AUTO(TestFormatReplaceOptimizationNoOffsets); |
| 52 TESTCASE_AUTO(TestFormatReplaceNoOptimizationNoOffsets); |
| 35 TESTCASE_AUTO_END; | 53 TESTCASE_AUTO_END; |
| 36 } | 54 } |
| 37 | 55 |
| 38 void SimplePatternFormatterTest::TestNoPlaceholders() { | 56 void SimplePatternFormatterTest::TestNoPlaceholders() { |
| 39 UErrorCode status = U_ZERO_ERROR; | 57 UErrorCode status = U_ZERO_ERROR; |
| 40 SimplePatternFormatter fmt("This doesn''t have templates '{0}"); | 58 SimplePatternFormatter fmt("This doesn''t have templates '{0}"); |
| 41 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); | 59 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); |
| 42 UnicodeString appendTo; | 60 UnicodeString appendTo; |
| 43 assertEquals( | 61 assertEquals( |
| 44 "format", | 62 "format", |
| 45 "This doesn't have templates {0}", | 63 "This doesn't have templates {0}", |
| 46 fmt.format("unused", appendTo, status)); | 64 fmt.format("unused", appendTo, status)); |
| 47 fmt.compile("This has {} bad {012d placeholders", status); | 65 fmt.compile("This has {} bad {012d placeholders", status); |
| 48 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); | 66 assertEquals("PlaceholderCount", 0, fmt.getPlaceholderCount()); |
| 49 appendTo.remove(); | 67 appendTo.remove(); |
| 50 assertEquals( | 68 assertEquals( |
| 51 "format", | 69 "format", |
| 52 "This has {} bad {012d placeholders", | 70 "This has {} bad {012d placeholders", |
| 53 fmt.format("unused", appendTo, status)); | 71 fmt.format("unused", appendTo, status)); |
| 54 assertSuccess("Status", status); | 72 assertSuccess("Status", status); |
| 55 } | 73 } |
| 56 | 74 |
| 57 void SimplePatternFormatterTest::TestOnePlaceholder() { | 75 void SimplePatternFormatterTest::TestOnePlaceholder() { |
| 58 UErrorCode status = U_ZERO_ERROR; | 76 UErrorCode status = U_ZERO_ERROR; |
| 59 SimplePatternFormatter fmt; | 77 SimplePatternFormatter fmt; |
| 60 fmt.compile("{0} meter", status); | 78 fmt.compile("{0} meter", status); |
| 79 if (!assertSuccess("Status", status)) { |
| 80 return; |
| 81 } |
| 61 assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount()); | 82 assertEquals("PlaceholderCount", 1, fmt.getPlaceholderCount()); |
| 62 UnicodeString appendTo; | 83 UnicodeString appendTo; |
| 63 assertEquals( | 84 assertEquals( |
| 64 "format", | 85 "format", |
| 65 "1 meter", | 86 "1 meter", |
| 66 fmt.format("1", appendTo, status)); | 87 fmt.format("1", appendTo, status)); |
| 67 assertSuccess("Status", status); | |
| 68 | 88 |
| 69 // assignment | 89 // assignment |
| 70 SimplePatternFormatter s; | 90 SimplePatternFormatter s; |
| 71 s = fmt; | 91 s = fmt; |
| 72 appendTo.remove(); | 92 appendTo.remove(); |
| 73 assertEquals( | 93 assertEquals( |
| 74 "Assignment", | 94 "Assignment", |
| 75 "1 meter", | 95 "1 meter", |
| 76 s.format("1", appendTo, status)); | 96 s.format("1", appendTo, status)); |
| 77 | 97 |
| 78 // Copy constructor | 98 // Copy constructor |
| 79 SimplePatternFormatter r(fmt); | 99 SimplePatternFormatter r(fmt); |
| 80 appendTo.remove(); | 100 appendTo.remove(); |
| 81 assertEquals( | 101 assertEquals( |
| 82 "Copy constructor", | 102 "Copy constructor", |
| 83 "1 meter", | 103 "1 meter", |
| 84 r.format("1", appendTo, status)); | 104 r.format("1", appendTo, status)); |
| 85 assertSuccess("Status", status); | 105 assertSuccess("Status", status); |
| 86 } | 106 } |
| 87 | 107 |
| 88 void SimplePatternFormatterTest::TestManyPlaceholders() { | 108 void SimplePatternFormatterTest::TestManyPlaceholders() { |
| 89 UErrorCode status = U_ZERO_ERROR; | 109 UErrorCode status = U_ZERO_ERROR; |
| 90 SimplePatternFormatter fmt; | 110 SimplePatternFormatter fmt; |
| 91 fmt.compile( | 111 fmt.compile( |
| 92 "Templates {2}{1}{5} and {4} are out of order.", status); | 112 "Templates {2}{1}{5} and {4} are out of order.", status); |
| 93 assertSuccess("Status", status); | 113 if (!assertSuccess("Status", status)) { |
| 94 assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(2)); | 114 return; |
| 115 } |
| 95 assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount()); | 116 assertEquals("PlaceholderCount", 6, fmt.getPlaceholderCount()); |
| 96 UnicodeString values[] = { | 117 UnicodeString values[] = { |
| 97 "freddy", "tommy", "frog", "billy", "leg", "{0}"}; | 118 "freddy", "tommy", "frog", "billy", "leg", "{0}"}; |
| 98 UnicodeString *params[] = { | 119 UnicodeString *params[] = { |
| 99 &values[0], &values[1], &values[2], &values[3], &values[4], &values[5
]}; | 120 &values[0], &values[1], &values[2], &values[3], &values[4], &values[5
]}; |
| 100 int32_t offsets[6]; | 121 int32_t offsets[6]; |
| 101 int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27}; | 122 int32_t expectedOffsets[6] = {-1, 22, 18, -1, 35, 27}; |
| 102 UnicodeString appendTo("Prefix: "); | 123 UnicodeString appendTo("Prefix: "); |
| 103 assertEquals( | 124 assertEquals( |
| 104 "format", | 125 "format", |
| 105 "Prefix: Templates frogtommy{0} and leg are out of order.", | 126 "Prefix: Templates frogtommy{0} and leg are out of order.", |
| 106 fmt.format( | 127 fmt.formatAndAppend( |
| 107 params, | 128 params, |
| 108 UPRV_LENGTHOF(params), | 129 UPRV_LENGTHOF(params), |
| 109 appendTo, | 130 appendTo, |
| 110 offsets, | 131 offsets, |
| 111 UPRV_LENGTHOF(offsets), | 132 UPRV_LENGTHOF(offsets), |
| 112 status)); | 133 status)); |
| 113 assertSuccess("Status", status); | 134 if (!assertSuccess("Status", status)) { |
| 114 for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) { | 135 return; |
| 115 if (expectedOffsets[i] != offsets[i]) { | |
| 116 errln("Expected %d, got %d", expectedOffsets[i], offsets[i]); | |
| 117 } | |
| 118 } | 136 } |
| 137 verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets)); |
| 119 appendTo.remove(); | 138 appendTo.remove(); |
| 120 | 139 |
| 121 // Not having enough placeholder params results in error. | |
| 122 fmt.format( | |
| 123 params, | |
| 124 UPRV_LENGTHOF(params) - 1, | |
| 125 appendTo, | |
| 126 offsets, | |
| 127 UPRV_LENGTHOF(offsets), | |
| 128 status); | |
| 129 if (status != U_ILLEGAL_ARGUMENT_ERROR) { | |
| 130 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); | |
| 131 } | |
| 132 | |
| 133 // Ensure we don't write to offsets array beyond its length. | 140 // Ensure we don't write to offsets array beyond its length. |
| 134 status = U_ZERO_ERROR; | 141 status = U_ZERO_ERROR; |
| 135 offsets[UPRV_LENGTHOF(offsets) - 1] = 289; | 142 offsets[UPRV_LENGTHOF(offsets) - 1] = 289; |
| 136 appendTo.remove(); | 143 appendTo.remove(); |
| 137 fmt.format( | 144 fmt.formatAndAppend( |
| 138 params, | 145 params, |
| 139 UPRV_LENGTHOF(params), | 146 UPRV_LENGTHOF(params), |
| 140 appendTo, | 147 appendTo, |
| 141 offsets, | 148 offsets, |
| 142 UPRV_LENGTHOF(offsets) - 1, | 149 UPRV_LENGTHOF(offsets) - 1, |
| 143 status); | 150 status); |
| 144 assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) -
1]); | 151 assertEquals("Offsets buffer length", 289, offsets[UPRV_LENGTHOF(offsets) -
1]); |
| 145 | 152 |
| 146 // Test assignment | 153 // Test assignment |
| 147 SimplePatternFormatter s; | 154 SimplePatternFormatter s; |
| 148 s = fmt; | 155 s = fmt; |
| 149 appendTo.remove(); | 156 appendTo.remove(); |
| 150 assertEquals( | 157 assertEquals( |
| 151 "Assignment", | 158 "Assignment", |
| 152 "Templates frogtommy{0} and leg are out of order.", | 159 "Templates frogtommy{0} and leg are out of order.", |
| 153 s.format( | 160 s.formatAndAppend( |
| 154 params, | 161 params, |
| 155 UPRV_LENGTHOF(params), | 162 UPRV_LENGTHOF(params), |
| 156 appendTo, | 163 appendTo, |
| 157 NULL, | 164 NULL, |
| 158 0, | 165 0, |
| 159 status)); | 166 status)); |
| 160 | 167 |
| 161 // Copy constructor | 168 // Copy constructor |
| 162 SimplePatternFormatter r(fmt); | 169 SimplePatternFormatter r(fmt); |
| 163 appendTo.remove(); | 170 appendTo.remove(); |
| 164 assertEquals( | 171 assertEquals( |
| 165 "Copy constructor", | 172 "Copy constructor", |
| 166 "Templates frogtommy{0} and leg are out of order.", | 173 "Templates frogtommy{0} and leg are out of order.", |
| 167 r.format( | 174 r.formatAndAppend( |
| 168 params, | 175 params, |
| 169 UPRV_LENGTHOF(params), | 176 UPRV_LENGTHOF(params), |
| 170 appendTo, | 177 appendTo, |
| 171 NULL, | 178 NULL, |
| 172 0, | 179 0, |
| 173 status)); | 180 status)); |
| 174 r.compile("{0} meter", status); | 181 r.compile("{0} meter", status); |
| 175 assertEquals("PlaceholderCount", 1, r.getPlaceholderCount()); | 182 assertEquals("PlaceholderCount", 1, r.getPlaceholderCount()); |
| 176 appendTo.remove(); | 183 appendTo.remove(); |
| 177 assertEquals( | 184 assertEquals( |
| (...skipping 10 matching lines...) Expand all Loading... |
| 188 r.compile("{0}, {1} and {2}", status); | 195 r.compile("{0}, {1} and {2}", status); |
| 189 assertEquals("PlaceholderCount", 3, r.getPlaceholderCount()); | 196 assertEquals("PlaceholderCount", 3, r.getPlaceholderCount()); |
| 190 appendTo.remove(); | 197 appendTo.remove(); |
| 191 assertEquals( | 198 assertEquals( |
| 192 "3 arg", | 199 "3 arg", |
| 193 "foo, bar and baz", | 200 "foo, bar and baz", |
| 194 r.format("foo", "bar", "baz", appendTo, status)); | 201 r.format("foo", "bar", "baz", appendTo, status)); |
| 195 assertSuccess("Status", status); | 202 assertSuccess("Status", status); |
| 196 } | 203 } |
| 197 | 204 |
| 205 void SimplePatternFormatterTest::TestTooFewPlaceholderValues() { |
| 206 SimplePatternFormatter fmt("{0} and {1}"); |
| 207 UnicodeString appendTo; |
| 208 UnicodeString firstValue; |
| 209 UnicodeString *params[] = {&firstValue}; |
| 210 |
| 211 UErrorCode status = U_ZERO_ERROR; |
| 212 fmt.format( |
| 213 firstValue, appendTo, status); |
| 214 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 215 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 216 } |
| 217 |
| 218 status = U_ZERO_ERROR; |
| 219 fmt.formatAndAppend( |
| 220 params, UPRV_LENGTHOF(params), appendTo, NULL, 0, status); |
| 221 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 222 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 223 } |
| 224 |
| 225 status = U_ZERO_ERROR; |
| 226 fmt.formatAndReplace( |
| 227 params, UPRV_LENGTHOF(params), appendTo, NULL, 0, status); |
| 228 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 229 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 230 } |
| 231 } |
| 232 |
| 233 void SimplePatternFormatterTest::TestBadArguments() { |
| 234 SimplePatternFormatter fmt("pickle"); |
| 235 UnicodeString appendTo; |
| 236 UErrorCode status = U_ZERO_ERROR; |
| 237 |
| 238 // These succeed |
| 239 fmt.formatAndAppend( |
| 240 NULL, 0, appendTo, NULL, 0, status); |
| 241 fmt.formatAndReplace( |
| 242 NULL, 0, appendTo, NULL, 0, status); |
| 243 assertSuccess("", status); |
| 244 status = U_ZERO_ERROR; |
| 245 |
| 246 // fails |
| 247 fmt.formatAndAppend( |
| 248 NULL, 1, appendTo, NULL, 0, status); |
| 249 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 250 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 251 } |
| 252 status = U_ZERO_ERROR; |
| 253 |
| 254 // fails |
| 255 fmt.formatAndAppend( |
| 256 NULL, 0, appendTo, NULL, 1, status); |
| 257 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 258 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 259 } |
| 260 status = U_ZERO_ERROR; |
| 261 |
| 262 // fails because appendTo used as a parameter value |
| 263 const UnicodeString *params[] = {&appendTo}; |
| 264 fmt.formatAndAppend( |
| 265 params, UPRV_LENGTHOF(params), appendTo, NULL, 0, status); |
| 266 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 267 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 268 } |
| 269 status = U_ZERO_ERROR; |
| 270 |
| 271 |
| 272 // fails |
| 273 fmt.formatAndReplace( |
| 274 NULL, 1, appendTo, NULL, 0, status); |
| 275 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 276 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 277 } |
| 278 status = U_ZERO_ERROR; |
| 279 |
| 280 // fails |
| 281 fmt.formatAndReplace( |
| 282 NULL, 0, appendTo, NULL, 1, status); |
| 283 if (status != U_ILLEGAL_ARGUMENT_ERROR) { |
| 284 errln("Expected U_ILLEGAL_ARGUMENT_ERROR"); |
| 285 } |
| 286 } |
| 287 |
| 198 void SimplePatternFormatterTest::TestGetPatternWithNoPlaceholders() { | 288 void SimplePatternFormatterTest::TestGetPatternWithNoPlaceholders() { |
| 199 SimplePatternFormatter fmt("{0} has no {1} placeholders."); | 289 SimplePatternFormatter fmt("{0} has no {1} placeholders."); |
| 200 assertEquals( | 290 assertEquals( |
| 201 "", " has no placeholders.", fmt.getPatternWithNoPlaceholders()); | 291 "", " has no placeholders.", fmt.getPatternWithNoPlaceholders()); |
| 202 } | 292 } |
| 203 | 293 |
| 204 void SimplePatternFormatterTest::TestOptimization() { | 294 void SimplePatternFormatterTest::TestFormatReplaceNoOptimization() { |
| 205 UErrorCode status = U_ZERO_ERROR; | 295 UErrorCode status = U_ZERO_ERROR; |
| 206 SimplePatternFormatter fmt; | 296 SimplePatternFormatter fmt; |
| 207 fmt.compile("{2}, {0}, {1} and {3}", status); | 297 fmt.compile("{2}, {0}, {1} and {3}", status); |
| 298 if (!assertSuccess("Status", status)) { |
| 299 return; |
| 300 } |
| 301 UnicodeString result("original"); |
| 302 int offsets[4]; |
| 303 UnicodeString freddy("freddy"); |
| 304 UnicodeString frog("frog"); |
| 305 UnicodeString by("by"); |
| 306 const UnicodeString *params[] = {&result, &freddy, &frog, &by}; |
| 307 assertEquals( |
| 308 "", |
| 309 "frog, original, freddy and by", |
| 310 fmt.formatAndReplace( |
| 311 params, |
| 312 UPRV_LENGTHOF(params), |
| 313 result, |
| 314 offsets, |
| 315 UPRV_LENGTHOF(offsets), |
| 316 status)); |
| 317 if (!assertSuccess("Status", status)) { |
| 318 return; |
| 319 } |
| 320 int32_t expectedOffsets[] = {6, 16, 0, 27}; |
| 321 verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets)); |
| 322 } |
| 323 |
| 324 void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingText() { |
| 325 UErrorCode status = U_ZERO_ERROR; |
| 326 SimplePatternFormatter fmt; |
| 327 fmt.compile("boo {2}, {0}, {1} and {3}", status); |
| 328 if (!assertSuccess("Status", status)) { |
| 329 return; |
| 330 } |
| 331 UnicodeString result("original"); |
| 332 int offsets[4]; |
| 333 UnicodeString freddy("freddy"); |
| 334 UnicodeString frog("frog"); |
| 335 UnicodeString by("by"); |
| 336 const UnicodeString *params[] = {&freddy, &frog, &result, &by}; |
| 337 assertEquals( |
| 338 "", |
| 339 "boo original, freddy, frog and by", |
| 340 fmt.formatAndReplace( |
| 341 params, |
| 342 UPRV_LENGTHOF(params), |
| 343 result, |
| 344 offsets, |
| 345 UPRV_LENGTHOF(offsets), |
| 346 status)); |
| 347 if (!assertSuccess("Status", status)) { |
| 348 return; |
| 349 } |
| 350 int32_t expectedOffsets[] = {14, 22, 4, 31}; |
| 351 verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets)); |
| 352 } |
| 353 |
| 354 void SimplePatternFormatterTest::TestFormatReplaceOptimization() { |
| 355 UErrorCode status = U_ZERO_ERROR; |
| 356 SimplePatternFormatter fmt; |
| 357 fmt.compile("{2}, {0}, {1} and {3}", status); |
| 358 if (!assertSuccess("Status", status)) { |
| 359 return; |
| 360 } |
| 361 UnicodeString result("original"); |
| 362 int offsets[4]; |
| 363 UnicodeString freddy("freddy"); |
| 364 UnicodeString frog("frog"); |
| 365 UnicodeString by("by"); |
| 366 const UnicodeString *params[] = {&freddy, &frog, &result, &by}; |
| 367 assertEquals( |
| 368 "", |
| 369 "original, freddy, frog and by", |
| 370 fmt.formatAndReplace( |
| 371 params, |
| 372 UPRV_LENGTHOF(params), |
| 373 result, |
| 374 offsets, |
| 375 UPRV_LENGTHOF(offsets), |
| 376 status)); |
| 377 if (!assertSuccess("Status", status)) { |
| 378 return; |
| 379 } |
| 380 int32_t expectedOffsets[] = {10, 18, 0, 27}; |
| 381 verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets)); |
| 382 } |
| 383 |
| 384 void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationLeadingPlacehold
erUsedTwice() { |
| 385 UErrorCode status = U_ZERO_ERROR; |
| 386 SimplePatternFormatter fmt; |
| 387 fmt.compile("{2}, {0}, {1} and {3} {2}", status); |
| 388 if (!assertSuccess("Status", status)) { |
| 389 return; |
| 390 } |
| 391 UnicodeString result("original"); |
| 392 int offsets[4]; |
| 393 UnicodeString freddy("freddy"); |
| 394 UnicodeString frog("frog"); |
| 395 UnicodeString by("by"); |
| 396 const UnicodeString *params[] = {&freddy, &frog, &result, &by}; |
| 397 assertEquals( |
| 398 "", |
| 399 "original, freddy, frog and by original", |
| 400 fmt.formatAndReplace( |
| 401 params, |
| 402 UPRV_LENGTHOF(params), |
| 403 result, |
| 404 offsets, |
| 405 UPRV_LENGTHOF(offsets), |
| 406 status)); |
| 407 if (!assertSuccess("Status", status)) { |
| 408 return; |
| 409 } |
| 410 int32_t expectedOffsets[] = {10, 18, 30, 27}; |
| 411 verifyOffsets(expectedOffsets, offsets, UPRV_LENGTHOF(expectedOffsets)); |
| 412 } |
| 413 |
| 414 void SimplePatternFormatterTest::TestFormatReplaceOptimizationNoOffsets() { |
| 415 UErrorCode status = U_ZERO_ERROR; |
| 416 SimplePatternFormatter fmt; |
| 417 fmt.compile("{2}, {0}, {1} and {3}", status); |
| 418 if (!assertSuccess("Status", status)) { |
| 419 return; |
| 420 } |
| 421 UnicodeString result("original"); |
| 422 UnicodeString freddy("freddy"); |
| 423 UnicodeString frog("frog"); |
| 424 UnicodeString by("by"); |
| 425 const UnicodeString *params[] = {&freddy, &frog, &result, &by}; |
| 426 assertEquals( |
| 427 "", |
| 428 "original, freddy, frog and by", |
| 429 fmt.formatAndReplace( |
| 430 params, |
| 431 UPRV_LENGTHOF(params), |
| 432 result, |
| 433 NULL, |
| 434 0, |
| 435 status)); |
| 208 assertSuccess("Status", status); | 436 assertSuccess("Status", status); |
| 209 assertTrue("startsWithPlaceholder", fmt.startsWithPlaceholder(2)); | 437 } |
| 210 assertFalse("startsWithPlaceholder", fmt.startsWithPlaceholder(0)); | 438 |
| 211 UnicodeString values[] = { | 439 void SimplePatternFormatterTest::TestFormatReplaceNoOptimizationNoOffsets() { |
| 212 "freddy", "frog", "leg", "by"}; | 440 UErrorCode status = U_ZERO_ERROR; |
| 213 UnicodeString *params[] = { | 441 SimplePatternFormatter fmt("Placeholders {0} and {1}"); |
| 214 &values[0], &values[1], &values[2], &values[3]}; | 442 UnicodeString result("previous:"); |
| 215 int32_t offsets[4]; | 443 UnicodeString frog("frog"); |
| 216 int32_t expectedOffsets[4] = {5, 13, 0, 22}; | 444 const UnicodeString *params[] = {&result, &frog}; |
| 217 | 445 assertEquals( |
| 218 // The pattern starts with {2}, so format should append the result of | 446 "", |
| 219 // the rest of the pattern to values[2], the value for {2}. | 447 "Placeholders previous: and frog", |
| 220 assertEquals( | 448 fmt.formatAndReplace( |
| 221 "format", | 449 params, |
| 222 "leg, freddy, frog and by", | 450 UPRV_LENGTHOF(params), |
| 223 fmt.format( | 451 result, |
| 224 params, | 452 NULL, |
| 225 UPRV_LENGTHOF(params), | 453 0, |
| 226 values[2], | |
| 227 offsets, | |
| 228 UPRV_LENGTHOF(offsets), | |
| 229 status)); | 454 status)); |
| 230 assertSuccess("Status", status); | 455 assertSuccess("Status", status); |
| 231 for (int32_t i = 0; i < UPRV_LENGTHOF(expectedOffsets); ++i) { | 456 } |
| 232 if (expectedOffsets[i] != offsets[i]) { | 457 |
| 233 errln("Expected %d, got %d", expectedOffsets[i], offsets[i]); | 458 void SimplePatternFormatterTest::verifyOffsets( |
| 459 const int32_t *expected, const int32_t *actual, int32_t count) { |
| 460 for (int32_t i = 0; i < count; ++i) { |
| 461 if (expected[i] != actual[i]) { |
| 462 errln("Expected %d, got %d", expected[i], actual[i]); |
| 234 } | 463 } |
| 235 } | 464 } |
| 236 } | 465 } |
| 237 | 466 |
| 238 extern IntlTest *createSimplePatternFormatterTest() { | 467 extern IntlTest *createSimplePatternFormatterTest() { |
| 239 return new SimplePatternFormatterTest(); | 468 return new SimplePatternFormatterTest(); |
| 240 } | 469 } |
| 470 |
| OLD | NEW |