~ubuntu-branches/ubuntu/precise/kompozer/precise

« back to all changes in this revision

Viewing changes to mozilla/intl/unicharutil/tests/UnicharSelfTest.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Anthony Yarusso
  • Date: 2007-08-27 01:11:03 UTC
  • Revision ID: james.westby@ubuntu.com-20070827011103-2jgf4s6532gqu2ka
Tags: upstream-0.7.10
ImportĀ upstreamĀ versionĀ 0.7.10

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
 
2
/* ***** BEGIN LICENSE BLOCK *****
 
3
 * Version: NPL 1.1/GPL 2.0/LGPL 2.1
 
4
 *
 
5
 * The contents of this file are subject to the Netscape Public License
 
6
 * Version 1.1 (the "License"); you may not use this file except in
 
7
 * compliance with the License. You may obtain a copy of the License at
 
8
 * http://www.mozilla.org/NPL/
 
9
 *
 
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.
 
14
 *
 
15
 * The Original Code is mozilla.org code.
 
16
 *
 
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.
 
21
 *
 
22
 * Contributor(s):
 
23
 *   Pierre Phaneuf <pp@ludusdesign.com>
 
24
 *
 
25
 *
 
26
 * Alternatively, the contents of this file may be used under the terms of
 
27
 * either the GNU General Public License Version 2 or later (the "GPL"), or
 
28
 * the GNU Lesser General Public License Version 2.1 or later (the "LGPL"),
 
29
 * in which case the provisions of the GPL or the LGPL are applicable instead
 
30
 * of those above. If you wish to allow use of your version of this file only
 
31
 * under the terms of either the GPL or the LGPL, and not to allow others to
 
32
 * use your version of this file under the terms of the NPL, indicate your
 
33
 * decision by deleting the provisions above and replace them with the notice
 
34
 * and other provisions required by the GPL or the LGPL. If you do not delete
 
35
 * the provisions above, a recipient may use your version of this file under
 
36
 * the terms of any one of the NPL, the GPL or the LGPL.
 
37
 *
 
38
 * ***** END LICENSE BLOCK ***** */
 
39
#include <stdio.h>
 
40
#include "nsISupports.h"
 
41
#include "nsXPCOM.h"
 
42
#include "nsIServiceManager.h"
 
43
#include "nsIComponentManager.h"
 
44
#include "nsICaseConversion.h"
 
45
#include "nsIEntityConverter.h"
 
46
#include "nsISaveAsCharset.h"
 
47
#include "nsIUnicodeEncoder.h"
 
48
#include "nsUnicharUtilCIID.h"
 
49
#include "nsIPersistentProperties2.h"
 
50
#include "nsIURL.h"
 
51
#include "nsNetUtil.h"
 
52
#include "nsCOMPtr.h"
 
53
#include "nsReadableUtils.h"
 
54
#include "nsCRT.h"
 
55
#include "nsIUnicodeNormalizer.h"
 
56
#include "nsString.h"
 
57
 
 
58
NS_DEFINE_CID(kUnicharUtilCID, NS_UNICHARUTIL_CID);
 
59
NS_DEFINE_IID(kCaseConversionIID, NS_ICASECONVERSION_IID);
 
60
NS_DEFINE_CID(kEntityConverterCID, NS_ENTITYCONVERTER_CID);
 
61
NS_DEFINE_CID(kSaveAsCharsetCID, NS_SAVEASCHARSET_CID);
 
62
NS_DEFINE_CID(kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID);
 
63
 
 
64
#define TESTLEN 29
 
65
#define T2LEN TESTLEN
 
66
#define T3LEN TESTLEN
 
67
#define T4LEN TESTLEN
 
68
 
 
69
// test data for ToUpper 
 
70
static PRUnichar t2data  [T2LEN+1] = {
 
71
  0x0031 ,  //  0
 
72
  0x0019 ,  //  1
 
73
  0x0043 ,  //  2
 
74
  0x0067 ,  //  3
 
75
  0x00C8 ,  //  4
 
76
  0x00E9 ,  //  5
 
77
  0x0147 ,  //  6
 
78
  0x01C4 ,  //  7
 
79
  0x01C6 ,  //  8
 
80
  0x01C5 ,  //  9
 
81
  0x03C0 ,  // 10
 
82
  0x03B2 ,  // 11
 
83
  0x0438 ,  // 12
 
84
  0x04A5 ,  // 13
 
85
  0x05D0 ,  // 14
 
86
  0x0A20 ,  // 15
 
87
  0x30B0 ,  // 16
 
88
  0x5185 ,  // 17
 
89
  0xC021 ,  // 18
 
90
  0xFF48 ,  // 19
 
91
  0x01C7 ,  // 20
 
92
  0x01C8 ,  // 21
 
93
  0x01C9 ,  // 22
 
94
  0x01CA ,  // 23
 
95
  0x01CB ,  // 24
 
96
  0x01CC ,  // 25
 
97
  0x01F1 ,  // 26
 
98
  0x01F2 ,  // 27
 
99
  0x01F3 ,  // 28
 
100
  0x00  
 
101
};
 
102
// expected result for ToUpper 
 
103
static PRUnichar t2result[T2LEN+1] =  {
 
104
  0x0031 ,  //  0
 
105
  0x0019 ,  //  1
 
106
  0x0043 ,  //  2
 
107
  0x0047 ,  //  3
 
108
  0x00C8 ,  //  4
 
109
  0x00C9 ,  //  5
 
110
  0x0147 ,  //  6
 
111
  0x01C4 ,  //  7
 
112
  0x01C4 ,  //  8
 
113
  0x01C4 ,  //  9
 
114
  0x03A0 ,  // 10
 
115
  0x0392 ,  // 11
 
116
  0x0418 ,  // 12
 
117
  0x04A4 ,  // 13
 
118
  0x05D0 ,  // 14
 
119
  0x0A20 ,  // 15
 
120
  0x30B0 ,  // 16
 
121
  0x5185 ,  // 17
 
122
  0xC021 ,  // 18
 
123
  0xFF28 ,  // 19
 
124
  0x01C7 ,  // 20
 
125
  0x01C7 ,  // 21
 
126
  0x01C7 ,  // 22
 
127
  0x01CA ,  // 23
 
128
  0x01CA ,  // 24
 
129
  0x01CA ,  // 25
 
130
  0x01F1 ,  // 26
 
131
  0x01F1 ,  // 27
 
132
  0x01F1 ,  // 28
 
133
  0x00  
 
134
};
 
135
// test data for ToLower 
 
136
static PRUnichar t3data  [T3LEN+1] =  {
 
137
  0x0031 ,  //  0
 
138
  0x0019 ,  //  1
 
139
  0x0043 ,  //  2
 
140
  0x0067 ,  //  3
 
141
  0x00C8 ,  //  4
 
142
  0x00E9 ,  //  5
 
143
  0x0147 ,  //  6
 
144
  0x01C4 ,  //  7
 
145
  0x01C6 ,  //  8
 
146
  0x01C5 ,  //  9
 
147
  0x03A0 ,  // 10
 
148
  0x0392 ,  // 11
 
149
  0x0418 ,  // 12
 
150
  0x04A4 ,  // 13
 
151
  0x05D0 ,  // 14
 
152
  0x0A20 ,  // 15
 
153
  0x30B0 ,  // 16
 
154
  0x5187 ,  // 17
 
155
  0xC023 ,  // 18
 
156
  0xFF28 ,  // 19
 
157
  0x01C7 ,  // 20
 
158
  0x01C8 ,  // 21
 
159
  0x01C9 ,  // 22
 
160
  0x01CA ,  // 23
 
161
  0x01CB ,  // 24
 
162
  0x01CC ,  // 25
 
163
  0x01F1 ,  // 26
 
164
  0x01F2 ,  // 27
 
165
  0x01F3 ,  // 28
 
166
  0x00  
 
167
};
 
168
// expected result for ToLower 
 
169
static PRUnichar t3result[T3LEN+1] =  {
 
170
  0x0031 ,  //  0
 
171
  0x0019 ,  //  1
 
172
  0x0063 ,  //  2
 
173
  0x0067 ,  //  3
 
174
  0x00E8 ,  //  4
 
175
  0x00E9 ,  //  5
 
176
  0x0148 ,  //  6
 
177
  0x01C6 ,  //  7
 
178
  0x01C6 ,  //  8
 
179
  0x01C6 ,  //  9
 
180
  0x03C0 ,  // 10
 
181
  0x03B2 ,  // 11
 
182
  0x0438 ,  // 12
 
183
  0x04A5 ,  // 13
 
184
  0x05D0 ,  // 14
 
185
  0x0A20 ,  // 15
 
186
  0x30B0 ,  // 16
 
187
  0x5187 ,  // 17
 
188
  0xC023 ,  // 18
 
189
  0xFF48 ,  // 19
 
190
  0x01C9 ,  // 20
 
191
  0x01C9 ,  // 21
 
192
  0x01C9 ,  // 22
 
193
  0x01CC ,  // 23
 
194
  0x01CC ,  // 24
 
195
  0x01CC ,  // 25
 
196
  0x01F3 ,  // 26
 
197
  0x01F3 ,  // 27
 
198
  0x01F3 ,  // 28
 
199
  0x00  
 
200
};
 
201
// test data for ToTitle 
 
202
static PRUnichar t4data  [T4LEN+1] =  {
 
203
  0x0031 ,  //  0
 
204
  0x0019 ,  //  1
 
205
  0x0043 ,  //  2
 
206
  0x0067 ,  //  3
 
207
  0x00C8 ,  //  4
 
208
  0x00E9 ,  //  5
 
209
  0x0147 ,  //  6
 
210
  0x01C4 ,  //  7
 
211
  0x01C6 ,  //  8
 
212
  0x01C5 ,  //  9
 
213
  0x03C0 ,  // 10
 
214
  0x03B2 ,  // 11
 
215
  0x0438 ,  // 12
 
216
  0x04A5 ,  // 13
 
217
  0x05D0 ,  // 14
 
218
  0x0A20 ,  // 15
 
219
  0x30B0 ,  // 16
 
220
  0x5189 ,  // 17
 
221
  0xC013 ,  // 18
 
222
  0xFF52 ,  // 19
 
223
  0x01C7 ,  // 20
 
224
  0x01C8 ,  // 21
 
225
  0x01C9 ,  // 22
 
226
  0x01CA ,  // 23
 
227
  0x01CB ,  // 24
 
228
  0x01CC ,  // 25
 
229
  0x01F1 ,  // 26
 
230
  0x01F2 ,  // 27
 
231
  0x01F3 ,  // 28
 
232
  0x00  
 
233
};
 
234
// expected result for ToTitle 
 
235
static PRUnichar t4result[T4LEN+1] =  {
 
236
  0x0031 ,  //  0
 
237
  0x0019 ,  //  1
 
238
  0x0043 ,  //  2
 
239
  0x0047 ,  //  3
 
240
  0x00C8 ,  //  4
 
241
  0x00C9 ,  //  5
 
242
  0x0147 ,  //  6
 
243
  0x01C5 ,  //  7
 
244
  0x01C5 ,  //  8
 
245
  0x01C5 ,  //  9
 
246
  0x03A0 ,  // 10
 
247
  0x0392 ,  // 11
 
248
  0x0418 ,  // 12
 
249
  0x04A4 ,  // 13
 
250
  0x05D0 ,  // 14
 
251
  0x0A20 ,  // 15
 
252
  0x30B0 ,  // 16
 
253
  0x5189 ,  // 17
 
254
  0xC013 ,  // 18
 
255
  0xFF32 ,  // 19
 
256
  0x01C8 ,  // 20
 
257
  0x01C8 ,  // 21
 
258
  0x01C8 ,  // 22
 
259
  0x01CB ,  // 23
 
260
  0x01CB ,  // 24
 
261
  0x01CB ,  // 25
 
262
  0x01F2 ,  // 26
 
263
  0x01F2 ,  // 27
 
264
  0x01F2 ,  // 28
 
265
  0x00  
 
266
};
 
267
 
 
268
void TestCaseConversion()
 
269
{
 
270
   printf("==============================\n");
 
271
   printf("Start nsICaseConversion Test \n");
 
272
   printf("==============================\n");
 
273
   nsICaseConversion *t = NULL;
 
274
   nsresult res;
 
275
   res = nsServiceManager::GetService(kUnicharUtilCID,
 
276
                                kCaseConversionIID,
 
277
                                (nsISupports**) &t);
 
278
           
 
279
   printf("Test 1 - GetService():\n");
 
280
   if(NS_FAILED(res) || ( t == NULL ) ) {
 
281
     printf("\t1st GetService failed\n");
 
282
   } else {
 
283
     res = nsServiceManager::ReleaseService(kUnicharUtilCID, t);
 
284
   }
 
285
 
 
286
   res = nsServiceManager::GetService(kUnicharUtilCID,
 
287
                                kCaseConversionIID,
 
288
                                (nsISupports**) &t);
 
289
           
 
290
   if(NS_FAILED(res) || ( t == NULL ) ) {
 
291
     printf("\t2nd GetService failed\n");
 
292
   } else {
 
293
     int i;
 
294
     PRUnichar ch;
 
295
     PRUnichar buf[256];
 
296
 
 
297
    printf("Test 2 - ToUpper(PRUnichar, PRUnichar*):\n");
 
298
    for(i=0;i < T2LEN ; i++)
 
299
    {
 
300
         res = t->ToUpper(t2data[i], &ch);
 
301
         if(NS_FAILED(res)) {
 
302
            printf("\tFailed!! return value != NS_OK\n");
 
303
            break;
 
304
         }
 
305
         if(ch != t2result[i]) 
 
306
            printf("\tFailed!! result unexpected %d\n", i);
 
307
     }
 
308
 
 
309
 
 
310
    printf("Test 3 - ToLower(PRUnichar, PRUnichar*):\n");
 
311
    for(i=0;i < T3LEN; i++)
 
312
    {
 
313
         res = t->ToLower(t3data[i], &ch);
 
314
         if(NS_FAILED(res)) {
 
315
            printf("\tFailed!! return value != NS_OK\n");
 
316
            break;
 
317
         }
 
318
         if(ch != t3result[i]) 
 
319
            printf("\tFailed!! result unexpected %d\n", i);
 
320
     }
 
321
 
 
322
 
 
323
    printf("Test 4 - ToTitle(PRUnichar, PRUnichar*):\n");
 
324
    for(i=0;i < T4LEN; i++)
 
325
    {
 
326
         res = t->ToTitle(t4data[i], &ch);
 
327
         if(NS_FAILED(res)) {
 
328
            printf("\tFailed!! return value != NS_OK\n");
 
329
            break;
 
330
         }
 
331
         if(ch != t4result[i]) 
 
332
            printf("\tFailed!! result unexpected %d\n", i);
 
333
     }
 
334
 
 
335
 
 
336
    printf("Test 5 - ToUpper(PRUnichar*, PRUnichar*, PRUint32):\n");
 
337
    res = t->ToUpper(t2data, buf, T2LEN);
 
338
    if(NS_FAILED(res)) {
 
339
       printf("\tFailed!! return value != NS_OK\n");
 
340
    } else {
 
341
       for(i = 0; i < T2LEN; i++)
 
342
       {
 
343
          if(buf[i] != t2result[i])
 
344
          {
 
345
            printf("\tFailed!! result unexpected %d\n", i);
 
346
            break;
 
347
          }
 
348
       }
 
349
    }
 
350
 
 
351
    printf("Test 6 - ToLower(PRUnichar*, PRUnichar*, PRUint32):\n");
 
352
    res = t->ToLower(t3data, buf, T3LEN);
 
353
    if(NS_FAILED(res)) {
 
354
       printf("\tFailed!! return value != NS_OK\n");
 
355
    } else {
 
356
       for(i = 0; i < T3LEN; i++)
 
357
       {
 
358
          if(buf[i] != t3result[i])
 
359
          {
 
360
            printf("\tFailed!! result unexpected %d\n", i);
 
361
            break;
 
362
          }
 
363
       }
 
364
    }
 
365
 
 
366
     printf("Test 7 - ToTitle(PRUnichar*, PRUnichar*, PRUint32):\n");
 
367
     printf("!!! To Be Implemented !!!\n");
 
368
 
 
369
   res = nsServiceManager::ReleaseService(kUnicharUtilCID, t);
 
370
   }
 
371
   printf("==============================\n");
 
372
   printf("Finish nsICaseConversion Test \n");
 
373
   printf("==============================\n");
 
374
 
 
375
}
 
376
 
 
377
static void TestEntityConversion(PRUint32 version)
 
378
{
 
379
  printf("==============================\n");
 
380
  printf("Start nsIEntityConverter Test \n");
 
381
  printf("==============================\n");
 
382
 
 
383
  PRUint32 i;
 
384
  nsString inString;
 
385
  PRUnichar uChar;
 
386
  nsresult res;
 
387
 
 
388
 
 
389
  inString.Assign(NS_ConvertASCIItoUCS2("\xA0\xA1\xA2\xA3"));
 
390
  uChar = (PRUnichar) 8364; //euro
 
391
  inString.Append(&uChar, 1);
 
392
  uChar = (PRUnichar) 9830; //
 
393
  inString.Append(&uChar, 1);
 
394
 
 
395
  nsCOMPtr <nsIEntityConverter> entityConv;
 
396
  res = nsComponentManager::CreateInstance(kEntityConverterCID, NULL, NS_GET_IID(nsIEntityConverter), getter_AddRefs(entityConv));
 
397
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n"); return;}
 
398
 
 
399
 
 
400
  // convert char by char
 
401
  for (i = 0; i < inString.Length(); i++) {
 
402
    char *entity = NULL;
 
403
    res = entityConv->ConvertToEntity(inString[i], version, &entity);
 
404
    if (NS_SUCCEEDED(res) && NULL != entity) {
 
405
      printf("%c %s\n", inString[i], entity);
 
406
      nsMemory::Free(entity);
 
407
    }
 
408
  }
 
409
 
 
410
  // convert at once as a string
 
411
  PRUnichar *entities;
 
412
  res = entityConv->ConvertToEntities(inString.get(), version, &entities);
 
413
  if (NS_SUCCEEDED(res) && NULL != entities) {
 
414
    for (i = 0; i < nsCRT::strlen(entities); i++) {
 
415
      printf("%c", (char) entities[i]);
 
416
      if (';' == (char) entities[i])
 
417
        printf("\n");
 
418
    }
 
419
    nsMemory::Free(entities);
 
420
  }
 
421
 
 
422
  printf("==============================\n");
 
423
  printf("Finish nsIEntityConverter Test \n");
 
424
  printf("==============================\n\n");
 
425
}
 
426
 
 
427
static void TestSaveAsCharset()
 
428
{
 
429
  printf("==============================\n");
 
430
  printf("Start nsISaveAsCharset Test \n");
 
431
  printf("==============================\n");
 
432
 
 
433
  nsresult res;
 
434
 
 
435
  nsString inString;
 
436
  inString.Assign(NS_ConvertASCIItoUCS2("\x61\x62\x80\xA0\x63"));
 
437
  char *outString;
 
438
  
 
439
  // first, dump input string
 
440
  for (PRUint32 i = 0; i < inString.Length(); i++) {
 
441
    printf("%c ", inString[i]);
 
442
  }
 
443
  printf("\n");
 
444
 
 
445
  nsCOMPtr <nsISaveAsCharset> saveAsCharset;
 
446
  res = nsComponentManager::CreateInstance(kSaveAsCharsetCID, NULL, NS_GET_IID(nsISaveAsCharset), getter_AddRefs(saveAsCharset));
 
447
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
448
  
 
449
  printf("ISO-8859-1 attr_plainTextDefault entityNone\n");
 
450
  res = saveAsCharset->Init("ISO-8859-1", 
 
451
                                 nsISaveAsCharset::attr_plainTextDefault, 
 
452
                                 nsIEntityConverter::entityNone);
 
453
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
454
  res = saveAsCharset->Convert(inString.get(), &outString);
 
455
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
456
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
457
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
458
 
 
459
  printf("ISO-2022-JP attr_plainTextDefault entityNone\n");
 
460
  res = saveAsCharset->Init("ISO-2022-JP", 
 
461
                                 nsISaveAsCharset::attr_plainTextDefault,
 
462
                                 nsIEntityConverter::entityNone);
 
463
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
464
  res = saveAsCharset->Convert(inString.get(), &outString);
 
465
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
466
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
467
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
468
  if (NS_ERROR_UENC_NOMAPPING == res) {
 
469
    outString = ToNewUTF8String(inString);
 
470
    if (NULL == outString) {printf("\tFailed!! output null\n");}
 
471
    else {printf("Fall back to UTF-8: %s\n", outString); nsMemory::Free(outString);}
 
472
  }
 
473
 
 
474
  printf("ISO-2022-JP attr_FallbackQuestionMark entityNone\n");
 
475
  res = saveAsCharset->Init("ISO-2022-JP", 
 
476
                                 nsISaveAsCharset::attr_FallbackQuestionMark,
 
477
                                 nsIEntityConverter::entityNone);
 
478
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
479
  res = saveAsCharset->Convert(inString.get(), &outString);
 
480
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
481
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
482
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
483
 
 
484
  printf("ISO-2022-JP attr_FallbackEscapeU entityNone\n");
 
485
  res = saveAsCharset->Init("ISO-2022-JP", 
 
486
                                 nsISaveAsCharset::attr_FallbackEscapeU,
 
487
                                 nsIEntityConverter::entityNone);
 
488
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
489
  res = saveAsCharset->Convert(inString.get(), &outString);
 
490
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
491
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
492
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
493
 
 
494
  printf("ISO-8859-1 attr_htmlTextDefault html40Latin1\n");
 
495
  res = saveAsCharset->Init("ISO-8859-1", 
 
496
                                 nsISaveAsCharset::attr_htmlTextDefault, 
 
497
                                 nsIEntityConverter::html40Latin1);
 
498
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
499
  res = saveAsCharset->Convert(inString.get(), &outString);
 
500
  if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
501
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
502
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
503
 
 
504
  printf("ISO-8859-1 attr_FallbackHexNCR+attr_EntityAfterCharsetConv html40Latin1 \n");
 
505
  res = saveAsCharset->Init("ISO-8859-1", 
 
506
                                 nsISaveAsCharset::attr_FallbackHexNCR + 
 
507
                                 nsISaveAsCharset::attr_EntityAfterCharsetConv, 
 
508
                                 nsIEntityConverter::html40Latin1);
 
509
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
510
  res = saveAsCharset->Convert(inString.get(), &outString);
 
511
  if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
 
512
  if (NULL == outString) {printf("\tFailed!! output null\n");}
 
513
  else {printf("%s\n", outString); nsMemory::Free(outString);}
 
514
 
 
515
 
 
516
  printf("==============================\n");
 
517
  printf("Finish nsISaveAsCharset Test \n");
 
518
  printf("==============================\n\n");
 
519
}
 
520
 
 
521
static PRUnichar normStr[] = 
 
522
{
 
523
  0x00E1,   
 
524
  0x0061,
 
525
  0x0301,
 
526
  0x0107,
 
527
  0x0063,
 
528
  0x0301,
 
529
  0x0000
 
530
};
 
531
 
 
532
static PRUnichar nfdForm[] = 
 
533
{
 
534
  0x0061,
 
535
  0x0301,
 
536
  0x0061,
 
537
  0x0301,
 
538
  0x0063,
 
539
  0x0301,
 
540
  0x0063,
 
541
  0x0301,
 
542
  0x0000
 
543
};
 
544
 
 
545
void TestNormalization()
 
546
{
 
547
   printf("==============================\n");
 
548
   printf("Start nsIUnicodeNormalizer Test \n");
 
549
   printf("==============================\n");
 
550
   nsIUnicodeNormalizer *t = NULL;
 
551
   nsresult res;
 
552
   res = nsServiceManager::GetService(kUnicodeNormalizerCID,
 
553
                                      NS_GET_IID(nsIUnicodeNormalizer),
 
554
                                      (nsISupports**) &t);
 
555
           
 
556
   printf("Test 1 - GetService():\n");
 
557
   if(NS_FAILED(res) || ( t == NULL ) ) {
 
558
     printf("\t1st Norm GetService failed\n");
 
559
   } else {
 
560
     res = nsServiceManager::ReleaseService(kUnicodeNormalizerCID, t);
 
561
   }
 
562
 
 
563
   res = nsServiceManager::GetService(kUnicodeNormalizerCID,
 
564
                                NS_GET_IID(nsIUnicodeNormalizer),
 
565
                                (nsISupports**) &t);
 
566
           
 
567
   if(NS_FAILED(res) || ( t == NULL ) ) {
 
568
     printf("\t2nd GetService failed\n");
 
569
   } else {
 
570
    printf("Test 2 - NormalizeUnicode(PRUint32, const nsAString&, nsAString&):\n");
 
571
    nsAutoString resultStr;
 
572
    res =  t->NormalizeUnicodeNFD(nsDependentString(normStr), resultStr);
 
573
    if (resultStr.Equals(nfdForm)) {
 
574
      printf(" Succeeded in NFD UnicodeNormalizer test. \n");
 
575
    } else {
 
576
      printf(" Failed in NFD UnicodeNormalizer test. \n");
 
577
    }
 
578
 
 
579
 
 
580
    res = nsServiceManager::ReleaseService(kUnicodeNormalizerCID, t);
 
581
   }
 
582
   printf("==============================\n");
 
583
   printf("Finish nsIUnicodeNormalizer Test \n");
 
584
   printf("==============================\n");
 
585
 
 
586
}
 
587
 
 
588
 
 
589
int main(int argc, char** argv) {
 
590
   
 
591
   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
 
592
   if (NS_FAILED(rv)) {
 
593
      printf("NS_InitXPCOM2 failed\n");
 
594
      return 1;
 
595
   }
 
596
 
 
597
   // --------------------------------------------
 
598
 
 
599
   TestCaseConversion();
 
600
 
 
601
   // --------------------------------------------
 
602
 
 
603
   TestEntityConversion(nsIEntityConverter::html40);
 
604
 
 
605
   // --------------------------------------------
 
606
 
 
607
   TestSaveAsCharset();
 
608
 
 
609
   // --------------------------------------------
 
610
 
 
611
   TestNormalization();
 
612
 
 
613
   // --------------------------------------------
 
614
   printf("Finish All The Test Cases\n");
 
615
   nsresult res = NS_OK;
 
616
   res = nsComponentManager::FreeLibraries();
 
617
 
 
618
   if(NS_FAILED(res))
 
619
      printf("nsComponentManager failed\n");
 
620
   else
 
621
      printf("nsComponentManager FreeLibraries Done\n");
 
622
   return 0;
 
623
}
 
624