home *** CD-ROM | disk | FTP | other *** search
/ AmigActive 6 / AACD06.ISO / AACD / Programming / ICU / src / icu / source / test / intltest / pptest.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  1999-10-19  |  5.8 KB  |  207 lines

  1. /*
  2. ********************************************************************
  3. * COPYRIGHT: 
  4. * (C) Copyright International Business Machines Corporation, 1998
  5. * Licensed Material - Program-Property of IBM - All Rights Reserved. 
  6. * US Government Users Restricted Rights - Use, duplication, or disclosure 
  7. * restricted by GSA ADP Schedule Contract with IBM Corp. 
  8. *
  9. ********************************************************************
  10. */
  11.  
  12. #include "pptest.h"
  13.  
  14. #include "numfmt.h"
  15. #include "decimfmt.h"
  16.  
  17. // *****************************************************************************
  18. // class ParsePositionTest
  19. // *****************************************************************************
  20.  
  21. #define CASE(id,test) case id: name = #test; if (exec) { logln(#test "---"); logln((UnicodeString)""); test(); } break;
  22.  
  23. void ParsePositionTest::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  24. {
  25.     // if (exec) logln((UnicodeString)"TestSuite ParsePositionTest");
  26.     switch (index) {
  27.         CASE(0, TestParsePosition)
  28.         CASE(1, TestFieldPosition)
  29.         CASE(2, TestFieldPosition_example)
  30.         CASE(3, Test4109023)
  31.  
  32.         default: name = ""; break;
  33.     }
  34. }
  35.  
  36. bool_t 
  37. ParsePositionTest::failure(UErrorCode status, const char* msg)
  38. {
  39.     if(U_FAILURE(status)) {
  40.         errln(UnicodeString("FAIL: ") + msg + " failed, error " + errorName(status));
  41.         return TRUE;
  42.     }
  43.  
  44.     return FALSE;
  45. }
  46.  
  47. void ParsePositionTest::TestParsePosition() 
  48. {
  49.     ParsePosition pp1(0);
  50.     if (pp1.getIndex() == 0) {
  51.         logln("PP constructor() tested.");
  52.     }else{
  53.         errln("*** PP getIndex or constructor() result");
  54.     }
  55.  
  56.     {
  57.         int to = 5;
  58.         ParsePosition pp2( to );
  59.         if (pp2.getIndex() == 5) {
  60.             logln("PP getIndex and constructor(UTextOffset) tested.");
  61.         }else{
  62.             errln("*** PP getIndex or constructor(UTextOffset) result");
  63.         }
  64.         pp2.setIndex( 3 );
  65.         if (pp2.getIndex() == 3) {
  66.             logln("PP setIndex tested.");
  67.         }else{
  68.             errln("*** PP getIndex or setIndex result");
  69.         }
  70.     }
  71.  
  72.     ParsePosition pp2(3), pp3(5);
  73.     //pp2 = new ParsePosition( 3 );
  74.     //pp3 = new ParsePosition( 5 );
  75.     ParsePosition pp4(5);
  76.     if ( pp2 != pp3) {
  77.         logln("PP not equals tested.");
  78.     }else{
  79.         errln("*** PP not equals fails");
  80.     }
  81.     if (pp3 == pp4) {
  82.         logln("PP equals tested.");
  83.     }else{
  84.         errln(UnicodeString("*** PP equals fails (") + pp3.getIndex() + " != " + pp4.getIndex() + ")");
  85.     }
  86.  
  87.     ParsePosition pp5;
  88.     pp5 = pp4;
  89.     if (pp4 == pp5) {
  90.         logln("PP operator= tested.");
  91.     }else{
  92.         errln("*** PP operator= operator== or operator != result");
  93.     }
  94.  
  95. }
  96.  
  97. void ParsePositionTest::TestFieldPosition() 
  98. {
  99.     FieldPosition fp( 7 );
  100.  
  101.     if (fp.getField() == 7) {
  102.         logln("FP constructor(int) and getField tested.");
  103.     }else{
  104.         errln("*** FP constructor(int) or getField");
  105.     }
  106.  
  107.     FieldPosition fph( 3 );
  108.     if ( fph.getField() != 3) 
  109.         errln("*** FP getField or heap constr.");
  110.  
  111.     bool_t err1 = FALSE;
  112.     bool_t err2 = FALSE;
  113.     bool_t err3 = FALSE;
  114. //        for (long i = -50; i < 50; i++ ) {
  115. //            fp.setField( i+8 );
  116. //            fp.setBeginIndex( i+6 );
  117. //            fp.setEndIndex( i+7 );
  118. //            if (fp.getField() != i+8)  err1 = TRUE;
  119. //            if (fp.getBeginIndex() != i+6) err2 = TRUE;
  120. //            if (fp.getEndIndex() != i+7) err3 = TRUE;
  121. //        }
  122.     if (!err1) {
  123.         logln("FP setField and getField tested.");
  124.     }else{
  125.         errln("*** FP setField or getField");
  126.     }
  127.     if (!err2) {
  128.         logln("FP setBeginIndex and getBeginIndex tested.");
  129.     }else{
  130.         errln("*** FP setBeginIndex or getBeginIndex");
  131.     }
  132.     if (!err3) {
  133.         logln("FP setEndIndex and getEndIndex tested.");
  134.     }else{
  135.         errln("*** FP setEndIndex or getEndIndex");
  136.     }
  137.  
  138.     logln("");
  139. }
  140.  
  141. void ParsePositionTest::TestFieldPosition_example() 
  142. {
  143.     //***** no error detection yet !!!!!!!
  144.     //***** this test is for compiler checks and visual verification only.
  145.     double doubleNum[] = { 
  146.         123456789.0, 
  147.         -12345678.9, 
  148.         1234567.89, 
  149.         -123456.789,
  150.         12345.6789, 
  151.         -1234.56789, 
  152.         123.456789, 
  153.         -12.3456789, 
  154.         1.23456789};
  155.     int dNumSize = 9;
  156.  
  157.     UErrorCode status = U_ZERO_ERROR;
  158.     NumberFormat *nf = NumberFormat::createInstance(status);
  159.     failure(status, "NumberFormat::createInstance");
  160.  
  161.     if(nf->getDynamicClassID() != DecimalFormat::getStaticClassID()) {
  162.         errln("NumberFormat::createInstance returned unexpected class type");
  163.         return;
  164.     }
  165.     DecimalFormat *fmt = (DecimalFormat*) nf;
  166.     fmt->setDecimalSeparatorAlwaysShown(TRUE);
  167.  
  168.     const int tempLen = 20;
  169.     UnicodeString temp;
  170.  
  171.     for (int i=0; i < dNumSize; i++) {
  172.         temp.remove();
  173.         //temp = new StringBuffer(); // Get new buffer
  174.  
  175.         FieldPosition pos(NumberFormat::INTEGER_FIELD);
  176.         UnicodeString buf;// = new StringBuffer();
  177.         //char fmtText[tempLen];
  178.         //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
  179.         UnicodeString res;
  180.         res = fmt->format(doubleNum[i], buf, pos);
  181.         int tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ?
  182.             tempLen : (tempLen - pos.getEndIndex());
  183.         for (int j=0; j<tempOffset; j++) 
  184.             temp += UnicodeString("="/*'='*/); // initialize
  185.         //cout << temp << fmtText   << endl;
  186.         logln("FP " + temp + res);
  187.     }
  188.  
  189.     logln("");
  190.     delete nf;
  191. }
  192.  
  193. /* @bug 4109023
  194.  * Need to override ParsePosition.equals and FieldPosition.equals.
  195.  */
  196. void ParsePositionTest::Test4109023()
  197. {
  198.     ParsePosition p(3);
  199.     ParsePosition p2(3);
  200.     if (p != p2)
  201.         errln("Error : ParsePosition.equals() failed");
  202.     FieldPosition fp(2);
  203.     FieldPosition fp2(2);
  204.     if (fp != fp2)
  205.         errln("Error : FieldPosition.equals() failed");
  206. }
  207.