void TestMessageFormat::testFormat()
{
    UErrorCode err = U_ZERO_ERROR;
    GregorianCalendar cal(err);   

    const Formattable ftarray[] = 
    {
        Formattable( UDate(8.71068e+011), Formattable::kIsDate )
    };
    const int32_t ft_cnt = sizeof(ftarray) / sizeof(Formattable);
    Formattable ft_arr( ftarray, ft_cnt );

    Formattable* fmt = new Formattable(UDate(8.71068e+011), Formattable::kIsDate);
   
    UnicodeString result;

    //UnicodeString formatStr = "At {1,time} on {1,date}, you made a {2} of {0,number,currency}.";
    UnicodeString formatStr = "On {0,date}, it began.";
    UnicodeString compareStr = "On Aug 8, 1997, it began.";

    err = U_ZERO_ERROR;
    MessageFormat msg( formatStr, err);
    FieldPosition fp(0);

    result = "";
    fp = 0;
    result = msg.format(
        *fmt,
        result,
        //FieldPosition(0),
        fp,
        err);

    if (err != U_ILLEGAL_ARGUMENT_ERROR) {
        errln("*** MSG format without expected error code.");
    }
    err = U_ZERO_ERROR;

    result = "";
    fp = 0;
    result = msg.format(
        ft_arr,
        result,
        //FieldPosition(0),
        fp,
        err);

    logln("MSG format( Formattable&, ... ) expected:" + compareStr);
    logln("MSG format( Formattable&, ... )   result:" + result);
    if (result != compareStr) {
        errln("***  MSG format( Formattable&, .... ) err.");
    }else{
        logln("MSG format( Formattable&, ... ) tested.");
    }

    delete fmt;

}
Пример #2
0
void test_Formattable( void )
{
    UErrorCode status = U_ZERO_ERROR;
    Formattable* ftp = new Formattable();
    if (!ftp || !(ftp->getType() == Formattable::kLong) || !(ftp->getLong() == 0)) {
        it_errln("*** Formattable constructor or getType or getLong");
    }
    delete ftp;

    Formattable fta, ftb;
    fta.setLong(1); ftb.setLong(2);
    if ((fta != ftb) || !(fta == ftb)) {
        it_logln("FT setLong, operator== and operator!= tested.");
        status = U_ZERO_ERROR;
        fta.getLong(&status);
        if ( status == U_INVALID_FORMAT_ERROR){
            it_errln("*** FT getLong(UErrorCode* status) failed on real Long");
        } else {
            it_logln("FT getLong(UErrorCode* status) tested.");
        }
    }else{
        it_errln("*** Formattable setLong or operator== or !=");
    }
    fta = ftb;
    if ((fta == ftb) || !(fta != ftb)) {
        it_logln("FT operator= tested.");
    }else{
        it_errln("*** FT operator= or operator== or operator!=");
    }
    
    fta.setDouble( 3.0 );
    if ((fta.getType() == Formattable::kDouble) && (fta.getDouble() == 3.0)) {
        it_logln("FT set- and getDouble tested.");
    }else{
        it_errln("*** FT set- or getDouble");
    }
    
    fta.getDate(status = U_ZERO_ERROR);
    if (status != U_INVALID_FORMAT_ERROR){
        it_errln("*** FT getDate with status should fail on non-Date");
    }
    fta.setDate( 4.0 );
    if ((fta.getType() == Formattable::kDate) && (fta.getDate() == 4.0)) {
        it_logln("FT set- and getDate tested.");	  
        status = U_ZERO_ERROR;
        fta.getDate(status);
        if ( status == U_INVALID_FORMAT_ERROR){
            it_errln("*** FT getDate with status failed on real Date");
        } else {
            it_logln("FT getDate with status tested.");
        }
    }else{
        it_errln("*** FT set- or getDate");
    }

    status = U_ZERO_ERROR;
    fta.getLong(&status);
    if (status != U_INVALID_FORMAT_ERROR){
        it_errln("*** FT getLong(UErrorCode* status) should fail on non-Long");
    }

    fta.setString("abc");
    const Formattable ftc(fta);
    UnicodeString res;

    {
        UBool t;
        t = (fta.getType() == Formattable::kString) 
            && (fta.getString(res) == "abc")
            && (fta.getString() == "abc");
        res = fta.getString(status = U_ZERO_ERROR);
        t = t && (status != U_INVALID_FORMAT_ERROR && res == "abc");
        res = ftc.getString(status = U_ZERO_ERROR);
        t = t && (status != U_INVALID_FORMAT_ERROR && res == "abc");
        ftc.getString(res,status = U_ZERO_ERROR);
        t = t && (status != U_INVALID_FORMAT_ERROR && res == "abc"); 
        if (t) {
            it_logln("FT set- and getString tested.");
        }else{
            it_errln("*** FT set- or getString");
        }
    }

    UnicodeString ucs = "unicode-string";
    UnicodeString* ucs_ptr = new UnicodeString("pointed-to-unicode-string");

    const Formattable ftarray[] = 
    {
        Formattable( 1.0, Formattable::kIsDate ),
        2.0,
        (int32_t)3,
        ucs,
        ucs_ptr
    };
    const int32_t ft_cnt = UPRV_LENGTHOF(ftarray);
    Formattable ft_arr( ftarray, ft_cnt );
    UnicodeString temp;
    if ((ft_arr[0].getType() == Formattable::kDate)   && (ft_arr[0].getDate()   == 1.0)
     && (ft_arr[1].getType() == Formattable::kDouble) && (ft_arr[1].getDouble() == 2.0)
     && (ft_arr[2].getType() == Formattable::kLong)   && (ft_arr[2].getLong()   == (int32_t)3)
     && (ft_arr[3].getType() == Formattable::kString) && (ft_arr[3].getString(temp) == ucs)
     && (ft_arr[4].getType() == Formattable::kString) && (ft_arr[4].getString(temp) == *ucs_ptr) ) {
        it_logln("FT constr. for date, double, long, ustring, ustring* and array tested");
    }else{
        it_errln("*** FT constr. for date, double, long, ustring, ustring* or array");
    }

    int32_t i, res_cnt;
    const Formattable* res_array = ft_arr.getArray( res_cnt );
    if (res_cnt == ft_cnt) {
        UBool same  = TRUE;
        for (i = 0; i < res_cnt; i++ ) {
            if (res_array[i] != ftarray[i]) {
                same = FALSE;
            }
        }
        if (same) {
            it_logln("FT getArray tested");
            res_array = ft_arr.getArray( res_cnt, status = U_ZERO_ERROR);
            if (status == U_INVALID_FORMAT_ERROR){
                it_errln("*** FT getArray with status failed on real array");
            } else {
                it_logln("FT getArray with status tested on real array");
            }
        }else{
            it_errln("*** FT getArray comparison");
        }
    }else{
        it_errln(UnicodeString("*** FT getArray count res_cnt=") + res_cnt + UnicodeString("ft_cnt=") + ft_cnt);
    }
    
    res_array = fta.getArray(res_cnt, status = U_ZERO_ERROR);
    if (status == U_INVALID_FORMAT_ERROR){
        if (res_cnt == 0 && res_array == NULL){
            it_logln("FT getArray with status tested on non array");
        } else {
            it_errln("*** FT getArray with status return values are not consistent");
        }
    } else {
        it_errln("*** FT getArray with status should fail on non-array");
    }


    Formattable *pf;
    for(i = 0; i < ft_cnt; ++i) {
        pf = ftarray[i].clone();
        if(pf == (ftarray + i) || *pf != ftarray[i]) {
            it_errln(UnicodeString("Formattable.clone() failed for item ") + i);
        }
        delete pf;
    }

    const Formattable ftarr1[] = { Formattable( (int32_t)1 ), Formattable( (int32_t)2 ) };
    const Formattable ftarr2[] = { Formattable( (int32_t)3 ), Formattable( (int32_t)4 ) };

    const int32_t ftarr1_cnt = (int32_t)(sizeof(ftarr1) / sizeof(Formattable));
    const int32_t ftarr2_cnt = (int32_t)(sizeof(ftarr2) / sizeof(Formattable));

    ft_arr.setArray( ftarr1, ftarr1_cnt );
    if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)1)) {
        it_logln("FT setArray tested");
    }else{
        it_errln("*** FT setArray");
    }

    Formattable* ft_dynarr = new Formattable[ftarr2_cnt];
    for (i = 0; i < ftarr2_cnt; i++ ) {
        ft_dynarr[i] = ftarr2[i];
    }
    if ((ft_dynarr[0].getType() == Formattable::kLong) && (ft_dynarr[0].getLong() == (int32_t)3)
     && (ft_dynarr[1].getType() == Formattable::kLong) && (ft_dynarr[1].getLong() == (int32_t)4)) {
        it_logln("FT operator= and array operations tested");
    }else{
        it_errln("*** FT operator= or array operations");
    }

    ft_arr.adoptArray( ft_dynarr, ftarr2_cnt );
    if ((ft_arr[0].getType() == Formattable::kLong) && (ft_arr[0].getLong() == (int32_t)3)
     && (ft_arr[1].getType() == Formattable::kLong) && (ft_arr[1].getLong() == (int32_t)4)) {
        it_logln("FT adoptArray tested");
    }else{
        it_errln("*** FT adoptArray or operator[]");
    }

    ft_arr.setLong(0);   // calls 'dispose' and deletes adopted array !

    UnicodeString* ucs_dyn = new UnicodeString("ttt");
    UnicodeString tmp2;

    fta.adoptString( ucs_dyn );
    if ((fta.getType() == Formattable::kString) && (fta.getString(tmp2) == "ttt")) {
        it_logln("FT adoptString tested");
    }else{
        it_errln("*** FT adoptString or getString");
    }
    fta.setLong(0);   // calls 'dispose' and deletes adopted string !

    it_logln();
}