home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / cintltst / nccbtst.c < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  23.7 KB  |  701 lines

  1. /*
  2. *****************************************************************************************
  3. *                                                                                       *
  4. * COPYRIGHT:                                                                            *
  5. *   (C) Copyright Taligent, Inc.,  1996                                                 *
  6. *   (C) Copyright International Business Machines Corporation,  1999                    *
  7. *   Licensed Material - Program-Property of IBM - All Rights Reserved.                  *
  8. *   US Government Users Restricted Rights - Use, duplication, or disclosure             *
  9. *   restricted by GSA ADP Schedule Contract with IBM Corp.                              *
  10. *                                                                                       *
  11. *****************************************************************************************
  12. ********************************************************************************
  13. *
  14. * File NCCBTST.C
  15. *
  16. * Modification History:
  17. *        Name                             Description            
  18. *    Madhu Katragadda     7/21/1999      Testing error callback routines
  19. **************************************************************************************
  20. */
  21. #include <stdio.h>
  22. #include <stdlib.h>
  23. #include <string.h>
  24. #include <ctype.h>
  25. #include "uloc.h"
  26. #include "ucnv.h"
  27. #include "ucnv_err.h"
  28. #include "cintltst.h"
  29. #include "utypes.h"
  30. #include "ustring.h"
  31. #include "nccbtst.h"
  32. #define NEW_MAX_BUFFER 999
  33.  
  34. #define nct_min(x,y)  ((x<y) ? x : y)
  35.  
  36. static int32_t  gInBufferSize = 0;
  37. static int32_t  gOutBufferSize = 0;
  38. static char     gNuConvTestName[1024];
  39.  
  40. void printSeq(const char* a, int len)
  41. {
  42.     int i=0;
  43.     log_verbose("\n{");
  44.     while (i<len) log_verbose("%02X, ", (unsigned char) a[i++]);
  45.     log_verbose("}\n");
  46. }
  47. void printUSeq(const UChar* a, int len)
  48. {
  49.     int i=0;
  50.     log_verbose("\n{");
  51.     while (i<len) log_verbose("%04X, ", (unsigned char) a[i++]);
  52.     log_verbose("}\n");
  53. }
  54.  
  55. void printSeqErr(const char* a, int len)
  56. {
  57.     int i=0;
  58.     fprintf(stderr, "\n{");
  59.     while (i<len)  fprintf(stderr, "%02X, ", (unsigned char) a[i++]);
  60.     fprintf(stderr, "}\n");
  61. }
  62. void printUSeqErr(const UChar* a, int len)
  63. {
  64.     int i=0;
  65.     fprintf(stderr, "\n{");
  66.     while (i<len) fprintf(stderr, "%04X, ", (unsigned char) a[i++]);
  67.     fprintf(stderr,"}\n");
  68. }
  69. void setNuConvTestName(const char *codepage, const char *direction)
  70. {
  71.   sprintf(gNuConvTestName, "[testing %s %s Unicode, InputBufSiz=%d, OutputBufSiz=%d]",
  72.       codepage,
  73.       direction,
  74.       gInBufferSize,
  75.       gOutBufferSize);
  76. }
  77.  
  78.  
  79.  
  80. void addTestConvertErrorCallBack(TestNode** root)
  81. {
  82.    addTest(root, &TestSkipCallBack,  "tsconv/nccbtst/TestSkipCallBack");
  83.    addTest(root, &TestSubCallBack,   "tsconv/nccbtst/TestSubCallBack");
  84.    addTest(root, &TestSubWithValueCallBack, "tsconv/nccbtst/TestSubWithValueCallBack");
  85.    addTest(root, &TestLegalAndOtherCallBack,  "tsconv/nccbtst/TestLegalAndOtherCallBack");
  86.     addTest(root, &TestSingleByteCallBack,  "tsconv/nccbtst/TestSingleByteCallBack");
  87. }
  88.  
  89. void TestSkipCallBack()
  90. {
  91.      TestSkip(NEW_MAX_BUFFER, NEW_MAX_BUFFER);
  92.      TestSkip(1,NEW_MAX_BUFFER);
  93.      TestSkip(1,1);
  94.      TestSkip(NEW_MAX_BUFFER, 1);
  95. }
  96. void TestSubCallBack()
  97. {
  98.      TestSub(NEW_MAX_BUFFER, NEW_MAX_BUFFER);
  99.      TestSub(1,NEW_MAX_BUFFER);
  100.      TestSub(1,1);
  101.      TestSub(NEW_MAX_BUFFER, 1);
  102. }
  103. void TestSubWithValueCallBack()
  104. {
  105.      TestSubWithValue(NEW_MAX_BUFFER, NEW_MAX_BUFFER);
  106.      TestSubWithValue(1,NEW_MAX_BUFFER);
  107.      TestSubWithValue(1,1);
  108.      TestSubWithValue(NEW_MAX_BUFFER, 1);
  109. }
  110. void TestLegalAndOtherCallBack()
  111. {
  112.      TestLegalAndOthers(NEW_MAX_BUFFER, NEW_MAX_BUFFER);
  113.      TestLegalAndOthers(1,NEW_MAX_BUFFER);
  114.      TestLegalAndOthers(1,1);
  115.      TestLegalAndOthers(NEW_MAX_BUFFER, 1);
  116. }
  117. void TestSingleByteCallBack()
  118. {
  119.      TestSingleByte(NEW_MAX_BUFFER, NEW_MAX_BUFFER);
  120.      TestSingleByte(1,NEW_MAX_BUFFER);
  121.      TestSingleByte(1,1);
  122.      TestSingleByte(NEW_MAX_BUFFER, 1);
  123. }
  124. void TestSkip(int32_t inputsize, int32_t outputsize)
  125. {
  126.     UChar    sampleText[] =  { 0x0000, 0xAC00, 0xAC01, 0xEF67, 0xD700 };
  127.     UChar  sampleText2[] =  { 0x6D63, 0x6D64, 0x6D65, 0x6D66 };
  128.  
  129.     const char expskipIBM_949[]= { 
  130.         (char)0x00, (char)0xb0, (char)0xa1, (char)0xb0, (char)0xa2, (char)0xc8, (char)0xd3 };
  131.     
  132.     const char expskipIBM_943[] = { 
  133.         (char)0x9f, (char)0xaf, (char)0x9f, (char)0xb1, (char)0x89, (char)0x59 };
  134.     
  135.     const char expskipIBM_930[] = { 
  136.         (char)0x0e, (char)0x5d, (char)0x5f, (char)0x5d, (char)0x63, (char)0x46, (char)0x6b };
  137.     
  138.     UChar IBM_949skiptoUnicode[]= {0x0000, 0xAC00, 0xAC01, 0xD700 };
  139.     UChar IBM_943skiptoUnicode[]= { 0x6D63, 0x6D64, 0x6D66 };
  140.     UChar IBM_930skiptoUnicode[]= { 0x6D63, 0x6D64, 0x6D66 };
  141.  
  142.     
  143.     int32_t  toIBM949Offsskip [] = { 0, 1, 1, 2, 2, 4, 4};
  144.     int32_t  toIBM943Offsskip [] = { 0, 0, 1, 1, 3, 3};
  145.     int32_t  toIBM930Offsskip [] = { 0, 0, 0, 1, 1, 3, 3};
  146.     
  147.     int32_t  fromIBM949Offs [] = { 0, 1, 3, 5};
  148.     int32_t  fromIBM943Offs [] = { 0, 2, 4};
  149.     int32_t  fromIBM930Offs [] = { 1, 3, 5};
  150.  
  151.     gInBufferSize = inputsize;
  152.     gOutBufferSize = outputsize;
  153.     /*From Unicode*/
  154.     if(!testConvertFromUnicode(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
  155.             expskipIBM_949, sizeof(expskipIBM_949), "ibm-949",
  156.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SKIP, toIBM949Offsskip ))
  157.         log_err("u-> ibm-949 with skip did not match.\n");
  158.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  159.             expskipIBM_943, sizeof(expskipIBM_943), "ibm-943",
  160.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SKIP, toIBM943Offsskip ))
  161.         log_err("u-> ibm-943 with skip did not match.\n");
  162.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  163.             expskipIBM_930, sizeof(expskipIBM_930), "ibm-930",
  164.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SKIP, toIBM930Offsskip ))
  165.         log_err("u-> ibm-930 with skip did not match.\n");
  166.  
  167.     /*to Unicode*/
  168.     if(!testConvertToUnicode(expskipIBM_949, sizeof(expskipIBM_949),
  169.              IBM_949skiptoUnicode, sizeof(IBM_949skiptoUnicode)/sizeof(IBM_949skiptoUnicode),"ibm-949",
  170.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SKIP, fromIBM949Offs ))
  171.         log_err("ibm-949->u with skip did not match.\n");
  172.     if(!testConvertToUnicode(expskipIBM_943, sizeof(expskipIBM_943),
  173.              IBM_943skiptoUnicode, sizeof(IBM_943skiptoUnicode)/sizeof(IBM_943skiptoUnicode[0]),"ibm-943",
  174.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SKIP, fromIBM943Offs ))
  175.         log_err("ibm-943->u with skip did not match.\n");
  176.     
  177.     if(!testConvertToUnicode(expskipIBM_930, sizeof(expskipIBM_930),
  178.              IBM_930skiptoUnicode, sizeof(IBM_930skiptoUnicode)/sizeof(IBM_930skiptoUnicode[0]),"ibm-930",
  179.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SKIP, fromIBM930Offs ))
  180.         log_err("ibm-930->u with skip did not match.\n");
  181.  
  182. }
  183. void TestSub(int32_t inputsize, int32_t outputsize)
  184. {
  185.     UChar    sampleText[] =  { 0x0000, 0xAC00, 0xAC01, 0xEF67, 0xD700 };
  186.     UChar sampleText2[]=    { 0x6D63, 0x6D64, 0x6D65, 0x6D66 };
  187.     
  188.     const char expsubIBM_949[] = 
  189.      { (char)0x00, (char)0xb0, (char)0xa1, (char)0xb0, (char)0xa2, (char)0xaf, (char)0xfe, (char)0xc8, (char)0xd3 };
  190.     
  191.     const char expsubIBM_943[] = { 
  192.         (char)0x9f, (char)0xaf, (char)0x9f, (char)0xb1, (char)0xfc, (char)0xfc, (char)0x89, (char)0x59 };
  193.     
  194.     const char expsubIBM_930[] = { 
  195.         (char)0x0e, (char)0x5d, (char)0x5f, (char)0x5d, (char)0x63, (char)0xfe, (char)0xfe, (char)0x46, (char)0x6b };
  196.         
  197.     UChar IBM_949subtoUnicode[]= {0x0000, 0xAC00, 0xAC01, 0xfffd, 0xD700 };
  198.     UChar IBM_943subtoUnicode[]= {0x6D63, 0x6D64, 0xfffd, 0x6D66 };
  199.     UChar IBM_930subtoUnicode[]= {0x6D63, 0x6D64, 0xfffd, 0x6D66 };
  200.     
  201.     int32_t toIBM949Offssub [] ={ 0, 1, 1, 2, 2, 3, 3, 4, 4};
  202.     int32_t toIBM943Offssub [] ={ 0, 0, 1, 1, 2, 2, 3, 3};
  203.     int32_t toIBM930Offssub [] ={ 0, 0, 0, 1, 1, 2, 2, 3, 3};
  204.     
  205.     int32_t  fromIBM949Offs [] = { 0, 1, 3, 5, 7};
  206.     int32_t  fromIBM943Offs [] = { 0, 2, 4, 6};
  207.     int32_t  fromIBM930Offs [] = { 1, 3, 5, 7};
  208.     
  209.     gInBufferSize = inputsize;
  210.     gOutBufferSize = outputsize;
  211.  
  212.     /*from unicode*/
  213.     if(!testConvertFromUnicode(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
  214.             expsubIBM_949, sizeof(expsubIBM_949), "ibm-949", 
  215.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SUBSTITUTE, toIBM949Offssub ))
  216.         log_err("u-> ibm-949 with subst did not match.\n");
  217.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  218.             expsubIBM_943, sizeof(expsubIBM_943), "ibm-943",
  219.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SUBSTITUTE, toIBM943Offssub ))
  220.         log_err("u-> ibm-943 with subst did not match.\n");
  221.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  222.             expsubIBM_930, sizeof(expsubIBM_930), "ibm-930", 
  223.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SUBSTITUTE, toIBM930Offssub ))
  224.         log_err("u-> ibm-930 with subst did not match.\n");
  225.     
  226.     /*to unicode*/
  227.     if(!testConvertToUnicode(expsubIBM_949, sizeof(expsubIBM_949),
  228.              IBM_949subtoUnicode, sizeof(IBM_949subtoUnicode)/sizeof(IBM_949subtoUnicode[0]),"ibm-949",
  229.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE, fromIBM949Offs ))
  230.         log_err("ibm-949->u with substitute did not match.\n");
  231.     if(!testConvertToUnicode(expsubIBM_943, sizeof(expsubIBM_943),
  232.              IBM_943subtoUnicode, sizeof(IBM_943subtoUnicode)/sizeof(IBM_943subtoUnicode[0]),"ibm-943",
  233.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE, fromIBM943Offs ))
  234.         log_err("ibm-943->u with substitute did not match.\n");
  235.     if(!testConvertToUnicode(expsubIBM_930, sizeof(expsubIBM_930),
  236.              IBM_930subtoUnicode, sizeof(IBM_930subtoUnicode)/sizeof(IBM_930subtoUnicode[0]),"ibm-930",
  237.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE, fromIBM930Offs ))
  238.         log_err("ibm-930->u with substitute did not match.\n");
  239.  
  240. }
  241.  
  242. void TestSubWithValue(int32_t inputsize, int32_t outputsize)
  243. {
  244.     UChar    sampleText[] =  { 0x0000, 0xAC00, 0xAC01, 0xEF67, 0xD700 };
  245.     UChar  sampleText2[] =  { 0x6D63, 0x6D64, 0x6D65, 0x6D66 };
  246.     
  247.     const char sampleTxtToU[]= { (char)0x00, (char)0x9f, (char)0xaf, (char)0xff, (char)0x89, (char)0xd3 };
  248.     
  249.     UChar IBM_943toUnicode[] = { 0x0000, 0x6D63, '%', 'X', 'F', 'F', 0x6D66};
  250.     
  251.     int32_t  fromIBM943Offs [] =     { 0, 1, 3, 3, 3, 3, 4};
  252.     
  253.     
  254.     const char expsubwvalIBM_949[]=    { 
  255.         (char)0x00, (char)0xb0, (char)0xa1, (char)0xb0, (char)0xa2, '%', 'U', 'E', 'F', '6', '7', (char)0xc8, (char)0xd3 }; 
  256.     
  257.     const char expsubwvalIBM_943[]=    { 
  258.         (char)0x9f, (char)0xaf, (char)0x9f, (char)0xb1, '%', 'U', '6', 'D', '6', '5', (char)0x89, (char)0x59 };
  259.     
  260.     const char expsubwvalIBM_930[] = {
  261.         (char)0x0e, (char)0x5d, (char)0x5f, (char)0x5d, (char)0x63, (char)0x0f, (char)0x6c, (char)0xe4, (char)0xf6, (char)0xc4, (char)0xf6, (char)0xf5, (char)0x46, (char)0x6b };
  262.  
  263.     
  264.     int32_t toIBM949Offs [] ={ 0, 1, 1, 2, 2, 3, 3, 3, 3, 3, 3, 4, 4};
  265.     int32_t toIBM943Offs [] = { 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 3, 3};
  266.     int32_t toIBM930Offs [] = { 0, 0, 0, 1, 1, 2, 2, 2, 2, 2, 2, 2, 3, 3};
  267.  
  268.     gInBufferSize = inputsize;
  269.     gOutBufferSize = outputsize;
  270.  
  271.     /*from Unicode*/
  272.     if(!testConvertFromUnicode(sampleText, sizeof(sampleText)/sizeof(sampleText[0]),
  273.             expsubwvalIBM_949, sizeof(expsubwvalIBM_949), "ibm-949", 
  274.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_ESCAPE, toIBM949Offs ))
  275.         log_err("u-> ibm-949 with subst with value did not match.\n");
  276.  
  277.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  278.             expsubwvalIBM_943, sizeof(expsubwvalIBM_943), "ibm-943",
  279.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_ESCAPE, toIBM943Offs ))
  280.         log_err("u-> ibm-943 with sub with value did not match.\n");
  281.  
  282.     if(!testConvertFromUnicode(sampleText2, sizeof(sampleText2)/sizeof(sampleText2[0]),
  283.             expsubwvalIBM_930, sizeof(expsubwvalIBM_930), "ibm-930", 
  284.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_ESCAPE, toIBM930Offs ))
  285.         log_err("u-> ibm-930 with subst with value did not match.\n");
  286.   
  287.     /*to Unicode*/
  288.     if(!testConvertToUnicode(sampleTxtToU, sizeof(sampleTxtToU),
  289.              IBM_943toUnicode, sizeof(IBM_943toUnicode)/sizeof(IBM_943toUnicode),"ibm-943",
  290.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_ESCAPE, fromIBM943Offs ))
  291.         log_err("ibm-943->u with substitute did not match.\n");
  292. }
  293. void TestLegalAndOthers(int32_t inputsize, int32_t outputsize)
  294. {
  295.     UChar    legalText[] =    { 0x0000, 0xAC00, 0xAC01, 0xD700 };
  296.     const char templegal949[] ={ (char)0x00, (char)0xb0, (char)0xa1, (char)0xb0, (char)0xa2, (char)0xc8, (char)0xd3 };
  297.     int32_t  to949legal[] = {0, 1, 1, 2, 2, 3, 3};
  298.     
  299.  
  300.     const char text943[] = {
  301.         (char)0x82, (char)0xa9, (char)0x82, (char)0x20, /*(char)0xc8,*/  'a', (char)0x8a, (char)0xbf, (char)0x8e, (char)0x9a };
  302.         UChar toUnicode943sub[] = { 0x304b, 0xfffd, /*0xff88,*/ 0x0061, 0x6f22,  0x5b57};
  303.         UChar toUnicode943skip[]= { 0x304b, /*0xff88,*/ 0x0061, 0x6f22,  0x5b57};
  304.         UChar toUnicode943stop[]= { 0x304b};
  305.  
  306.     int32_t  fromIBM943Offssub[]  = {0, 2, 4, 5, 7};
  307.     int32_t  fromIBM943Offsskip[] = { 0, 4, 5, 7};
  308.     int32_t  fromIBM943Offsstop[] = { 0};
  309.  
  310.     gInBufferSize = inputsize;
  311.     gOutBufferSize = outputsize;
  312.     /*checking with a legal value*/
  313.     if(!testConvertFromUnicode(legalText, sizeof(legalText)/sizeof(legalText[0]),
  314.             templegal949, sizeof(templegal949), "ibm-949",
  315.             (UConverterFromUCallback)UCNV_FROM_U_CALLBACK_SKIP, to949legal ))
  316.         log_err("u-> ibm-949 with skip did not match.\n");
  317.     
  318.     /*checking illegal value for ibm-943 with substitute*/ 
  319.     if(!testConvertToUnicode(text943, sizeof(text943),
  320.              toUnicode943sub, sizeof(toUnicode943sub)/sizeof(toUnicode943sub[0]),"ibm-943",
  321.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE, fromIBM943Offssub ))
  322.         log_err("ibm-943->u with subst did not match.\n");
  323.     /*checking illegal value for ibm-943 with skip */
  324.     if(!testConvertToUnicode(text943, sizeof(text943),
  325.              toUnicode943skip, sizeof(toUnicode943skip)/sizeof(toUnicode943skip[0]),"ibm-943",
  326.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SKIP, fromIBM943Offsskip ))
  327.         log_err("ibm-943->u with skip did not match.\n");
  328.  
  329.     /*checking illegal value for ibm-943 with stop */
  330.     if(!testConvertToUnicode(text943, sizeof(text943),
  331.              toUnicode943stop, sizeof(toUnicode943stop)/sizeof(toUnicode943stop[0]),"ibm-943",
  332.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_STOP, fromIBM943Offsstop ))
  333.         log_err("ibm-943->u with stop did not match.\n");
  334.  
  335. }
  336. void TestSingleByte(int32_t inputsize, int32_t outputsize)
  337. {
  338.     const char sampleText[] = {(char)0x82, (char)0xa9, 'a', 'b', 'c' , (char)0x82, (char)0xff, /*(char)0x82, (char)0xa9,*/ '2', '3'};
  339.     UChar toUnicode943sub[] = {0x304b, 0x0061, 0x0062, 0x0063,  0xfffd,/*0x304b,*/ 0x0032, 0x0033};
  340.     int32_t  fromIBM943Offssub[]  = {0, 2, 3, 4, 5, 7, 8};
  341.     /*checking illegal value for ibm-943 with substitute*/ 
  342.     gInBufferSize = inputsize;
  343.     gOutBufferSize = outputsize;
  344.     
  345.     if(!testConvertToUnicode(sampleText, sizeof(sampleText),
  346.              toUnicode943sub, sizeof(toUnicode943sub)/sizeof(toUnicode943sub[0]),"ibm-943",
  347.             (UConverterToUCallback)UCNV_TO_U_CALLBACK_SUBSTITUTE, fromIBM943Offssub ))
  348.         log_err("ibm-943->u with subst did not match.\n");
  349. }
  350.  
  351. bool_t testConvertFromUnicode(const UChar *source, int sourceLen,  const char *expect, int expectLen, 
  352.                 const char *codepage, UConverterFromUCallback callback , int32_t *expectOffsets)
  353. {
  354.     
  355.         
  356.     UErrorCode status = U_ZERO_ERROR;
  357.     UConverter *conv = 0;
  358.     char    junkout[NEW_MAX_BUFFER]; /* FIX */
  359.     int32_t    junokout[NEW_MAX_BUFFER]; /* FIX */
  360.     const UChar *src;
  361.     char *end;
  362.     char *targ;
  363.     int32_t *offs;
  364.     int i;
  365.     int32_t   realBufferSize;
  366.     char *realBufferEnd;
  367.     const UChar *realSourceEnd;
  368.     const UChar *sourceLimit;
  369.     bool_t checkOffsets = TRUE;
  370.     bool_t doFlush;
  371.     UConverterFromUCallback action;
  372.     char junk[9999];
  373.     char offset_str[9999];
  374.     char expOff[9999];
  375.     char *p;
  376.     
  377.     
  378.     for(i=0;i<NEW_MAX_BUFFER;i++)
  379.         junkout[i] = 0xF0;
  380.     for(i=0;i<NEW_MAX_BUFFER;i++)
  381.         junokout[i] = 0xFF;
  382.     setNuConvTestName(codepage, "FROM");
  383.  
  384.     log_verbose("\nTesting========= %s  FROM \n  inputbuffer= %d   outputbuffer= %d\n", codepage, gInBufferSize, 
  385.             gOutBufferSize);
  386.  
  387.     conv = ucnv_open(codepage, &status);
  388.     if(U_FAILURE(status))
  389.     {
  390.         log_err("Couldn't open converter %s\n",codepage);    
  391.         return FALSE;
  392.     }
  393.  
  394.     log_verbose("Converter opened..\n");
  395.     /*----setting the callback routine----*/
  396.        
  397.     
  398.     ucnv_setFromUCallBack (conv,  /*action*/callback, &status);
  399.     if (U_FAILURE(status)) 
  400.     { 
  401.         log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status));  
  402.     }
  403.     action=ucnv_getFromUCallBack(conv);
  404.     /*------------------------*/
  405.     src = source;
  406.     targ = junkout;
  407.     offs = junokout;
  408.  
  409.     realBufferSize = (sizeof(junkout)/sizeof(junkout[0]));
  410.     realBufferEnd = junkout + realBufferSize;
  411.     realSourceEnd = source + sourceLen;
  412.  
  413.     if ( gOutBufferSize != realBufferSize )
  414.       checkOffsets = FALSE;
  415.  
  416.     if( gInBufferSize != NEW_MAX_BUFFER )
  417.       checkOffsets = FALSE;
  418.  
  419.     do
  420.       {
  421.         end = nct_min(targ + gOutBufferSize, realBufferEnd);
  422.         sourceLimit = nct_min(src + gInBufferSize, realSourceEnd);
  423.  
  424.         doFlush = (sourceLimit == realSourceEnd);
  425.  
  426.         if(targ == realBufferEnd)
  427.           {
  428.         log_err("Error, overflowed the real buffer while about to call fromUnicode! targ=%08lx %s", targ, gNuConvTestName);
  429.         return FALSE;
  430.           }
  431.         log_verbose("calling fromUnicode @ SOURCE:%08lx to %08lx  TARGET: %08lx to %08lx, flush=%s\n", src,sourceLimit, targ,end, doFlush?"TRUE":"FALSE");
  432.         
  433.  
  434.         status = U_ZERO_ERROR;
  435.  
  436.         ucnv_fromUnicode (conv,
  437.                   &targ,
  438.                   end,
  439.                   &src,
  440.                   sourceLimit,
  441.                   checkOffsets ? offs : NULL,
  442.                   doFlush, /* flush if we're at the end of the input data */
  443.                   &status);
  444.     
  445.       } while ( (status == U_INDEX_OUTOFBOUNDS_ERROR) || (sourceLimit < realSourceEnd) );
  446.         
  447.      if(U_FAILURE(status))
  448.       {
  449.         log_err("Problem tdoing fromUnicode, errcode %d %s\n", myErrorName(status), gNuConvTestName);
  450.         return FALSE;
  451.       }
  452.  
  453.     log_verbose("\nConversion done [%d uchars in -> %d chars out]. \nResult :",
  454.         sourceLen, targ-junkout);
  455.     if(VERBOSITY)
  456.     {
  457.         
  458.         junk[0] = 0;
  459.         offset_str[0] = 0;
  460.         for(p = junkout;p<targ;p++)
  461.         {
  462.             sprintf(junk + strlen(junk), "0x%02x, ", (0xFF) & (unsigned int)*p);
  463.             sprintf(offset_str + strlen(offset_str), "0x%02x, ", (0xFF) & (unsigned int)junokout[p-junkout]);
  464.         }
  465.         
  466.         log_verbose(junk);
  467.         printSeq(expect, expectLen);
  468.         if ( checkOffsets )
  469.           {
  470.             log_verbose("\nOffsets:");
  471.             log_verbose(offset_str);
  472.           }
  473.         log_verbose("\n");
  474.     }
  475.     ucnv_close(conv);
  476.  
  477.  
  478.     if(expectLen != targ-junkout)
  479.     {
  480.         log_err("Expected %d chars out, got %d %s\n", expectLen, targ-junkout, gNuConvTestName);
  481.         log_verbose("Expected %d chars out, got %d %s\n", expectLen, targ-junkout, gNuConvTestName);
  482.         printSeqErr(junkout, targ-junkout);
  483.         printSeqErr(expect, expectLen);
  484.         return FALSE;
  485.     }
  486.  
  487.     if (checkOffsets && (expectOffsets != 0) )
  488.     {
  489.         log_verbose("\ncomparing %d offsets..\n", targ-junkout);
  490.         if(memcmp(junokout,expectOffsets,(targ-junkout) * sizeof(int32_t) )){
  491.             log_err("\ndid not get the expected offsets while %s \n", gNuConvTestName);
  492.             log_err("Got  : ");
  493.             printSeqErr(junkout, targ-junkout);
  494.             for(p=junkout;p<targ;p++)
  495.                 log_err("%d, ", junokout[p-junkout]); 
  496.             log_err("\nExpected: ");
  497.             for(i=0; i<(targ-junkout); i++)
  498.                 log_err("%d,", expectOffsets[i]);
  499.         }
  500.     }
  501.  
  502.     log_verbose("\n\ncomparing..\n");
  503.     if(!memcmp(junkout, expect, expectLen))
  504.     {
  505.         log_verbose("Matches!\n");
  506.         return TRUE;
  507.     }
  508.     else
  509.     {    
  510.         log_err("String does not match. %s\n", gNuConvTestName);
  511.         log_verbose("String does not match. %s\n", gNuConvTestName);
  512.         printSeqErr(junkout, expectLen);
  513.         printSeqErr(expect, expectLen);
  514.         return FALSE;
  515.     }
  516. }
  517.  
  518. bool_t testConvertToUnicode( const char *source, int sourcelen, const UChar *expect, int expectlen, 
  519.                const char *codepage, UConverterToUCallback callback, int32_t *expectOffsets)
  520. {
  521.     UErrorCode status = U_ZERO_ERROR;
  522.     UConverter *conv = 0;
  523.     UChar    junkout[NEW_MAX_BUFFER]; /* FIX */
  524.     int32_t    junokout[NEW_MAX_BUFFER]; /* FIX */
  525.     const char *src;
  526.     const char *realSourceEnd;
  527.     const char *srcLimit;
  528.     UChar *targ;
  529.     UChar *end;
  530.     int32_t *offs;
  531.     int i;
  532.     bool_t   checkOffsets = TRUE;
  533.     char junk[9999];
  534.     char offset_str[9999];
  535.     UChar *p;
  536.     UConverterToUCallback action;    
  537.  
  538.     int32_t   realBufferSize;
  539.     UChar *realBufferEnd;
  540.     
  541.  
  542.     for(i=0;i<NEW_MAX_BUFFER;i++)
  543.         junkout[i] = 0xFFFE;
  544.  
  545.     for(i=0;i<NEW_MAX_BUFFER;i++)
  546.         junokout[i] = -1;
  547.     
  548.     setNuConvTestName(codepage, "TO");
  549.  
  550.     log_verbose("\n=========  %s\n", gNuConvTestName);
  551.  
  552.     conv = ucnv_open(codepage, &status);
  553.     if(U_FAILURE(status))
  554.     {
  555.         log_err("Couldn't open converter %s\n",gNuConvTestName);
  556.         return FALSE;
  557.     }
  558.  
  559.     log_verbose("Converter opened..\n");
  560.    
  561.     src = source;
  562.     targ = junkout;
  563.     offs = junokout;
  564.     
  565.     realBufferSize = (sizeof(junkout)/sizeof(junkout[0]));
  566.     realBufferEnd = junkout + realBufferSize;
  567.     realSourceEnd = src + sourcelen;
  568.     /*----setting the callback routine----*/
  569.     
  570.        
  571.     ucnv_setToUCallBack (conv, callback, &status);
  572.     if (U_FAILURE(status)) 
  573.     { 
  574.         log_err("FAILURE in setting the callback Function! %s\n", myErrorName(status));  
  575.     }
  576.  
  577.     action = ucnv_getToUCallBack(conv);
  578.     /*-------------------------------------*/
  579.     if ( gOutBufferSize != realBufferSize )
  580.       checkOffsets = FALSE;
  581.  
  582.     if( gInBufferSize != NEW_MAX_BUFFER )
  583.       checkOffsets = FALSE;
  584.  
  585.     do
  586.       {
  587.         end = nct_min( targ + gOutBufferSize, realBufferEnd);
  588.         srcLimit = nct_min(realSourceEnd, src + gInBufferSize);
  589.  
  590.         if(targ == realBufferEnd)
  591.           {
  592.         log_err("Error, the end would overflow the real output buffer while about to call toUnicode! tarjey=%08lx %s",targ,gNuConvTestName);
  593.         return FALSE;
  594.           }
  595.         log_verbose("calling toUnicode @ %08lx to %08lx\n", targ,end);
  596.  
  597.       
  598.  
  599.         status = U_ZERO_ERROR;
  600.  
  601.         ucnv_toUnicode (conv,
  602.                 &targ,
  603.                 end,
  604.                 &src,
  605.                 srcLimit,
  606.                 checkOffsets ? offs : NULL,
  607.                 (srcLimit == realSourceEnd), /* flush if we're at the end of hte source data */
  608.                 &status);
  609.  
  610.               
  611.  
  612.       } while ( (status == U_INDEX_OUTOFBOUNDS_ERROR) || (srcLimit < realSourceEnd) ); /* while we just need another buffer */
  613.     /*check for an INVALID character for testing the call back function STOP*/
  614.     if(status == U_INVALID_CHAR_FOUND || status == U_ILLEGAL_CHAR_FOUND )
  615.     {
  616.         for(p = junkout;p<targ;p++)
  617.           sprintf(junk + strlen(junk), "0x%04x, ", (0xFFFF) & (unsigned int)*p);
  618.         /*        printUSeqErr(junkout, expectlen);*/
  619.         if(!memcmp(junkout, expect, expectlen*2))
  620.         {
  621.         log_verbose("Matches!\n");
  622.         return TRUE;
  623.         }
  624.         else
  625.         {    
  626.         log_err("String does not match. %s\n", gNuConvTestName);
  627.         log_verbose("String does not match. %s\n", gNuConvTestName);
  628.         printUSeq(junkout, expectlen);
  629.         printUSeq(expect, expectlen);
  630.         return FALSE;
  631.         }
  632.         
  633.     }
  634.     if(U_FAILURE(status))
  635.     {
  636.         log_err("Problem doing toUnicode, errcode %d %s\n", myErrorName(status), gNuConvTestName);
  637.         return FALSE;
  638.     }
  639.  
  640.     log_verbose("\nConversion done. %d bytes -> %d chars.\nResult :",
  641.         sourcelen, targ-junkout);
  642.     if(VERBOSITY)
  643.     {
  644.         
  645.         junk[0] = 0;
  646.         offset_str[0] = 0;
  647.  
  648.         for(p = junkout;p<targ;p++)
  649.         {
  650.             sprintf(junk + strlen(junk), "0x%04x, ", (0xFFFF) & (unsigned int)*p);
  651.             sprintf(offset_str + strlen(offset_str), "0x%04x, ", (0xFFFF) & (unsigned int)junokout[p-junkout]);
  652.         }
  653.         
  654.         log_verbose(junk);
  655.  
  656.         if ( checkOffsets )
  657.           {
  658.             log_verbose("\nOffsets:");
  659.             log_verbose(offset_str);
  660.           }
  661.         log_verbose("\n");
  662.     }
  663.     ucnv_close(conv);
  664.  
  665.     log_verbose("comparing %d uchars (%d bytes)..\n",expectlen,expectlen*2);
  666.  
  667.     if (checkOffsets && (expectOffsets != 0))
  668.     {
  669.         if(memcmp(junokout,expectOffsets,(targ-junkout) * sizeof(int32_t)))
  670.         {
  671.         log_err("\n\ndid not get the expected offsets while %s \n", gNuConvTestName);            log_err("\nGot  : ");
  672.  
  673.         for(p=junkout;p<targ;p++)
  674.           log_err("%d, ", junokout[p-junkout]); 
  675.         log_err("\nExpected: ");
  676.         for(i=0; i<(targ-junkout); i++)
  677.           log_err("%d,", expectOffsets[i]);
  678.         log_err("");
  679.         for(i=0; i<(targ-junkout); i++)
  680.           log_err("%X,", junkout[i]);
  681.         log_err("");
  682.         for(i=0; i<(src-source); i++)
  683.           log_err("%X,", (unsigned char)source[i]);
  684.         }
  685.     }
  686.  
  687.     if(!memcmp(junkout, expect, expectlen*2))
  688.     {
  689.         log_verbose("Matches!\n");
  690.         return TRUE;
  691.     }
  692.     else
  693.     {    
  694.         log_err("String does not match. %s\n", gNuConvTestName);
  695.         log_verbose("String does not match. %s\n", gNuConvTestName);
  696.         printUSeq(junkout, expectlen);
  697.         printUSeq(expect, expectlen);
  698.         return FALSE;
  699.     }
  700. }
  701.