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