UDate DateFormatRoundTripTest::generateDate() { double a = randFraction(); // Now 'a' ranges from 0..1; scale it to range from 0 to 8000 years a *= 8000; // Range from (4000-1970) BC to (8000-1970) AD a -= 4000; // Now scale up to ms a *= 365.25 * 24 * 60 * 60 * 1000; //return new Date((long)a); return a; }
UDate DateFormatRoundTripTest::generateDate(UDate minDate) { // Bring range in conformance to generateDate() below. if(minDate < (U_MILLIS_PER_YEAR * -(4000-1970))) { minDate = (U_MILLIS_PER_YEAR * -(4000-1970)); } for(int i=0;i<8;i++) { double a = randFraction(); // Range from (min) to (8000-1970) AD double dateRange = (0.0 - minDate) + (U_MILLIS_PER_YEAR + (8000-1970)); a *= dateRange; // Now offset from minDate a += minDate; // Last sanity check if(a>=minDate) { return a; } } return minDate; }
/** * Return a random value from -range..+range. */ double NumberFormatRoundTripTest::randomDouble(double range) { double a = randFraction(); return (2.0 * range * a) - range; }
void DateFormatRoundTripTest::test(const Locale& loc) { UnicodeString temp; #if !INFINITE logln("Locale: " + loc.getDisplayName(temp)); #endif // Total possibilities = 24 // 4 date // 4 time // 16 date-time UBool TEST_TABLE [24];//= new boolean[24]; int32_t i = 0; for(i = 0; i < 24; ++i) TEST_TABLE[i] = TRUE; // If we have some sparseness, implement it here. Sparseness decreases // test time by eliminating some tests, up to 23. for(i = 0; i < SPARSENESS; ) { int random = (int)(randFraction() * 24); if (random >= 0 && random < 24 && TEST_TABLE[i]) { TEST_TABLE[i] = FALSE; ++i; } } int32_t itable = 0; int32_t style = 0; for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { if(TEST_TABLE[itable++]) { logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); DateFormat *df = DateFormat::createDateInstance((DateFormat::EStyle)style, loc); if(df == NULL) { errln(UnicodeString("Could not DF::createDateInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); } else { test(df, loc); delete df; } } } for(style = DateFormat::FULL; style <= DateFormat::SHORT; ++style) { if (TEST_TABLE[itable++]) { logln("Testing style " + UnicodeString(styleName((DateFormat::EStyle)style))); DateFormat *df = DateFormat::createTimeInstance((DateFormat::EStyle)style, loc); if(df == NULL) { errln(UnicodeString("Could not DF::createTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)style)) + " Locale: " + loc.getDisplayName(temp)); } else { test(df, loc, TRUE); delete df; } } } for(int32_t dstyle = DateFormat::FULL; dstyle <= DateFormat::SHORT; ++dstyle) { for(int32_t tstyle = DateFormat::FULL; tstyle <= DateFormat::SHORT; ++tstyle) { if(TEST_TABLE[itable++]) { logln("Testing dstyle" + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) ); DateFormat *df = DateFormat::createDateTimeInstance((DateFormat::EStyle)dstyle, (DateFormat::EStyle)tstyle, loc); if(df == NULL) { dataerrln(UnicodeString("Could not DF::createDateTimeInstance ") + UnicodeString(styleName((DateFormat::EStyle)dstyle)) + ", tstyle" + UnicodeString(styleName((DateFormat::EStyle)tstyle)) + "Locale: " + loc.getDisplayName(temp)); } else { test(df, loc); delete df; } } } } }
void IntlTestNumberFormat::testFormat(/* char* par */) { if (U_FAILURE(fStatus)) { dataerrln((UnicodeString)"**** FAIL: createXxxInstance failed. - " + u_errorName(fStatus)); if (fFormat != 0) errln("**** FAIL: Non-null format returned by createXxxInstance upon failure."); delete fFormat; fFormat = 0; return; } if (fFormat == 0) { errln((UnicodeString)"**** FAIL: Null format returned by createXxxInstance."); return; } UnicodeString str; // Assume it's a DecimalFormat and get some info DecimalFormat *s = (DecimalFormat*)fFormat; logln((UnicodeString)" Pattern " + s->toPattern(str)); #if defined(OS390) || defined(OS400) tryIt(-2.02147304840132e-68); tryIt(3.88057859588817e-68); // Test rounding when only some digits are shown because exponent is close to -maxfrac tryIt(-2.64651110485945e+65); // Overflows to +INF when shown as a percent tryIt(9.29526819488338e+64); // Ok -- used to fail? #else tryIt(-2.02147304840132e-100); tryIt(3.88057859588817e-096); // Test rounding when only some digits are shown because exponent is close to -maxfrac tryIt(-2.64651110485945e+306); // Overflows to +INF when shown as a percent tryIt(9.29526819488338e+250); // Ok -- used to fail? #endif // These PASS now, with the sprintf/atof based format-parse. // These fail due to round-off // The least significant digit drops by one during each format-parse cycle. // Both numbers DON'T have a round-off problem when multiplied by 100! (shown as %) #ifdef OS390 tryIt(-9.18228054496402e+64); tryIt(-9.69413034454191e+64); #else tryIt(-9.18228054496402e+255); tryIt(-9.69413034454191e+273); #endif #ifndef OS390 tryIt(1.234e-200); tryIt(-2.3e-168); tryIt(uprv_getNaN()); tryIt(uprv_getInfinity()); tryIt(-uprv_getInfinity()); #endif tryIt((int32_t)251887531); tryIt(5e-20 / 9); tryIt(5e20 / 9); tryIt(1.234e-50); tryIt(9.99999999999996); tryIt(9.999999999999996); tryIt((int32_t)INT32_MIN); tryIt((int32_t)INT32_MAX); tryIt((double)INT32_MIN); tryIt((double)INT32_MAX); tryIt((double)INT32_MIN - 1.0); tryIt((double)INT32_MAX + 1.0); tryIt(5.0 / 9.0 * 1e-20); tryIt(4.0 / 9.0 * 1e-20); tryIt(5.0 / 9.0 * 1e+20); tryIt(4.0 / 9.0 * 1e+20); tryIt(2147483647.); tryIt((int32_t)0); tryIt(0.0); tryIt((int32_t)1); tryIt((int32_t)10); tryIt((int32_t)100); tryIt((int32_t)-1); tryIt((int32_t)-10); tryIt((int32_t)-100); tryIt((int32_t)-1913860352); for (int32_t z=0; z<10; ++z) { double d = randFraction() * 2e10 - 1e10; tryIt(d); } double it = getSafeDouble(100000.0); tryIt(0.0); tryIt(it); tryIt((int32_t)0); tryIt(uprv_floor(it)); tryIt((int32_t)randLong()); // try again it = getSafeDouble(100.0); tryIt(it); tryIt(uprv_floor(it)); tryIt((int32_t)randLong()); // try again with very large numbers it = getSafeDouble(100000000000.0); tryIt(it); // try again with very large numbers // and without going outside of the int32_t range it = randFraction() * INT32_MAX; tryIt(it); tryIt((int32_t)uprv_floor(it)); delete fFormat; }