1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/intl/unicharutil/tests/UnicharSelfTest.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,722 @@ 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 "nsXPCOM.h" 1.11 +#include "nsIEntityConverter.h" 1.12 +#include "nsISaveAsCharset.h" 1.13 +#include "nsCOMPtr.h" 1.14 +#include "nsIUnicodeNormalizer.h" 1.15 +#include "nsStringAPI.h" 1.16 +#include "nsUnicharUtils.h" 1.17 +#include "nsMemory.h" 1.18 +#include "nsComponentManagerUtils.h" 1.19 +#include "nsServiceManagerUtils.h" 1.20 + 1.21 +NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID); 1.22 +NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID); 1.23 +NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID); 1.24 + 1.25 +#define TESTLEN 32 1.26 +#define T2LEN TESTLEN 1.27 +#define T3LEN TESTLEN 1.28 +#define T4LEN TESTLEN 1.29 + 1.30 +// test data for ToUpper 1.31 +static char16_t t2data [T2LEN+1] = { 1.32 + 0x0031 , // 0 1.33 + 0x0019 , // 1 1.34 + 0x0043 , // 2 1.35 + 0x0067 , // 3 1.36 + 0x00C8 , // 4 1.37 + 0x00E9 , // 5 1.38 + 0x0147 , // 6 1.39 + 0x01C4 , // 7 1.40 + 0x01C6 , // 8 1.41 + 0x01C5 , // 9 1.42 + 0x03C0 , // 10 1.43 + 0x03B2 , // 11 1.44 + 0x0438 , // 12 1.45 + 0x04A5 , // 13 1.46 + 0x05D0 , // 14 1.47 + 0x0A20 , // 15 1.48 + 0x30B0 , // 16 1.49 + 0x5185 , // 17 1.50 + 0xC021 , // 18 1.51 + 0xFF48 , // 19 1.52 + 0x01C7 , // 20 1.53 + 0x01C8 , // 21 1.54 + 0x01C9 , // 22 1.55 + 0x01CA , // 23 1.56 + 0x01CB , // 24 1.57 + 0x01CC , // 25 1.58 + 0x01F1 , // 26 1.59 + 0x01F2 , // 27 1.60 + 0x01F3 , // 28 1.61 + 0x0250 , // 29 1.62 + 0x0271 , // 30 1.63 + 0xA641 , // 31 1.64 + 0x00 1.65 +}; 1.66 +// expected result for ToUpper 1.67 +static char16_t t2result[T2LEN+1] = { 1.68 + 0x0031 , // 0 1.69 + 0x0019 , // 1 1.70 + 0x0043 , // 2 1.71 + 0x0047 , // 3 1.72 + 0x00C8 , // 4 1.73 + 0x00C9 , // 5 1.74 + 0x0147 , // 6 1.75 + 0x01C4 , // 7 1.76 + 0x01C4 , // 8 1.77 + 0x01C4 , // 9 1.78 + 0x03A0 , // 10 1.79 + 0x0392 , // 11 1.80 + 0x0418 , // 12 1.81 + 0x04A4 , // 13 1.82 + 0x05D0 , // 14 1.83 + 0x0A20 , // 15 1.84 + 0x30B0 , // 16 1.85 + 0x5185 , // 17 1.86 + 0xC021 , // 18 1.87 + 0xFF28 , // 19 1.88 + 0x01C7 , // 20 1.89 + 0x01C7 , // 21 1.90 + 0x01C7 , // 22 1.91 + 0x01CA , // 23 1.92 + 0x01CA , // 24 1.93 + 0x01CA , // 25 1.94 + 0x01F1 , // 26 1.95 + 0x01F1 , // 27 1.96 + 0x01F1 , // 28 1.97 + 0x2C6F , // 29 1.98 + 0x2C6E , // 30 1.99 + 0xA640 , // 31 1.100 + 0x00 1.101 +}; 1.102 +// test data for ToLower 1.103 +static char16_t t3data [T3LEN+1] = { 1.104 + 0x0031 , // 0 1.105 + 0x0019 , // 1 1.106 + 0x0043 , // 2 1.107 + 0x0067 , // 3 1.108 + 0x00C8 , // 4 1.109 + 0x00E9 , // 5 1.110 + 0x0147 , // 6 1.111 + 0x01C4 , // 7 1.112 + 0x01C6 , // 8 1.113 + 0x01C5 , // 9 1.114 + 0x03A0 , // 10 1.115 + 0x0392 , // 11 1.116 + 0x0418 , // 12 1.117 + 0x04A4 , // 13 1.118 + 0x05D0 , // 14 1.119 + 0x0A20 , // 15 1.120 + 0x30B0 , // 16 1.121 + 0x5187 , // 17 1.122 + 0xC023 , // 18 1.123 + 0xFF28 , // 19 1.124 + 0x01C7 , // 20 1.125 + 0x01C8 , // 21 1.126 + 0x01C9 , // 22 1.127 + 0x01CA , // 23 1.128 + 0x01CB , // 24 1.129 + 0x01CC , // 25 1.130 + 0x01F1 , // 26 1.131 + 0x01F2 , // 27 1.132 + 0x01F3 , // 28 1.133 + 0x2C6F , // 29 1.134 + 0x2C6E , // 30 1.135 + 0xA640 , // 31 1.136 + 0x00 1.137 +}; 1.138 +// expected result for ToLower 1.139 +static char16_t t3result[T3LEN+1] = { 1.140 + 0x0031 , // 0 1.141 + 0x0019 , // 1 1.142 + 0x0063 , // 2 1.143 + 0x0067 , // 3 1.144 + 0x00E8 , // 4 1.145 + 0x00E9 , // 5 1.146 + 0x0148 , // 6 1.147 + 0x01C6 , // 7 1.148 + 0x01C6 , // 8 1.149 + 0x01C6 , // 9 1.150 + 0x03C0 , // 10 1.151 + 0x03B2 , // 11 1.152 + 0x0438 , // 12 1.153 + 0x04A5 , // 13 1.154 + 0x05D0 , // 14 1.155 + 0x0A20 , // 15 1.156 + 0x30B0 , // 16 1.157 + 0x5187 , // 17 1.158 + 0xC023 , // 18 1.159 + 0xFF48 , // 19 1.160 + 0x01C9 , // 20 1.161 + 0x01C9 , // 21 1.162 + 0x01C9 , // 22 1.163 + 0x01CC , // 23 1.164 + 0x01CC , // 24 1.165 + 0x01CC , // 25 1.166 + 0x01F3 , // 26 1.167 + 0x01F3 , // 27 1.168 + 0x01F3 , // 28 1.169 + 0x0250 , // 29 1.170 + 0x0271 , // 30 1.171 + 0xA641 , // 31 1.172 + 0x00 1.173 +}; 1.174 +// test data for ToTitle 1.175 +static char16_t t4data [T4LEN+2] = { 1.176 + 0x0031 , // 0 1.177 + 0x0019 , // 1 1.178 + 0x0043 , // 2 1.179 + 0x0067 , // 3 1.180 + 0x00C8 , // 4 1.181 + 0x00E9 , // 5 1.182 + 0x0147 , // 6 1.183 + 0x01C4 , // 7 1.184 + 0x01C6 , // 8 1.185 + 0x01C5 , // 9 1.186 + 0x03C0 , // 10 1.187 + 0x03B2 , // 11 1.188 + 0x0438 , // 12 1.189 + 0x04A5 , // 13 1.190 + 0x05D0 , // 14 1.191 + 0x0A20 , // 15 1.192 + 0x30B0 , // 16 1.193 + 0x5189 , // 17 1.194 + 0xC013 , // 18 1.195 + 0xFF52 , // 19 1.196 + 0x01C7 , // 20 1.197 + 0x01C8 , // 21 1.198 + 0x01C9 , // 22 1.199 + 0x01CA , // 23 1.200 + 0x01CB , // 24 1.201 + 0x01CC , // 25 1.202 + 0x01F1 , // 26 1.203 + 0x01F2 , // 27 1.204 + 0x01F3 , // 28 1.205 + 0x0250 , // 29 1.206 + 0x0271 , // 30 1.207 + 0xA641 , // 31 1.208 + 0x0041 , // Dummy entry to prevent overflow 1.209 + 0x00 1.210 +}; 1.211 +// expected result for ToTitle 1.212 +static char16_t t4result[T4LEN+2] = { 1.213 + 0x0031 , // 0 1.214 + 0x0019 , // 1 1.215 + 0x0043 , // 2 1.216 + 0x0047 , // 3 1.217 + 0x00C8 , // 4 1.218 + 0x00C9 , // 5 1.219 + 0x0147 , // 6 1.220 + 0x01C4 , // 7 1.221 + 0x01C5 , // 8 1.222 + 0x01C5 , // 9 1.223 + 0x03A0 , // 10 1.224 + 0x0392 , // 11 1.225 + 0x0418 , // 12 1.226 + 0x04A4 , // 13 1.227 + 0x05D0 , // 14 1.228 + 0x0A20 , // 15 1.229 + 0x30B0 , // 16 1.230 + 0x5189 , // 17 1.231 + 0xC013 , // 18 1.232 + 0xFF32 , // 19 1.233 + 0x01C7 , // 20 1.234 + 0x01C8 , // 21 1.235 + 0x01C8 , // 22 1.236 + 0x01CA , // 23 1.237 + 0x01CB , // 24 1.238 + 0x01CB , // 25 1.239 + 0x01F1 , // 26 1.240 + 0x01F2 , // 27 1.241 + 0x01F2 , // 28 1.242 + 0x2C6F , // 29 1.243 + 0x2C6E , // 30 1.244 + 0xA640 , // 31 1.245 + 0x0041 , // Dummy entry to prevent overflow 1.246 + 0x00 1.247 +}; 1.248 + 1.249 +static unsigned char t6lhs[] = { 1.250 + 0x31 , // 0 1.251 + 0x19 , // 1 1.252 + 0x43 , // 2 1.253 + 0x67 , // 3 1.254 + 0xC3, 0x88 , // 4 1.255 + 0xC3, 0xA9 , // 5 1.256 + 0xC5, 0x87 , // 6 1.257 + 0xC7, 0x84 , // 7 1.258 + 0xC7, 0x86 , // 8 1.259 + 0xC7, 0x85 , // 9 1.260 + 0xCF, 0x80 , // 10 1.261 + 0xCE, 0xB2 , // 11 1.262 + 0xD0, 0xB8 , // 12 1.263 + 0xD2, 0xA5 , // 13 1.264 + 0xD7, 0x90 , // 14 1.265 + 0xE0, 0xA8, 0xA0 , // 15 1.266 + 0xE3, 0x82, 0xB0 , // 16 1.267 + 0xE5, 0x86, 0x85 , // 17 1.268 + 0xEC, 0x80, 0xA1 , // 18 1.269 + 0xEF, 0xBD, 0x88 , // 19 1.270 + 0xC7, 0x87 , // 20 1.271 + 0xC7, 0x88 , // 21 1.272 + 0xC7, 0x89 , // 22 1.273 + 0xC7, 0x8A , // 23 1.274 + 0xC7, 0x8B , // 24 1.275 + 0xC7, 0x8C , // 25 1.276 + 0xC7, 0xB1 , // 26 1.277 + 0xC7, 0xB2 , // 27 1.278 + 0xC7, 0xB3 , // 28 1.279 + 0xC9, 0x90 , // 29 1.280 + 0xC9, 0xB1 , // 30 1.281 + 0xEA, 0x99, 0x81 , // 31 1.282 + 0x00 1.283 +}; 1.284 + 1.285 +static unsigned char t6rhs[] = { 1.286 + 0x31 , // 0 1.287 + 0x19 , // 1 1.288 + 0x43 , // 2 1.289 + 0x47 , // 3 1.290 + 0xC3, 0x88 , // 4 1.291 + 0xC3, 0x89 , // 5 1.292 + 0xC5, 0x87 , // 6 1.293 + 0xC7, 0x84 , // 7 1.294 + 0xC7, 0x84 , // 8 1.295 + 0xC7, 0x84 , // 9 1.296 + 0xCE, 0xA0 , // 10 1.297 + 0xCE, 0x92 , // 11 1.298 + 0xD0, 0x98 , // 12 1.299 + 0xD2, 0xA4 , // 13 1.300 + 0xD7, 0x90 , // 14 1.301 + 0xE0, 0xA8, 0xA0 , // 15 1.302 + 0xE3, 0x82, 0xB0 , // 16 1.303 + 0xE5, 0x86, 0x85 , // 17 1.304 + 0xEC, 0x80, 0xA1 , // 18 1.305 + 0xEF, 0xBC, 0xA8 , // 19 1.306 + 0xC7, 0x87 , // 20 1.307 + 0xC7, 0x87 , // 21 1.308 + 0xC7, 0x87 , // 22 1.309 + 0xC7, 0x8a , // 23 1.310 + 0xC7, 0x8a , // 24 1.311 + 0xC7, 0x8a , // 25 1.312 + 0xC7, 0xB1 , // 26 1.313 + 0xC7, 0xB1 , // 27 1.314 + 0xC7, 0xB1 , // 28 1.315 + 0xE2, 0xB1, 0xAF , // 29 1.316 + 0xE2, 0xB1, 0xAE , // 30 1.317 + 0xEA, 0x99, 0x80 , // 31 1.318 + 0x00 1.319 +}; 1.320 + 1.321 +static const char *t7lhs = "aBcDeFGHIJKL1!!2!!a!uuuu"; 1.322 +static const char *t7rhs = "AbCdEFghijkL1!!2!!A!UUuU"; 1.323 + 1.324 +static const char *t8lhs = "aazzz"; 1.325 +static const char *t8rhs = "aBa"; 1.326 + 1.327 +static const char *t9lhs = "@a"; 1.328 +static const char *t9rhs = "`a"; 1.329 + 1.330 +bool CharByCharCompareEqual(const char *a, const char *b, 1.331 + uint32_t aLen, uint32_t bLen) 1.332 +{ 1.333 + // Do basically a CaseInsensitiveCompare(), but using 1.334 + // CaseInsensitiveUTF8CharsEqual(). 1.335 + 1.336 + const char *aEnd = a + aLen; 1.337 + const char *bEnd = b + bLen; 1.338 + while (a < aEnd && b < bEnd) { 1.339 + bool err; 1.340 + if (!CaseInsensitiveUTF8CharsEqual(a, b, aEnd, bEnd, &a, &b, &err) || err) 1.341 + return false; 1.342 + } 1.343 + return true; 1.344 +} 1.345 + 1.346 +void TestCaseConversion() 1.347 +{ 1.348 + printf("==========================\n"); 1.349 + printf("Start case conversion test\n"); 1.350 + printf("==========================\n"); 1.351 + 1.352 + int i; 1.353 + char16_t buf[256]; 1.354 + 1.355 + printf("Test 1 - ToUpper(char16_t, char16_t*):\n"); 1.356 + for(i=0;i < T2LEN ; i++) 1.357 + { 1.358 + char16_t ch = ToUpperCase(t2data[i]); 1.359 + if(ch != t2result[i]) 1.360 + printf("\tFailed!! result unexpected %d\n", i); 1.361 + } 1.362 + 1.363 + 1.364 + printf("Test 2 - ToLower(char16_t, char16_t*):\n"); 1.365 + for(i=0;i < T3LEN; i++) 1.366 + { 1.367 + char16_t ch = ToLowerCase(t3data[i]); 1.368 + if(ch != t3result[i]) 1.369 + printf("\tFailed!! result unexpected %d\n", i); 1.370 + } 1.371 + 1.372 + printf("Test 3 - ToTitle(char16_t, char16_t*):\n"); 1.373 + for(i=0;i < T4LEN; i++) 1.374 + { 1.375 + char16_t ch = ToTitleCase(t4data[i]); 1.376 + if(ch != t4result[i]) 1.377 + printf("\tFailed!! result unexpected %d\n", i); 1.378 + } 1.379 + 1.380 + printf("Test 4 - ToUpper(char16_t*, char16_t*, uint32_t):\n"); 1.381 + ToUpperCase(t2data, buf, T2LEN); 1.382 + for(i = 0; i < T2LEN; i++) 1.383 + { 1.384 + if(buf[i] != t2result[i]) 1.385 + { 1.386 + printf("\tFailed!! result unexpected %d\n", i); 1.387 + break; 1.388 + } 1.389 + } 1.390 + 1.391 + printf("Test 5 - ToLower(char16_t*, char16_t*, uint32_t):\n"); 1.392 + ToLowerCase(t3data, buf, T3LEN); 1.393 + for(i = 0; i < T3LEN; i++) 1.394 + { 1.395 + if(buf[i] != t3result[i]) 1.396 + { 1.397 + printf("\tFailed!! result unexpected %d\n", i); 1.398 + break; 1.399 + } 1.400 + } 1.401 + 1.402 + printf("Test 6 - CaseInsensitiveCompare UTF-8 (1):\n"); 1.403 + if (CaseInsensitiveCompare((char*)t6lhs, (char*)t6rhs, sizeof(t6lhs), sizeof(t6rhs))) 1.404 + printf("\tFailed!\n"); 1.405 + if (!CharByCharCompareEqual((char*)t6lhs, (char*)t6rhs, sizeof(t6lhs), sizeof(t6rhs))) 1.406 + printf("\tFailed character-by-character comparison!\n"); 1.407 + 1.408 + printf("Test 7 - CaseInsensitiveCompare UTF-8 (2):\n"); 1.409 + if (CaseInsensitiveCompare(t7lhs, t7rhs, strlen(t7lhs), strlen(t7rhs))) 1.410 + printf("\tFailed!\n"); 1.411 + if (!CharByCharCompareEqual(t7lhs, t7rhs, sizeof(t7lhs), sizeof(t7rhs))) 1.412 + printf("\tFailed character-by-character comparison!\n"); 1.413 + 1.414 + printf("Test 8a - CaseInsensitiveCompare UTF-8 (3):\n"); 1.415 + if (CaseInsensitiveCompare(t8lhs, t8rhs, strlen(t8lhs), strlen(t8rhs)) != -1) 1.416 + printf("\tFailed!\n"); 1.417 + if (CharByCharCompareEqual(t8lhs, t8rhs, strlen(t8lhs), strlen(t8rhs))) 1.418 + printf("\tFailed character-by-character comparison!\n"); 1.419 + 1.420 + printf("Test 8b - CaseInsensitiveCompare UTF-8 (4):\n"); 1.421 + if (CaseInsensitiveCompare(t8rhs, t8lhs, strlen(t8rhs), strlen(t8lhs)) != 1) 1.422 + printf("\tFailed!\n"); 1.423 + 1.424 + // This test may seem a bit strange. But it's actually an easy bug to make 1.425 + // if we tried to be clever and say that two ASCII characters x and y are 1.426 + // case-insensitively equal if (x & ~0x20) == (y & ~0x20). 1.427 + printf("Test 9 - CaseInsensitiveCompare UTF-8 (5):\n"); 1.428 + if (CaseInsensitiveCompare(t9rhs, t9lhs, strlen(t9lhs), strlen(t9rhs)) != 1) 1.429 + printf("\tFailed!\n"); 1.430 + if (CharByCharCompareEqual(t9lhs, t9rhs, strlen(t9lhs), strlen(t9rhs))) 1.431 + printf("\tFailed character-by-character comparison!\n"); 1.432 + 1.433 + printf("===========================\n"); 1.434 + printf("Finish case conversion test\n"); 1.435 + printf("===========================\n"); 1.436 +} 1.437 + 1.438 +static void FuzzOneInvalidCaseConversion() 1.439 +{ 1.440 + uint32_t aLen = rand() % 32; 1.441 + uint32_t bLen = rand() % 32; 1.442 + 1.443 + // We could use a static length-32 buffer for these, but then Valgrind 1.444 + // wouldn't be able to detect errors. 1.445 + unsigned char *aBuf = (unsigned char*)malloc(aLen * sizeof(unsigned char)); 1.446 + unsigned char *bBuf = (unsigned char*)malloc(bLen * sizeof(unsigned char)); 1.447 + 1.448 + for (uint32_t i = 0; i < aLen; i++) { 1.449 + aBuf[i] = rand() & 0xff; 1.450 + } 1.451 + 1.452 + for (uint32_t i = 0; i < bLen; i++) { 1.453 + bBuf[i] = rand() & 0xff; 1.454 + } 1.455 + 1.456 + if (!CaseInsensitiveCompare((char*)aBuf, (char*)bBuf, aLen, bLen)) 1.457 + printf("\tSurprise, two random strings compared insensitively as equal!\n"); 1.458 + if (CharByCharCompareEqual((char*)aBuf, (char*)bBuf, aLen, bLen)) 1.459 + printf("\tSurprise, two random strings compared as exactly equal!\n"); 1.460 + 1.461 + free(aBuf); 1.462 + free(bBuf); 1.463 +} 1.464 + 1.465 +static void FuzzCaseConversion() 1.466 +{ 1.467 + printf("==========================\n"); 1.468 + printf("Start fuzz case conversion\n"); 1.469 + printf("==========================\n"); 1.470 + 1.471 + srand(0); 1.472 + 1.473 + printf("Fuzzing invalid UTF8 data...\n"); 1.474 + for (uint32_t i = 0; i < 100000; i++) { 1.475 + FuzzOneInvalidCaseConversion(); 1.476 + } 1.477 + 1.478 + printf("===========================\n"); 1.479 + printf("Finish fuzz case conversion\n"); 1.480 + printf("===========================\n"); 1.481 +} 1.482 + 1.483 +static void TestEntityConversion(uint32_t version) 1.484 +{ 1.485 + printf("==============================\n"); 1.486 + printf("Start nsIEntityConverter Test \n"); 1.487 + printf("==============================\n"); 1.488 + 1.489 + uint32_t i; 1.490 + nsString inString; 1.491 + char16_t uChar; 1.492 + nsresult res; 1.493 + 1.494 + 1.495 + inString.Assign(NS_ConvertASCIItoUTF16("\xA0\xA1\xA2\xA3")); 1.496 + uChar = (char16_t) 8364; //euro 1.497 + inString.Append(&uChar, 1); 1.498 + uChar = (char16_t) 9830; // 1.499 + inString.Append(&uChar, 1); 1.500 + 1.501 + nsCOMPtr <nsIEntityConverter> entityConv = do_CreateInstance(kEntityConverterCID, &res);; 1.502 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n"); return;} 1.503 + 1.504 + const char16_t *data; 1.505 + uint32_t length = NS_StringGetData(inString, &data); 1.506 + 1.507 + // convert char by char 1.508 + for (i = 0; i < length; i++) { 1.509 + char *entity = nullptr; 1.510 + res = entityConv->ConvertToEntity(data[i], version, &entity); 1.511 + if (NS_SUCCEEDED(res) && entity) { 1.512 + printf("%c %s\n", data[i], entity); 1.513 + nsMemory::Free(entity); 1.514 + } 1.515 + } 1.516 + 1.517 + // convert at once as a string 1.518 + char16_t *entities; 1.519 + res = entityConv->ConvertToEntities(inString.get(), version, &entities); 1.520 + if (NS_SUCCEEDED(res) && entities) { 1.521 + for (char16_t *centity = entities; *centity; ++centity) { 1.522 + printf("%c", (char) *centity); 1.523 + if (';' == (char) *centity) 1.524 + printf("\n"); 1.525 + } 1.526 + nsMemory::Free(entities); 1.527 + } 1.528 + 1.529 + printf("==============================\n"); 1.530 + printf("Finish nsIEntityConverter Test \n"); 1.531 + printf("==============================\n\n"); 1.532 +} 1.533 + 1.534 +static void TestSaveAsCharset() 1.535 +{ 1.536 + printf("==============================\n"); 1.537 + printf("Start nsISaveAsCharset Test \n"); 1.538 + printf("==============================\n"); 1.539 + 1.540 + nsresult res; 1.541 + 1.542 + nsString inString; 1.543 + inString.Assign(NS_ConvertASCIItoUTF16("\x61\x62\x80\xA0\x63")); 1.544 + char *outString; 1.545 + 1.546 + const char16_t *data; 1.547 + uint32_t length = NS_StringGetData(inString, &data); 1.548 + 1.549 + // first, dump input string 1.550 + for (uint32_t i = 0; i < length; i++) { 1.551 + printf("%c ", data[i]); 1.552 + } 1.553 + printf("\n"); 1.554 + 1.555 + nsCOMPtr <nsISaveAsCharset> saveAsCharset = do_CreateInstance(kSaveAsCharsetCID, &res); 1.556 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.557 + 1.558 + printf("ISO-8859-1 attr_plainTextDefault entityNone\n"); 1.559 + res = saveAsCharset->Init("ISO-8859-1", 1.560 + nsISaveAsCharset::attr_plainTextDefault, 1.561 + nsIEntityConverter::entityNone); 1.562 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.563 + res = saveAsCharset->Convert(inString.get(), &outString); 1.564 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.565 + if (!outString) {printf("\tFailed!! output null\n");} 1.566 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.567 + 1.568 + printf("ISO-2022-JP attr_plainTextDefault entityNone\n"); 1.569 + res = saveAsCharset->Init("ISO-2022-JP", 1.570 + nsISaveAsCharset::attr_plainTextDefault, 1.571 + nsIEntityConverter::entityNone); 1.572 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.573 + res = saveAsCharset->Convert(inString.get(), &outString); 1.574 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.575 + if (!outString) {printf("\tFailed!! output null\n");} 1.576 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.577 + if (NS_ERROR_UENC_NOMAPPING == res) { 1.578 + outString = ToNewUTF8String(inString); 1.579 + if (!outString) {printf("\tFailed!! output null\n");} 1.580 + else {printf("Fall back to UTF-8: %s\n", outString); nsMemory::Free(outString);} 1.581 + } 1.582 + 1.583 + printf("ISO-2022-JP attr_FallbackQuestionMark entityNone\n"); 1.584 + res = saveAsCharset->Init("ISO-2022-JP", 1.585 + nsISaveAsCharset::attr_FallbackQuestionMark, 1.586 + nsIEntityConverter::entityNone); 1.587 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.588 + res = saveAsCharset->Convert(inString.get(), &outString); 1.589 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.590 + if (!outString) {printf("\tFailed!! output null\n");} 1.591 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.592 + 1.593 + printf("ISO-2022-JP attr_FallbackEscapeU entityNone\n"); 1.594 + res = saveAsCharset->Init("ISO-2022-JP", 1.595 + nsISaveAsCharset::attr_FallbackEscapeU, 1.596 + nsIEntityConverter::entityNone); 1.597 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.598 + res = saveAsCharset->Convert(inString.get(), &outString); 1.599 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.600 + if (!outString) {printf("\tFailed!! output null\n");} 1.601 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.602 + 1.603 + printf("ISO-8859-1 attr_htmlTextDefault html40Latin1\n"); 1.604 + res = saveAsCharset->Init("ISO-8859-1", 1.605 + nsISaveAsCharset::attr_htmlTextDefault, 1.606 + nsIEntityConverter::html40Latin1); 1.607 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.608 + res = saveAsCharset->Convert(inString.get(), &outString); 1.609 + if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.610 + if (!outString) {printf("\tFailed!! output null\n");} 1.611 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.612 + 1.613 + printf("ISO-8859-1 attr_FallbackHexNCR+attr_EntityAfterCharsetConv html40Latin1 \n"); 1.614 + res = saveAsCharset->Init("ISO-8859-1", 1.615 + nsISaveAsCharset::attr_FallbackHexNCR + 1.616 + nsISaveAsCharset::attr_EntityAfterCharsetConv, 1.617 + nsIEntityConverter::html40Latin1); 1.618 + if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.619 + res = saveAsCharset->Convert(inString.get(), &outString); 1.620 + if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");} 1.621 + if (!outString) {printf("\tFailed!! output null\n");} 1.622 + else {printf("%s\n", outString); nsMemory::Free(outString);} 1.623 + 1.624 + 1.625 + printf("==============================\n"); 1.626 + printf("Finish nsISaveAsCharset Test \n"); 1.627 + printf("==============================\n\n"); 1.628 +} 1.629 + 1.630 +static char16_t normStr[] = 1.631 +{ 1.632 + 0x00E1, 1.633 + 0x0061, 1.634 + 0x0301, 1.635 + 0x0107, 1.636 + 0x0063, 1.637 + 0x0301, 1.638 + 0x0000 1.639 +}; 1.640 + 1.641 +static char16_t nfdForm[] = 1.642 +{ 1.643 + 0x0061, 1.644 + 0x0301, 1.645 + 0x0061, 1.646 + 0x0301, 1.647 + 0x0063, 1.648 + 0x0301, 1.649 + 0x0063, 1.650 + 0x0301, 1.651 + 0x0000 1.652 +}; 1.653 + 1.654 +void TestNormalization() 1.655 +{ 1.656 + printf("==============================\n"); 1.657 + printf("Start nsIUnicodeNormalizer Test \n"); 1.658 + printf("==============================\n"); 1.659 + nsIUnicodeNormalizer *t = nullptr; 1.660 + nsresult res; 1.661 + res = CallGetService(kUnicodeNormalizerCID, &t); 1.662 + 1.663 + printf("Test 1 - GetService():\n"); 1.664 + if(NS_FAILED(res) || !t) { 1.665 + printf("\t1st Norm GetService failed\n"); 1.666 + } else { 1.667 + NS_RELEASE(t); 1.668 + } 1.669 + 1.670 + res = CallGetService(kUnicodeNormalizerCID, &t); 1.671 + 1.672 + if(NS_FAILED(res) || !t) { 1.673 + printf("\t2nd GetService failed\n"); 1.674 + } else { 1.675 + printf("Test 2 - NormalizeUnicode(uint32_t, const nsAString&, nsAString&):\n"); 1.676 + nsAutoString resultStr; 1.677 + res = t->NormalizeUnicodeNFD(nsDependentString(normStr), resultStr); 1.678 + if (resultStr.Equals(nsDependentString(nfdForm))) { 1.679 + printf(" Succeeded in NFD UnicodeNormalizer test. \n"); 1.680 + } else { 1.681 + printf(" Failed in NFD UnicodeNormalizer test. \n"); 1.682 + } 1.683 + 1.684 + NS_RELEASE(t); 1.685 + } 1.686 + printf("==============================\n"); 1.687 + printf("Finish nsIUnicodeNormalizer Test \n"); 1.688 + printf("==============================\n"); 1.689 + 1.690 +} 1.691 + 1.692 + 1.693 +int main(int argc, char** argv) { 1.694 + 1.695 + nsresult rv = NS_InitXPCOM2(nullptr, nullptr, nullptr); 1.696 + if (NS_FAILED(rv)) { 1.697 + printf("NS_InitXPCOM2 failed\n"); 1.698 + return 1; 1.699 + } 1.700 + 1.701 + // -------------------------------------------- 1.702 + 1.703 + TestCaseConversion(); 1.704 + 1.705 + // -------------------------------------------- 1.706 + 1.707 + FuzzCaseConversion(); 1.708 + 1.709 + // -------------------------------------------- 1.710 + 1.711 + TestEntityConversion(nsIEntityConverter::html40); 1.712 + 1.713 + // -------------------------------------------- 1.714 + 1.715 + TestSaveAsCharset(); 1.716 + 1.717 + // -------------------------------------------- 1.718 + 1.719 + TestNormalization(); 1.720 + 1.721 + // -------------------------------------------- 1.722 + printf("Finish All The Test Cases\n"); 1.723 + 1.724 + return 0; 1.725 +}