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

  1.  
  2. /*
  3. ********************************************************************
  4. * COPYRIGHT: 
  5. * (C) Copyright Taligent, Inc., 1997
  6. * (C) Copyright International Business Machines Corporation, 1997 - 1998
  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.  
  15. #include "utypes.h"
  16.  
  17. #include "intltest.h"
  18. #include "tfsmalls.h"
  19.  
  20. #include "msgfmt.h"
  21. #include "choicfmt.h"
  22.  
  23. #include "parsepos.h"
  24. #include "fieldpos.h"
  25. #include "fmtable.h"
  26.  
  27.  
  28. static bool_t chkstatus( UErrorCode &status, char* msg = NULL )
  29. {
  30.     bool_t ok = (status == U_ZERO_ERROR);
  31.     if (!ok) it_errln( msg );
  32.     return ok;
  33. }
  34.  
  35. void test_ParsePosition( void )
  36. {
  37.     ParsePosition* pp1 = new ParsePosition();
  38.     if (pp1 && (pp1->getIndex() == 0)) {
  39.         it_out << "PP constructor() tested." << endl;
  40.     }else{
  41.         it_errln("*** PP getIndex or constructor() result");
  42.     }
  43.     delete pp1;
  44.  
  45.     
  46.     {
  47.         UTextOffset to = 5;
  48.         ParsePosition pp2( to );
  49.         if (pp2.getIndex() == 5) {
  50.             it_out << "PP getIndex and constructor(UTextOffset) tested." << endl;
  51.         }else{
  52.             it_errln("*** PP getIndex or constructor(UTextOffset) result");
  53.         }
  54.         pp2.setIndex( 3 );
  55.         if (pp2.getIndex() == 3) {
  56.             it_out << "PP setIndex tested." << endl;
  57.         }else{
  58.             it_errln("*** PP getIndex or setIndex result");
  59.         }
  60.     }
  61.  
  62.     ParsePosition pp2, pp3;
  63.     pp2 = 3;
  64.     pp3 = 5;
  65.     ParsePosition pp4( pp3 );
  66.     if ((pp2 != pp3) && (pp3 == pp4)) {
  67.         it_out << "PP copy contructor, operator== and operator != tested." << endl;
  68.     }else{
  69.         it_errln("*** PP operator== or operator != result");
  70.     }
  71.  
  72.     ParsePosition pp5;
  73.     pp5 = pp4;
  74.     if ((pp4 == pp5) && (!(pp4 != pp5))) {
  75.         it_out << "PP operator= tested." << endl;
  76.     }else{
  77.         it_errln("*** PP operator= operator== or operator != result");
  78.     }
  79.  
  80.  
  81. }
  82.  
  83. #include "decimfmt.h"
  84.  
  85. void test_FieldPosition_example( void )
  86. {
  87.     //***** no error detection yet !!!!!!!
  88.     //***** this test is for compiler checks and visual verification only.
  89.     double doubleNum[] = { 123456789.0, -12345678.9, 1234567.89, -123456.789,
  90.         12345.6789, -1234.56789, 123.456789, -12.3456789, 1.23456789};
  91.     int32_t dNumSize = (int32_t)(sizeof(doubleNum)/sizeof(double));
  92.  
  93.     UErrorCode status = U_ZERO_ERROR;
  94.     DecimalFormat* fmt = (DecimalFormat*) NumberFormat::createInstance(status);
  95.     fmt->setDecimalSeparatorAlwaysShown(TRUE);
  96.     
  97.     const int32_t tempLen = 20;
  98.     char temp[tempLen];
  99.     
  100.     for (int32_t i=0; i<dNumSize; i++) {
  101.         FieldPosition pos(NumberFormat::INTEGER_FIELD);
  102.         UnicodeString buf;
  103.         //char fmtText[tempLen];
  104.         //ToCharString(fmt->format(doubleNum[i], buf, pos), fmtText);
  105.         UnicodeString res = fmt->format(doubleNum[i], buf, pos);
  106.         for (int32_t j=0; j<tempLen; j++) temp[j] = '='; // clear with spaces
  107.         UTextOffset tempOffset = (tempLen <= (tempLen - pos.getEndIndex())) ? 
  108.             tempLen : (tempLen - pos.getEndIndex());
  109.         temp[tempOffset] = '\0';
  110.         //cout << temp << fmtText   << endl;
  111.         it_out << "FP " << temp << res << endl;
  112.     }
  113.     delete fmt;
  114.     
  115.     it_out << endl;
  116.  
  117. }
  118.  
  119. void test_FieldPosition( void )
  120. {
  121.  
  122.     FieldPosition fp( 7 );
  123.  
  124.     if (fp.getField() == 7) {
  125.         it_out << "FP constructor(int32_t) and getField tested." << endl;
  126.     }else{
  127.         it_errln("*** FP constructor(int32_t) or getField");
  128.     }
  129.  
  130.     FieldPosition* fph = new FieldPosition( 3 );
  131.     if ( fph->getField() != 3) it_errln("*** FP getField or heap constr.");
  132.     delete fph;
  133.  
  134.     bool_t err1 = FALSE;
  135.     bool_t err2 = FALSE;
  136.     bool_t err3 = FALSE;
  137.     for (int32_t i = -50; i < 50; i++ ) {
  138.         fp.setField( i+8 );
  139.         fp.setBeginIndex( i+6 );
  140.         fp.setEndIndex( i+7 );
  141.         if (fp.getField() != i+8)  err1 = TRUE;
  142.         if (fp.getBeginIndex() != i+6) err2 = TRUE;
  143.         if (fp.getEndIndex() != i+7) err3 = TRUE;
  144.     }
  145.     if (!err1) {
  146.         it_out << "FP setField and getField tested." << endl;
  147.     }else{
  148.         it_errln("*** FP setField or getField");
  149.     }
  150.     if (!err2) {
  151.         it_out << "FP setBeginIndex and getBeginIndex tested." << endl;
  152.     }else{
  153.         it_errln("*** FP setBeginIndex or getBeginIndex");
  154.     }
  155.     if (!err3) {
  156.         it_out << "FP setEndIndex and getEndIndex tested." << endl;
  157.     }else{
  158.         it_errln("*** FP setEndIndex or getEndIndex");
  159.     }
  160.  
  161.     it_out << endl;
  162.  
  163. }
  164.  
  165. void test_Formattable( void )
  166. {
  167.     UErrorCode status = U_ZERO_ERROR;
  168.  
  169.     Formattable* ftp = new Formattable();
  170.     if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
  171.         it_errln("*** Formattable constructor or getType or getLong");
  172.     }
  173.     delete ftp;
  174.  
  175.     Formattable fta, ftb;
  176.     fta.setLong(1); ftb.setLong(2);
  177.     if ((fta != ftb) || !(fta == ftb)) {
  178.         it_out << "FT setLong, operator== and operator!= tested." << endl;
  179.     }else{
  180.         it_errln("*** Formattable setLong or operator== or !=");
  181.     }
  182.     fta = ftb;
  183.     if ((fta == ftb) || !(fta != ftb)) {
  184.         it_out << "FT operator= tested." << endl;
  185.     }else{
  186.         it_errln("*** FT operator= or operator== or operator!=");
  187.     }
  188.     
  189.     fta.setDouble( 3.0 );
  190.     if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
  191.         it_out << "FT set- and getDouble tested." << endl;
  192.     }else{
  193.         it_errln("*** FT set- or getDouble");
  194.     }
  195.  
  196.     fta.setDate( 4.0 );
  197.     if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
  198.         it_out << "FT set- and getDate tested." << endl;
  199.     }else{
  200.         it_errln("*** FT set- or getDate");
  201.     }
  202.  
  203.     fta.setString("abc");
  204.     UnicodeString res;
  205.     if ((fta.getType() == Formattable::kString) && (fta.getString(res) == "abc")) {
  206.         it_out << "FT set- and getString tested." << endl;
  207.     }else{
  208.         it_errln("*** FT set- or getString");
  209.     }
  210.  
  211.  
  212.     UnicodeString ucs = "unicode-string";
  213.     UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");
  214.  
  215.     const Formattable ftarray[] = 
  216.     {
  217.         Formattable( 1.0, Formattable::kIsDate ),
  218.         2.0,
  219.         T_INT32(3),
  220.         ucs,
  221.         ucs_ptr
  222.     };
  223.     const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable);
  224.     Formattable ft_arr( ftarray, ft_cnt );
  225.     UnicodeString temp;
  226.     if ((ft_arr[0].getType() == Formattable::kDate)   && (ft_arr[0].getDate()   == 1.0)
  227.      && (ft_arr[1].getType() == Formattable::kDouble) && (ft_arr[1].getDouble() == 2.0)
  228.      && (ft_arr[2].getType() == Formattable::kLong)   && (ft_arr[2].getLong()   == T_INT32(3))
  229.      && (ft_arr[3].getType() == Formattable::kString) && (ft_arr[3].getString(temp) == ucs)
  230.      && (ft_arr[4].getType() == Formattable::kString) && (ft_arr[4].getString(temp) == *ucs_ptr) ) {
  231.         it_out << "FT constr. for date, double, long, ustring, ustring* and array tested" << endl;
  232.     }else{
  233.         it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
  234.     }
  235.  
  236.     int32_t res_cnt;
  237.     const Formattable* res_array = ft_arr.getArray( res_cnt );
  238.     if (res_cnt == ft_cnt) {
  239.         bool_t same  = TRUE;
  240.         for (int32_t i = 0; i < res_cnt; i++ ) {
  241.             if (res_array[i] != ftarray[i]) {
  242.                 same = FALSE;
  243.             }
  244.         }
  245.         if (same) {
  246.             it_out << "FT getArray tested" << endl;
  247.         }else{
  248.             it_errln("*** FT getArray comparison");
  249.         }
  250.     }else{
  251.         it_out << res_cnt << " " << ft_cnt << endl;
  252.         it_errln("*** FT getArray count");
  253.     }
  254.  
  255.     const Formattable ftarr1[] = { Formattable( T_INT32(1) ), Formattable( T_INT32(2) ) };
  256.     const Formattable ftarr2[] = { Formattable( T_INT32(3) ), Formattable( T_INT32(4) ) };
  257.  
  258.     const int32_t ftarr1_cnt = sizeof(ftarr1) / sizeof(Formattable);
  259.     const int32_t ftarr2_cnt = sizeof(ftarr2) / sizeof(Formattable);
  260.  
  261.     ft_arr.setArray( ftarr1, ftarr1_cnt );
  262.     if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == T_INT32(1))) {
  263.         it_out << "FT setArray tested" << endl;
  264.     }else{
  265.         it_errln("*** FT setArray");
  266.     }
  267.  
  268.     Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
  269.     for (int32_t i = 0; i < ftarr2_cnt; i++ ) {
  270.         ft_dynarr[i] = ftarr2[i];
  271.     }
  272.     if ((ft_dynarr[0].getType() == Formattable::kLong) && (ft_dynarr[0].getLong() == T_INT32(3))
  273.      && (ft_dynarr[1].getType() == Formattable::kLong) && (ft_dynarr[1].getLong() == T_INT32(4))) {
  274.         it_out << "FT operator= and array operations tested" << endl;
  275.     }else{
  276.         it_errln("*** FT operator= or array operations");
  277.     }
  278.  
  279.     ft_arr.adoptArray( ft_dynarr, ftarr2_cnt );
  280.     if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == T_INT32(3))
  281.      && (ft_arr[1].getType() == Formattable::kLong) && (ft_arr[1].getLong() == T_INT32(4))) {
  282.         it_out << "FT adoptArray tested" << endl;
  283.     }else{
  284.         it_errln("*** FT adoptArray or operator[]");
  285.     }
  286.  
  287.     ft_arr.setLong(0);   // calls 'dispose' and deletes adopted array !
  288.  
  289.     UnicodeString* ucs_dyn = new UnicodeString("ttt");
  290.     UnicodeString tmp2;
  291.  
  292.     fta.adoptString( ucs_dyn );
  293.     if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
  294.         it_out << "FT adoptString tested" << endl;
  295.     }else{
  296.         it_errln("*** FT adoptString or getString");
  297.     }
  298.     fta.setLong(0);   // calls 'dispose' and deletes adopted string !
  299.  
  300.     it_out << endl;
  301.  
  302. }
  303.  
  304. void TestFormatSmallClasses::runIndexedTest( int32_t index, bool_t exec, char* &name, char* par )
  305. {
  306.     switch (index) {
  307.         case 0: name = "pp"; 
  308.                 if (exec) logln("TestSuite Format/SmallClasses/ParsePosition (f/chc/sma/pp): ");
  309.                 if (exec) test_ParsePosition(); 
  310.                 break;
  311.         case 1: name = "fp"; 
  312.                 if (exec) logln("TestSuite Format/SmallClasses/FieldPosition (f/chc/sma/fp): ");
  313.                 if (exec) test_FieldPosition(); 
  314.                 break;
  315.         case 2: name = "fpe"; 
  316.                 if (exec) logln("TestSuite Format/SmallClasses/FieldPositionExample (f/chc/sma/fpe): ");
  317.                 if (exec) test_FieldPosition_example(); 
  318.                 break;
  319.         case 3: name = "ft"; 
  320.                 if (exec) logln("TestSuite Format/SmallClasses/Formattable (f/chc/sma/ft): ");
  321.                 if (exec) test_Formattable(); 
  322.                 break;
  323.         default: name = ""; break; //needed to end loop
  324.     }
  325. }
  326.