1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/uconv/tests/nsTestUConv.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,985 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this 1.7 + * file, You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include <stdio.h> 1.10 +#include <string.h> 1.11 +#include "nsXPCOM.h" 1.12 +#include "nsIComponentManager.h" 1.13 +#include "nsIServiceManager.h" 1.14 +#include "nsISupports.h" 1.15 +#include "nsICharsetConverterManager.h" 1.16 +#include "nsIPlatformCharset.h" 1.17 +#include "nsReadableUtils.h" 1.18 + 1.19 + 1.20 +static NS_DEFINE_CID(kCharsetConverterManagerCID, NS_ICHARSETCONVERTERMANAGER_CID); 1.21 +static NS_DEFINE_CID(kPlatformCharsetCID, NS_PLATFORMCHARSET_CID); 1.22 + 1.23 +/** 1.24 + * Test program for the Unicode Converters. 1.25 + * 1.26 + * Error messages format inside of a test. 1.27 + * 1.28 + * - silent while all is OK. 1.29 + * 1.30 + * - "ERROR at T001.easyConversion.Convert() code=0xfffd.\n" 1.31 + * - "ERROR at T001.easyConversion.ConvResLen expected=0x02 result=0x04.\n" 1.32 + * 1.33 + * - "Test Passed.\n" for a successful end. 1.34 + * 1.35 + * @created 01/Dec/1998 1.36 + * @author Catalin Rotaru [CATA] 1.37 + */ 1.38 + 1.39 +//---------------------------------------------------------------------- 1.40 +// Global variables and macros 1.41 + 1.42 +#define GENERAL_BUFFER 20000 // general purpose buffer; for Unicode divide by 2 1.43 + 1.44 +#define ARRAY_SIZE(_array) \ 1.45 + (sizeof(_array) / sizeof(_array[0])) 1.46 + 1.47 +nsICharsetConverterManager * ccMan = nullptr; 1.48 + 1.49 +/** 1.50 + * Test data for Latin1 charset. 1.51 + */ 1.52 + 1.53 +char bLatin1_d0[] = { 1.54 + "\x00\x0d\x7f\x80\xff" 1.55 +}; 1.56 + 1.57 +char16_t cLatin1_d0[] = { 1.58 + 0x0000,0x000d,0x007f,0x20ac,0x00ff 1.59 +}; 1.60 + 1.61 +int32_t bLatin1_s0 = ARRAY_SIZE(bLatin1_d0)-1; 1.62 +int32_t cLatin1_s0 = ARRAY_SIZE(cLatin1_d0); 1.63 + 1.64 +//---------------------------------------------------------------------- 1.65 +// Converter Manager test code 1.66 + 1.67 +nsresult testCharsetConverterManager() 1.68 +{ 1.69 + printf("\n[T001] CharsetConverterManager\n"); 1.70 + 1.71 + return NS_OK; 1.72 +} 1.73 + 1.74 +//---------------------------------------------------------------------- 1.75 +// Helper functions and macros for testing decoders and encoders 1.76 + 1.77 +#define CREATE_DECODER(_charset) \ 1.78 + nsIUnicodeDecoder * dec; \ 1.79 + nsAutoString str;str.AssignASCII(_charset); \ 1.80 + nsresult res = ccMan->GetUnicodeDecoder(&str,&dec); \ 1.81 + if (NS_FAILED(res)) { \ 1.82 + printf("ERROR at GetUnicodeDecoder() code=0x%x.\n",res); \ 1.83 + return res; \ 1.84 + } 1.85 + 1.86 +#define CREATE_ENCODER(_charset) \ 1.87 + nsIUnicodeEncoder * enc; \ 1.88 + nsAutoString str; str.AssignASCII(_charset); \ 1.89 + nsresult res = ccMan->GetUnicodeEncoder(&str,&enc); \ 1.90 + if (NS_FAILED(res)) { \ 1.91 + printf("ERROR at GetUnicodeEncoder() code=0x%x.\n",res); \ 1.92 + return res; \ 1.93 + } 1.94 + 1.95 +/** 1.96 + * Decoder test. 1.97 + * 1.98 + * This method will test the conversion only. 1.99 + */ 1.100 +nsresult testDecoder(nsIUnicodeDecoder * aDec, 1.101 + const char * aSrc, int32_t aSrcLength, 1.102 + const char16_t * aRes, int32_t aResLength, 1.103 + const char * aTestName) 1.104 +{ 1.105 + nsresult res; 1.106 + 1.107 + // prepare for conversion 1.108 + int32_t srcLen = aSrcLength; 1.109 + char16_t dest[GENERAL_BUFFER/2]; 1.110 + int32_t destLen = GENERAL_BUFFER/2; 1.111 + 1.112 + // conversion 1.113 + res = aDec->Convert(aSrc, &srcLen, dest, &destLen); 1.114 + // we want a perfect result here - the test data should be complete! 1.115 + if (res != NS_OK) { 1.116 + printf("ERROR at %s.easy.Decode() code=0x%x.\n",aTestName,res); 1.117 + return NS_ERROR_UNEXPECTED; 1.118 + } 1.119 + 1.120 + // compare results 1.121 + if (aResLength != destLen) { 1.122 + printf("ERROR at %s.easy.DecResLen expected=0x%x result=0x%x.\n", 1.123 + aTestName, aResLength, destLen); 1.124 + return NS_ERROR_UNEXPECTED; 1.125 + } 1.126 + for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) { 1.127 + printf("ERROR at %s.easy.DecResChar[%d] expected=0x%x result=0x%x.\n", 1.128 + aTestName, i, aRes[i], dest[i]); 1.129 + return NS_ERROR_UNEXPECTED; 1.130 + } 1.131 + 1.132 + return NS_OK; 1.133 +} 1.134 + 1.135 +/** 1.136 + * Encoder test. 1.137 + * 1.138 + * This method will test the conversion only. 1.139 + */ 1.140 +nsresult testEncoder(nsIUnicodeEncoder * aEnc, 1.141 + const char16_t * aSrc, int32_t aSrcLength, 1.142 + const char * aRes, int32_t aResLength, 1.143 + const char * aTestName) 1.144 +{ 1.145 + nsresult res; 1.146 + 1.147 + // prepare for conversion 1.148 + int32_t srcLen = 0; 1.149 + char dest[GENERAL_BUFFER]; 1.150 + int32_t destLen = 0; 1.151 + int32_t bcr, bcw; 1.152 + 1.153 + // conversion 1.154 + bcr = aSrcLength; 1.155 + bcw = GENERAL_BUFFER; 1.156 + res = aEnc->Convert(aSrc, &bcr, dest, &bcw); 1.157 + srcLen += bcr; 1.158 + destLen += bcw; 1.159 + // we want a perfect result here - the test data should be complete! 1.160 + if (res != NS_OK) { 1.161 + printf("ERROR at %s.easy.Encode() code=0x%x.\n",aTestName,res); 1.162 + return NS_ERROR_UNEXPECTED; 1.163 + } 1.164 + 1.165 + // finish 1.166 + bcw = GENERAL_BUFFER - destLen; 1.167 + res = aEnc->Finish(dest + destLen, &bcw); 1.168 + destLen += bcw; 1.169 + // we want a perfect result here - the test data should be complete! 1.170 + if (res != NS_OK) { 1.171 + printf("ERROR at %s.easy.Finish() code=0x%x.\n",aTestName,res); 1.172 + return NS_ERROR_UNEXPECTED; 1.173 + } 1.174 + 1.175 + // compare results 1.176 + if (aResLength != destLen) { 1.177 + printf("ERROR at %s.easy.EncResLen expected=0x%x result=0x%x.\n", 1.178 + aTestName, aResLength, destLen); 1.179 + return NS_ERROR_UNEXPECTED; 1.180 + } 1.181 + for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) { 1.182 + printf("ERROR at %s.easy.EncResChar[%d] expected=0x%x result=0x%x.\n", 1.183 + aTestName, i, aRes[i], dest[i]); 1.184 + return NS_ERROR_UNEXPECTED; 1.185 + } 1.186 + 1.187 + return NS_OK; 1.188 +} 1.189 + 1.190 +/** 1.191 + * Decoder test. 1.192 + * 1.193 + * This method will test a given converter under a given set of data and some 1.194 + * very stressful conditions. 1.195 + */ 1.196 +nsresult testStressDecoder(nsIUnicodeDecoder * aDec, 1.197 + const char * aSrc, int32_t aSrcLength, 1.198 + const char16_t * aRes, int32_t aResLength, 1.199 + const char * aTestName) 1.200 +{ 1.201 + nsresult res; 1.202 + 1.203 + // get estimated length 1.204 + int32_t estimatedLength; 1.205 + res = aDec->GetMaxLength(aSrc, aSrcLength, &estimatedLength); 1.206 + if (NS_FAILED(res)) { 1.207 + printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res); 1.208 + return res; 1.209 + } 1.210 + bool exactLength = (res == NS_EXACT_LENGTH); 1.211 + 1.212 + // prepare for conversion 1.213 + int32_t srcLen = 0; 1.214 + int32_t srcOff = 0; 1.215 + char16_t dest[1024]; 1.216 + int32_t destLen = 0; 1.217 + int32_t destOff = 0; 1.218 + 1.219 + // controlled conversion 1.220 + for (;srcOff < aSrcLength;) { 1.221 + res = aDec->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen); 1.222 + if (NS_FAILED(res)) { 1.223 + printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res); 1.224 + return res; 1.225 + } 1.226 + 1.227 + srcOff+=srcLen; 1.228 + destOff+=destLen; 1.229 + 1.230 + // give a little input each time; it'll be consumed if enough output space 1.231 + srcLen = 1; 1.232 + // give output space only when requested: sadic! 1.233 + if (res == NS_PARTIAL_MORE_OUTPUT) { 1.234 + destLen = 1; 1.235 + } else { 1.236 + destLen = 0; 1.237 + } 1.238 + } 1.239 + 1.240 + // we want perfect result here - the test data should be complete! 1.241 + if (res != NS_OK) { 1.242 + printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res); 1.243 + return NS_ERROR_UNEXPECTED; 1.244 + } 1.245 + 1.246 + // compare lengths 1.247 + if (exactLength) { 1.248 + if (destOff != estimatedLength) { 1.249 + printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n", 1.250 + aTestName, estimatedLength, destOff); 1.251 + return NS_ERROR_UNEXPECTED; 1.252 + } 1.253 + } else { 1.254 + if (destOff > estimatedLength) { 1.255 + printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n", 1.256 + aTestName, estimatedLength, destOff); 1.257 + return NS_ERROR_UNEXPECTED; 1.258 + } 1.259 + } 1.260 + 1.261 + // compare results 1.262 + if (aResLength != destOff) { 1.263 + printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 1.264 + aTestName, aResLength, destOff); 1.265 + return NS_ERROR_UNEXPECTED; 1.266 + } 1.267 + for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) { 1.268 + printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 1.269 + aTestName, i, aRes[i], dest[i]); 1.270 + return NS_ERROR_UNEXPECTED; 1.271 + } 1.272 + 1.273 + return NS_OK; 1.274 +} 1.275 + 1.276 +/** 1.277 + * Encoder test. 1.278 + * 1.279 + * This method will test a given converter under a given set of data and some 1.280 + * very stressful conditions. 1.281 + */ 1.282 +nsresult testStressEncoder(nsIUnicodeEncoder * aEnc, 1.283 + const char16_t * aSrc, int32_t aSrcLength, 1.284 + const char * aRes, int32_t aResLength, 1.285 + const char * aTestName) 1.286 +{ 1.287 + nsresult res; 1.288 + 1.289 + // get estimated length 1.290 + int32_t estimatedLength; 1.291 + res = aEnc->GetMaxLength(aSrc, aSrcLength, &estimatedLength); 1.292 + if (NS_FAILED(res)) { 1.293 + printf("ERROR at %s.stress.Length() code=0x%x.\n",aTestName,res); 1.294 + return res; 1.295 + } 1.296 + bool exactLength = (res == NS_OK_UENC_EXACTLENGTH); 1.297 + 1.298 + // prepare for conversion 1.299 + int32_t srcLen = 0; 1.300 + int32_t srcOff = 0; 1.301 + char dest[GENERAL_BUFFER]; 1.302 + int32_t destLen = 0; 1.303 + int32_t destOff = 0; 1.304 + 1.305 + // controlled conversion 1.306 + for (;srcOff < aSrcLength;) { 1.307 + res = aEnc->Convert(aSrc + srcOff, &srcLen, dest + destOff, &destLen); 1.308 + if (NS_FAILED(res)) { 1.309 + printf("ERROR at %s.stress.Convert() code=0x%x.\n",aTestName,res); 1.310 + return res; 1.311 + } 1.312 + 1.313 + srcOff+=srcLen; 1.314 + destOff+=destLen; 1.315 + 1.316 + // give a little input each time; it'll be consumed if enough output space 1.317 + srcLen = 1; 1.318 + // give output space only when requested: sadic! 1.319 + if (res == NS_OK_UENC_MOREOUTPUT) { 1.320 + destLen = 1; 1.321 + } else { 1.322 + destLen = 0; 1.323 + } 1.324 + } 1.325 + 1.326 + if (res != NS_OK) if (res != NS_OK_UENC_MOREOUTPUT) { 1.327 + printf("ERROR at %s.stress.postConvert() code=0x%x.\n",aTestName,res); 1.328 + return NS_ERROR_UNEXPECTED; 1.329 + } 1.330 + 1.331 + for (;;) { 1.332 + res = aEnc->Finish(dest + destOff, &destLen); 1.333 + if (NS_FAILED(res)) { 1.334 + printf("ERROR at %s.stress.Finish() code=0x%x.\n",aTestName,res); 1.335 + return res; 1.336 + } 1.337 + 1.338 + destOff+=destLen; 1.339 + 1.340 + // give output space only when requested: sadic! 1.341 + if (res == NS_OK_UENC_MOREOUTPUT) { 1.342 + destLen = 1; 1.343 + } else break; 1.344 + } 1.345 + 1.346 + // compare lengths 1.347 + if (exactLength) { 1.348 + if (destOff != estimatedLength) { 1.349 + printf("ERROR at %s.stress.EstimatedLen expected=0x%x result=0x%x.\n", 1.350 + aTestName, estimatedLength, destOff); 1.351 + return NS_ERROR_UNEXPECTED; 1.352 + } 1.353 + } else { 1.354 + if (destOff > estimatedLength) { 1.355 + printf("ERROR at %s.stress.EstimatedLen expected<=0x%x result=0x%x.\n", 1.356 + aTestName, estimatedLength, destOff); 1.357 + return NS_ERROR_UNEXPECTED; 1.358 + } 1.359 + } 1.360 + 1.361 + // compare results 1.362 + if (aResLength != destOff) { 1.363 + printf("ERROR at %s.stress.ConvResLen expected=0x%x result=0x%x.\n", 1.364 + aTestName, aResLength, destOff); 1.365 + return NS_ERROR_UNEXPECTED; 1.366 + } 1.367 + for (int32_t i=0; i<aResLength; i++) if (aRes[i] != dest[i]) { 1.368 + printf("ERROR at %s.stress.ConvResChar[%d] expected=0x%x result=0x%x.\n", 1.369 + aTestName, i, aRes[i], dest[i]); 1.370 + return NS_ERROR_UNEXPECTED; 1.371 + } 1.372 + 1.373 + return NS_OK; 1.374 +} 1.375 + 1.376 +/** 1.377 + * Reset decoder. 1.378 + */ 1.379 +nsresult resetDecoder(nsIUnicodeDecoder * aDec, const char * aTestName) 1.380 +{ 1.381 + nsresult res = aDec->Reset(); 1.382 + 1.383 + if (NS_FAILED(res)) { 1.384 + printf("ERROR at %s.dec.Reset() code=0x%x.\n",aTestName,res); 1.385 + return res; 1.386 + } 1.387 + 1.388 + return res; 1.389 +} 1.390 + 1.391 +/** 1.392 + * Reset encoder. 1.393 + */ 1.394 +nsresult resetEncoder(nsIUnicodeEncoder * aEnc, const char * aTestName) 1.395 +{ 1.396 + nsresult res = aEnc->Reset(); 1.397 + 1.398 + if (NS_FAILED(res)) { 1.399 + printf("ERROR at %s.enc.Reset() code=0x%x.\n",aTestName,res); 1.400 + return res; 1.401 + } 1.402 + 1.403 + return res; 1.404 +} 1.405 + 1.406 +/** 1.407 + * A standard decoder test. 1.408 + */ 1.409 +nsresult standardDecoderTest(char * aTestName, char * aCharset, char * aSrc, 1.410 + int32_t aSrcLen, char16_t * aRes, int32_t aResLen) 1.411 +{ 1.412 + printf("\n[%s] Unicode <- %s\n", aTestName, aCharset); 1.413 + 1.414 + // create converter 1.415 + CREATE_DECODER(aCharset); 1.416 + 1.417 + // test converter - easy test 1.418 + res = testDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName); 1.419 + 1.420 + // reset converter 1.421 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, aTestName); 1.422 + 1.423 + // test converter - stress test 1.424 + if (NS_SUCCEEDED(res)) 1.425 + res = testStressDecoder(dec, aSrc, aSrcLen, aRes, aResLen, aTestName); 1.426 + 1.427 + // release converter 1.428 + NS_RELEASE(dec); 1.429 + 1.430 + if (NS_FAILED(res)) { 1.431 + return res; 1.432 + } else { 1.433 + printf("Test Passed.\n"); 1.434 + return NS_OK; 1.435 + } 1.436 +} 1.437 + 1.438 +nsresult loadBinaryFile(char * aFile, char * aBuff, int32_t * aBuffLen) 1.439 +{ 1.440 + FILE * f = fopen(aFile, "rb"); 1.441 + if (!f) { 1.442 + printf("ERROR at opening file: \"%s\".\n", aFile); 1.443 + return NS_ERROR_UNEXPECTED; 1.444 + } 1.445 + 1.446 + int32_t n = fread(aBuff, 1, *aBuffLen, f); 1.447 + if (n >= *aBuffLen) { 1.448 + printf("ERROR at reading from file \"%s\": too much input data.\n", aFile); 1.449 + return NS_ERROR_UNEXPECTED; 1.450 + } 1.451 + 1.452 + *aBuffLen = n; 1.453 + fclose(f); 1.454 + return NS_OK; 1.455 +} 1.456 + 1.457 +nsresult loadUnicodeFile(char * aFile, char16_t * aBuff, int32_t * aBuffLen) 1.458 +{ 1.459 + int32_t buffLen = 2*(*aBuffLen); 1.460 + 1.461 + nsresult res = loadBinaryFile(aFile, (char *)aBuff, &buffLen); 1.462 + if (NS_FAILED(res)) return res; 1.463 + 1.464 + *aBuffLen = buffLen/2; 1.465 + return NS_OK; 1.466 +} 1.467 + 1.468 +nsresult testDecoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile) 1.469 +{ 1.470 + // create converter 1.471 + CREATE_DECODER(aCharset); 1.472 + 1.473 + int32_t srcLen = GENERAL_BUFFER; 1.474 + char src[GENERAL_BUFFER]; 1.475 + int32_t expLen = GENERAL_BUFFER/2; 1.476 + char16_t exp[GENERAL_BUFFER/2]; 1.477 + 1.478 + res = loadBinaryFile(aSrcFile, src, &srcLen); 1.479 + if (NS_FAILED(res)) return res; 1.480 + 1.481 + res = loadUnicodeFile(aResultFile, exp, &expLen); 1.482 + if (NS_FAILED(res)) return res; 1.483 + 1.484 + // test converter - easy test 1.485 + res = testDecoder(dec, src, srcLen, exp, expLen, "dec"); 1.486 + 1.487 + // release converter 1.488 + NS_RELEASE(dec); 1.489 + 1.490 + if (NS_FAILED(res)) { 1.491 + return res; 1.492 + } else { 1.493 + printf("Test Passed.\n"); 1.494 + return NS_OK; 1.495 + } 1.496 + 1.497 + return NS_OK; 1.498 +} 1.499 + 1.500 +nsresult testEncoderFromFiles(char * aCharset, char * aSrcFile, char * aResultFile) 1.501 +{ 1.502 + // XXX write me 1.503 + return NS_OK; 1.504 +} 1.505 + 1.506 +//---------------------------------------------------------------------- 1.507 +// Decoders testing functions 1.508 + 1.509 +/** 1.510 + * Test the ISO2022JP decoder. 1.511 + */ 1.512 +nsresult testISO2022JPDecoder() 1.513 +{ 1.514 + char * testName = "T102"; 1.515 + printf("\n[%s] Unicode <- ISO2022JP\n", testName); 1.516 + 1.517 + // create converter 1.518 + CREATE_DECODER("iso-2022-jp"); 1.519 + 1.520 + // test data 1.521 + char src[] = {"\x0d\x7f\xdd" "\x1b(J\xaa\xdc\x41" "\x1b$B\x21\x21" "\x1b$@\x32\x37" "\x1b(J\x1b(B\xcc"}; 1.522 + char16_t exp[] = {0x000d,0x007f,0xfffd, 0xff6a,0xFF9C,0x0041, 0x3000, 0x5378, 0xfffd}; 1.523 + 1.524 + // test converter - normal operation 1.525 + res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.526 + 1.527 + // reset converter 1.528 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); 1.529 + 1.530 + // test converter - stress test 1.531 + if (NS_SUCCEEDED(res)) 1.532 + res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.533 + 1.534 + // release converter 1.535 + NS_RELEASE(dec); 1.536 + 1.537 + if (NS_FAILED(res)) { 1.538 + return res; 1.539 + } else { 1.540 + printf("Test Passed.\n"); 1.541 + return NS_OK; 1.542 + } 1.543 +} 1.544 + 1.545 +/** 1.546 + * Test the EUCJP decoder. 1.547 + */ 1.548 +nsresult testEUCJPDecoder() 1.549 +{ 1.550 + char * testName = "T103"; 1.551 + printf("\n[%s] Unicode <- EUCJP\n", testName); 1.552 + 1.553 + // create converter 1.554 + CREATE_DECODER("euc-jp"); 1.555 + 1.556 + // test data 1.557 + char src[] = {"\x45"}; 1.558 + char16_t exp[] = {0x0045}; 1.559 + 1.560 + // test converter - normal operation 1.561 + res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.562 + 1.563 + // reset converter 1.564 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); 1.565 + 1.566 + // test converter - stress test 1.567 + if (NS_SUCCEEDED(res)) 1.568 + res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.569 + 1.570 + // release converter 1.571 + NS_RELEASE(dec); 1.572 + 1.573 + if (NS_FAILED(res)) { 1.574 + return res; 1.575 + } else { 1.576 + printf("Test Passed.\n"); 1.577 + return NS_OK; 1.578 + } 1.579 +} 1.580 + 1.581 +/** 1.582 + * Test the ISO88597 decoder. 1.583 + */ 1.584 +nsresult testISO88597Decoder() 1.585 +{ 1.586 + char * testName = "T104"; 1.587 + printf("\n[%s] Unicode <- ISO88597\n", testName); 1.588 + 1.589 + // create converter 1.590 + CREATE_DECODER("iso-8859-7"); 1.591 + 1.592 + // test data 1.593 + char src[] = { 1.594 + "\x09\x0d\x20\x40" 1.595 + "\x80\x98\xa3\xaf" 1.596 + "\xa7\xb1\xb3\xc9" 1.597 + "\xd9\xe3\xf4\xff" 1.598 + }; 1.599 + char16_t exp[] = { 1.600 + 0x0009, 0x000d, 0x0020, 0x0040, 1.601 + 0xfffd, 0xfffd, 0x00a3, 0x2015, 1.602 + 0x00a7, 0x00b1, 0x00b3, 0x0399, 1.603 + 0x03a9, 0x03b3, 0x03c4, 0xfffd 1.604 + }; 1.605 + 1.606 + // test converter - normal operation 1.607 + res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.608 + 1.609 + // reset converter 1.610 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); 1.611 + 1.612 + // test converter - stress test 1.613 + if (NS_SUCCEEDED(res)) 1.614 + res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.615 + 1.616 + // release converter 1.617 + NS_RELEASE(dec); 1.618 + 1.619 + if (NS_FAILED(res)) { 1.620 + return res; 1.621 + } else { 1.622 + printf("Test Passed.\n"); 1.623 + return NS_OK; 1.624 + } 1.625 +} 1.626 + 1.627 +/** 1.628 + * Test the SJIS decoder. 1.629 + */ 1.630 +nsresult testSJISDecoder() 1.631 +{ 1.632 + char * testName = "T105"; 1.633 + printf("\n[%s] Unicode <- SJIS\n", testName); 1.634 + 1.635 + // create converter 1.636 + CREATE_DECODER("Shift_JIS"); 1.637 + 1.638 + // test data 1.639 + char src[] = { 1.640 + "Japanese" /* English */ 1.641 + "\x8a\xbf\x8e\x9a" /* Kanji */ 1.642 + "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */ 1.643 + "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */ 1.644 + "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */ 1.645 + }; 1.646 + char16_t exp[] = { 1.647 + 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065, 1.648 + 0x6f22, 0x5b57, 1.649 + 0x30ab, 0x30bf, 0x30ab, 0x30ca, 1.650 + 0x3072, 0x3089, 0x304c, 0x306a, 1.651 + 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23 1.652 + }; 1.653 + 1.654 + // test converter - normal operation 1.655 + res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.656 + 1.657 + // reset converter 1.658 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); 1.659 + 1.660 + // test converter - stress test 1.661 + if (NS_SUCCEEDED(res)) 1.662 + res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.663 + 1.664 + // release converter 1.665 + NS_RELEASE(dec); 1.666 + 1.667 + if (NS_FAILED(res)) { 1.668 + return res; 1.669 + } else { 1.670 + printf("Test Passed.\n"); 1.671 + return NS_OK; 1.672 + } 1.673 +} 1.674 + 1.675 +/** 1.676 + * Test the UTF8 decoder. 1.677 + */ 1.678 +nsresult testUTF8Decoder() 1.679 +{ 1.680 + char * testName = "T106"; 1.681 + printf("\n[%s] Unicode <- UTF8\n", testName); 1.682 + 1.683 + // create converter 1.684 + CREATE_DECODER("utf-8"); 1.685 + 1.686 +#ifdef NOPE // XXX decomment this when I have test data 1.687 + // test data 1.688 + char src[] = {}; 1.689 + char16_t exp[] = {}; 1.690 + 1.691 + // test converter - normal operation 1.692 + res = testDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.693 + 1.694 + // reset converter 1.695 + if (NS_SUCCEEDED(res)) res = resetDecoder(dec, testName); 1.696 + 1.697 + // test converter - stress test 1.698 + if (NS_SUCCEEDED(res)) 1.699 + res = testStressDecoder(dec, src, ARRAY_SIZE(src)-1, exp, ARRAY_SIZE(exp), testName); 1.700 +#endif 1.701 + 1.702 + // release converter 1.703 + NS_RELEASE(dec); 1.704 + 1.705 + if (NS_FAILED(res)) { 1.706 + return res; 1.707 + } else { 1.708 + printf("Test Passed.\n"); 1.709 + return NS_OK; 1.710 + } 1.711 +} 1.712 + 1.713 +//---------------------------------------------------------------------- 1.714 +// Encoders testing functions 1.715 + 1.716 +/** 1.717 + * Test the Latin1 encoder. 1.718 + */ 1.719 +nsresult testLatin1Encoder() 1.720 +{ 1.721 + char * testName = "T201"; 1.722 + printf("\n[%s] Unicode -> Latin1\n", testName); 1.723 + 1.724 + // create converter 1.725 + CREATE_ENCODER("iso-8859-1"); 1.726 + enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); 1.727 + 1.728 + // test data 1.729 + char16_t src[] = {0x0001,0x0002,0xffff,0x00e3}; 1.730 + char exp[] = {"\x01\x02\xcc\xe3"}; 1.731 + 1.732 + // test converter - easy test 1.733 + res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.734 + 1.735 + // reset converter 1.736 + if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); 1.737 + 1.738 + // test converter - stress test 1.739 + if (NS_SUCCEEDED(res)) 1.740 + res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.741 + 1.742 + // release converter 1.743 + NS_RELEASE(enc); 1.744 + 1.745 + if (NS_FAILED(res)) { 1.746 + return res; 1.747 + } else { 1.748 + printf("Test Passed.\n"); 1.749 + return NS_OK; 1.750 + } 1.751 +} 1.752 + 1.753 +/** 1.754 + * Test the Shift-JIS encoder. 1.755 + */ 1.756 +nsresult testSJISEncoder() 1.757 +{ 1.758 + char * testName = "T202"; 1.759 + printf("\n[%s] Unicode -> SJIS\n", testName); 1.760 + 1.761 + // create converter 1.762 + CREATE_ENCODER("Shift_JIS"); 1.763 + enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); 1.764 + 1.765 + // test data 1.766 + char16_t src[] = { 1.767 + 0x004A, 0x0061, 0x0070, 0x0061, 0x006E, 0x0065, 0x0073, 0x0065, 1.768 + 0x6f22, 0x5b57, 1.769 + 0x30ab, 0x30bf, 0x30ab, 0x30ca, 1.770 + 0x3072, 0x3089, 0x304c, 0x306a, 1.771 + 0xff11, 0xff12, 0xff13, 0xff21, 0xff22, 0xff23 1.772 + }; 1.773 + char exp[] = { 1.774 + "Japanese" /* English */ 1.775 + "\x8a\xbf\x8e\x9a" /* Kanji */ 1.776 + "\x83\x4a\x83\x5e\x83\x4a\x83\x69" /* Kantakana */ 1.777 + "\x82\xd0\x82\xe7\x82\xaa\x82\xc8" /* Hiragana */ 1.778 + "\x82\x50\x82\x51\x82\x52\x82\x60\x82\x61\x82\x62" /* full width 123ABC */ 1.779 + }; 1.780 + 1.781 + // test converter - easy test 1.782 + res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.783 + 1.784 + // reset converter 1.785 + if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); 1.786 + 1.787 + // test converter - stress test 1.788 + if (NS_SUCCEEDED(res)) 1.789 + res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.790 + 1.791 + // release converter 1.792 + NS_RELEASE(enc); 1.793 + 1.794 + if (NS_FAILED(res)) { 1.795 + return res; 1.796 + } else { 1.797 + printf("Test Passed.\n"); 1.798 + return NS_OK; 1.799 + } 1.800 +} 1.801 + 1.802 +/** 1.803 + * Test the EUC-JP encoder. 1.804 + */ 1.805 +nsresult testEUCJPEncoder() 1.806 +{ 1.807 + char * testName = "T203"; 1.808 + printf("\n[%s] Unicode -> EUCJP\n", testName); 1.809 + 1.810 + // create converter 1.811 + CREATE_ENCODER("euc-jp"); 1.812 + enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); 1.813 + 1.814 + // test data 1.815 + char16_t src[] = {0x0045, 0x0054}; 1.816 + char exp[] = {"\x45\x54"}; 1.817 + 1.818 + // test converter - easy test 1.819 + res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.820 + 1.821 + // reset converter 1.822 + if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); 1.823 + 1.824 + // test converter - stress test 1.825 + if (NS_SUCCEEDED(res)) 1.826 + res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.827 + 1.828 + // release converter 1.829 + NS_RELEASE(enc); 1.830 + 1.831 + if (NS_FAILED(res)) { 1.832 + return res; 1.833 + } else { 1.834 + printf("Test Passed.\n"); 1.835 + return NS_OK; 1.836 + } 1.837 +} 1.838 + 1.839 +/** 1.840 + * Test the ISO-2022-JP encoder. 1.841 + */ 1.842 +nsresult testISO2022JPEncoder() 1.843 +{ 1.844 + char * testName = "T204"; 1.845 + printf("\n[%s] Unicode -> ISO2022JP\n", testName); 1.846 + 1.847 + // create converter 1.848 + CREATE_ENCODER("iso-2022-jp"); 1.849 + enc->SetOutputErrorBehavior(enc->kOnError_Replace, nullptr, 0x00cc); 1.850 + 1.851 + // test data 1.852 + char16_t src[] = {0x000d,0x007f, 0xff6a,0xFF9C, 0x3000, 0x5378}; 1.853 + char exp[] = {"\x0d\x7f" "\x1b(J\xaa\xdc" "\x1b$@\x21\x21\x32\x37\x1b(B"}; 1.854 + 1.855 + // test converter - easy test 1.856 + res = testEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.857 + 1.858 + // reset converter 1.859 + if (NS_SUCCEEDED(res)) res = resetEncoder(enc, testName); 1.860 + 1.861 + // test converter - stress test 1.862 + if (NS_SUCCEEDED(res)) 1.863 + res = testStressEncoder(enc, src, ARRAY_SIZE(src), exp, ARRAY_SIZE(exp)-1, testName); 1.864 + 1.865 + // release converter 1.866 + NS_RELEASE(enc); 1.867 + 1.868 + if (NS_FAILED(res)) { 1.869 + return res; 1.870 + } else { 1.871 + printf("Test Passed.\n"); 1.872 + return NS_OK; 1.873 + } 1.874 +} 1.875 + 1.876 +nsresult testPlatformCharset() 1.877 +{ 1.878 + nsIPlatformCharset *cinfo; 1.879 + nsresult res = CallGetService(kPlatformCharsetCID, &cinfo); 1.880 + if (NS_FAILED(res)) { 1.881 + printf("ERROR at GetService() code=0x%x.\n",res); 1.882 + return res; 1.883 + } 1.884 + 1.885 + nsString value; 1.886 + res = cinfo->GetCharset(kPlatformCharsetSel_PlainTextInClipboard , value); 1.887 + printf("Clipboard plain text encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); 1.888 + 1.889 + res = cinfo->GetCharset(kPlatformCharsetSel_FileName , value); 1.890 + printf("File Name encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); 1.891 + 1.892 + res = cinfo->GetCharset(kPlatformCharsetSel_Menu , value); 1.893 + printf("Menu encoding = %s\n", NS_LossyConvertUTF16toASCII(value).get()); 1.894 + 1.895 + cinfo->Release(); 1.896 + return res; 1.897 + 1.898 +} 1.899 + 1.900 +//---------------------------------------------------------------------- 1.901 +// Testing functions 1.902 + 1.903 +nsresult testAll() 1.904 +{ 1.905 + nsresult res; 1.906 + 1.907 + // test the manager(s) 1.908 + res = testCharsetConverterManager(); 1.909 + if (NS_FAILED(res)) return res; 1.910 + 1.911 + testPlatformCharset(); 1.912 + 1.913 + // test decoders 1.914 + standardDecoderTest("T101", "ISO-8859-1", bLatin1_d0, bLatin1_s0, cLatin1_d0, cLatin1_s0); 1.915 + testISO2022JPDecoder(); 1.916 + testEUCJPDecoder(); 1.917 + testISO88597Decoder(); 1.918 + testSJISDecoder(); 1.919 + testUTF8Decoder(); 1.920 + testMUTF7Decoder(); 1.921 + testUTF7Decoder(); 1.922 + 1.923 + // test encoders 1.924 + testLatin1Encoder(); 1.925 + testSJISEncoder(); 1.926 + testEUCJPEncoder(); 1.927 + testISO2022JPEncoder(); 1.928 + testMUTF7Encoder(); 1.929 + testUTF7Encoder(); 1.930 + 1.931 + // return 1.932 + return NS_OK; 1.933 +} 1.934 + 1.935 +nsresult testFromArgs(int argc, char **argv) 1.936 +{ 1.937 + nsresult res = NS_OK; 1.938 + if ((argc == 5) && (!strcmp(argv[1], "-tdec"))) { 1.939 + res = testDecoderFromFiles(argv[2], argv[3], argv[4]); 1.940 + } else if ((argc == 5) && (!strcmp(argv[1], "-tenc"))) { 1.941 + res = testEncoderFromFiles(argv[2], argv[3], argv[4]); 1.942 + } else { 1.943 + printf("Usage:\n"); 1.944 + printf(" TestUConv.exe\n"); 1.945 + printf(" TestUConv.exe -tdec encoding inputEncodedFile expectedResultUnicodeFile\n"); 1.946 + printf(" TestUConv.exe -tenc encoding inputUnicodeFile expectedResultEncodedFile\n"); 1.947 + } 1.948 + 1.949 + return res; 1.950 +} 1.951 + 1.952 +//---------------------------------------------------------------------- 1.953 +// Main program functions 1.954 + 1.955 +nsresult init() 1.956 +{ 1.957 + nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.958 + if (NS_FAILED(rv)) 1.959 + return rv; 1.960 + return CallGetService(kCharsetConverterManagerCID, &ccMan); 1.961 +} 1.962 + 1.963 +nsresult done() 1.964 +{ 1.965 + NS_RELEASE(ccMan); 1.966 + return NS_OK; 1.967 +} 1.968 + 1.969 +int main(int argc, char **argv) 1.970 +{ 1.971 + nsresult res; 1.972 + 1.973 + res = init(); 1.974 + if (NS_FAILED(res)) return -1; 1.975 + 1.976 + if (argc <= 1) { 1.977 + printf("*** Unicode Converters Test ***\n"); 1.978 + res = testAll(); 1.979 + printf("\n***--------- Done --------***\n"); 1.980 + } else { 1.981 + res = testFromArgs(argc, argv); 1.982 + } 1.983 + 1.984 + done(); 1.985 + 1.986 + if (NS_FAILED(res)) return -1; 1.987 + else return 0; 1.988 +}