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 Communicator client 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.
23 * Pierre Phaneuf <pp@ludusdesign.com>
25 * Alternatively, the contents of this file may be used under the terms of
26 * either of the GNU General Public License Version 2 or later (the "GPL"),
27 * or the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
28 * in which case the provisions of the GPL or the LGPL are applicable instead
29 * of those above. If you wish to allow use of your version of this file only
30 * under the terms of either the GPL or the LGPL, and not to allow others to
31 * use your version of this file under the terms of the MPL, indicate your
32 * decision by deleting the provisions above and replace them with the notice
33 * and other provisions required by the GPL or the LGPL. If you do not delete
34 * the provisions above, a recipient may use your version of this file under
35 * the terms of any one of the MPL, the GPL or the LGPL.
37 * ***** END LICENSE BLOCK ***** */
42 #include "nsIComponentManager.h"
43 #include "nsIServiceManager.h"
44 #include "nsISupports.h"
45 #include "nsICharsetConverterManager.h"
46 #include "nsIPlatformCharset.h"
47 #include "nsICharRepresentable.h"
49 #include "nsReadableUtils.h"
52 static NS_DEFINE_CID(kCharsetConverterManagerCID
, NS_ICHARSETCONVERTERMANAGER_CID
);
53 static NS_DEFINE_CID(kPlatformCharsetCID
, NS_PLATFORMCHARSET_CID
);
55 //#define TEST_IS_REPRESENTABLE
58 * Test program for the Unicode Converters.
60 * Error messages format inside of a test.
62 * - silent while all is OK.
64 * - "ERROR at T001.easyConversion.Convert() code=0xfffd.\n"
65 * - "ERROR at T001.easyConversion.ConvResLen expected=0x02 result=0x04.\n"
67 * - "Test Passed.\n" for a successful end.
69 * @created 01/Dec/1998
70 * @author Catalin Rotaru [CATA]
73 //----------------------------------------------------------------------
74 // Global variables and macros
76 #define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2
78 #define ARRAY_SIZE(_array) \
79 (sizeof(_array) / sizeof(_array[0]))
81 nsICharsetConverterManager
* ccMan
= NULL
;
84 * Test data for Latin1 charset.
88 "\x00\x0d\x7f\x80\xff"
91 PRUnichar cLatin1_d0
[] = {
92 0x0000,0x000d,0x007f,0x20ac,0x00ff
95 PRInt32 bLatin1_s0
= ARRAY_SIZE(bLatin1_d0
)-1;
96 PRInt32 cLatin1_s0
= ARRAY_SIZE(cLatin1_d0
);
98 //----------------------------------------------------------------------
99 // Converter Manager test code
101 nsresult
testCharsetConverterManager()
103 printf("\n[T001] CharsetConverterManager\n");
108 //----------------------------------------------------------------------
109 // Helper functions and macros for testing decoders and encoders
111 #define CREATE_DECODER(_charset) \
112 nsIUnicodeDecoder * dec; \
113 nsAutoString str;str.AssignWithConversion(_charset); \
114 nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); \
115 if (NS_FAILED(res)) { \
116 printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res); \
120 #define CREATE_ENCODER(_charset) \
121 nsIUnicodeEncoder * enc; \
122 nsAutoString str; str.AssignWithConversion(_charset); \
123 nsresult res = ccMan->GetUnicodeEncoder(&str,&enc); \
124 if (NS_FAILED(res)) { \
125 printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res); \
132 * This method will test the conversion only.
134 nsresult
testDecoder(nsIUnicodeDecoder
* aDec
,
135 const char * aSrc
, PRInt32 aSrcLength
,
136 const PRUnichar
* aRes
, PRInt32 aResLength
,
137 const char * aTestName
)
141 // prepare for conversion
142 PRInt32 srcLen
= aSrcLength
;
143 PRUnichar dest
[GENERAL_BUFFER
/2];
144 PRInt32 destLen
= GENERAL_BUFFER
/2;
147 res
= aDec
->Convert(aSrc
, &srcLen
, dest
, &destLen
);
148 // we want a perfect result here - the test data should be complete!
150 printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName
,res
);
151 return NS_ERROR_UNEXPECTED
;
155 if (aResLength
!= destLen
) {
156 printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n",
157 aTestName
, aResLength
, destLen
);
158 return NS_ERROR_UNEXPECTED
;
160 for (PRInt32 i
=0; i
<aResLength
; i
++) if (aRes
[i
] != dest
[i
]) {
161 printf("ERROR at %s.easy.DecResChar[%d] expected=0x%x result=0x%x.\n",
162 aTestName
, i
, aRes
[i
], dest
[i
]);
163 return NS_ERROR_UNEXPECTED
;
172 * This method will test the conversion only.
174 nsresult
testEncoder(nsIUnicodeEncoder
* aEnc
,
175 const PRUnichar
* aSrc
, PRInt32 aSrcLength
,
176 const char * aRes
, PRInt32 aResLength
,
177 const char * aTestName
)
181 // prepare for conversion
183 char dest
[GENERAL_BUFFER
];
189 bcw
= GENERAL_BUFFER
;
190 res
= aEnc
->Convert(aSrc
, &bcr
, dest
, &bcw
);
193 // we want a perfect result here - the test data should be complete!
195 printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName
,res
);
196 return NS_ERROR_UNEXPECTED
;
200 bcw
= GENERAL_BUFFER
- destLen
;
201 res
= aEnc
->Finish(dest
+ destLen
, &bcw
);
203 // we want a perfect result here - the test data should be complete!
205 printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName
,res
);
206 return NS_ERROR_UNEXPECTED
;
210 if (aResLength
!= destLen
) {
211 printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n",
212 aTestName
, aResLength
, destLen
);
213 return NS_ERROR_UNEXPECTED
;
215 for (PRInt32 i
=0; i
<aResLength
; i
++) if (aRes
[i
] != dest
[i
]) {
216 printf("ERROR at %s.easy.EncResChar[%d] expected=0x%x result=0x%x.\n",
217 aTestName
, i
, aRes
[i
], dest
[i
]);
218 return NS_ERROR_UNEXPECTED
;
221 #ifdef TEST_IS_REPRESENTABLE
222 nsICharRepresentable
* rp
= nsnull
;
223 res
= aEnc
->QueryInterface(NS_GET_IID(nsICharRepresentable
),(void**) &rp
);
224 if(NS_SUCCEEDED(res
)) {
225 PRUint32
*info
= (PRUint32
*)PR_Calloc((0x10000 >> 5), 4);
227 for(int i
=0;i
< 0x10000;i
++)
229 if(IS_REPRESENTABLE(info
, i
))
241 * This method will test a given converter under a given set of data and some
242 * very stressful conditions.
244 nsresult
testStressDecoder(nsIUnicodeDecoder
* aDec
,
245 const char * aSrc
, PRInt32 aSrcLength
,
246 const PRUnichar
* aRes
, PRInt32 aResLength
,
247 const char * aTestName
)
251 // get estimated length
252 PRInt32 estimatedLength
;
253 res
= aDec
->GetMaxLength(aSrc
, aSrcLength
, &estimatedLength
);
254 if (NS_FAILED(res
)) {
255 printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName
,res
);
258 PRBool exactLength
= (res
== NS_EXACT_LENGTH
);
260 // prepare for conversion
263 PRUnichar dest
[1024];
267 // controlled conversion
268 for (;srcOff
< aSrcLength
;) {
269 res
= aDec
->Convert(aSrc
+ srcOff
, &srcLen
, dest
+ destOff
, &destLen
);
270 if (NS_FAILED(res
)) {
271 printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName
,res
);
278 // give a little input each time; it'll be consumed if enough output space
280 // give output space only when requested: sadic!
281 if (res
== NS_PARTIAL_MORE_OUTPUT
) {
288 // we want perfect result here - the test data should be complete!
290 printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName
,res
);
291 return NS_ERROR_UNEXPECTED
;
296 if (destOff
!= estimatedLength
) {
297 printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
298 aTestName
, estimatedLength
, destOff
);
299 return NS_ERROR_UNEXPECTED
;
302 if (destOff
> estimatedLength
) {
303 printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
304 aTestName
, estimatedLength
, destOff
);
305 return NS_ERROR_UNEXPECTED
;
310 if (aResLength
!= destOff
) {
311 printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n",
312 aTestName
, aResLength
, destOff
);
313 return NS_ERROR_UNEXPECTED
;
315 for (PRInt32 i
=0; i
<aResLength
; i
++) if (aRes
[i
] != dest
[i
]) {
316 printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n",
317 aTestName
, i
, aRes
[i
], dest
[i
]);
318 return NS_ERROR_UNEXPECTED
;
327 * This method will test a given converter under a given set of data and some
328 * very stressful conditions.
330 nsresult
testStressEncoder(nsIUnicodeEncoder
* aEnc
,
331 const PRUnichar
* aSrc
, PRInt32 aSrcLength
,
332 const char * aRes
, PRInt32 aResLength
,
333 const char * aTestName
)
337 // get estimated length
338 PRInt32 estimatedLength
;
339 res
= aEnc
->GetMaxLength(aSrc
, aSrcLength
, &estimatedLength
);
340 if (NS_FAILED(res
)) {
341 printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName
,res
);
344 PRBool exactLength
= (res
== NS_OK_UENC_EXACTLENGTH
);
346 // prepare for conversion
349 char dest
[GENERAL_BUFFER
];
353 // controlled conversion
354 for (;srcOff
< aSrcLength
;) {
355 res
= aEnc
->Convert(aSrc
+ srcOff
, &srcLen
, dest
+ destOff
, &destLen
);
356 if (NS_FAILED(res
)) {
357 printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName
,res
);
364 // give a little input each time; it'll be consumed if enough output space
366 // give output space only when requested: sadic!
367 if (res
== NS_OK_UENC_MOREOUTPUT
) {
374 if (res
!= NS_OK
) if (res
!= NS_OK_UENC_MOREOUTPUT
) {
375 printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName
,res
);
376 return NS_ERROR_UNEXPECTED
;
380 res
= aEnc
->Finish(dest
+ destOff
, &destLen
);
381 if (NS_FAILED(res
)) {
382 printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName
,res
);
388 // give output space only when requested: sadic!
389 if (res
== NS_OK_UENC_MOREOUTPUT
) {
396 if (destOff
!= estimatedLength
) {
397 printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n",
398 aTestName
, estimatedLength
, destOff
);
399 return NS_ERROR_UNEXPECTED
;
402 if (destOff
> estimatedLength
) {
403 printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n",
404 aTestName
, estimatedLength
, destOff
);
405 return NS_ERROR_UNEXPECTED
;
410 if (aResLength
!= destOff
) {
411 printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n",
412 aTestName
, aResLength
, destOff
);
413 return NS_ERROR_UNEXPECTED
;
415 for (PRInt32 i
=0; i
<aResLength
; i
++) if (aRes
[i
] != dest
[i
]) {
416 printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n",
417 aTestName
, i
, aRes
[i
], dest
[i
]);
418 return NS_ERROR_UNEXPECTED
;
427 nsresult
resetDecoder(nsIUnicodeDecoder
* aDec
, const char * aTestName
)
429 nsresult res
= aDec
->Reset();
431 if (NS_FAILED(res
)) {
432 printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName
,res
);
442 nsresult
resetEncoder(nsIUnicodeEncoder
* aEnc
, const char * aTestName
)
444 nsresult res
= aEnc
->Reset();
446 if (NS_FAILED(res
)) {
447 printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName
,res
);
455 * A standard decoder test.
457 nsresult
standardDecoderTest(char * aTestName
, char * aCharset
, char * aSrc
,
458 PRInt32 aSrcLen
, PRUnichar
* aRes
, PRInt32 aResLen
)
460 printf("\n[%s] Unicode <- %s\n", aTestName
, aCharset
);
463 CREATE_DECODER(aCharset
);
465 // test converter - easy test
466 res
= testDecoder(dec
, aSrc
, aSrcLen
, aRes
, aResLen
, aTestName
);
469 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, aTestName
);
471 // test converter - stress test
472 if (NS_SUCCEEDED(res
))
473 res
= testStressDecoder(dec
, aSrc
, aSrcLen
, aRes
, aResLen
, aTestName
);
478 if (NS_FAILED(res
)) {
481 printf("Test Passed.\n");
486 nsresult
loadBinaryFile(char * aFile
, char * aBuff
, PRInt32
* aBuffLen
)
488 FILE * f
= fopen(aFile
, "rb");
490 printf("ERROR at opening file: \"%s\".\n", aFile
);
491 return NS_ERROR_UNEXPECTED
;
494 PRInt32 n
= fread(aBuff
, 1, *aBuffLen
, f
);
495 if (n
>= *aBuffLen
) {
496 printf("ERROR at reading from file \"%s\": too much input data.\n", aFile
);
497 return NS_ERROR_UNEXPECTED
;
505 nsresult
loadUnicodeFile(char * aFile
, PRUnichar
* aBuff
, PRInt32
* aBuffLen
)
507 PRInt32 buffLen
= 2*(*aBuffLen
);
509 nsresult res
= loadBinaryFile(aFile
, (char *)aBuff
, &buffLen
);
510 if (NS_FAILED(res
)) return res
;
512 *aBuffLen
= buffLen
/2;
516 nsresult
testDecoderFromFiles(char * aCharset
, char * aSrcFile
, char * aResultFile
)
519 CREATE_DECODER(aCharset
);
521 PRInt32 srcLen
= GENERAL_BUFFER
;
522 char src
[GENERAL_BUFFER
];
523 PRInt32 expLen
= GENERAL_BUFFER
/2;
524 PRUnichar exp
[GENERAL_BUFFER
/2];
526 res
= loadBinaryFile(aSrcFile
, src
, &srcLen
);
527 if (NS_FAILED(res
)) return res
;
529 res
= loadUnicodeFile(aResultFile
, exp
, &expLen
);
530 if (NS_FAILED(res
)) return res
;
532 // test converter - easy test
533 res
= testDecoder(dec
, src
, srcLen
, exp
, expLen
, "dec");
538 if (NS_FAILED(res
)) {
541 printf("Test Passed.\n");
548 nsresult
testEncoderFromFiles(char * aCharset
, char * aSrcFile
, char * aResultFile
)
554 //----------------------------------------------------------------------
555 // Decoders testing functions
558 * Test the ISO2022JP decoder.
560 nsresult
testISO2022JPDecoder()
562 char * testName
= "T102";
563 printf("\n[%s] Unicode <- ISO2022JP\n", testName
);
566 CREATE_DECODER("iso-2022-jp");
569 char src
[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"};
570 PRUnichar exp
[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd};
572 // test converter - normal operation
573 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
576 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
578 // test converter - stress test
579 if (NS_SUCCEEDED(res
))
580 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
585 if (NS_FAILED(res
)) {
588 printf("Test Passed.\n");
594 * Test the EUCJP decoder.
596 nsresult
testEUCJPDecoder()
598 char * testName
= "T103";
599 printf("\n[%s] Unicode <- EUCJP\n", testName
);
602 CREATE_DECODER("euc-jp");
605 char src
[] = {"\x45"};
606 PRUnichar exp
[] = {0x0045};
608 // test converter - normal operation
609 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
612 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
614 // test converter - stress test
615 if (NS_SUCCEEDED(res
))
616 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
621 if (NS_FAILED(res
)) {
624 printf("Test Passed.\n");
630 * Test the ISO88597 decoder.
632 nsresult
testISO88597Decoder()
634 char * testName
= "T104";
635 printf("\n[%s] Unicode <- ISO88597\n", testName
);
638 CREATE_DECODER("iso-8859-7");
648 0x0009, 0x000d, 0x0020, 0x0040,
649 0xfffd, 0xfffd, 0x00a3, 0x2015,
650 0x00a7, 0x00b1, 0x00b3, 0x0399,
651 0x03a9, 0x03b3, 0x03c4, 0xfffd
654 // test converter - normal operation
655 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
658 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
660 // test converter - stress test
661 if (NS_SUCCEEDED(res
))
662 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
667 if (NS_FAILED(res
)) {
670 printf("Test Passed.\n");
676 * Test the SJIS decoder.
678 nsresult
testSJISDecoder()
680 char * testName
= "T105";
681 printf("\n[%s] Unicode <- SJIS\n", testName
);
684 CREATE_DECODER("Shift_JIS");
688 "Japanese" /* English */
689 "\x8a\xbf\x8e\x9a" /* Kanji */
690 "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
691 "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
692 "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
695 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
697 0x30ab, 0x30bf, 0x30ab, 0x30ca,
698 0x3072, 0x3089, 0x304c, 0x306a,
699 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
702 // test converter - normal operation
703 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
706 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
708 // test converter - stress test
709 if (NS_SUCCEEDED(res
))
710 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
715 if (NS_FAILED(res
)) {
718 printf("Test Passed.\n");
724 * Test the UTF8 decoder.
726 nsresult
testUTF8Decoder()
728 char * testName
= "T106";
729 printf("\n[%s] Unicode <- UTF8\n", testName
);
732 CREATE_DECODER("utf-8");
734 #ifdef NOPE // XXX decomment this when I have test data
737 PRUnichar exp
[] = {};
739 // test converter - normal operation
740 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
743 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
745 // test converter - stress test
746 if (NS_SUCCEEDED(res
))
747 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
753 if (NS_FAILED(res
)) {
756 printf("Test Passed.\n");
762 * Test the M-UTF-7 decoder.
764 nsresult
testMUTF7Decoder()
766 char * testName
= "T107";
767 printf("\n[%s] Unicode <- MUTF7\n", testName
);
770 CREATE_DECODER("x-imap4-modified-utf7");
773 char src
[] = {"\x50\x51\x52\x53&AAAAAAAA-&-&AAA-"};
774 PRUnichar exp
[] = {0x0050,0x0051,0x0052,0x0053,0x0000,0x0000,0x0000,'&',0x0000};
776 // test converter - normal operation
777 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
780 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
782 // test converter - stress test
783 if (NS_SUCCEEDED(res
))
784 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
789 if (NS_FAILED(res
)) {
792 printf("Test Passed.\n");
798 * Test the UTF-7 decoder.
800 nsresult
testUTF7Decoder()
802 char * testName
= "T108";
803 printf("\n[%s] Unicode <- UTF7\n", testName
);
806 CREATE_DECODER("utf-7");
809 char src
[] = {"+ADwAIQ-DOC"};
810 PRUnichar exp
[] = {'<','!','D','O','C'};
812 // test converter - normal operation
813 res
= testDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
816 if (NS_SUCCEEDED(res
)) res
= resetDecoder(dec
, testName
);
818 // test converter - stress test
819 if (NS_SUCCEEDED(res
))
820 res
= testStressDecoder(dec
, src
, ARRAY_SIZE(src
)-1, exp
, ARRAY_SIZE(exp
), testName
);
825 if (NS_FAILED(res
)) {
828 printf("Test Passed.\n");
833 //----------------------------------------------------------------------
834 // Encoders testing functions
837 * Test the Latin1 encoder.
839 nsresult
testLatin1Encoder()
841 char * testName
= "T201";
842 printf("\n[%s] Unicode -> Latin1\n", testName
);
845 CREATE_ENCODER("iso-8859-1");
846 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
849 PRUnichar src
[] = {0x0001,0x0002,0xffff,0x00e3};
850 char exp
[] = {"\x01\x02\xcc\xe3"};
852 // test converter - easy test
853 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
856 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
858 // test converter - stress test
859 if (NS_SUCCEEDED(res
))
860 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
865 if (NS_FAILED(res
)) {
868 printf("Test Passed.\n");
874 * Test the Shift-JIS encoder.
876 nsresult
testSJISEncoder()
878 char * testName
= "T202";
879 printf("\n[%s] Unicode -> SJIS\n", testName
);
882 CREATE_ENCODER("Shift_JIS");
883 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
887 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
889 0x30ab, 0x30bf, 0x30ab, 0x30ca,
890 0x3072, 0x3089, 0x304c, 0x306a,
891 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
894 "Japanese" /* English */
895 "\x8a\xbf\x8e\x9a" /* Kanji */
896 "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */
897 "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */
898 "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */
901 // test converter - easy test
902 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
905 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
907 // test converter - stress test
908 if (NS_SUCCEEDED(res
))
909 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
914 if (NS_FAILED(res
)) {
917 printf("Test Passed.\n");
923 * Test the EUC-JP encoder.
925 nsresult
testEUCJPEncoder()
927 char * testName
= "T203";
928 printf("\n[%s] Unicode -> EUCJP\n", testName
);
931 CREATE_ENCODER("euc-jp");
932 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
935 PRUnichar src
[] = {0x0045, 0x0054};
936 char exp
[] = {"\x45\x54"};
938 // test converter - easy test
939 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
942 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
944 // test converter - stress test
945 if (NS_SUCCEEDED(res
))
946 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
951 if (NS_FAILED(res
)) {
954 printf("Test Passed.\n");
960 * Test the ISO-2022-JP encoder.
962 nsresult
testISO2022JPEncoder()
964 char * testName
= "T204";
965 printf("\n[%s] Unicode -> ISO2022JP\n", testName
);
968 CREATE_ENCODER("iso-2022-jp");
969 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
972 PRUnichar src
[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378};
973 char exp
[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"};
975 // test converter - easy test
976 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
979 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
981 // test converter - stress test
982 if (NS_SUCCEEDED(res
))
983 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
988 if (NS_FAILED(res
)) {
991 printf("Test Passed.\n");
997 * Test the M-UTF-7 encoder.
999 nsresult
testMUTF7Encoder()
1001 char * testName
= "T205";
1002 printf("\n[%s] Unicode -> MUTF-7\n", testName
);
1005 CREATE_ENCODER("x-imap4-modified-utf7");
1006 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
1009 PRUnichar src
[] = {0x0050,0x0051,0x0052,0x0053,0x0000,0x0000,0x0000,'&',0x0000};
1010 char exp
[] = {"\x50\x51\x52\x53&AAAAAAAA-&-&AAA-"};
1012 // test converter - easy test
1013 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
1016 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
1018 // test converter - stress test
1019 if (NS_SUCCEEDED(res
))
1020 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
1022 // release converter
1025 if (NS_FAILED(res
)) {
1028 printf("Test Passed.\n");
1034 * Test the UTF-7 encoder.
1036 nsresult
testUTF7Encoder()
1038 char * testName
= "T206";
1039 printf("\n[%s] Unicode -> UTF-7\n", testName
);
1042 CREATE_ENCODER("utf-7");
1043 enc
->SetOutputErrorBehavior(enc
->kOnError_Replace
, NULL
, 0x00cc);
1046 PRUnichar src
[] = {'e','t','i','r','a',0x0a};
1047 char exp
[] = {"etira\x0a"};
1049 // test converter - easy test
1050 res
= testEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
1053 if (NS_SUCCEEDED(res
)) res
= resetEncoder(enc
, testName
);
1055 // test converter - stress test
1056 if (NS_SUCCEEDED(res
))
1057 res
= testStressEncoder(enc
, src
, ARRAY_SIZE(src
), exp
, ARRAY_SIZE(exp
)-1, testName
);
1059 // release converter
1062 if (NS_FAILED(res
)) {
1065 printf("Test Passed.\n");
1070 nsresult
testPlatformCharset()
1072 nsIPlatformCharset
*cinfo
;
1073 nsresult res
= CallGetService(kPlatformCharsetCID
, &cinfo
);
1074 if (NS_FAILED(res
)) {
1075 printf("ERROR at GetService() code=0x%x.\n",res
);
1080 res
= cinfo
->GetCharset(kPlatformCharsetSel_PlainTextInClipboard
, value
);
1081 printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUTF16toASCII(value
).get());
1083 res
= cinfo
->GetCharset(kPlatformCharsetSel_FileName
, value
);
1084 printf("File Name encoding = %s\n", NS_LossyConvertUTF16toASCII(value
).get());
1086 res
= cinfo
->GetCharset(kPlatformCharsetSel_Menu
, value
);
1087 printf("Menu encoding = %s\n", NS_LossyConvertUTF16toASCII(value
).get());
1094 //----------------------------------------------------------------------
1095 // Testing functions
1101 // test the manager(s)
1102 res
= testCharsetConverterManager();
1103 if (NS_FAILED(res
)) return res
;
1105 testPlatformCharset();
1108 standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0
, bLatin1_s0
, cLatin1_d0
, cLatin1_s0
);
1109 testISO2022JPDecoder();
1111 testISO88597Decoder();
1118 testLatin1Encoder();
1121 testISO2022JPEncoder();
1129 nsresult
testFromArgs(int argc
, char **argv
)
1131 nsresult res
= NS_OK
;
1132 if ((argc
== 5) && (!strcmp(argv
[1], "-tdec"))) {
1133 res
= testDecoderFromFiles(argv
[2], argv
[3], argv
[4]);
1134 } else if ((argc
== 5) && (!strcmp(argv
[1], "-tenc"))) {
1135 res
= testEncoderFromFiles(argv
[2], argv
[3], argv
[4]);
1138 printf(" TestUConv.exe\n");
1139 printf(" TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n");
1140 printf(" TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n");
1146 //----------------------------------------------------------------------
1147 // Main program functions
1151 nsresult rv
= NS_InitXPCOM2(nsnull
, nsnull
, nsnull
);
1154 return CallGetService(kCharsetConverterManagerCID
, &ccMan
);
1163 int main(int argc
, char **argv
)
1168 if (NS_FAILED(res
)) return -1;
1171 printf("*** Unicode Converters Test ***\n");
1173 printf("\n***--------- Done --------***\n");
1175 res
= testFromArgs(argc
, argv
);
1180 if (NS_FAILED(res
)) return -1;