Bug 448909 - Need more controls WHATWG Video tag (followup patch). r=mconnor
[wine-gecko.git] / intl / uconv / tests / nsTestUConv.cpp
blob968a3e0f8576b0a220429d466ebbcc12d06f0cb7
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 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.
22 * Contributor(s):
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 ***** */
39 #include <stdio.h>
40 #include <string.h>
41 #include "nsXPCOM.h"
42 #include "nsIComponentManager.h"
43 #include "nsIServiceManager.h"
44 #include "nsISupports.h"
45 #include "nsICharsetConverterManager.h"
46 #include "nsIPlatformCharset.h"
47 #include "nsICharRepresentable.h"
48 #include "prmem.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
57 /**
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;
83 /**
84 * Test data for Latin1 charset.
87 char bLatin1_d0[] = {
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");
105 return NS_OK;
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); \
117 return 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); \
126 return res; \
130 * Decoder test.
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)
139 nsresult res;
141 // prepare for conversion
142 PRInt32 srcLen = aSrcLength;
143 PRUnichar dest[GENERAL_BUFFER/2];
144 PRInt32 destLen = GENERAL_BUFFER/2;
146 // conversion
147 res = aDec->Convert(aSrc, &srcLen, dest, &destLen);
148 // we want a perfect result here - the test data should be complete!
149 if (res != NS_OK) {
150 printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res);
151 return NS_ERROR_UNEXPECTED;
154 // compare results
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;
166 return NS_OK;
170 * Encoder test.
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)
179 nsresult res;
181 // prepare for conversion
182 PRInt32 srcLen = 0;
183 char dest[GENERAL_BUFFER];
184 PRInt32 destLen = 0;
185 PRInt32 bcr, bcw;
187 // conversion
188 bcr = aSrcLength;
189 bcw = GENERAL_BUFFER;
190 res = aEnc->Convert(aSrc, &bcr, dest, &bcw);
191 srcLen += bcr;
192 destLen += bcw;
193 // we want a perfect result here - the test data should be complete!
194 if (res != NS_OK) {
195 printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res);
196 return NS_ERROR_UNEXPECTED;
199 // finish
200 bcw = GENERAL_BUFFER - destLen;
201 res = aEnc->Finish(dest + destLen, &bcw);
202 destLen += bcw;
203 // we want a perfect result here - the test data should be complete!
204 if (res != NS_OK) {
205 printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res);
206 return NS_ERROR_UNEXPECTED;
209 // compare results
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);
226 rp->FillInfo(info);
227 for(int i=0;i< 0x10000;i++)
229 if(IS_REPRESENTABLE(info, i))
230 printf("%4x\n", i);
233 #endif
235 return NS_OK;
239 * Decoder test.
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)
249 nsresult res;
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);
256 return res;
258 PRBool exactLength = (res == NS_EXACT_LENGTH);
260 // prepare for conversion
261 PRInt32 srcLen = 0;
262 PRInt32 srcOff = 0;
263 PRUnichar dest[1024];
264 PRInt32 destLen = 0;
265 PRInt32 destOff = 0;
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);
272 return res;
275 srcOff+=srcLen;
276 destOff+=destLen;
278 // give a little input each time; it'll be consumed if enough output space
279 srcLen = 1;
280 // give output space only when requested: sadic!
281 if (res == NS_PARTIAL_MORE_OUTPUT) {
282 destLen = 1;
283 } else {
284 destLen = 0;
288 // we want perfect result here - the test data should be complete!
289 if (res != NS_OK) {
290 printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res);
291 return NS_ERROR_UNEXPECTED;
294 // compare lengths
295 if (exactLength) {
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;
301 } else {
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;
309 // compare results
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;
321 return NS_OK;
325 * Encoder test.
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)
335 nsresult res;
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);
342 return res;
344 PRBool exactLength = (res == NS_OK_UENC_EXACTLENGTH);
346 // prepare for conversion
347 PRInt32 srcLen = 0;
348 PRInt32 srcOff = 0;
349 char dest[GENERAL_BUFFER];
350 PRInt32 destLen = 0;
351 PRInt32 destOff = 0;
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);
358 return res;
361 srcOff+=srcLen;
362 destOff+=destLen;
364 // give a little input each time; it'll be consumed if enough output space
365 srcLen = 1;
366 // give output space only when requested: sadic!
367 if (res == NS_OK_UENC_MOREOUTPUT) {
368 destLen = 1;
369 } else {
370 destLen = 0;
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;
379 for (;;) {
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);
383 return res;
386 destOff+=destLen;
388 // give output space only when requested: sadic!
389 if (res == NS_OK_UENC_MOREOUTPUT) {
390 destLen = 1;
391 } else break;
394 // compare lengths
395 if (exactLength) {
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;
401 } else {
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;
409 // compare results
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;
421 return NS_OK;
425 * Reset decoder.
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);
433 return res;
436 return res;
440 * Reset encoder.
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);
448 return res;
451 return 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);
462 // create converter
463 CREATE_DECODER(aCharset);
465 // test converter - easy test
466 res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName);
468 // reset converter
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);
475 // release converter
476 NS_RELEASE(dec);
478 if (NS_FAILED(res)) {
479 return res;
480 } else {
481 printf("Test Passed.\n");
482 return NS_OK;
486 nsresult loadBinaryFile(char * aFile, char * aBuff, PRInt32 * aBuffLen)
488 FILE * f = fopen(aFile, "rb");
489 if (f == NULL) {
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;
500 *aBuffLen = n;
501 fclose(f);
502 return NS_OK;
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;
513 return NS_OK;
516 nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
518 // create converter
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");
535 // release converter
536 NS_RELEASE(dec);
538 if (NS_FAILED(res)) {
539 return res;
540 } else {
541 printf("Test Passed.\n");
542 return NS_OK;
545 return NS_OK;
548 nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile)
550 // XXX write me
551 return NS_OK;
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);
565 // create converter
566 CREATE_DECODER("iso-2022-jp");
568 // test data
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);
575 // reset converter
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);
582 // release converter
583 NS_RELEASE(dec);
585 if (NS_FAILED(res)) {
586 return res;
587 } else {
588 printf("Test Passed.\n");
589 return NS_OK;
594 * Test the EUCJP decoder.
596 nsresult testEUCJPDecoder()
598 char * testName = "T103";
599 printf("\n[%s] Unicode <- EUCJP\n", testName);
601 // create converter
602 CREATE_DECODER("euc-jp");
604 // test data
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);
611 // reset converter
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);
618 // release converter
619 NS_RELEASE(dec);
621 if (NS_FAILED(res)) {
622 return res;
623 } else {
624 printf("Test Passed.\n");
625 return NS_OK;
630 * Test the ISO88597 decoder.
632 nsresult testISO88597Decoder()
634 char * testName = "T104";
635 printf("\n[%s] Unicode <- ISO88597\n", testName);
637 // create converter
638 CREATE_DECODER("iso-8859-7");
640 // test data
641 char src[] = {
642 "\x09\x0d\x20\x40"
643 "\x80\x98\xa3\xaf"
644 "\xa7\xb1\xb3\xc9"
645 "\xd9\xe3\xf4\xff"
647 PRUnichar exp[] = {
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);
657 // reset converter
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);
664 // release converter
665 NS_RELEASE(dec);
667 if (NS_FAILED(res)) {
668 return res;
669 } else {
670 printf("Test Passed.\n");
671 return NS_OK;
676 * Test the SJIS decoder.
678 nsresult testSJISDecoder()
680 char * testName = "T105";
681 printf("\n[%s] Unicode <- SJIS\n", testName);
683 // create converter
684 CREATE_DECODER("Shift_JIS");
686 // test data
687 char src[] = {
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 */
694 PRUnichar exp[] = {
695 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
696 0x6f22, 0x5b57,
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);
705 // reset converter
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);
712 // release converter
713 NS_RELEASE(dec);
715 if (NS_FAILED(res)) {
716 return res;
717 } else {
718 printf("Test Passed.\n");
719 return NS_OK;
724 * Test the UTF8 decoder.
726 nsresult testUTF8Decoder()
728 char * testName = "T106";
729 printf("\n[%s] Unicode <- UTF8\n", testName);
731 // create converter
732 CREATE_DECODER("utf-8");
734 #ifdef NOPE // XXX decomment this when I have test data
735 // test data
736 char src[] = {};
737 PRUnichar exp[] = {};
739 // test converter - normal operation
740 res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName);
742 // reset converter
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);
748 #endif
750 // release converter
751 NS_RELEASE(dec);
753 if (NS_FAILED(res)) {
754 return res;
755 } else {
756 printf("Test Passed.\n");
757 return NS_OK;
762 * Test the M-UTF-7 decoder.
764 nsresult testMUTF7Decoder()
766 char * testName = "T107";
767 printf("\n[%s] Unicode <- MUTF7\n", testName);
769 // create converter
770 CREATE_DECODER("x-imap4-modified-utf7");
772 // test data
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);
779 // reset converter
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);
786 // release converter
787 NS_RELEASE(dec);
789 if (NS_FAILED(res)) {
790 return res;
791 } else {
792 printf("Test Passed.\n");
793 return NS_OK;
798 * Test the UTF-7 decoder.
800 nsresult testUTF7Decoder()
802 char * testName = "T108";
803 printf("\n[%s] Unicode <- UTF7\n", testName);
805 // create converter
806 CREATE_DECODER("utf-7");
808 // test data
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);
815 // reset converter
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);
822 // release converter
823 NS_RELEASE(dec);
825 if (NS_FAILED(res)) {
826 return res;
827 } else {
828 printf("Test Passed.\n");
829 return NS_OK;
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);
844 // create converter
845 CREATE_ENCODER("iso-8859-1");
846 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
848 // test data
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);
855 // reset converter
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);
862 // release converter
863 NS_RELEASE(enc);
865 if (NS_FAILED(res)) {
866 return res;
867 } else {
868 printf("Test Passed.\n");
869 return NS_OK;
874 * Test the Shift-JIS encoder.
876 nsresult testSJISEncoder()
878 char * testName = "T202";
879 printf("\n[%s] Unicode -> SJIS\n", testName);
881 // create converter
882 CREATE_ENCODER("Shift_JIS");
883 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
885 // test data
886 PRUnichar src[] = {
887 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065,
888 0x6f22, 0x5b57,
889 0x30ab, 0x30bf, 0x30ab, 0x30ca,
890 0x3072, 0x3089, 0x304c, 0x306a,
891 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23
893 char exp[] = {
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);
904 // reset converter
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);
911 // release converter
912 NS_RELEASE(enc);
914 if (NS_FAILED(res)) {
915 return res;
916 } else {
917 printf("Test Passed.\n");
918 return NS_OK;
923 * Test the EUC-JP encoder.
925 nsresult testEUCJPEncoder()
927 char * testName = "T203";
928 printf("\n[%s] Unicode -> EUCJP\n", testName);
930 // create converter
931 CREATE_ENCODER("euc-jp");
932 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
934 // test data
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);
941 // reset converter
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);
948 // release converter
949 NS_RELEASE(enc);
951 if (NS_FAILED(res)) {
952 return res;
953 } else {
954 printf("Test Passed.\n");
955 return NS_OK;
960 * Test the ISO-2022-JP encoder.
962 nsresult testISO2022JPEncoder()
964 char * testName = "T204";
965 printf("\n[%s] Unicode -> ISO2022JP\n", testName);
967 // create converter
968 CREATE_ENCODER("iso-2022-jp");
969 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
971 // test data
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);
978 // reset converter
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);
985 // release converter
986 NS_RELEASE(enc);
988 if (NS_FAILED(res)) {
989 return res;
990 } else {
991 printf("Test Passed.\n");
992 return NS_OK;
997 * Test the M-UTF-7 encoder.
999 nsresult testMUTF7Encoder()
1001 char * testName = "T205";
1002 printf("\n[%s] Unicode -> MUTF-7\n", testName);
1004 // create converter
1005 CREATE_ENCODER("x-imap4-modified-utf7");
1006 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
1008 // test data
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);
1015 // reset converter
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
1023 NS_RELEASE(enc);
1025 if (NS_FAILED(res)) {
1026 return res;
1027 } else {
1028 printf("Test Passed.\n");
1029 return NS_OK;
1034 * Test the UTF-7 encoder.
1036 nsresult testUTF7Encoder()
1038 char * testName = "T206";
1039 printf("\n[%s] Unicode -> UTF-7\n", testName);
1041 // create converter
1042 CREATE_ENCODER("utf-7");
1043 enc->SetOutputErrorBehavior(enc->kOnError_Replace, NULL, 0x00cc);
1045 // test data
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);
1052 // reset converter
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
1060 NS_RELEASE(enc);
1062 if (NS_FAILED(res)) {
1063 return res;
1064 } else {
1065 printf("Test Passed.\n");
1066 return NS_OK;
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);
1076 return res;
1079 nsString value;
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());
1089 cinfo->Release();
1090 return res;
1094 //----------------------------------------------------------------------
1095 // Testing functions
1097 nsresult testAll()
1099 nsresult res;
1101 // test the manager(s)
1102 res = testCharsetConverterManager();
1103 if (NS_FAILED(res)) return res;
1105 testPlatformCharset();
1107 // test decoders
1108 standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0);
1109 testISO2022JPDecoder();
1110 testEUCJPDecoder();
1111 testISO88597Decoder();
1112 testSJISDecoder();
1113 testUTF8Decoder();
1114 testMUTF7Decoder();
1115 testUTF7Decoder();
1117 // test encoders
1118 testLatin1Encoder();
1119 testSJISEncoder();
1120 testEUCJPEncoder();
1121 testISO2022JPEncoder();
1122 testMUTF7Encoder();
1123 testUTF7Encoder();
1125 // return
1126 return NS_OK;
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]);
1136 } else {
1137 printf("Usage:\n");
1138 printf(" TestUConv.exe\n");
1139 printf(" TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n");
1140 printf(" TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n");
1143 return res;
1146 //----------------------------------------------------------------------
1147 // Main program functions
1149 nsresult init()
1151 nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
1152 if (NS_FAILED(rv))
1153 return rv;
1154 return CallGetService(kCharsetConverterManagerCID, &ccMan);
1157 nsresult done()
1159 NS_RELEASE(ccMan);
1160 return NS_OK;
1163 int main(int argc, char **argv)
1165 nsresult res;
1167 res = init();
1168 if (NS_FAILED(res)) return -1;
1170 if (argc <= 1) {
1171 printf("*** Unicode Converters Test ***\n");
1172 res = testAll();
1173 printf("\n***--------- Done --------***\n");
1174 } else {
1175 res = testFromArgs(argc, argv);
1178 done();
1180 if (NS_FAILED(res)) return -1;
1181 else return 0;