Bug 470455 - test_database_sync_embed_visits.js leaks, r=sdwilsh
[wine-gecko.git] / intl / lwbrk / tests / TestLineBreak.cpp
blob7b285e8a969b1e140f82d9f30fff623f73a814d4
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* ***** BEGIN LICENSE BLOCK *****
3 * Version: MPL 1.1/GPL 2.0/LGPL 2.1
5 * The contents of this file are subject to the Mozilla Public License Version
6 * 1.1 (the "License"); you may not use this file except in compliance with
7 * the License. You may obtain a copy of the License at
8 * http://www.mozilla.org/MPL/
10 * Software distributed under the License is distributed on an "AS IS" basis,
11 * WITHOUT WARRANTY OF ANY KIND, either express or implied. See the License
12 * for the specific language governing rights and limitations under the
13 * License.
15 * The Original Code is mozilla.org code.
17 * The Initial Developer of the Original Code is
18 * Netscape Communications Corporation.
19 * Portions created by the Initial Developer are Copyright (C) 1998
20 * the Initial Developer. All Rights Reserved.
22 * Contributor(s):
24 * Alternatively, the contents of this file may be used under the terms of
25 * either of the GNU General Public License Version 2 or later (the "GPL"),
26 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
27 * in which case the provisions of the GPL or the LGPL are applicable instead
28 * of those above. If you wish to allow use of your version of this file only
29 * under the terms of either the GPL or the LGPL, and not to allow others to
30 * use your version of this file under the terms of the MPL, indicate your
31 * decision by deleting the provisions above and replace them with the notice
32 * and other provisions required by the GPL or the LGPL. If you do not delete
33 * the provisions above, a recipient may use your version of this file under
34 * the terms of any one of the MPL, the GPL or the LGPL.
36 * ***** END LICENSE BLOCK ***** */
37 #include <stdio.h>
38 #include "nsXPCOM.h"
39 #include "nsIComponentManager.h"
40 #include "nsISupports.h"
41 #include "nsServiceManagerUtils.h"
42 #include "nsILineBreaker.h"
43 #include "nsIWordBreaker.h"
44 #include "nsLWBrkCIID.h"
45 #include "nsStringAPI.h"
46 #include "nsEmbedString.h"
48 #define WORK_AROUND_SERVICE_MANAGER_ASSERT
50 NS_DEFINE_CID(kLBrkCID, NS_LBRK_CID);
51 NS_DEFINE_CID(kWBrkCID, NS_WBRK_CID);
54 static char teng1[] =
55 // 1 2 3 4 5 6 7
56 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
57 "This is a test to test(reasonable) line break. This 0.01123 = 45 x 48.";
59 static PRUint32 exp1[] = {
60 4,5,7,8,9,10,14,15,17,18,22,34,35,39,40,41,42,43,49,50,54,55,62,63,64,65,
61 67,68,69,70
64 static PRUint32 wexp1[] = {
66 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,
67 64,65,67,68,69,70,72
69 // 1 2 3 4 5 6 7
70 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
71 static char teng2[] =
72 "()((reasonab(l)e) line break. .01123=45x48.";
74 static PRUint32 lexp2[] = {
75 2,12,15,17,18,22,23,24,30,31,37,38,
77 static PRUint32 wexp2[] = {
78 4,12,13,14,15,16,17,18,22,24,29,30,31,32,37,38,43
81 // 1 2 3 4 5 6 7
82 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
83 static char teng3[] =
84 "It's a test to test(ronae ) line break....";
85 static PRUint32 exp3[] = {
86 4, 5, 6,7,11,12,14,15,19,25,27,28,32,33
88 static PRUint32 wexp3[] = {
89 4,5,6,7,11,12,14,15,19,20,25,26,27,28,32,33,38
92 static char ruler1[] =
93 " 1 2 3 4 5 6 7 ";
94 static char ruler2[] =
95 "0123456789012345678901234567890123456789012345678901234567890123456789012";
98 PRBool TestASCIILB(nsILineBreaker *lb,
99 const char* in, const PRUint32 len,
100 const PRUint32* out, PRUint32 outlen)
102 NS_ConvertASCIItoUTF16 eng1(in);
103 PRUint32 i,j;
104 PRUint32 res[256];
105 PRBool ok = PR_TRUE;
106 PRInt32 curr;
107 for(i = 0, curr = 0; (curr != NS_LINEBREAKER_NEED_MORE_TEXT) &&
108 (i < 256); i++)
110 curr = lb->Next(eng1.get(), eng1.Length(), curr);
111 res [i] = curr != NS_LINEBREAKER_NEED_MORE_TEXT ? curr : eng1.Length();
114 if (i != outlen)
116 ok = PR_FALSE;
117 printf("WARNING!!! return size wrong, expect %d but got %d \n",
118 outlen, i);
120 printf("string = \n%s\n", in);
121 printf("%s\n", ruler1);
122 printf("%s\n", ruler2);
123 printf("Expect = \n");
124 for(j=0;j<outlen;j++)
126 printf("%d,", out[j]);
128 printf("\nResult = \n");
129 for(j=0;j<i;j++)
131 printf("%d,", res[j]);
133 printf("\n");
134 for(j=0;j<i;j++)
136 if(j < outlen)
138 if (res[j] != out[j])
140 ok = PR_FALSE;
141 printf("[%d] expect %d but got %d\n", j, out[j], res[j]);
143 } else {
144 ok = PR_FALSE;
145 printf("[%d] additional %d\n", j, res[j]);
148 return ok;
151 PRBool TestASCIIWB(nsIWordBreaker *lb,
152 const char* in, const PRUint32 len,
153 const PRUint32* out, PRUint32 outlen)
155 NS_ConvertASCIItoUTF16 eng1(in);
157 PRUint32 i,j;
158 PRUint32 res[256];
159 PRBool ok = PR_TRUE;
160 PRInt32 curr = 0;
162 for(i = 0, curr = lb->NextWord(eng1.get(), eng1.Length(), curr);
163 (curr != NS_WORDBREAKER_NEED_MORE_TEXT) && (i < 256);
164 curr = lb->NextWord(eng1.get(), eng1.Length(), curr), i++)
166 res [i] = curr != NS_WORDBREAKER_NEED_MORE_TEXT ? curr : eng1.Length();
168 if (i != outlen)
170 ok = PR_FALSE;
171 printf("WARNING!!! return size wrong, expect %d but got %d\n",
172 outlen, i);
174 printf("string = \n%s\n", in);
175 printf("%s\n", ruler1);
176 printf("%s\n", ruler2);
177 printf("Expect = \n");
178 for(j=0;j<outlen;j++)
180 printf("%d,", out[j]);
182 printf("\nResult = \n");
183 for(j=0;j<i;j++)
185 printf("%d,", res[j]);
187 printf("\n");
188 for(j=0;j<i;j++)
190 if(j < outlen)
192 if (res[j] != out[j])
194 ok = PR_FALSE;
195 printf("[%d] expect %d but got %d\n", j, out[j], res[j]);
197 } else {
198 ok = PR_FALSE;
199 printf("[%d] additional %d\n", j, res[j]);
202 return ok;
206 PRBool TestLineBreaker()
208 printf("===========================\n");
209 printf("Finish nsILineBreaker Test \n");
210 printf("===========================\n");
211 nsILineBreaker *t = NULL;
212 nsresult res;
213 PRBool ok = PR_TRUE;
214 res = CallGetService(kLBrkCID, &t);
216 printf("Test 1 - GetService():\n");
217 if(NS_FAILED(res) || ( t == NULL ) ) {
218 printf("\t1st GetService failed\n");
219 ok = PR_FALSE;
222 NS_IF_RELEASE(t);
224 res = CallGetService(kLBrkCID, &t);
226 if(NS_FAILED(res) || ( t == NULL ) ) {
227 printf("\t2nd GetService failed\n");
228 ok = PR_FALSE;
229 } else {
230 printf("Test 4 - {First,Next}ForwardBreak():\n");
231 if( TestASCIILB(t, teng1, sizeof(teng1)/sizeof(char),
232 exp1, sizeof(exp1)/sizeof(PRUint32)) )
234 printf("Test 4 Passed\n\n");
235 } else {
236 ok = PR_FALSE;
237 printf("Test 4 Failed\n\n");
240 printf("Test 5 - {First,Next}ForwardBreak():\n");
241 if(TestASCIILB(t, teng2, sizeof(teng2)/sizeof(char),
242 lexp2, sizeof(lexp2)/sizeof(PRUint32)) )
244 printf("Test 5 Passed\n\n");
245 } else {
246 ok = PR_FALSE;
247 printf("Test 5 Failed\n\n");
250 printf("Test 6 - {First,Next}ForwardBreak():\n");
251 if(TestASCIILB(t, teng3, sizeof(teng3)/sizeof(char),
252 exp3, sizeof(exp3)/sizeof(PRUint32)) )
254 printf("Test 6 Passed\n\n");
255 } else {
256 ok = PR_FALSE;
257 printf("Test 6 Failed\n\n");
261 NS_RELEASE(t);
265 printf("===========================\n");
266 printf("Finish nsILineBreaker Test \n");
267 printf("===========================\n");
269 return ok;
272 PRBool TestWordBreaker()
274 printf("===========================\n");
275 printf("Finish nsIWordBreaker Test \n");
276 printf("===========================\n");
277 nsIWordBreaker *t = NULL;
278 nsresult res;
279 PRBool ok = PR_TRUE;
280 res = CallGetService(kWBrkCID, &t);
282 printf("Test 1 - GetService():\n");
283 if(NS_FAILED(res) || ( t == NULL ) ) {
284 printf("\t1st GetService failed\n");
285 ok = PR_FALSE;
286 } else {
287 NS_RELEASE(t);
290 res = CallGetService(kWBrkCID, &t);
292 if(NS_FAILED(res) || ( t == NULL ) ) {
293 printf("\t2nd GetService failed\n");
294 ok = PR_FALSE;
295 } else {
297 printf("Test 4 - {First,Next}ForwardBreak():\n");
298 if( TestASCIIWB(t, teng1, sizeof(teng1)/sizeof(char),
299 wexp1, sizeof(wexp1)/sizeof(PRUint32)) )
301 printf("Test 4 Passed\n\n");
302 } else {
303 ok = PR_FALSE;
304 printf("Test 4 Failed\n\n");
307 printf("Test 5 - {First,Next}ForwardBreak():\n");
308 if(TestASCIIWB(t, teng2, sizeof(teng2)/sizeof(char),
309 wexp2, sizeof(wexp2)/sizeof(PRUint32)) )
311 printf("Test 5 Passed\n\n");
312 } else {
313 ok = PR_FALSE;
314 printf("Test 5 Failed\n\n");
317 printf("Test 6 - {First,Next}ForwardBreak():\n");
318 if(TestASCIIWB(t, teng3, sizeof(teng3)/sizeof(char),
319 wexp3, sizeof(wexp3)/sizeof(PRUint32)) )
321 printf("Test 6 Passed\n\n");
322 } else {
323 ok = PR_FALSE;
324 printf("Test 6 Failed\n\n");
328 NS_RELEASE(t);
331 printf("===========================\n");
332 printf("Finish nsIWordBreaker Test \n");
333 printf("===========================\n");
335 return ok;
338 void SamplePrintWordWithBreak();
339 void SampleFindWordBreakFromPosition(PRUint32 fragN, PRUint32 offset);
340 // Sample Code
342 // 012345678901234
343 static const char wb0[] = "T";
344 static const char wb1[] = "h";
345 static const char wb2[] = "is is a int";
346 static const char wb3[] = "ernationali";
347 static const char wb4[] = "zation work.";
349 static const char* wb[] = {wb0,wb1,wb2,wb3,wb4};
350 void SampleWordBreakUsage()
352 SamplePrintWordWithBreak();
353 SampleFindWordBreakFromPosition(0,0); // This
354 SampleFindWordBreakFromPosition(1,0); // This
355 SampleFindWordBreakFromPosition(2,0); // This
356 SampleFindWordBreakFromPosition(2,1); // This
357 SampleFindWordBreakFromPosition(2,9); // [space]
358 SampleFindWordBreakFromPosition(2,10); // internationalization
359 SampleFindWordBreakFromPosition(3,4); // internationalization
360 SampleFindWordBreakFromPosition(3,8); // internationalization
361 SampleFindWordBreakFromPosition(4,6); // [space]
362 SampleFindWordBreakFromPosition(4,7); // work
366 void SamplePrintWordWithBreak()
368 PRUint32 numOfFragment = sizeof(wb) / sizeof(char*);
369 nsIWordBreaker *wbk = NULL;
371 CallGetService(kWBrkCID, &wbk);
373 nsAutoString result;
375 for(PRUint32 i = 0; i < numOfFragment; i++)
377 NS_ConvertASCIItoUTF16 fragText(wb[i]);
379 PRInt32 cur = 0;
380 cur = wbk->NextWord(fragText.get(), fragText.Length(), cur);
381 PRUint32 start = 0;
382 for(PRUint32 j = 0; cur != NS_WORDBREAKER_NEED_MORE_TEXT ; j++)
384 result.Append(Substring(fragText, start, cur - start));
385 result.Append('^');
386 start = (cur >= 0 ? cur : cur - start);
387 cur = wbk->NextWord(fragText.get(), fragText.Length(), cur);
390 result.Append(Substring(fragText, fragText.Length() - start));
392 if( i != (numOfFragment -1 ))
394 NS_ConvertASCIItoUTF16 nextFragText(wb[i+1]);
396 PRBool canBreak = PR_TRUE;
397 canBreak = wbk->BreakInBetween( fragText.get(),
398 fragText.Length(),
399 nextFragText.get(),
400 nextFragText.Length());
401 if(canBreak)
402 result.Append('^');
404 fragText.Assign(nextFragText);
407 printf("Output From SamplePrintWordWithBreak() \n\n");
408 printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get());
410 NS_IF_RELEASE(wbk);
413 void SampleFindWordBreakFromPosition(PRUint32 fragN, PRUint32 offset)
415 PRUint32 numOfFragment = sizeof(wb) / sizeof(char*);
416 nsIWordBreaker *wbk = NULL;
418 CallGetService(kWBrkCID, &wbk);
420 NS_ConvertASCIItoUTF16 fragText(wb[fragN]);
422 nsWordRange res = wbk->FindWord(fragText.get(), fragText.Length(), offset);
424 PRBool canBreak;
425 nsAutoString result(Substring(fragText, res.mBegin, res.mEnd-res.mBegin));
427 if((PRUint32)fragText.Length() == res.mEnd) // if we hit the end of the fragment
429 nsAutoString curFragText = fragText;
430 for(PRUint32 p = fragN +1; p < numOfFragment ;p++)
432 NS_ConvertASCIItoUTF16 nextFragText(wb[p]);
433 canBreak = wbk->BreakInBetween(curFragText.get(),
434 curFragText.Length(),
435 nextFragText.get(),
436 nextFragText.Length());
437 if(canBreak)
438 break;
440 nsWordRange r = wbk->FindWord(nextFragText.get(), nextFragText.Length(),
443 result.Append(Substring(nextFragText, r.mBegin, r.mEnd - r.mBegin));
445 if((PRUint32)nextFragText.Length() != r.mEnd)
446 break;
448 nextFragText.Assign(curFragText);
452 if(0 == res.mBegin) // if we hit the beginning of the fragment
454 nsAutoString curFragText = fragText;
455 for(PRUint32 p = fragN ; p > 0 ;p--)
457 NS_ConvertASCIItoUTF16 prevFragText(wb[p-1]);
458 canBreak = wbk->BreakInBetween(prevFragText.get(),
459 prevFragText.Length(),
460 curFragText.get(),
461 curFragText.Length());
462 if(canBreak)
463 break;
465 nsWordRange r = wbk->FindWord(prevFragText.get(), prevFragText.Length(),
466 prevFragText.Length());
468 result.Insert(Substring(prevFragText, r.mBegin, r.mEnd - r.mBegin), 0);
470 if(0 != r.mBegin)
471 break;
473 prevFragText.Assign(curFragText);
477 printf("Output From SamplePrintWordWithBreak() \n\n");
478 printf("[%s]\n", NS_ConvertUTF16toUTF8(result).get());
480 NS_IF_RELEASE(wbk);
483 // Main
485 int main(int argc, char** argv) {
487 NS_InitXPCOM2(nsnull, nsnull, nsnull);
489 // --------------------------------------------
490 printf("Test Line Break\n");
492 PRBool lbok ;
493 PRBool wbok ;
494 lbok =TestWordBreaker();
495 if(lbok)
496 printf("Line Break Test\nOK\n");
497 else
498 printf("Line Break Test\nFailed\n");
500 wbok = TestLineBreaker();
501 if(wbok)
502 printf("Word Break Test\nOK\n");
503 else
504 printf("Word Break Test\nFailed\n");
506 SampleWordBreakUsage();
509 // --------------------------------------------
510 printf("Finish All The Test Cases\n");
512 if(lbok && wbok)
513 printf("Line/Word Break Test\nOK\n");
514 else
515 printf("Line/Word Break Test\nFailed\n");
516 return 0;