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
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.
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 ***** */
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
);
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,
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,
70 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
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
82 //01234567890123456789012345678901234567890123456789012345678901234567890123456789
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
[] =
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
);
107 for(i
= 0, curr
= 0; (curr
!= NS_LINEBREAKER_NEED_MORE_TEXT
) &&
110 curr
= lb
->Next(eng1
.get(), eng1
.Length(), curr
);
111 res
[i
] = curr
!= NS_LINEBREAKER_NEED_MORE_TEXT
? curr
: eng1
.Length();
117 printf("WARNING!!! return size wrong, expect %d but got %d \n",
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");
131 printf("%d,", res
[j
]);
138 if (res
[j
] != out
[j
])
141 printf("[%d] expect %d but got %d\n", j
, out
[j
], res
[j
]);
145 printf("[%d] additional %d\n", j
, res
[j
]);
151 PRBool
TestASCIIWB(nsIWordBreaker
*lb
,
152 const char* in
, const PRUint32 len
,
153 const PRUint32
* out
, PRUint32 outlen
)
155 NS_ConvertASCIItoUTF16
eng1(in
);
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();
171 printf("WARNING!!! return size wrong, expect %d but got %d\n",
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");
185 printf("%d,", res
[j
]);
192 if (res
[j
] != out
[j
])
195 printf("[%d] expect %d but got %d\n", j
, out
[j
], res
[j
]);
199 printf("[%d] additional %d\n", j
, res
[j
]);
206 PRBool
TestLineBreaker()
208 printf("===========================\n");
209 printf("Finish nsILineBreaker Test \n");
210 printf("===========================\n");
211 nsILineBreaker
*t
= NULL
;
214 res
= CallGetService(kLBrkCID
, &t
);
216 printf("Test 1 - GetService():\n");
217 if(NS_FAILED(res
) || ( t
== NULL
) ) {
218 printf("\t1st GetService failed\n");
224 res
= CallGetService(kLBrkCID
, &t
);
226 if(NS_FAILED(res
) || ( t
== NULL
) ) {
227 printf("\t2nd GetService failed\n");
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");
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");
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");
257 printf("Test 6 Failed\n\n");
265 printf("===========================\n");
266 printf("Finish nsILineBreaker Test \n");
267 printf("===========================\n");
272 PRBool
TestWordBreaker()
274 printf("===========================\n");
275 printf("Finish nsIWordBreaker Test \n");
276 printf("===========================\n");
277 nsIWordBreaker
*t
= NULL
;
280 res
= CallGetService(kWBrkCID
, &t
);
282 printf("Test 1 - GetService():\n");
283 if(NS_FAILED(res
) || ( t
== NULL
) ) {
284 printf("\t1st GetService failed\n");
290 res
= CallGetService(kWBrkCID
, &t
);
292 if(NS_FAILED(res
) || ( t
== NULL
) ) {
293 printf("\t2nd GetService failed\n");
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");
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");
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");
324 printf("Test 6 Failed\n\n");
331 printf("===========================\n");
332 printf("Finish nsIWordBreaker Test \n");
333 printf("===========================\n");
338 void SamplePrintWordWithBreak();
339 void SampleFindWordBreakFromPosition(PRUint32 fragN
, PRUint32 offset
);
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
);
375 for(PRUint32 i
= 0; i
< numOfFragment
; i
++)
377 NS_ConvertASCIItoUTF16
fragText(wb
[i
]);
380 cur
= wbk
->NextWord(fragText
.get(), fragText
.Length(), cur
);
382 for(PRUint32 j
= 0; cur
!= NS_WORDBREAKER_NEED_MORE_TEXT
; j
++)
384 result
.Append(Substring(fragText
, start
, cur
- start
));
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(),
400 nextFragText
.Length());
404 fragText
.Assign(nextFragText
);
407 printf("Output From SamplePrintWordWithBreak() \n\n");
408 printf("[%s]\n", NS_ConvertUTF16toUTF8(result
).get());
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
);
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(),
436 nextFragText
.Length());
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
)
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(),
461 curFragText
.Length());
465 nsWordRange r
= wbk
->FindWord(prevFragText
.get(), prevFragText
.Length(),
466 prevFragText
.Length());
468 result
.Insert(Substring(prevFragText
, r
.mBegin
, r
.mEnd
- r
.mBegin
), 0);
473 prevFragText
.Assign(curFragText
);
477 printf("Output From SamplePrintWordWithBreak() \n\n");
478 printf("[%s]\n", NS_ConvertUTF16toUTF8(result
).get());
485 int main(int argc
, char** argv
) {
487 NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
489 // --------------------------------------------
490 printf("Test Line Break\n");
494 lbok
=TestWordBreaker();
496 printf("Line Break Test\nOK\n");
498 printf("Line Break Test\nFailed\n");
500 wbok
= TestLineBreaker();
502 printf("Word Break Test\nOK\n");
504 printf("Word Break Test\nFailed\n");
506 SampleWordBreakUsage();
509 // --------------------------------------------
510 printf("Finish All The Test Cases\n");
513 printf("Line/Word Break Test\nOK\n");
515 printf("Line/Word Break Test\nFailed\n");