int32_t FieldsSet::parseFrom(const UnicodeString& str, const FieldsSet* inheritFrom, UErrorCode& status) { int goodFields = 0; if(U_FAILURE(status)) { return -1; } int32_t destCount = 0; UnicodeString *dest = split(str, 0x002C /* ',' */, destCount); for(int i = 0; i < destCount; i += 1) { int32_t dc = 0; UnicodeString *kv = split(dest[i], 0x003D /* '=' */, dc); if(dc != 2) { it_errln(UnicodeString("dc == ") + dc + UnicodeString("?")); } int32_t field = handleParseName(inheritFrom, kv[0], kv[1], status); if(U_FAILURE(status)) { char ch[256]; const UChar *u = kv[0].getBuffer(); int32_t len = kv[0].length(); u_UCharsToChars(u, ch, len); ch[len] = 0; /* include terminating \0 */ it_errln(UnicodeString("Parse Failed: Field ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status))); return -1; } if(field != -1) { handleParseValue(inheritFrom, field, kv[1], status); if(U_FAILURE(status)) { char ch[256]; const UChar *u = kv[1].getBuffer(); int32_t len = kv[1].length(); u_UCharsToChars(u, ch, len); ch[len] = 0; /* include terminating \0 */ it_errln(UnicodeString("Parse Failed: Value ") + UnicodeString(ch) + UnicodeString(", err ") + UnicodeString(u_errorName(status))); return -1; } goodFields += 1; } delete[] kv; } delete[] dest; return goodFields; }
void test_ParsePosition( void ) { ParsePosition* pp1 = new ParsePosition(); if (pp1 && (pp1->getIndex() == 0)) { it_logln("PP constructor() tested."); }else{ it_errln("*** PP getIndex or constructor() result"); } delete pp1; { int32_t to = 5; ParsePosition pp2( to ); if (pp2.getIndex() == 5) { it_logln("PP getIndex and constructor(int32_t) tested."); }else{ it_errln("*** PP getIndex or constructor(int32_t) result"); } pp2.setIndex( 3 ); if (pp2.getIndex() == 3) { it_logln("PP setIndex tested."); }else{ it_errln("*** PP getIndex or setIndex result"); } } ParsePosition pp2, pp3; pp2 = 3; pp3 = 5; ParsePosition pp4( pp3 ); if ((pp2 != pp3) && (pp3 == pp4)) { it_logln("PP copy contructor, operator== and operator != tested."); }else{ it_errln("*** PP operator== or operator != result"); } ParsePosition pp5; pp5 = pp4; if ((pp4 == pp5) && (!(pp4 != pp5))) { it_logln("PP operator= tested."); }else{ it_errln("*** PP operator= operator== or operator != result"); } }
void test_FieldPosition( void ) { FieldPosition fp( 7 ); if (fp.getField() == 7) { it_logln("FP constructor(int32_t) and getField tested."); }else{ it_errln("*** FP constructor(int32_t) or getField"); } FieldPosition* fph = new FieldPosition( 3 ); if ( fph->getField() != 3) it_errln("*** FP getField or heap constr."); delete fph; UBool err1 = FALSE; UBool err2 = FALSE; UBool err3 = FALSE; for (int32_t i = -50; i < 50; i++ ) { fp.setField( i+8 ); fp.setBeginIndex( i+6 ); fp.setEndIndex( i+7 ); if (fp.getField() != i+8) err1 = TRUE; if (fp.getBeginIndex() != i+6) err2 = TRUE; if (fp.getEndIndex() != i+7) err3 = TRUE; } if (!err1) { it_logln("FP setField and getField tested."); }else{ it_errln("*** FP setField or getField"); } if (!err2) { it_logln("FP setBeginIndex and getBeginIndex tested."); }else{ it_errln("*** FP setBeginIndex or getBeginIndex"); } if (!err3) { it_logln("FP setEndIndex and getEndIndex tested."); }else{ it_errln("*** FP setEndIndex or getEndIndex"); } it_logln(""); }
void FieldsSet::parseValueDefault(const FieldsSet* inheritFrom, int32_t field, const UnicodeString& substr, UErrorCode& status) { int32_t value = -1; if(substr.length()==0) { // inherit requested // inherit if((inheritFrom == NULL) || !inheritFrom->isSet((UCalendarDateFields)field)) { // couldn't inherit from field it_errln(UnicodeString("Parse Failed: Couldn't inherit field ") + field + UnicodeString(" [") + UnicodeString(udbg_enumName(fEnum, field)) + UnicodeString("]")); status = U_ILLEGAL_ARGUMENT_ERROR; return; } value = inheritFrom->get((UCalendarDateFields)field); } else { value = udbg_stoi(substr); } set(field, value); }
static UBool chkstatus( UErrorCode &status, const char* msg = NULL ) { UBool ok = U_SUCCESS(status); if (!ok) it_errln( msg ); return ok; }
void TestChoiceFormat::TestComplexExample( void ) { UErrorCode status = U_ZERO_ERROR; const double filelimits[] = {-1, 0,1,2}; const UnicodeString filepart[] = {"are corrupted files", "are no files","is one file","are {2} files"}; ChoiceFormat* fileform = new ChoiceFormat( filelimits, filepart, 4); if (!fileform) { it_errln("*** test_complex_example fileform"); return; } Format* filenumform = NumberFormat::createInstance( status ); if (!filenumform) { dataerrln((UnicodeString)"*** test_complex_example filenumform - " + u_errorName(status)); delete fileform; return; } if (!chkstatus( status, "*** test_simple_example filenumform" )) { delete fileform; delete filenumform; return; } //const Format* testFormats[] = { fileform, NULL, filenumform }; //pattform->setFormats( testFormats, 3 ); MessageFormat* pattform = new MessageFormat("There {0} on {1}", status ); if (!pattform) { it_errln("*** test_complex_example pattform"); delete fileform; delete filenumform; return; } if (!chkstatus( status, "*** test_complex_example pattform" )) { delete fileform; delete filenumform; delete pattform; return; } pattform->setFormat( 0, *fileform ); pattform->setFormat( 2, *filenumform ); Formattable testArgs[] = {(int32_t)0, "Disk_A", (int32_t)0}; UnicodeString str; UnicodeString res1, res2; pattform->toPattern( res1 ); it_logln("MessageFormat toPattern: " + res1); fileform->toPattern( res1 ); it_logln("ChoiceFormat toPattern: " + res1); if (res1 == "-1#are corrupted files|0#are no files|1#is one file|2#are {2} files") { it_logln("toPattern tested!"); }else{ it_errln("*** ChoiceFormat to Pattern result!"); } FieldPosition fpos(FieldPosition::DONT_CARE); UnicodeString checkstr[] = { "There are corrupted files on Disk_A", "There are no files on Disk_A", "There is one file on Disk_A", "There are 2 files on Disk_A", "There are 3 files on Disk_A" }; // if (status != U_ZERO_ERROR) return; // TODO: analyze why we have such a bad bail out here! if (U_FAILURE(status)) { delete fileform; delete filenumform; delete pattform; return; } int32_t i; int32_t start = -1; for (i = start; i < 4; ++i) { str = ""; status = U_ZERO_ERROR; testArgs[0] = Formattable((int32_t)i); testArgs[2] = testArgs[0]; res2 = pattform->format(testArgs, 3, str, fpos, status ); if (!chkstatus( status, "*** test_complex_example format" )) { delete fileform; delete filenumform; delete pattform; return; } it_logln(i + UnicodeString(" -> ") + res2); if (res2 != checkstr[i - start]) { it_errln("*** test_complex_example res string"); it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 + UnicodeString("' unlike '") + checkstr[i] + UnicodeString("' ! ")); } } it_logln(); it_logln("------ additional testing in complex test ------"); it_logln(); // #if 0 // ICU 4.8 deprecates and disables the ChoiceFormat getters. int32_t retCount; const double* retLimits = fileform->getLimits( retCount ); if ((retCount == 4) && (retLimits) && (retLimits[0] == -1.0) && (retLimits[1] == 0.0) && (retLimits[2] == 1.0) && (retLimits[3] == 2.0)) { it_logln("getLimits tested!"); }else{ it_errln("*** getLimits unexpected result!"); } const UnicodeString* retFormats = fileform->getFormats( retCount ); if ((retCount == 4) && (retFormats) && (retFormats[0] == "are corrupted files") && (retFormats[1] == "are no files") && (retFormats[2] == "is one file") && (retFormats[3] == "are {2} files")) { it_logln("getFormats tested!"); }else{ it_errln("*** getFormats unexpected result!"); } #endif UnicodeString checkstr2[] = { "There is no folder on Disk_A", "There is one folder on Disk_A", "There are many folders on Disk_A", "There are many folders on Disk_A" }; fileform->applyPattern("0#is no folder|1#is one folder|2#are many folders", status ); if (status == U_ZERO_ERROR) it_logln("status applyPattern OK!"); if (!chkstatus( status, "*** test_complex_example pattform" )) { delete fileform; delete filenumform; delete pattform; return; } pattform->setFormat( 0, *fileform ); fpos = 0; for (i = 0; i < 4; ++i) { str = ""; status = U_ZERO_ERROR; testArgs[0] = Formattable((int32_t)i); testArgs[2] = testArgs[0]; res2 = pattform->format(testArgs, 3, str, fpos, status ); if (!chkstatus( status, "*** test_complex_example format 2" )) { delete fileform; delete filenumform; delete pattform; return; } it_logln(UnicodeString() + i + UnicodeString(" -> ") + res2); if (res2 != checkstr2[i]) { it_errln("*** test_complex_example res string"); it_errln(UnicodeString("*** ") + i + UnicodeString(" -> '") + res2 + UnicodeString("' unlike '") + checkstr2[i] + UnicodeString("' ! ")); } } const double limits_A[] = {1,2,3,4,5,6,7}; const UnicodeString monthNames_A[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat"}; ChoiceFormat* form_A = new ChoiceFormat(limits_A, monthNames_A, 7); ChoiceFormat* form_A2 = new ChoiceFormat(limits_A, monthNames_A, 7); const double limits_B[] = {1,2,3,4,5,6,7}; const UnicodeString monthNames_B[] = {"Sun","Mon","Tue","Wed","Thur","Fri","Sat_BBB"}; ChoiceFormat* form_B = new ChoiceFormat(limits_B, monthNames_B, 7); if (!form_A || !form_B || !form_A2) { it_errln("*** test-choiceFormat not allocatable!"); }else{ if (*form_A == *form_A2) { it_logln("operator== tested."); }else{ it_errln("*** operator=="); } if (*form_A != *form_B) { it_logln("operator!= tested."); }else{ it_errln("*** operator!="); } ChoiceFormat* form_A3 = (ChoiceFormat*) form_A->clone(); if (!form_A3) { it_errln("*** ChoiceFormat->clone is nil."); }else{ if ((*form_A3 == *form_A) && (*form_A3 != *form_B)) { it_logln("method clone tested."); }else{ it_errln("*** ChoiceFormat clone or operator==, or operator!= ."); } } ChoiceFormat form_Assigned( *form_A ); UBool ok = (form_Assigned == *form_A) && (form_Assigned != *form_B); form_Assigned = *form_B; ok = ok && (form_Assigned != *form_A) && (form_Assigned == *form_B); if (ok) { it_logln("copy constructor and operator= tested."); }else{ it_errln("*** copy constructor or operator= or operator == or operator != ."); } delete form_A3; } delete form_A; delete form_A2; delete form_B; const char* testPattern = "0#none|1#one|2#many"; ChoiceFormat form_pat( testPattern, status ); if (!chkstatus( status, "*** ChoiceFormat contructor( newPattern, status)" )) { delete fileform; delete filenumform; delete pattform; return; } form_pat.toPattern( res1 ); if (res1 == "0#none|1#one|2#many") { it_logln("ChoiceFormat contructor( newPattern, status) tested"); }else{ it_errln("*** ChoiceFormat contructor( newPattern, status) or toPattern result!"); } double d_a2[] = { 3.0, 4.0 }; UnicodeString s_a2[] = { "third", "forth" }; form_pat.setChoices( d_a2, s_a2, 2 ); form_pat.toPattern( res1 ); it_logln(UnicodeString("ChoiceFormat adoptChoices toPattern: ") + res1); if (res1 == "3#third|4#forth") { it_logln("ChoiceFormat adoptChoices tested"); }else{ it_errln("*** ChoiceFormat adoptChoices result!"); } str = ""; fpos = 0; status = U_ZERO_ERROR; double arg_double = 3.0; res1 = form_pat.format( arg_double, str, fpos ); it_logln(UnicodeString("ChoiceFormat format:") + res1); if (res1 != "third") it_errln("*** ChoiceFormat format (double, ...) result!"); str = ""; fpos = 0; status = U_ZERO_ERROR; int64_t arg_64 = 3; res1 = form_pat.format( arg_64, str, fpos ); it_logln(UnicodeString("ChoiceFormat format:") + res1); if (res1 != "third") it_errln("*** ChoiceFormat format (int64_t, ...) result!"); str = ""; fpos = 0; status = U_ZERO_ERROR; int32_t arg_long = 3; res1 = form_pat.format( arg_long, str, fpos ); it_logln(UnicodeString("ChoiceFormat format:") + res1); if (res1 != "third") it_errln("*** ChoiceFormat format (int32_t, ...) result!"); Formattable ft( (int32_t)3 ); str = ""; fpos = 0; status = U_ZERO_ERROR; res1 = form_pat.format( ft, str, fpos, status ); if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" )) { delete fileform; delete filenumform; delete pattform; return; } it_logln(UnicodeString("ChoiceFormat format:") + res1); if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable, ...) result!"); Formattable fta[] = { (int32_t)3 }; str = ""; fpos = 0; status = U_ZERO_ERROR; res1 = form_pat.format( fta, 1, str, fpos, status ); if (!chkstatus( status, "*** test_complex_example format (int32_t, ...)" )) { delete fileform; delete filenumform; delete pattform; return; } it_logln(UnicodeString("ChoiceFormat format:") + res1); if (res1 != "third") it_errln("*** ChoiceFormat format (Formattable[], cnt, ...) result!"); ParsePosition parse_pos = 0; Formattable result; UnicodeString parsetext("third"); form_pat.parse( parsetext, result, parse_pos ); double rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble(); if (rd == 3.0) { it_logln("parse( ..., ParsePos ) tested."); }else{ it_errln("*** ChoiceFormat parse( ..., ParsePos )!"); } form_pat.parse( parsetext, result, status ); rd = (result.getType() == Formattable::kLong) ? result.getLong() : result.getDouble(); if (rd == 3.0) { it_logln("parse( ..., UErrorCode ) tested."); }else{ it_errln("*** ChoiceFormat parse( ..., UErrorCode )!"); } /* UClassID classID = ChoiceFormat::getStaticClassID(); if (classID == form_pat.getDynamicClassID()) { it_out << "getStaticClassID and getDynamicClassID tested." << endl; }else{ it_errln("*** getStaticClassID and getDynamicClassID!"); } */ it_logln(); delete fileform; delete filenumform; delete pattform; }
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(); }