intl/lwbrk/tests/TestLineBreak.cpp

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5 #include <stdio.h>
michael@0 6 #include "nsXPCOM.h"
michael@0 7 #include "nsIComponentManager.h"
michael@0 8 #include "nsISupports.h"
michael@0 9 #include "nsServiceManagerUtils.h"
michael@0 10 #include "nsILineBreaker.h"
michael@0 11 #include "nsIWordBreaker.h"
michael@0 12 #include "nsLWBrkCIID.h"
michael@0 13 #include "nsStringAPI.h"
michael@0 14 #include "nsEmbedString.h"
michael@0 15 #include "TestHarness.h"
michael@0 16
michael@0 17 #define WORK_AROUND_SERVICE_MANAGER_ASSERT
michael@0 18
michael@0 19 NS_DEFINE_CID(kLBrkCID, NS_LBRK_CID);
michael@0 20 NS_DEFINE_CID(kWBrkCID, NS_WBRK_CID);
michael@0 21
michael@0 22
michael@0 23 static char teng1[] =
michael@0 24 // 1 2 3 4 5 6 7
michael@0 25 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
michael@0 26 "This is a test to test(reasonable) line break. This 0.01123 = 45 x 48.";
michael@0 27
michael@0 28 static uint32_t exp1[] = {
michael@0 29 4,7,9,14,17,34,39,40,41,42,49,54,62,64,67,69,73
michael@0 30 };
michael@0 31
michael@0 32 static uint32_t wexp1[] = {
michael@0 33
michael@0 34 4,5,7,8,9,10,14,15,17,18,22,23,33,34,35,39,43,48,49,50,54,55,56,57,62,63,
michael@0 35 64,65,67,68,69,70,72
michael@0 36 };
michael@0 37 // 1 2 3 4 5 6 7
michael@0 38 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
michael@0 39 static char teng2[] =
michael@0 40 "()((reasonab(l)e) line break. .01123=45x48.";
michael@0 41
michael@0 42 static uint32_t lexp2[] = {
michael@0 43 17,22,23,30,44
michael@0 44 };
michael@0 45 static uint32_t wexp2[] = {
michael@0 46 4,12,13,14,15,16,17,18,22,24,29,30,31,32,37,38,43
michael@0 47 };
michael@0 48
michael@0 49 // 1 2 3 4 5 6 7
michael@0 50 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
michael@0 51 static char teng3[] =
michael@0 52 "It's a test to test(ronae ) line break....";
michael@0 53 static uint32_t exp3[] = {
michael@0 54 4,6,11,14,25,27,32,42
michael@0 55 };
michael@0 56 static uint32_t wexp3[] = {
michael@0 57 2,3,4,5,6,7,11,12,14,15,19,20,25,26,27,28,32,33,38
michael@0 58 };
michael@0 59
michael@0 60 static char ruler1[] =
michael@0 61 " 1 2 3 4 5 6 7 ";
michael@0 62 static char ruler2[] =
michael@0 63 "0123456789012345678901234567890123456789012345678901234567890123456789012";
michael@0 64
michael@0 65
michael@0 66 bool TestASCIILB(nsILineBreaker *lb,
michael@0 67 const char* in, const uint32_t len,
michael@0 68 const uint32_t* out, uint32_t outlen)
michael@0 69 {
michael@0 70 NS_ConvertASCIItoUTF16 eng1(in);
michael@0 71 uint32_t i,j;
michael@0 72 uint32_t res[256];
michael@0 73 bool ok = true;
michael@0 74 int32_t curr;
michael@0 75 for(i = 0, curr = 0; (curr != NS_LINEBREAKER_NEED_MORE_TEXT) &&
michael@0 76 (i < 256); i++)
michael@0 77 {
michael@0 78 curr = lb->Next(eng1.get(), eng1.Length(), curr);
michael@0 79 res [i] = curr != NS_LINEBREAKER_NEED_MORE_TEXT ? curr : eng1.Length();
michael@0 80
michael@0 81 }
michael@0 82 if (i != outlen)
michael@0 83 {
michael@0 84 ok = false;
michael@0 85 printf("WARNING!!! return size wrong, expect %d but got %d \n",
michael@0 86 outlen, i);
michael@0 87 }
michael@0 88 printf("string = \n%s\n", in);
michael@0 89 printf("%s\n", ruler1);
michael@0 90 printf("%s\n", ruler2);
michael@0 91 printf("Expect = \n");
michael@0 92 for(j=0;j<outlen;j++)
michael@0 93 {
michael@0 94 printf("%d,", out[j]);
michael@0 95 }
michael@0 96 printf("\nResult = \n");
michael@0 97 for(j=0;j<i;j++)
michael@0 98 {
michael@0 99 printf("%d,", res[j]);
michael@0 100 }
michael@0 101 printf("\n");
michael@0 102 for(j=0;j<i;j++)
michael@0 103 {
michael@0 104 if(j < outlen)
michael@0 105 {
michael@0 106 if (res[j] != out[j])
michael@0 107 {
michael@0 108 ok = false;
michael@0 109 printf("[%d] expect %d but got %d\n", j, out[j], res[j]);
michael@0 110 }
michael@0 111 } else {
michael@0 112 ok = false;
michael@0 113 printf("[%d] additional %d\n", j, res[j]);
michael@0 114 }
michael@0 115 }
michael@0 116 return ok;
michael@0 117 }
michael@0 118
michael@0 119 bool TestASCIIWB(nsIWordBreaker *lb,
michael@0 120 const char* in, const uint32_t len,
michael@0 121 const uint32_t* out, uint32_t outlen)
michael@0 122 {
michael@0 123 NS_ConvertASCIItoUTF16 eng1(in);
michael@0 124
michael@0 125 uint32_t i,j;
michael@0 126 uint32_t res[256];
michael@0 127 bool ok = true;
michael@0 128 int32_t curr = 0;
michael@0 129
michael@0 130 for(i = 0, curr = lb->NextWord(eng1.get(), eng1.Length(), curr);
michael@0 131 (curr != NS_WORDBREAKER_NEED_MORE_TEXT) && (i < 256);
michael@0 132 curr = lb->NextWord(eng1.get(), eng1.Length(), curr), i++)
michael@0 133 {
michael@0 134 res [i] = curr != NS_WORDBREAKER_NEED_MORE_TEXT ? curr : eng1.Length();
michael@0 135 }
michael@0 136 if (i != outlen)
michael@0 137 {
michael@0 138 ok = false;
michael@0 139 printf("WARNING!!! return size wrong, expect %d but got %d\n",
michael@0 140 outlen, i);
michael@0 141 }
michael@0 142 printf("string = \n%s\n", in);
michael@0 143 printf("%s\n", ruler1);
michael@0 144 printf("%s\n", ruler2);
michael@0 145 printf("Expect = \n");
michael@0 146 for(j=0;j<outlen;j++)
michael@0 147 {
michael@0 148 printf("%d,", out[j]);
michael@0 149 }
michael@0 150 printf("\nResult = \n");
michael@0 151 for(j=0;j<i;j++)
michael@0 152 {
michael@0 153 printf("%d,", res[j]);
michael@0 154 }
michael@0 155 printf("\n");
michael@0 156 for(j=0;j<i;j++)
michael@0 157 {
michael@0 158 if(j < outlen)
michael@0 159 {
michael@0 160 if (res[j] != out[j])
michael@0 161 {
michael@0 162 ok = false;
michael@0 163 printf("[%d] expect %d but got %d\n", j, out[j], res[j]);
michael@0 164 }
michael@0 165 } else {
michael@0 166 ok = false;
michael@0 167 printf("[%d] additional %d\n", j, res[j]);
michael@0 168 }
michael@0 169 }
michael@0 170 return ok;
michael@0 171 }
michael@0 172
michael@0 173
michael@0 174 bool TestLineBreaker()
michael@0 175 {
michael@0 176 printf("===========================\n");
michael@0 177 printf("Finish nsILineBreaker Test \n");
michael@0 178 printf("===========================\n");
michael@0 179 nsILineBreaker *t = nullptr;
michael@0 180 nsresult res;
michael@0 181 bool ok = true;
michael@0 182 res = CallGetService(kLBrkCID, &t);
michael@0 183
michael@0 184 printf("Test 1 - GetService():\n");
michael@0 185 if (NS_FAILED(res) || !t) {
michael@0 186 printf("\t1st GetService failed\n");
michael@0 187 ok = false;
michael@0 188 }
michael@0 189
michael@0 190 NS_IF_RELEASE(t);
michael@0 191
michael@0 192 res = CallGetService(kLBrkCID, &t);
michael@0 193
michael@0 194 if (NS_FAILED(res) || !t) {
michael@0 195 printf("\t2nd GetService failed\n");
michael@0 196 ok = false;
michael@0 197 } else {
michael@0 198 printf("Test 4 - {First,Next}ForwardBreak():\n");
michael@0 199 if( TestASCIILB(t, teng1, sizeof(teng1)/sizeof(char),
michael@0 200 exp1, sizeof(exp1)/sizeof(uint32_t)) )
michael@0 201 {
michael@0 202 printf("Test 4 Passed\n\n");
michael@0 203 } else {
michael@0 204 ok = false;
michael@0 205 printf("Test 4 Failed\n\n");
michael@0 206 }
michael@0 207
michael@0 208 printf("Test 5 - {First,Next}ForwardBreak():\n");
michael@0 209 if(TestASCIILB(t, teng2, sizeof(teng2)/sizeof(char),
michael@0 210 lexp2, sizeof(lexp2)/sizeof(uint32_t)) )
michael@0 211 {
michael@0 212 printf("Test 5 Passed\n\n");
michael@0 213 } else {
michael@0 214 ok = false;
michael@0 215 printf("Test 5 Failed\n\n");
michael@0 216 }
michael@0 217
michael@0 218 printf("Test 6 - {First,Next}ForwardBreak():\n");
michael@0 219 if(TestASCIILB(t, teng3, sizeof(teng3)/sizeof(char),
michael@0 220 exp3, sizeof(exp3)/sizeof(uint32_t)) )
michael@0 221 {
michael@0 222 printf("Test 6 Passed\n\n");
michael@0 223 } else {
michael@0 224 ok = false;
michael@0 225 printf("Test 6 Failed\n\n");
michael@0 226 }
michael@0 227
michael@0 228
michael@0 229 NS_RELEASE(t);
michael@0 230
michael@0 231 }
michael@0 232
michael@0 233 printf("===========================\n");
michael@0 234 printf("Finish nsILineBreaker Test \n");
michael@0 235 printf("===========================\n");
michael@0 236
michael@0 237 return ok;
michael@0 238 }
michael@0 239
michael@0 240 bool TestWordBreaker()
michael@0 241 {
michael@0 242 printf("===========================\n");
michael@0 243 printf("Finish nsIWordBreaker Test \n");
michael@0 244 printf("===========================\n");
michael@0 245 nsIWordBreaker *t = nullptr;
michael@0 246 nsresult res;
michael@0 247 bool ok = true;
michael@0 248 res = CallGetService(kWBrkCID, &t);
michael@0 249
michael@0 250 printf("Test 1 - GetService():\n");
michael@0 251 if (NS_FAILED(res) || !t) {
michael@0 252 printf("\t1st GetService failed\n");
michael@0 253 ok = false;
michael@0 254 } else {
michael@0 255 NS_RELEASE(t);
michael@0 256 }
michael@0 257
michael@0 258 res = CallGetService(kWBrkCID, &t);
michael@0 259
michael@0 260 if (NS_FAILED(res) || !t) {
michael@0 261 printf("\t2nd GetService failed\n");
michael@0 262 ok = false;
michael@0 263 } else {
michael@0 264
michael@0 265 printf("Test 4 - {First,Next}ForwardBreak():\n");
michael@0 266 if( TestASCIIWB(t, teng1, sizeof(teng1)/sizeof(char),
michael@0 267 wexp1, sizeof(wexp1)/sizeof(uint32_t)) )
michael@0 268 {
michael@0 269 printf("Test 4 Passed\n\n");
michael@0 270 } else {
michael@0 271 ok = false;
michael@0 272 printf("Test 4 Failed\n\n");
michael@0 273 }
michael@0 274
michael@0 275 printf("Test 5 - {First,Next}ForwardBreak():\n");
michael@0 276 if(TestASCIIWB(t, teng2, sizeof(teng2)/sizeof(char),
michael@0 277 wexp2, sizeof(wexp2)/sizeof(uint32_t)) )
michael@0 278 {
michael@0 279 printf("Test 5 Passed\n\n");
michael@0 280 } else {
michael@0 281 ok = false;
michael@0 282 printf("Test 5 Failed\n\n");
michael@0 283 }
michael@0 284
michael@0 285 printf("Test 6 - {First,Next}ForwardBreak():\n");
michael@0 286 if(TestASCIIWB(t, teng3, sizeof(teng3)/sizeof(char),
michael@0 287 wexp3, sizeof(wexp3)/sizeof(uint32_t)) )
michael@0 288 {
michael@0 289 printf("Test 6 Passed\n\n");
michael@0 290 } else {
michael@0 291 ok = false;
michael@0 292 printf("Test 6 Failed\n\n");
michael@0 293 }
michael@0 294
michael@0 295
michael@0 296 NS_RELEASE(t);
michael@0 297 }
michael@0 298
michael@0 299 printf("===========================\n");
michael@0 300 printf("Finish nsIWordBreaker Test \n");
michael@0 301 printf("===========================\n");
michael@0 302
michael@0 303 return ok;
michael@0 304 }
michael@0 305
michael@0 306 void SamplePrintWordWithBreak();
michael@0 307 void SampleFindWordBreakFromPosition(uint32_t fragN, uint32_t offset);
michael@0 308 // Sample Code
michael@0 309
michael@0 310 // 012345678901234
michael@0 311 static const char wb0[] = "T";
michael@0 312 static const char wb1[] = "h";
michael@0 313 static const char wb2[] = "is is a int";
michael@0 314 static const char wb3[] = "ernationali";
michael@0 315 static const char wb4[] = "zation work.";
michael@0 316
michael@0 317 static const char* wb[] = {wb0,wb1,wb2,wb3,wb4};
michael@0 318 void SampleWordBreakUsage()
michael@0 319 {
michael@0 320 SamplePrintWordWithBreak();
michael@0 321 SampleFindWordBreakFromPosition(0,0); // This
michael@0 322 SampleFindWordBreakFromPosition(1,0); // This
michael@0 323 SampleFindWordBreakFromPosition(2,0); // This
michael@0 324 SampleFindWordBreakFromPosition(2,1); // This
michael@0 325 SampleFindWordBreakFromPosition(2,9); // [space]
michael@0 326 SampleFindWordBreakFromPosition(2,10); // internationalization
michael@0 327 SampleFindWordBreakFromPosition(3,4); // internationalization
michael@0 328 SampleFindWordBreakFromPosition(3,8); // internationalization
michael@0 329 SampleFindWordBreakFromPosition(4,6); // [space]
michael@0 330 SampleFindWordBreakFromPosition(4,7); // work
michael@0 331 }
michael@0 332
michael@0 333
michael@0 334 void SamplePrintWordWithBreak()
michael@0 335 {
michael@0 336 uint32_t numOfFragment = sizeof(wb) / sizeof(char*);
michael@0 337 nsIWordBreaker *wbk = nullptr;
michael@0 338
michael@0 339 CallGetService(kWBrkCID, &wbk);
michael@0 340
michael@0 341 nsAutoString result;
michael@0 342
michael@0 343 for(uint32_t i = 0; i < numOfFragment; i++)
michael@0 344 {
michael@0 345 NS_ConvertASCIItoUTF16 fragText(wb[i]);
michael@0 346
michael@0 347 int32_t cur = 0;
michael@0 348 cur = wbk->NextWord(fragText.get(), fragText.Length(), cur);
michael@0 349 uint32_t start = 0;
michael@0 350 for(uint32_t j = 0; cur != NS_WORDBREAKER_NEED_MORE_TEXT ; j++)
michael@0 351 {
michael@0 352 result.Append(Substring(fragText, start, cur - start));
michael@0 353 result.Append('^');
michael@0 354 start = (cur >= 0 ? cur : cur - start);
michael@0 355 cur = wbk->NextWord(fragText.get(), fragText.Length(), cur);
michael@0 356 }
michael@0 357
michael@0 358 result.Append(Substring(fragText, fragText.Length() - start));
michael@0 359
michael@0 360 if( i != (numOfFragment -1 ))
michael@0 361 {
michael@0 362 NS_ConvertASCIItoUTF16 nextFragText(wb[i+1]);
michael@0 363
michael@0 364 bool canBreak = true;
michael@0 365 canBreak = wbk->BreakInBetween( fragText.get(),
michael@0 366 fragText.Length(),
michael@0 367 nextFragText.get(),
michael@0 368 nextFragText.Length());
michael@0 369 if(canBreak)
michael@0 370 result.Append('^');
michael@0 371
michael@0 372 fragText.Assign(nextFragText);
michael@0 373 }
michael@0 374 }
michael@0 375 printf("Output From SamplePrintWordWithBreak() \n\n");
michael@0 376 printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get());
michael@0 377
michael@0 378 NS_IF_RELEASE(wbk);
michael@0 379 }
michael@0 380
michael@0 381 void SampleFindWordBreakFromPosition(uint32_t fragN, uint32_t offset)
michael@0 382 {
michael@0 383 uint32_t numOfFragment = sizeof(wb) / sizeof(char*);
michael@0 384 nsIWordBreaker *wbk = nullptr;
michael@0 385
michael@0 386 CallGetService(kWBrkCID, &wbk);
michael@0 387
michael@0 388 NS_ConvertASCIItoUTF16 fragText(wb[fragN]);
michael@0 389
michael@0 390 nsWordRange res = wbk->FindWord(fragText.get(), fragText.Length(), offset);
michael@0 391
michael@0 392 bool canBreak;
michael@0 393 nsAutoString result(Substring(fragText, res.mBegin, res.mEnd-res.mBegin));
michael@0 394
michael@0 395 if((uint32_t)fragText.Length() == res.mEnd) // if we hit the end of the fragment
michael@0 396 {
michael@0 397 nsAutoString curFragText = fragText;
michael@0 398 for(uint32_t p = fragN +1; p < numOfFragment ;p++)
michael@0 399 {
michael@0 400 NS_ConvertASCIItoUTF16 nextFragText(wb[p]);
michael@0 401 canBreak = wbk->BreakInBetween(curFragText.get(),
michael@0 402 curFragText.Length(),
michael@0 403 nextFragText.get(),
michael@0 404 nextFragText.Length());
michael@0 405 if(canBreak)
michael@0 406 break;
michael@0 407
michael@0 408 nsWordRange r = wbk->FindWord(nextFragText.get(), nextFragText.Length(),
michael@0 409 0);
michael@0 410
michael@0 411 result.Append(Substring(nextFragText, r.mBegin, r.mEnd - r.mBegin));
michael@0 412
michael@0 413 if((uint32_t)nextFragText.Length() != r.mEnd)
michael@0 414 break;
michael@0 415
michael@0 416 nextFragText.Assign(curFragText);
michael@0 417 }
michael@0 418 }
michael@0 419
michael@0 420 if(0 == res.mBegin) // if we hit the beginning of the fragment
michael@0 421 {
michael@0 422 nsAutoString curFragText = fragText;
michael@0 423 for(uint32_t p = fragN ; p > 0 ;p--)
michael@0 424 {
michael@0 425 NS_ConvertASCIItoUTF16 prevFragText(wb[p-1]);
michael@0 426 canBreak = wbk->BreakInBetween(prevFragText.get(),
michael@0 427 prevFragText.Length(),
michael@0 428 curFragText.get(),
michael@0 429 curFragText.Length());
michael@0 430 if(canBreak)
michael@0 431 break;
michael@0 432
michael@0 433 nsWordRange r = wbk->FindWord(prevFragText.get(), prevFragText.Length(),
michael@0 434 prevFragText.Length());
michael@0 435
michael@0 436 result.Insert(Substring(prevFragText, r.mBegin, r.mEnd - r.mBegin), 0);
michael@0 437
michael@0 438 if(0 != r.mBegin)
michael@0 439 break;
michael@0 440
michael@0 441 prevFragText.Assign(curFragText);
michael@0 442 }
michael@0 443 }
michael@0 444
michael@0 445 printf("Output From SamplePrintWordWithBreak() \n\n");
michael@0 446 printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get());
michael@0 447
michael@0 448 NS_IF_RELEASE(wbk);
michael@0 449 }
michael@0 450
michael@0 451 // Main
michael@0 452
michael@0 453 int main(int argc, char** argv) {
michael@0 454
michael@0 455 int rv = 0;
michael@0 456 ScopedXPCOM xpcom("TestLineBreak");
michael@0 457 if (xpcom.failed())
michael@0 458 return -1;
michael@0 459
michael@0 460 // --------------------------------------------
michael@0 461 printf("Test Line Break\n");
michael@0 462
michael@0 463 bool lbok ;
michael@0 464 bool wbok ;
michael@0 465 lbok =TestWordBreaker();
michael@0 466 if(lbok)
michael@0 467 passed("Line Break Test");
michael@0 468 else {
michael@0 469 fail("Line Break Test");
michael@0 470 rv = -1;
michael@0 471 }
michael@0 472
michael@0 473 wbok = TestLineBreaker();
michael@0 474 if(wbok)
michael@0 475 passed("Word Break Test");
michael@0 476 else {
michael@0 477 fail("Word Break Test");
michael@0 478 rv = -1;
michael@0 479 }
michael@0 480
michael@0 481 SampleWordBreakUsage();
michael@0 482
michael@0 483
michael@0 484 // --------------------------------------------
michael@0 485 printf("Finish All The Test Cases\n");
michael@0 486
michael@0 487 if(lbok && wbok)
michael@0 488 passed("Line/Word Break Test");
michael@0 489 else {
michael@0 490 fail("Line/Word Break Test");
michael@0 491 rv = -1;
michael@0 492 }
michael@0 493 return rv;
michael@0 494 }

mercurial