Back to index

lightning-sunbird  0.9+nobinonly
Defines | Functions | Variables
UnicharSelfTest.cpp File Reference
#include <stdio.h>
#include "nsISupports.h"
#include "nsXPCOM.h"
#include "nsIServiceManager.h"
#include "nsIComponentManager.h"
#include "nsICaseConversion.h"
#include "nsIEntityConverter.h"
#include "nsISaveAsCharset.h"
#include "nsIUnicodeEncoder.h"
#include "nsUnicharUtilCIID.h"
#include "nsIPersistentProperties2.h"
#include "nsIURL.h"
#include "nsNetUtil.h"
#include "nsCOMPtr.h"
#include "nsReadableUtils.h"
#include "nsCRT.h"
#include "nsIUnicodeNormalizer.h"
#include "nsString.h"

Go to the source code of this file.

Defines

#define TESTLEN   29
#define T2LEN   TESTLEN
#define T3LEN   TESTLEN
#define T4LEN   TESTLEN

Functions

 NS_DEFINE_CID (kUnicharUtilCID, NS_UNICHARUTIL_CID)
 NS_DEFINE_CID (kEntityConverterCID, NS_ENTITYCONVERTER_CID)
 NS_DEFINE_CID (kSaveAsCharsetCID, NS_SAVEASCHARSET_CID)
 NS_DEFINE_CID (kUnicodeNormalizerCID, NS_UNICODE_NORMALIZER_CID)
void TestCaseConversion ()
static void TestEntityConversion (PRUint32 version)
static void TestSaveAsCharset ()
void TestNormalization ()
int main (int argc, char **argv)
 The Xalan testcases app.

Variables

static PRUnichar t2data [T2LEN+1]
static PRUnichar t2result [T2LEN+1]
static PRUnichar t3data [T3LEN+1]
static PRUnichar t3result [T3LEN+1]
static PRUnichar t4data [T4LEN+2]
static PRUnichar t4result [T4LEN+2]
static PRUnichar normStr []
static PRUnichar nfdForm []

Define Documentation

Definition at line 63 of file UnicharSelfTest.cpp.

Definition at line 64 of file UnicharSelfTest.cpp.

Definition at line 65 of file UnicharSelfTest.cpp.

#define TESTLEN   29

Definition at line 62 of file UnicharSelfTest.cpp.


Function Documentation

int main ( int  argc,
char **  argv 
)

The Xalan testcases app.

Definition at line 597 of file UnicharSelfTest.cpp.

                                {
   
   nsresult rv = NS_InitXPCOM2(nsnull, nsnull, nsnull);
   if (NS_FAILED(rv)) {
      printf("NS_InitXPCOM2 failed\n");
      return 1;
   }

   // --------------------------------------------

   TestCaseConversion();

   // --------------------------------------------

   TestEntityConversion(nsIEntityConverter::html40);

   // --------------------------------------------

   TestSaveAsCharset();

   // --------------------------------------------

   TestNormalization();

   // --------------------------------------------
   printf("Finish All The Test Cases\n");

   return 0;
}

Here is the call graph for this function:

NS_DEFINE_CID ( kUnicharUtilCID  ,
NS_UNICHARUTIL_CID   
)
NS_DEFINE_CID ( kEntityConverterCID  ,
NS_ENTITYCONVERTER_CID   
)
NS_DEFINE_CID ( kSaveAsCharsetCID  ,
NS_SAVEASCHARSET_CID   
)
NS_DEFINE_CID ( kUnicodeNormalizerCID  ,
NS_UNICODE_NORMALIZER_CID   
)

Definition at line 268 of file UnicharSelfTest.cpp.

{
   printf("==============================\n");
   printf("Start nsICaseConversion Test \n");
   printf("==============================\n");
   nsICaseConversion *t = NULL;
   nsresult res;
   res = CallGetService(kUnicharUtilCID, &t);
           
   printf("Test 1 - GetService():\n");
   if(NS_FAILED(res) || ( t == NULL ) ) {
     printf("\t1st GetService failed\n");
   } else {
     NS_RELEASE(t);
   }

   res = CallGetService(kUnicharUtilCID, &t);
           
   if(NS_FAILED(res) || ( t == NULL ) ) {
     printf("\t2nd GetService failed\n");
   } else {
     int i;
     PRUnichar ch;
     PRUnichar buf[256];

    printf("Test 2 - ToUpper(PRUnichar, PRUnichar*):\n");
    for(i=0;i < T2LEN ; i++)
    {
         res = t->ToUpper(t2data[i], &ch);
         if(NS_FAILED(res)) {
            printf("\tFailed!! return value != NS_OK\n");
            break;
         }
         if(ch != t2result[i]) 
            printf("\tFailed!! result unexpected %d\n", i);
     }


    printf("Test 3 - ToLower(PRUnichar, PRUnichar*):\n");
    for(i=0;i < T3LEN; i++)
    {
         res = t->ToLower(t3data[i], &ch);
         if(NS_FAILED(res)) {
            printf("\tFailed!! return value != NS_OK\n");
            break;
         }
         if(ch != t3result[i]) 
            printf("\tFailed!! result unexpected %d\n", i);
     }


    printf("Test 4 - ToTitle(PRUnichar, PRUnichar*):\n");
    for(i=0;i < T4LEN; i++)
    {
         res = t->ToTitle(t4data[i], &ch);
         if(NS_FAILED(res)) {
            printf("\tFailed!! return value != NS_OK\n");
            break;
         }
         if(ch != t4result[i]) 
            printf("\tFailed!! result unexpected %d\n", i);
     }


    printf("Test 5 - ToUpper(PRUnichar*, PRUnichar*, PRUint32):\n");
    res = t->ToUpper(t2data, buf, T2LEN);
    if(NS_FAILED(res)) {
       printf("\tFailed!! return value != NS_OK\n");
    } else {
       for(i = 0; i < T2LEN; i++)
       {
          if(buf[i] != t2result[i])
          {
            printf("\tFailed!! result unexpected %d\n", i);
            break;
          }
       }
    }

    printf("Test 6 - ToLower(PRUnichar*, PRUnichar*, PRUint32):\n");
    res = t->ToLower(t3data, buf, T3LEN);
    if(NS_FAILED(res)) {
       printf("\tFailed!! return value != NS_OK\n");
    } else {
       for(i = 0; i < T3LEN; i++)
       {
          if(buf[i] != t3result[i])
          {
            printf("\tFailed!! result unexpected %d\n", i);
            break;
          }
       }
    }

    /* 
     * It would be pointless to test ToTitle() with the whole buffer, since
     *  the expected result would be that only the first character would be
     *  transformed. Instead, pass a series of 2-character buffers starting
     *  with each character of the test cases, and check that the first
     *  character is transformed as expected and the second remains unchanged
     */
     printf("Test 7 - ToTitle(PRUnichar*, PRUnichar*, PRUint32):\n");
     for (i = 0; i < T4LEN; i++)
     {
       PRUnichar* titleTest = t4data + i;
       res = t->ToTitle(titleTest, buf, 2);
       if(NS_FAILED(res)) {
         printf("\tFailed!! return value != NS_OK\n");
       } else {
         if (buf[0] != t4result[i] || buf[1] != t4data[i + 1])
         {
           printf("\tFailed!! result unexpected %d\n", i);
           break;
         }
       }
     }

   NS_RELEASE(t);
   }
   printf("==============================\n");
   printf("Finish nsICaseConversion Test \n");
   printf("==============================\n");

}

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestEntityConversion ( PRUint32  version) [static]

Definition at line 393 of file UnicharSelfTest.cpp.

{
  printf("==============================\n");
  printf("Start nsIEntityConverter Test \n");
  printf("==============================\n");

  PRUint32 i;
  nsString inString;
  PRUnichar uChar;
  nsresult res;


  inString.Assign(NS_ConvertASCIItoUCS2("\xA0\xA1\xA2\xA3"));
  uChar = (PRUnichar) 8364; //euro
  inString.Append(&uChar, 1);
  uChar = (PRUnichar) 9830; //
  inString.Append(&uChar, 1);

  nsCOMPtr <nsIEntityConverter> entityConv = do_CreateInstance(kEntityConverterCID, &res);;
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n"); return;}

  // convert char by char
  for (i = 0; i < inString.Length(); i++) {
    char *entity = NULL;
    res = entityConv->ConvertToEntity(inString[i], version, &entity);
    if (NS_SUCCEEDED(res) && NULL != entity) {
      printf("%c %s\n", inString[i], entity);
      nsMemory::Free(entity);
    }
  }

  // convert at once as a string
  PRUnichar *entities;
  res = entityConv->ConvertToEntities(inString.get(), version, &entities);
  if (NS_SUCCEEDED(res) && NULL != entities) {
    for (i = 0; i < nsCRT::strlen(entities); i++) {
      printf("%c", (char) entities[i]);
      if (';' == (char) entities[i])
        printf("\n");
    }
    nsMemory::Free(entities);
  }

  printf("==============================\n");
  printf("Finish nsIEntityConverter Test \n");
  printf("==============================\n\n");
}

Here is the call graph for this function:

Here is the caller graph for this function:

Definition at line 558 of file UnicharSelfTest.cpp.

{
   printf("==============================\n");
   printf("Start nsIUnicodeNormalizer Test \n");
   printf("==============================\n");
   nsIUnicodeNormalizer *t = NULL;
   nsresult res;
   res = CallGetService(kUnicodeNormalizerCID, &t);
           
   printf("Test 1 - GetService():\n");
   if(NS_FAILED(res) || ( t == NULL ) ) {
     printf("\t1st Norm GetService failed\n");
   } else {
     NS_RELEASE(t);
   }

   res = CallGetService(kUnicodeNormalizerCID, &t);
           
   if(NS_FAILED(res) || ( t == NULL ) ) {
     printf("\t2nd GetService failed\n");
   } else {
    printf("Test 2 - NormalizeUnicode(PRUint32, const nsAString&, nsAString&):\n");
    nsAutoString resultStr;
    res =  t->NormalizeUnicodeNFD(nsDependentString(normStr), resultStr);
    if (resultStr.Equals(nfdForm)) {
      printf(" Succeeded in NFD UnicodeNormalizer test. \n");
    } else {
      printf(" Failed in NFD UnicodeNormalizer test. \n");
    }

    NS_RELEASE(t);
   }
   printf("==============================\n");
   printf("Finish nsIUnicodeNormalizer Test \n");
   printf("==============================\n");

}

Here is the call graph for this function:

Here is the caller graph for this function:

static void TestSaveAsCharset ( ) [static]

Definition at line 441 of file UnicharSelfTest.cpp.

{
  printf("==============================\n");
  printf("Start nsISaveAsCharset Test \n");
  printf("==============================\n");

  nsresult res;

  nsString inString;
  inString.Assign(NS_ConvertASCIItoUCS2("\x61\x62\x80\xA0\x63"));
  char *outString;
  
  // first, dump input string
  for (PRUint32 i = 0; i < inString.Length(); i++) {
    printf("%c ", inString[i]);
  }
  printf("\n");

  nsCOMPtr <nsISaveAsCharset> saveAsCharset = do_CreateInstance(kSaveAsCharsetCID, &res);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  
  printf("ISO-8859-1 attr_plainTextDefault entityNone\n");
  res = saveAsCharset->Init("ISO-8859-1", 
                                 nsISaveAsCharset::attr_plainTextDefault, 
                                 nsIEntityConverter::entityNone);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}

  printf("ISO-2022-JP attr_plainTextDefault entityNone\n");
  res = saveAsCharset->Init("ISO-2022-JP", 
                                 nsISaveAsCharset::attr_plainTextDefault,
                                 nsIEntityConverter::entityNone);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}
  if (NS_ERROR_UENC_NOMAPPING == res) {
    outString = ToNewUTF8String(inString);
    if (NULL == outString) {printf("\tFailed!! output null\n");}
    else {printf("Fall back to UTF-8: %s\n", outString); nsMemory::Free(outString);}
  }

  printf("ISO-2022-JP attr_FallbackQuestionMark entityNone\n");
  res = saveAsCharset->Init("ISO-2022-JP", 
                                 nsISaveAsCharset::attr_FallbackQuestionMark,
                                 nsIEntityConverter::entityNone);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}

  printf("ISO-2022-JP attr_FallbackEscapeU entityNone\n");
  res = saveAsCharset->Init("ISO-2022-JP", 
                                 nsISaveAsCharset::attr_FallbackEscapeU,
                                 nsIEntityConverter::entityNone);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}

  printf("ISO-8859-1 attr_htmlTextDefault html40Latin1\n");
  res = saveAsCharset->Init("ISO-8859-1", 
                                 nsISaveAsCharset::attr_htmlTextDefault, 
                                 nsIEntityConverter::html40Latin1);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}

  printf("ISO-8859-1 attr_FallbackHexNCR+attr_EntityAfterCharsetConv html40Latin1 \n");
  res = saveAsCharset->Init("ISO-8859-1", 
                                 nsISaveAsCharset::attr_FallbackHexNCR + 
                                 nsISaveAsCharset::attr_EntityAfterCharsetConv, 
                                 nsIEntityConverter::html40Latin1);
  if (NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  res = saveAsCharset->Convert(inString.get(), &outString);
  if (NS_ERROR_UENC_NOMAPPING != res && NS_FAILED(res)) {printf("\tFailed!! return value != NS_OK\n");}
  if (NULL == outString) {printf("\tFailed!! output null\n");}
  else {printf("%s\n", outString); nsMemory::Free(outString);}


  printf("==============================\n");
  printf("Finish nsISaveAsCharset Test \n");
  printf("==============================\n\n");
}

Here is the call graph for this function:

Here is the caller graph for this function:


Variable Documentation

PRUnichar nfdForm[] [static]
Initial value:
 
{
  0x0061,
  0x0301,
  0x0061,
  0x0301,
  0x0063,
  0x0301,
  0x0063,
  0x0301,
  0x0000
}

Definition at line 545 of file UnicharSelfTest.cpp.

PRUnichar normStr[] [static]
Initial value:
 
{
  0x00E1,   
  0x0061,
  0x0301,
  0x0107,
  0x0063,
  0x0301,
  0x0000
}

Definition at line 534 of file UnicharSelfTest.cpp.

PRUnichar t2data[T2LEN+1] [static]

Definition at line 68 of file UnicharSelfTest.cpp.

PRUnichar t2result[T2LEN+1] [static]

Definition at line 101 of file UnicharSelfTest.cpp.

PRUnichar t3data[T3LEN+1] [static]

Definition at line 134 of file UnicharSelfTest.cpp.

PRUnichar t3result[T3LEN+1] [static]

Definition at line 167 of file UnicharSelfTest.cpp.

PRUnichar t4data[T4LEN+2] [static]

Definition at line 200 of file UnicharSelfTest.cpp.

PRUnichar t4result[T4LEN+2] [static]

Definition at line 234 of file UnicharSelfTest.cpp.