From 7619d3574be1965aa105f12a44cfbd77c4696fd3 Mon Sep 17 00:00:00 2001 From: George Rhoten Date: Fri, 30 Nov 2001 22:23:45 +0000 Subject: [PATCH] ICU-104 Fix for HP/UX 11 CC X-SVN-Rev: 7229 --- icu4c/source/test/intltest/itrbnf.cpp | 1258 +++++++++++++------------ icu4c/source/test/intltest/itrbnf.h | 2 + 2 files changed, 642 insertions(+), 618 deletions(-) diff --git a/icu4c/source/test/intltest/itrbnf.cpp b/icu4c/source/test/intltest/itrbnf.cpp index 6ef2b364708..7b604b57ae8 100644 --- a/icu4c/source/test/intltest/itrbnf.cpp +++ b/icu4c/source/test/intltest/itrbnf.cpp @@ -9,6 +9,7 @@ #include "unicode/tblcoll.h" #include "unicode/coleitr.h" #include "unicode/ures.h" +#include "unicode/ustring.h" #include "llong.h" #include @@ -45,7 +46,7 @@ void IntlTestRBNF::runIndexedTest(int32_t index, UBool exec, const char* &name, TESTCASE(8, TestThaiSpellout); TESTCASE(9, TestAPI); TESTCASE(10, TestFractionalRuleSet); - TESTCASE(11, TestLLong); + TESTCASE(11, TestLLong); default: name = ""; break; @@ -59,7 +60,7 @@ IntlTestRBNF::TestAPI() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::US, status); + = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status); logln("RBNF API test starting"); // test clone @@ -99,7 +100,7 @@ IntlTestRBNF::TestAPI() { errln("Unable to access the rules string!"); } else { UParseError perror; - RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::US, perror, status); + RuleBasedNumberFormat ruleCtorResult(spelloutRules, Locale::getUS(), perror, status); if(!(ruleCtorResult == *formatter)) { errln("Formatter constructed from the original rules should be semantically equivalent to the original!"); } @@ -113,7 +114,7 @@ IntlTestRBNF::TestAPI() { logln("Testing getRules function"); UnicodeString rules = formatter->getRules(); UParseError perror; - RuleBasedNumberFormat fromRulesResult(rules, Locale::US, perror, status); + RuleBasedNumberFormat fromRulesResult(rules, Locale::getUS(), perror, status); if(!(fromRulesResult == *formatter)) { errln("Formatter constructed from rules obtained by getRules should be semantically equivalent to the original!"); @@ -230,635 +231,656 @@ void IntlTestRBNF::TestFractionalRuleSet() UErrorCode status = U_ZERO_ERROR; UParseError perror; - RuleBasedNumberFormat formatter(fracRules, Locale::ENGLISH, perror, status); + RuleBasedNumberFormat formatter(fracRules, Locale::getEnglish(), perror, status); if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); } else { static const char* testData[][2] = { - { "0", "0" }, + { "0", "0" }, { ".1", "1/10" }, - { ".11", "1/9" }, + { ".11", "1/9" }, { ".125", "1/8" }, - { ".1428", "1/7" }, - { ".1667", "1/6" }, - { ".2", "1/5" }, - { ".25", "1/4" }, + { ".1428", "1/7" }, + { ".1667", "1/6" }, + { ".2", "1/5" }, + { ".25", "1/4" }, { ".333", "1/3" }, { ".5", "1/2" }, - { "1.1", "1 1/10" }, - { "2.11", "2 1/9" }, + { "1.1", "1 1/10" }, + { "2.11", "2 1/9" }, { "3.125", "3 1/8" }, - { "4.1428", "4 1/7" }, - { "5.1667", "5 1/6" }, - { "6.2", "6 1/5" }, - { "7.25", "7 1/4" }, + { "4.1428", "4 1/7" }, + { "5.1667", "5 1/6" }, + { "6.2", "6 1/5" }, + { "7.25", "7 1/4" }, { "8.333", "8 1/3" }, { "9.5", "9 1/2" }, - { ".2222", "2/9" }, - { ".4444", "4/9" }, - { ".5555", "5/9" }, - { "1.2856", "1 2/7" }, - { NULL, NULL } - }; + { ".2222", "2/9" }, + { ".4444", "4/9" }, + { ".5555", "5/9" }, + { "1.2856", "1 2/7" }, + { NULL, NULL } + }; doTest(&formatter, testData, FALSE); // exact values aren't parsable from fractions - } + } } #define LLAssert(a) \ if (!(a)) errln("FAIL: " #a) -static int ustrcmp(const UChar* lhs, const UChar* rhs) { - while ((*lhs == *rhs) && *lhs) { - ++lhs; - ++rhs; - } - return *lhs - *rhs; +void IntlTestRBNF::TestLLongConstructors() +{ + logln("Testing constructors"); + + // constant (shouldn't really be public) + LLAssert(llong(llong::kD32).asDouble() == llong::kD32); + + // internal constructor (shouldn't really be public) + LLAssert(llong(0, 1).asDouble() == 1); + LLAssert(llong(1, 0).asDouble() == llong::kD32); + LLAssert(llong((uint32_t)-1, (uint32_t)-1).asDouble() == -1); + + // public empty constructor + LLAssert(llong().asDouble() == 0); + + // public int32_t constructor + LLAssert(llong((int32_t)0).asInt() == (int32_t)0); + LLAssert(llong((int32_t)1).asInt() == (int32_t)1); + LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1); + LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff); + LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1); + LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000); + + // public int16_t constructor + LLAssert(llong((int16_t)0).asInt() == (int16_t)0); + LLAssert(llong((int16_t)1).asInt() == (int16_t)1); + LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1); + LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff); + LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff); + LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000); + + // public int8_t constructor + LLAssert(llong((int8_t)0).asInt() == (int8_t)0); + LLAssert(llong((int8_t)1).asInt() == (int8_t)1); + LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1); + LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f); + LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff); + LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80); + + // public uint16_t constructor + LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0); + LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1); + LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1); + LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff); + LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff); + LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000); + + // public uint32_t constructor + LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0); + LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1); + LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1); + LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff); + LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1); + LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000); + + // public double constructor + LLAssert(llong((double)0).asDouble() == (double)0); + LLAssert(llong((double)1).asDouble() == (double)1); + LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff); + LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000); + LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001); + + // can't access uprv_maxmantissa, so fake it + double maxmantissa = (llong((int32_t)1) << 40).asDouble(); + LLAssert(llong(maxmantissa).asDouble() == maxmantissa); + LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa); + + // copy constructor + LLAssert(llong(llong(0, 1)).asDouble() == 1); + LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32); + LLAssert(llong(llong((uint32_t)-1, (uint32_t)-1)).asDouble() == -1); + + // asInt - test unsigned to signed narrowing conversion + LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff); + LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000); + + // asUInt - test signed to unsigned narrowing conversion + LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1); + LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000); + + // asDouble already tested + +} + +void IntlTestRBNF::TestLLongSimpleOperators() +{ + logln("Testing simple operators"); + // operator= + LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1); + + // operator <<= + LLAssert((llong(1, 1) <<= 0) == llong(1, 1)); + LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000)); + LLAssert((llong(1, 1) <<= 32) == llong(1, 0)); + LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0)); + LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used + LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used + + // operator << + LLAssert((llong((int32_t)1) << 5).asUInt() == 32); + + // operator >>= (sign extended) + LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc)); + LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde)); + LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff)); + LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000)); + LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used + LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used + + // operator >> sign extended) + LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde)); + + // ushr (right shift without sign extension) + LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc)); + LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde)); + LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1)); + LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000)); + LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used + LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used + + // operator&(llong) + LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); + + // operator|(llong) + LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); + + // operator^(llong) + LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); + + // operator&(uint32) + LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); + + // operator|(uint32) + LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); + + // operator^(uint32) + LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); + + // operator~ + LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa)); + + // operator&=(llong) + LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); + + // operator|=(llong) + LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); + + // operator^=(llong) + LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); + + // operator&=(uint32) + LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); + + // operator|=(uint32) + LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); + + // operator^=(uint32) + LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); + + // operator== + LLAssert(llong() == llong(0, 0)); + LLAssert(llong(1,0) == llong(1, 0)); + LLAssert(llong(0,1) == llong(0, 1)); + + // operator!= + LLAssert(llong(1,0) != llong(1,1)); + LLAssert(llong(0,1) != llong(1,1)); + LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff)); + + // operator> + LLAssert(llong(1, 1) > llong(1, 0)); + LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff)); + LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0)); + LLAssert(llong(1, 0) > llong(0, 0x7fffffff)); + LLAssert(llong(1, 0) > llong(0, 0xffffffff)); + LLAssert(llong(0, 0) > llong(0x80000000, 1)); + + // operator< + LLAssert(llong(1, 0) < llong(1, 1)); + LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000)); + LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1)); + LLAssert(llong(0, 0x7fffffff) < llong(1, 0)); + LLAssert(llong(0, 0xffffffff) < llong(1, 0)); + LLAssert(llong(0x80000000, 1) < llong(0, 0)); + + // operator>= + LLAssert(llong(1, 1) >= llong(1, 0)); + LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff)); + LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0)); + LLAssert(llong(1, 0) >= llong(0, 0x7fffffff)); + LLAssert(llong(1, 0) >= llong(0, 0xffffffff)); + LLAssert(llong(0, 0) >= llong(0x80000000, 1)); + LLAssert(llong() >= llong(0, 0)); + LLAssert(llong(1,0) >= llong(1, 0)); + LLAssert(llong(0,1) >= llong(0, 1)); + + // operator<= + LLAssert(llong(1, 0) <= llong(1, 1)); + LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000)); + LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1)); + LLAssert(llong(0, 0x7fffffff) <= llong(1, 0)); + LLAssert(llong(0, 0xffffffff) <= llong(1, 0)); + LLAssert(llong(0x80000000, 1) <= llong(0, 0)); + LLAssert(llong() <= llong(0, 0)); + LLAssert(llong(1,0) <= llong(1, 0)); + LLAssert(llong(0,1) <= llong(0, 1)); + + // operator==(int32) + LLAssert(llong() == (int32_t)0); + LLAssert(llong(0,1) == (int32_t)1); + + // operator!=(int32) + LLAssert(llong(1,0) != (int32_t)0); + LLAssert(llong(0,1) != (int32_t)2); + LLAssert(llong(0,0xffffffff) != (int32_t)-1); + + // operator>(int32) + LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff); + LLAssert(llong(0xffffffff, 0xffffffff) > (int32_t)-2); + LLAssert(llong(1, 0) > (int32_t)0x7fffffff); + LLAssert(llong(0, 0) > (int32_t)-1); + + // operator<(int32) + LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff); + LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1); + + // operator>=(int32) + LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff); + LLAssert(llong(0xffffffff, 0xffffffff) >= (int32_t)-2); + LLAssert(llong(1, 0) >= (int32_t)0x7fffffff); + LLAssert(llong(0, 0) >= (int32_t)-1); + LLAssert(llong() >= (int32_t)0); + LLAssert(llong(0,1) >= (int32_t)1); + + // operator<=(int32) + LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff); + LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1); + LLAssert(llong() <= (int32_t)0); + LLAssert(llong(0,1) <= (int32_t)1); + + // unsigned > + LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff))); + + // unsigned < + LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1))); + + // unsigned >= + LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff))); + LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1))); + + // unsigned <= + LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1))); + LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1))); + + // prefix inc + LLAssert(llong(1, 0) == ++llong(0,0xffffffff)); + + // prefix dec + LLAssert(llong(0,0xffffffff) == --llong(1, 0)); + + // postfix inc + { + llong n(0, 0xffffffff); + LLAssert(llong(0, 0xffffffff) == n++); + LLAssert(llong(1, 0) == n); + } + + // postfix dec + { + llong n(1, 0); + LLAssert(llong(1, 0) == n--); + LLAssert(llong(0, 0xffffffff) == n); + } + + // unary minus + LLAssert(llong(0, 0) == -llong(0, 0)); + LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1)); + LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff)); + LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1)); + LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow + + // operator-= + { + llong n; + LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff)); + LLAssert(n == llong(0xffffffff, 0xffffffff)); + + n = llong(1, 0); + LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff)); + LLAssert(n == llong(0, 0xffffffff)); + } + + // operator- + { + llong n; + LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff)); + LLAssert(n == llong(0, 0)); + + n = llong(1, 0); + LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff)); + LLAssert(n == llong(1, 0)); + } + + // operator+= + { + llong n(0xffffffff, 0xffffffff); + LLAssert((n += llong(0, 1)) == llong(0, 0)); + LLAssert(n == llong(0, 0)); + + n = llong(0, 0xffffffff); + LLAssert((n += llong(0, 1)) == llong(1, 0)); + LLAssert(n == llong(1, 0)); + } + + // operator+ + { + llong n(0xffffffff, 0xffffffff); + LLAssert((n + llong(0, 1)) == llong(0, 0)); + LLAssert(n == llong(0xffffffff, 0xffffffff)); + + n = llong(0, 0xffffffff); + LLAssert((n + llong(0, 1)) == llong(1, 0)); + LLAssert(n == llong(0, 0xffffffff)); + } + } void IntlTestRBNF::TestLLong() { - // constant (shouldn't really be public) - LLAssert(llong(llong::kD32).asDouble() == llong::kD32); - - // internal constructor (shouldn't really be public) - LLAssert(llong(0, 1).asDouble() == 1); - LLAssert(llong(1, 0).asDouble() == llong::kD32); - LLAssert(llong(-1, -1).asDouble() == -1); - - // public empty constructor - LLAssert(llong().asDouble() == 0); - - // public int32_t constructor - LLAssert(llong((int32_t)0).asInt() == (int32_t)0); - LLAssert(llong((int32_t)1).asInt() == (int32_t)1); - LLAssert(llong((int32_t)-1).asInt() == (int32_t)-1); - LLAssert(llong((int32_t)0x7fffffff).asInt() == (int32_t)0x7fffffff); - LLAssert(llong((int32_t)0xffffffff).asInt() == (int32_t)-1); - LLAssert(llong((int32_t)0x80000000).asInt() == (int32_t)0x80000000); - - // public int16_t constructor - LLAssert(llong((int16_t)0).asInt() == (int16_t)0); - LLAssert(llong((int16_t)1).asInt() == (int16_t)1); - LLAssert(llong((int16_t)-1).asInt() == (int16_t)-1); - LLAssert(llong((int16_t)0x7fff).asInt() == (int16_t)0x7fff); - LLAssert(llong((int16_t)0xffff).asInt() == (int16_t)0xffff); - LLAssert(llong((int16_t)0x8000).asInt() == (int16_t)0x8000); - - // public int8_t constructor - LLAssert(llong((int8_t)0).asInt() == (int8_t)0); - LLAssert(llong((int8_t)1).asInt() == (int8_t)1); - LLAssert(llong((int8_t)-1).asInt() == (int8_t)-1); - LLAssert(llong((int8_t)0x7f).asInt() == (int8_t)0x7f); - LLAssert(llong((int8_t)0xff).asInt() == (int8_t)0xff); - LLAssert(llong((int8_t)0x80).asInt() == (int8_t)0x80); - - // public uint16_t constructor - LLAssert(llong((uint16_t)0).asUInt() == (uint16_t)0); - LLAssert(llong((uint16_t)1).asUInt() == (uint16_t)1); - LLAssert(llong((uint16_t)-1).asUInt() == (uint16_t)-1); - LLAssert(llong((uint16_t)0x7fff).asUInt() == (uint16_t)0x7fff); - LLAssert(llong((uint16_t)0xffff).asUInt() == (uint16_t)0xffff); - LLAssert(llong((uint16_t)0x8000).asUInt() == (uint16_t)0x8000); - - // public uint32_t constructor - LLAssert(llong((uint32_t)0).asUInt() == (uint32_t)0); - LLAssert(llong((uint32_t)1).asUInt() == (uint32_t)1); - LLAssert(llong((uint32_t)-1).asUInt() == (uint32_t)-1); - LLAssert(llong((uint32_t)0x7fffffff).asUInt() == (uint32_t)0x7fffffff); - LLAssert(llong((uint32_t)0xffffffff).asUInt() == (uint32_t)-1); - LLAssert(llong((uint32_t)0x80000000).asUInt() == (uint32_t)0x80000000); - - // public double constructor - LLAssert(llong((double)0).asDouble() == (double)0); - LLAssert(llong((double)1).asDouble() == (double)1); - LLAssert(llong((double)0x7fffffff).asDouble() == (double)0x7fffffff); - LLAssert(llong((double)0x80000000).asDouble() == (double)0x80000000); - LLAssert(llong((double)0x80000001).asDouble() == (double)0x80000001); - - // can't access uprv_maxmantissa, so fake it - double maxmantissa = (llong((int32_t)1) << 40).asDouble(); - LLAssert(llong(maxmantissa).asDouble() == maxmantissa); - LLAssert(llong(-maxmantissa).asDouble() == -maxmantissa); - - // copy constructor - LLAssert(llong(llong(0, 1)).asDouble() == 1); - LLAssert(llong(llong(1, 0)).asDouble() == llong::kD32); - LLAssert(llong(llong(-1, -1)).asDouble() == -1); - - // asInt - test unsigned to signed narrowing conversion - LLAssert(llong((uint32_t)-1).asInt() == (int32_t)0x7fffffff); - LLAssert(llong(-1, 0).asInt() == (int32_t)0x80000000); - - // asUInt - test signed to unsigned narrowing conversion - LLAssert(llong((int32_t)-1).asUInt() == (uint32_t)-1); - LLAssert(llong((int32_t)0x80000000).asUInt() == (uint32_t)0x80000000); - - // asDouble already tested - - // operator= - LLAssert((llong(2,3) = llong((uint32_t)-1)).asUInt() == (uint32_t)-1); - - // operator <<= - LLAssert((llong(1, 1) <<= 0) == llong(1, 1)); - LLAssert((llong(1, 1) <<= 31) == llong(0x80000000, 0x80000000)); - LLAssert((llong(1, 1) <<= 32) == llong(1, 0)); - LLAssert((llong(1, 1) <<= 63) == llong(0x80000000, 0)); - LLAssert((llong(1, 1) <<= 64) == llong(1, 1)); // only lower 6 bits are used - LLAssert((llong(1, 1) <<= -1) == llong(0x80000000, 0)); // only lower 6 bits are used - - // operator << - LLAssert((llong((int32_t)1) << 5).asUInt() == 32); - - // operator >>= (sign extended) - LLAssert((llong(0x7fffa0a0, 0xbcbcdfdf) >>= 16) == llong(0x7fff,0xa0a0bcbc)); - LLAssert((llong(0x8000789a, 0xbcde0000) >>= 16) == llong(0xffff8000,0x789abcde)); - LLAssert((llong(0x80000000, 0) >>= 63) == llong(0xffffffff, 0xffffffff)); - LLAssert((llong(0x80000000, 0) >>= 47) == llong(0xffffffff, 0xffff0000)); - LLAssert((llong(0x80000000, 0x80000000) >> 64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used - LLAssert((llong(0x80000000, 0) >>= -1) == llong(0xffffffff, 0xffffffff)); // only lower 6 bits are used - - // operator >> sign extended) - LLAssert((llong(0x8000789a, 0xbcde0000) >> 16) == llong(0xffff8000,0x789abcde)); - - // ushr (right shift without sign extension) - LLAssert(llong(0x7fffa0a0, 0xbcbcdfdf).ushr(16) == llong(0x7fff,0xa0a0bcbc)); - LLAssert(llong(0x8000789a, 0xbcde0000).ushr(16) == llong(0x00008000,0x789abcde)); - LLAssert(llong(0x80000000, 0).ushr(63) == llong(0, 1)); - LLAssert(llong(0x80000000, 0).ushr(47) == llong(0, 0x10000)); - LLAssert(llong(0x80000000, 0x80000000).ushr(64) == llong(0x80000000, 0x80000000)); // only lower 6 bits are used - LLAssert(llong(0x80000000, 0).ushr(-1) == llong(0, 1)); // only lower 6 bits are used - - // operator&(llong) - LLAssert((llong(0x55555555, 0x55555555) & llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); - - // operator|(llong) - LLAssert((llong(0x55555555, 0x55555555) | llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); - - // operator^(llong) - LLAssert((llong(0x55555555, 0x55555555) ^ llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); - - // operator&(uint32) - LLAssert((llong(0x55555555, 0x55555555) & (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); - - // operator|(uint32) - LLAssert((llong(0x55555555, 0x55555555) | (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); - - // operator^(uint32) - LLAssert((llong(0x55555555, 0x55555555) ^ (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); - - // operator~ - LLAssert(~llong(0x55555555, 0x55555555) == llong(0xaaaaaaaa, 0xaaaaaaaa)); - - // operator&=(llong) - LLAssert((llong(0x55555555, 0x55555555) &= llong(0xaaaaffff, 0xffffaaaa)) == llong(0x00005555, 0x55550000)); - - // operator|=(llong) - LLAssert((llong(0x55555555, 0x55555555) |= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffffff, 0xffffffff)); - - // operator^=(llong) - LLAssert((llong(0x55555555, 0x55555555) ^= llong(0xaaaaffff, 0xffffaaaa)) == llong(0xffffaaaa, 0xaaaaffff)); - - // operator&=(uint32) - LLAssert((llong(0x55555555, 0x55555555) &= (uint32_t)0xffffaaaa) == llong(0, 0x55550000)); - - // operator|=(uint32) - LLAssert((llong(0x55555555, 0x55555555) |= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xffffffff)); - - // operator^=(uint32) - LLAssert((llong(0x55555555, 0x55555555) ^= (uint32_t)0xffffaaaa) == llong(0x55555555, 0xaaaaffff)); - - // operator== - LLAssert(llong() == llong(0, 0)); - LLAssert(llong(1,0) == llong(1, 0)); - LLAssert(llong(0,1) == llong(0, 1)); - - // operator!= - LLAssert(llong(1,0) != llong(1,1)); - LLAssert(llong(0,1) != llong(1,1)); - LLAssert(llong(0xffffffff,0xffffffff) != llong(0x7fffffff, 0xffffffff)); - - // operator> - LLAssert(llong(1, 1) > llong(1, 0)); - LLAssert(llong(0, 0x80000000) > llong(0, 0x7fffffff)); - LLAssert(llong(0x80000000, 1) > llong(0x80000000, 0)); - LLAssert(llong(1, 0) > llong(0, 0x7fffffff)); - LLAssert(llong(1, 0) > llong(0, 0xffffffff)); - LLAssert(llong(0, 0) > llong(0x80000000, 1)); - - // operator< - LLAssert(llong(1, 0) < llong(1, 1)); - LLAssert(llong(0, 0x7fffffff) < llong(0, 0x80000000)); - LLAssert(llong(0x80000000, 0) < llong(0x80000000, 1)); - LLAssert(llong(0, 0x7fffffff) < llong(1, 0)); - LLAssert(llong(0, 0xffffffff) < llong(1, 0)); - LLAssert(llong(0x80000000, 1) < llong(0, 0)); - - // operator>= - LLAssert(llong(1, 1) >= llong(1, 0)); - LLAssert(llong(0, 0x80000000) >= llong(0, 0x7fffffff)); - LLAssert(llong(0x80000000, 1) >= llong(0x80000000, 0)); - LLAssert(llong(1, 0) >= llong(0, 0x7fffffff)); - LLAssert(llong(1, 0) >= llong(0, 0xffffffff)); - LLAssert(llong(0, 0) >= llong(0x80000000, 1)); - LLAssert(llong() >= llong(0, 0)); - LLAssert(llong(1,0) >= llong(1, 0)); - LLAssert(llong(0,1) >= llong(0, 1)); - - // operator<= - LLAssert(llong(1, 0) <= llong(1, 1)); - LLAssert(llong(0, 0x7fffffff) <= llong(0, 0x80000000)); - LLAssert(llong(0x80000000, 0) <= llong(0x80000000, 1)); - LLAssert(llong(0, 0x7fffffff) <= llong(1, 0)); - LLAssert(llong(0, 0xffffffff) <= llong(1, 0)); - LLAssert(llong(0x80000000, 1) <= llong(0, 0)); - LLAssert(llong() <= llong(0, 0)); - LLAssert(llong(1,0) <= llong(1, 0)); - LLAssert(llong(0,1) <= llong(0, 1)); - - // operator==(int32) - LLAssert(llong() == (int32_t)0); - LLAssert(llong(0,1) == (int32_t)1); - - // operator!=(int32) - LLAssert(llong(1,0) != (int32_t)0); - LLAssert(llong(0,1) != (int32_t)2); - LLAssert(llong(0,0xffffffff) != (int32_t)-1); - - // operator>(int32) - LLAssert(llong(0, 0x80000000) > (int32_t)0x7fffffff); - LLAssert(llong(0xffffffff, 0xffffffff) > (int32_t)-2); - LLAssert(llong(1, 0) > (int32_t)0x7fffffff); - LLAssert(llong(0, 0) > (int32_t)-1); - - // operator<(int32) - LLAssert(llong(0, 0x7ffffffe) < (int32_t)0x7fffffff); - LLAssert(llong(0xffffffff, 0xfffffffe) < (int32_t)-1); - - // operator>=(int32) - LLAssert(llong(0, 0x80000000) >= (int32_t)0x7fffffff); - LLAssert(llong(0xffffffff, 0xffffffff) >= (int32_t)-2); - LLAssert(llong(1, 0) >= (int32_t)0x7fffffff); - LLAssert(llong(0, 0) >= (int32_t)-1); - LLAssert(llong() >= (int32_t)0); - LLAssert(llong(0,1) >= (int32_t)1); - - // operator<=(int32) - LLAssert(llong(0, 0x7ffffffe) <= (int32_t)0x7fffffff); - LLAssert(llong(0xffffffff, 0xfffffffe) <= (int32_t)-1); - LLAssert(llong() <= (int32_t)0); - LLAssert(llong(0,1) <= (int32_t)1); - - // unsigned > - LLAssert(llong((int32_t)-1).ugt(llong(0x7fffffff, 0xffffffff))); - - // unsigned < - LLAssert(llong(0x7fffffff, 0xffffffff).ult(llong((int32_t)-1))); - - // unsigned >= - LLAssert(llong((int32_t)-1).uge(llong(0x7fffffff, 0xffffffff))); - LLAssert(llong((int32_t)-1).uge(llong((int32_t)-1))); - - // unsigned <= - LLAssert(llong(0x7fffffff, 0xffffffff).ule(llong((int32_t)-1))); - LLAssert(llong((int32_t)-1).ule(llong((int32_t)-1))); - - // prefix inc - LLAssert(llong(1, 0) == ++llong(0,0xffffffff)); - - // prefix dec - LLAssert(llong(0,0xffffffff) == --llong(1, 0)); - - // postfix inc - { - llong n(0, 0xffffffff); - LLAssert(llong(0, 0xffffffff) == n++); - LLAssert(llong(1, 0) == n); - } - - // postfix dec - { - llong n(1, 0); - LLAssert(llong(1, 0) == n--); - LLAssert(llong(0, 0xffffffff) == n); - } - - // unary minus - LLAssert(llong(0, 0) == -llong(0, 0)); - LLAssert(llong(0xffffffff, 0xffffffff) == -llong(0, 1)); - LLAssert(llong(0, 1) == -llong(0xffffffff, 0xffffffff)); - LLAssert(llong(0x7fffffff, 0xffffffff) == -llong(0x80000000, 1)); - LLAssert(llong(0x80000000, 0) == -llong(0x80000000, 0)); // !!! we don't handle overflow - - // operator-= - { - llong n; - LLAssert((n -= llong(0, 1)) == llong(0xffffffff, 0xffffffff)); - LLAssert(n == llong(0xffffffff, 0xffffffff)); - - n = llong(1, 0); - LLAssert((n -= llong(0, 1)) == llong(0, 0xffffffff)); - LLAssert(n == llong(0, 0xffffffff)); - } - - // operator- - { - llong n; - LLAssert((n - llong(0, 1)) == llong(0xffffffff, 0xffffffff)); - LLAssert(n == llong(0, 0)); - - n = llong(1, 0); - LLAssert((n - llong(0, 1)) == llong(0, 0xffffffff)); - LLAssert(n == llong(1, 0)); - } - - // operator+= - { - llong n(0xffffffff, 0xffffffff); - LLAssert((n += llong(0, 1)) == llong(0, 0)); - LLAssert(n == llong(0, 0)); - - n = llong(0, 0xffffffff); - LLAssert((n += llong(0, 1)) == llong(1, 0)); - LLAssert(n == llong(1, 0)); - } - - // operator+ - { - llong n(0xffffffff, 0xffffffff); - LLAssert((n + llong(0, 1)) == llong(0, 0)); - LLAssert(n == llong(0xffffffff, 0xffffffff)); - - n = llong(0, 0xffffffff); - LLAssert((n + llong(0, 1)) == llong(1, 0)); - LLAssert(n == llong(0, 0xffffffff)); - } - - // operator*=, operator* - // small and large values, positive, &NEGative, zero - // also test commutivity - { - const llong ZERO; - const llong ONE(0, 1); - const llong NEG_ONE((int32_t)-1); - const llong THREE(0, 3); - const llong NEG_THREE((int32_t)-3); - const llong TWO_TO_16(0, 0x10000); - const llong NEG_TWO_TO_16 = -TWO_TO_16; - const llong TWO_TO_32(1, 0); - const llong NEG_TWO_TO_32 = -TWO_TO_32; - - const llong NINE(0, 9); - const llong NEG_NINE = -NINE; - - const llong TWO_TO_16X3(0, 0x00030000); - const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3; - - const llong TWO_TO_32X3(3, 0); - const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3; - - const llong TWO_TO_48(0x10000, 0); - const llong NEG_TWO_TO_48 = -TWO_TO_48; - - const llong* values[] = { - &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32 - }; - - const llong* answers[][9] = { - { &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO }, - { &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32 }, - { &ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32 }, - { &ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3 }, - { &ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3 }, - { &ZERO, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_48, &NEG_TWO_TO_48 }, - { &ZERO, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_48, &TWO_TO_48 }, - { &ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO }, - { &ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO }, - }; - - for (int i = 0; i < 9; ++i) { - for (int j = 0; j < 9; ++j) { - const llong lhs = *values[i]; - const llong rhs = *values[j]; - const llong ans = *answers[i][j]; - - llong n = lhs; - LLAssert((n *= rhs) == ans); - LLAssert(n == ans); - - n = lhs; - LLAssert((n * rhs) == ans); - LLAssert(n == lhs); - } - } - } - - // operator/=, operator/ - // test num = 0, div = 0, pos/neg, > 2^32, div > num - { - const llong ZERO; - const llong ONE(0, 1); - const llong NEG_ONE = -ONE; - const llong MAX(0x7fffffff, 0xffffffff); - const llong MIN(0x80000000, 0); - const llong TWO(0, 2); - const llong NEG_TWO = -TWO; - const llong FIVE(0, 5); - const llong NEG_FIVE = -FIVE; - const llong TWO_TO_32(1, 0); - const llong NEG_TWO_TO_32 = -TWO_TO_32; - const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0); - const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5; - const llong TWO_TO_32X5 = TWO_TO_32 * FIVE; - const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5; - - const llong* tuples[][3] = { // lhs, rhs, ans - { &ZERO, &ZERO, &ZERO }, - { &ONE, &ZERO,&MAX }, - { &NEG_ONE, &ZERO, &MIN }, - { &ONE, &ONE, &ONE }, - { &ONE, &NEG_ONE, &NEG_ONE }, - { &NEG_ONE, &ONE, &NEG_ONE }, - { &NEG_ONE, &NEG_ONE, &ONE }, - { &FIVE, &TWO, &TWO }, - { &FIVE, &NEG_TWO, &NEG_TWO }, - { &NEG_FIVE, &TWO, &NEG_TWO }, - { &NEG_FIVE, &NEG_TWO, &TWO }, - { &TWO, &FIVE, &ZERO }, - { &TWO, &NEG_FIVE, &ZERO }, - { &NEG_TWO, &FIVE, &ZERO }, - { &NEG_TWO, &NEG_FIVE, &ZERO }, - { &TWO_TO_32, &TWO_TO_32, &ONE }, - { &TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE }, - { &NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE }, - { &NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE }, - { &TWO_TO_32, &FIVE, &TWO_TO_32d5 }, - { &TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5 }, - { &NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5 }, - { &NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5 }, - { &TWO_TO_32X5, &FIVE, &TWO_TO_32 }, - { &TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32 }, - { &NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32 }, - { &NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32 }, - { &TWO_TO_32X5, &TWO_TO_32, &FIVE }, - { &TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE }, - { &NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE }, - { &NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE } - }; - const int TUPLE_COUNT = sizeof(tuples)/sizeof(tuples[0]); - for (int i = 0; i < TUPLE_COUNT; ++i) { - const llong lhs = *tuples[i][0]; - const llong rhs = *tuples[i][1]; - const llong ans = *tuples[i][2]; - - llong n = lhs; - if (!((n /= rhs) == ans)) { - errln("fail: (n /= rhs) == ans"); - } - LLAssert(n == ans); - - n = lhs; - LLAssert((n / rhs) == ans); - LLAssert(n == lhs); - } - } - - //operator%=, operator% - { - const llong ZERO; - const llong ONE(0, 1); - const llong TWO(0, 2); - const llong THREE(0,3); - const llong FOUR(0, 4); - const llong FIVE(0, 5); - const llong SIX(0, 6); - - const llong NEG_ONE = -ONE; - const llong NEG_TWO = -TWO; - const llong NEG_THREE = -THREE; - const llong NEG_FOUR = -FOUR; - const llong NEG_FIVE = -FIVE; - const llong NEG_SIX = -SIX; - - const llong NINETY_NINE(0, 99); - const llong HUNDRED(0, 100); - const llong HUNDRED_ONE(0, 101); - - const llong BIG(0x12345678, 0x9abcdef0); - const llong BIG_FIVE(BIG * FIVE); - const llong BIG_FIVEm1 = BIG_FIVE - ONE; - const llong BIG_FIVEp1 = BIG_FIVE + ONE; - - const llong* tuples[][3] = { - { &ZERO, &FIVE, &ZERO }, - { &ONE, &FIVE, &ONE }, - { &TWO, &FIVE, &TWO }, - { &THREE, &FIVE, &THREE }, - { &FOUR, &FIVE, &FOUR }, - { &FIVE, &FIVE, &ZERO }, - { &SIX, &FIVE, &ONE }, - { &ZERO, &NEG_FIVE, &ZERO }, - { &ONE, &NEG_FIVE, &ONE }, - { &TWO, &NEG_FIVE, &TWO }, - { &THREE, &NEG_FIVE, &THREE }, - { &FOUR, &NEG_FIVE, &FOUR }, - { &FIVE, &NEG_FIVE, &ZERO }, - { &SIX, &NEG_FIVE, &ONE }, - { &NEG_ONE, &FIVE, &NEG_ONE }, - { &NEG_TWO, &FIVE, &NEG_TWO }, - { &NEG_THREE, &FIVE, &NEG_THREE }, - { &NEG_FOUR, &FIVE, &NEG_FOUR }, - { &NEG_FIVE, &FIVE, &ZERO }, - { &NEG_SIX, &FIVE, &NEG_ONE }, - { &NEG_ONE, &NEG_FIVE, &NEG_ONE }, - { &NEG_TWO, &NEG_FIVE, &NEG_TWO }, - { &NEG_THREE, &NEG_FIVE, &NEG_THREE }, - { &NEG_FOUR, &NEG_FIVE, &NEG_FOUR }, - { &NEG_FIVE, &NEG_FIVE, &ZERO }, - { &NEG_SIX, &NEG_FIVE, &NEG_ONE }, - { &NINETY_NINE, &FIVE, &FOUR }, - { &HUNDRED, &FIVE, &ZERO }, - { &HUNDRED_ONE, &FIVE, &ONE }, - { &BIG_FIVEm1, &FIVE, &FOUR }, - { &BIG_FIVE, &FIVE, &ZERO }, - { &BIG_FIVEp1, &FIVE, &ONE } - }; - const int TUPLE_COUNT = sizeof(tuples)/sizeof(tuples[0]); - for (int i = 0; i < TUPLE_COUNT; ++i) { - const llong lhs = *tuples[i][0]; - const llong rhs = *tuples[i][1]; - const llong ans = *tuples[i][2]; - - llong n = lhs; - if (!((n %= rhs) == ans)) { - errln("fail: (n %= rhs) == ans"); - } - LLAssert(n == ans); - - n = lhs; - LLAssert((n % rhs) == ans); - LLAssert(n == lhs); - } - } - - // pow - LLAssert(llong(0, 0).pow(0) == llong(0, 0)); - LLAssert(llong(0, 0).pow(2) == llong(0, 0)); - LLAssert(llong(0, 2).pow(0) == llong(0, 1)); - LLAssert(llong(0, 2).pow(2) == llong(0, 4)); - LLAssert(llong(0, 2).pow(32) == llong(1, 0)); - LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); + logln("Starting TestLLong"); + + TestLLongConstructors(); + + TestLLongSimpleOperators(); + + logln("Testing operator*=, operator*"); + + // operator*=, operator* + // small and large values, positive, &NEGative, zero + // also test commutivity + { + const llong ZERO; + const llong ONE(0, 1); + const llong NEG_ONE((int32_t)-1); + const llong THREE(0, 3); + const llong NEG_THREE((int32_t)-3); + const llong TWO_TO_16(0, 0x10000); + const llong NEG_TWO_TO_16 = -TWO_TO_16; + const llong TWO_TO_32(1, 0); + const llong NEG_TWO_TO_32 = -TWO_TO_32; + + const llong NINE(0, 9); + const llong NEG_NINE = -NINE; + + const llong TWO_TO_16X3(0, 0x00030000); + const llong NEG_TWO_TO_16X3 = -TWO_TO_16X3; + + const llong TWO_TO_32X3(3, 0); + const llong NEG_TWO_TO_32X3 = -TWO_TO_32X3; + + const llong TWO_TO_48(0x10000, 0); + const llong NEG_TWO_TO_48 = -TWO_TO_48; + + const int32_t VALUE_WIDTH = 9; + const llong* values[VALUE_WIDTH] = { + &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32 + }; + + const llong* answers[VALUE_WIDTH*VALUE_WIDTH] = { + &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, &ZERO, + &ZERO, &ONE, &NEG_ONE, &THREE, &NEG_THREE, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_32, &NEG_TWO_TO_32, + &ZERO, &NEG_ONE, &ONE, &NEG_THREE, &THREE, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_32, &TWO_TO_32, + &ZERO, &THREE, &NEG_THREE, &NINE, &NEG_NINE, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32X3, &NEG_TWO_TO_32X3, + &ZERO, &NEG_THREE, &THREE, &NEG_NINE, &NINE, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32X3, &TWO_TO_32X3, + &ZERO, &TWO_TO_16, &NEG_TWO_TO_16, &TWO_TO_16X3, &NEG_TWO_TO_16X3, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_48, &NEG_TWO_TO_48, + &ZERO, &NEG_TWO_TO_16, &TWO_TO_16, &NEG_TWO_TO_16X3, &TWO_TO_16X3, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_48, &TWO_TO_48, + &ZERO, &TWO_TO_32, &NEG_TWO_TO_32, &TWO_TO_32X3, &NEG_TWO_TO_32X3, &TWO_TO_48, &NEG_TWO_TO_48, &ZERO, &ZERO, + &ZERO, &NEG_TWO_TO_32, &TWO_TO_32, &NEG_TWO_TO_32X3, &TWO_TO_32X3, &NEG_TWO_TO_48, &TWO_TO_48, &ZERO, &ZERO + }; + + for (int i = 0; i < VALUE_WIDTH; ++i) { + for (int j = 0; j < VALUE_WIDTH; ++j) { + llong lhs = *values[i]; + llong rhs = *values[j]; + llong ans = *answers[i*VALUE_WIDTH + j]; + + llong n = lhs; + + LLAssert((n *= rhs) == ans); + LLAssert(n == ans); + + n = lhs; + LLAssert((n * rhs) == ans); + LLAssert(n == lhs); + } + } + } + + logln("Testing operator/=, operator/"); + // operator/=, operator/ + // test num = 0, div = 0, pos/neg, > 2^32, div > num + { + const llong ZERO; + const llong ONE(0, 1); + const llong NEG_ONE = -ONE; + const llong MAX(0x7fffffff, 0xffffffff); + const llong MIN(0x80000000, 0); + const llong TWO(0, 2); + const llong NEG_TWO = -TWO; + const llong FIVE(0, 5); + const llong NEG_FIVE = -FIVE; + const llong TWO_TO_32(1, 0); + const llong NEG_TWO_TO_32 = -TWO_TO_32; + const llong TWO_TO_32d5 = llong(TWO_TO_32.asDouble()/5.0); + const llong NEG_TWO_TO_32d5 = -TWO_TO_32d5; + const llong TWO_TO_32X5 = TWO_TO_32 * FIVE; + const llong NEG_TWO_TO_32X5 = -TWO_TO_32X5; + + const llong* tuples[] = { // lhs, rhs, ans + &ZERO, &ZERO, &ZERO, + &ONE, &ZERO,&MAX, + &NEG_ONE, &ZERO, &MIN, + &ONE, &ONE, &ONE, + &ONE, &NEG_ONE, &NEG_ONE, + &NEG_ONE, &ONE, &NEG_ONE, + &NEG_ONE, &NEG_ONE, &ONE, + &FIVE, &TWO, &TWO, + &FIVE, &NEG_TWO, &NEG_TWO, + &NEG_FIVE, &TWO, &NEG_TWO, + &NEG_FIVE, &NEG_TWO, &TWO, + &TWO, &FIVE, &ZERO, + &TWO, &NEG_FIVE, &ZERO, + &NEG_TWO, &FIVE, &ZERO, + &NEG_TWO, &NEG_FIVE, &ZERO, + &TWO_TO_32, &TWO_TO_32, &ONE, + &TWO_TO_32, &NEG_TWO_TO_32, &NEG_ONE, + &NEG_TWO_TO_32, &TWO_TO_32, &NEG_ONE, + &NEG_TWO_TO_32, &NEG_TWO_TO_32, &ONE, + &TWO_TO_32, &FIVE, &TWO_TO_32d5, + &TWO_TO_32, &NEG_FIVE, &NEG_TWO_TO_32d5, + &NEG_TWO_TO_32, &FIVE, &NEG_TWO_TO_32d5, + &NEG_TWO_TO_32, &NEG_FIVE, &TWO_TO_32d5, + &TWO_TO_32X5, &FIVE, &TWO_TO_32, + &TWO_TO_32X5, &NEG_FIVE, &NEG_TWO_TO_32, + &NEG_TWO_TO_32X5, &FIVE, &NEG_TWO_TO_32, + &NEG_TWO_TO_32X5, &NEG_FIVE, &TWO_TO_32, + &TWO_TO_32X5, &TWO_TO_32, &FIVE, + &TWO_TO_32X5, &NEG_TWO_TO_32, &NEG_FIVE, + &NEG_TWO_TO_32X5, &NEG_TWO_TO_32, &FIVE, + &NEG_TWO_TO_32X5, &TWO_TO_32, &NEG_FIVE + }; + const int TUPLE_WIDTH = 3; + const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH; + for (int i = 0; i < TUPLE_COUNT; ++i) { + const llong lhs = *tuples[i*TUPLE_WIDTH+0]; + const llong rhs = *tuples[i*TUPLE_WIDTH+1]; + const llong ans = *tuples[i*TUPLE_WIDTH+2]; + + llong n = lhs; + if (!((n /= rhs) == ans)) { + errln("fail: (n /= rhs) == ans"); + } + LLAssert(n == ans); + + n = lhs; + LLAssert((n / rhs) == ans); + LLAssert(n == lhs); + } + } + + logln("Testing operator%=, operator%"); + //operator%=, operator% + { + const llong ZERO; + const llong ONE(0, 1); + const llong TWO(0, 2); + const llong THREE(0,3); + const llong FOUR(0, 4); + const llong FIVE(0, 5); + const llong SIX(0, 6); + + const llong NEG_ONE = -ONE; + const llong NEG_TWO = -TWO; + const llong NEG_THREE = -THREE; + const llong NEG_FOUR = -FOUR; + const llong NEG_FIVE = -FIVE; + const llong NEG_SIX = -SIX; + + const llong NINETY_NINE(0, 99); + const llong HUNDRED(0, 100); + const llong HUNDRED_ONE(0, 101); + + const llong BIG(0x12345678, 0x9abcdef0); + const llong BIG_FIVE(BIG * FIVE); + const llong BIG_FIVEm1 = BIG_FIVE - ONE; + const llong BIG_FIVEp1 = BIG_FIVE + ONE; + + const llong* tuples[] = { + &ZERO, &FIVE, &ZERO, + &ONE, &FIVE, &ONE, + &TWO, &FIVE, &TWO, + &THREE, &FIVE, &THREE, + &FOUR, &FIVE, &FOUR, + &FIVE, &FIVE, &ZERO, + &SIX, &FIVE, &ONE, + &ZERO, &NEG_FIVE, &ZERO, + &ONE, &NEG_FIVE, &ONE, + &TWO, &NEG_FIVE, &TWO, + &THREE, &NEG_FIVE, &THREE, + &FOUR, &NEG_FIVE, &FOUR, + &FIVE, &NEG_FIVE, &ZERO, + &SIX, &NEG_FIVE, &ONE, + &NEG_ONE, &FIVE, &NEG_ONE, + &NEG_TWO, &FIVE, &NEG_TWO, + &NEG_THREE, &FIVE, &NEG_THREE, + &NEG_FOUR, &FIVE, &NEG_FOUR, + &NEG_FIVE, &FIVE, &ZERO, + &NEG_SIX, &FIVE, &NEG_ONE, + &NEG_ONE, &NEG_FIVE, &NEG_ONE, + &NEG_TWO, &NEG_FIVE, &NEG_TWO, + &NEG_THREE, &NEG_FIVE, &NEG_THREE, + &NEG_FOUR, &NEG_FIVE, &NEG_FOUR, + &NEG_FIVE, &NEG_FIVE, &ZERO, + &NEG_SIX, &NEG_FIVE, &NEG_ONE, + &NINETY_NINE, &FIVE, &FOUR, + &HUNDRED, &FIVE, &ZERO, + &HUNDRED_ONE, &FIVE, &ONE, + &BIG_FIVEm1, &FIVE, &FOUR, + &BIG_FIVE, &FIVE, &ZERO, + &BIG_FIVEp1, &FIVE, &ONE + }; + const int TUPLE_WIDTH = 3; + const int TUPLE_COUNT = (int)(sizeof(tuples)/sizeof(tuples[0]))/TUPLE_WIDTH; + for (int i = 0; i < TUPLE_COUNT; ++i) { + const llong lhs = *tuples[i*TUPLE_WIDTH+0]; + const llong rhs = *tuples[i*TUPLE_WIDTH+1]; + const llong ans = *tuples[i*TUPLE_WIDTH+2]; + + llong n = lhs; + if (!((n %= rhs) == ans)) { + errln("fail: (n %= rhs) == ans"); + } + LLAssert(n == ans); + + n = lhs; + LLAssert((n % rhs) == ans); + LLAssert(n == lhs); + } + } + + logln("Testing pow"); + // pow + LLAssert(llong(0, 0).pow(0) == llong(0, 0)); + LLAssert(llong(0, 0).pow(2) == llong(0, 0)); + LLAssert(llong(0, 2).pow(0) == llong(0, 1)); + LLAssert(llong(0, 2).pow(2) == llong(0, 4)); + LLAssert(llong(0, 2).pow(32) == llong(1, 0)); + LLAssert(llong(0, 5).pow(10) == llong((double)5.0 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5 * 5)); // absolute value - { - const llong n(0xffffffff,0xffffffff); - LLAssert(n.abs() == llong(0, 1)); - } + { + const llong n(0xffffffff,0xffffffff); + LLAssert(n.abs() == llong(0, 1)); + } - // atoll - const char empty[] = ""; - const char zero[] = "0"; - const char neg_one[] = "-1"; - const char neg_12345[] = "-12345"; - const char big1[] = "123456789abcdef0"; - const char big2[] = "fFfFfFfFfFfFfFfF"; - LLAssert(llong::atoll(empty) == llong(0, 0)); - LLAssert(llong::atoll(zero) == llong(0, 0)); - LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff)); - LLAssert(llong::atoll(neg_12345) == -llong(0, 12345)); - LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0)); - LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff)); + logln("Testing atoll"); + // atoll + const char empty[] = ""; + const char zero[] = "0"; + const char neg_one[] = "-1"; + const char neg_12345[] = "-12345"; + const char big1[] = "123456789abcdef0"; + const char big2[] = "fFfFfFfFfFfFfFfF"; + LLAssert(llong::atoll(empty) == llong(0, 0)); + LLAssert(llong::atoll(zero) == llong(0, 0)); + LLAssert(llong::atoll(neg_one) == llong(0xffffffff, 0xffffffff)); + LLAssert(llong::atoll(neg_12345) == -llong(0, 12345)); + LLAssert(llong::atoll(big1, 16) == llong(0x12345678, 0x9abcdef0)); + LLAssert(llong::atoll(big2, 16) == llong(0xffffffff, 0xffffffff)); - // u_atoll - const UChar uempty[] = { 0 }; - const UChar uzero[] = { 0x30, 0 }; - const UChar uneg_one[] = { 0x2d, 0x31, 0 }; - const UChar uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 }; - const UChar ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 }; - const UChar ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 }; - LLAssert(llong::utoll(uempty) == llong(0, 0)); - LLAssert(llong::utoll(uzero) == llong(0, 0)); - LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff)); - LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345)); - LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0)); - LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff)); + // u_atoll + const UChar uempty[] = { 0 }; + const UChar uzero[] = { 0x30, 0 }; + const UChar uneg_one[] = { 0x2d, 0x31, 0 }; + const UChar uneg_12345[] = { 0x2d, 0x31, 0x32, 0x33, 0x34, 0x35, 0 }; + const UChar ubig1[] = { 0x31, 0x32, 0x33, 0x34, 0x35, 0x36, 0x37, 0x38, 0x39, 0x61, 0x62, 0x63, 0x64, 0x65, 0x66, 0x30, 0 }; + const UChar ubig2[] = { 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0x66, 0x46, 0 }; + LLAssert(llong::utoll(uempty) == llong(0, 0)); + LLAssert(llong::utoll(uzero) == llong(0, 0)); + LLAssert(llong::utoll(uneg_one) == llong(0xffffffff, 0xffffffff)); + LLAssert(llong::utoll(uneg_12345) == -llong(0, 12345)); + LLAssert(llong::utoll(ubig1, 16) == llong(0x12345678, 0x9abcdef0)); + LLAssert(llong::utoll(ubig2, 16) == llong(0xffffffff, 0xffffffff)); - // lltoa - { - char buf[64]; // ascii - LLAssert((llong(0, 0).lltoa(buf, sizeof(buf)) == 1) && (strcmp(buf, zero) == 0)); - LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0)); - LLAssert(((-llong(0, 12345)).lltoa(buf, sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0)); - LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0)); - } + logln("Testing lltoa"); + // lltoa + { + char buf[64]; // ascii + LLAssert((llong(0, 0).lltoa(buf, (uint32_t)sizeof(buf)) == 1) && (strcmp(buf, zero) == 0)); + LLAssert((llong(0xffffffff, 0xffffffff).lltoa(buf, (uint32_t)sizeof(buf)) == 2) && (strcmp(buf, neg_one) == 0)); + LLAssert(((-llong(0, 12345)).lltoa(buf, (uint32_t)sizeof(buf)) == 6) && (strcmp(buf, neg_12345) == 0)); + LLAssert((llong(0x12345678, 0x9abcdef0).lltoa(buf, (uint32_t)sizeof(buf), 16) == 16) && (strcmp(buf, big1) == 0)); + } - // u_lltoa - { - UChar buf[64]; - LLAssert((llong(0, 0).lltou(buf, sizeof(buf)) == 1) && (ustrcmp(buf, uzero) == 0)); - LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, sizeof(buf)) == 2) && (ustrcmp(buf, uneg_one) == 0)); - LLAssert(((-llong(0, 12345)).lltou(buf, sizeof(buf)) == 6) && (ustrcmp(buf, uneg_12345) == 0)); - LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, sizeof(buf), 16) == 16) && (ustrcmp(buf, ubig1) == 0)); - } + logln("Testing u_lltoa"); + // u_lltoa + { + UChar buf[64]; + LLAssert((llong(0, 0).lltou(buf, (uint32_t)sizeof(buf)) == 1) && (u_strcmp(buf, uzero) == 0)); + LLAssert((llong(0xffffffff, 0xffffffff).lltou(buf, (uint32_t)sizeof(buf)) == 2) && (u_strcmp(buf, uneg_one) == 0)); + LLAssert(((-llong(0, 12345)).lltou(buf, (uint32_t)sizeof(buf)) == 6) && (u_strcmp(buf, uneg_12345) == 0)); + LLAssert((llong(0x12345678, 0x9abcdef0).lltou(buf, (uint32_t)sizeof(buf), 16) == 16) && (u_strcmp(buf, ubig1) == 0)); + } } void @@ -866,7 +888,7 @@ IntlTestRBNF::TestEnglishSpellout() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::US, status); + = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getUS(), status); if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); @@ -899,9 +921,9 @@ IntlTestRBNF::TestEnglishSpellout() formatter->setLenient(TRUE); static const char* lpTestData[][2] = { - { "fifty-7", "57" }, - { " fifty-7", "57" }, - { " fifty-7", "57" }, + { "fifty-7", "57" }, + { " fifty-7", "57" }, + { " fifty-7", "57" }, { "2 thousand six HUNDRED fifty-7", "2,657" }, { "fifteen hundred and zero", "1,500" }, { "FOurhundred thiRTY six", "436" }, @@ -917,7 +939,7 @@ IntlTestRBNF::TestOrdinalAbbreviations() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::US, status); + = new RuleBasedNumberFormat(URBNF_ORDINAL, Locale::getUS(), status); if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); @@ -953,13 +975,13 @@ IntlTestRBNF::TestDurations() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_DURATION, Locale::US, status); + = new RuleBasedNumberFormat(URBNF_DURATION, Locale::getUS(), status); if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); } else { static const char* testData[][2] = { - { "3,600", "1:00:00" }, //move me and I fail + { "3,600", "1:00:00" }, //move me and I fail { "0", "0 sec." }, { "1", "1 sec." }, { "24", "24 sec." }, @@ -1128,8 +1150,8 @@ IntlTestRBNF::TestItalianSpellout() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::ITALIAN, status); - + = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getItalian(), status); + if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); } else { @@ -1167,7 +1189,7 @@ IntlTestRBNF::TestGermanSpellout() { UErrorCode status = U_ZERO_ERROR; RuleBasedNumberFormat* formatter - = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::GERMANY, status); + = new RuleBasedNumberFormat(URBNF_SPELLOUT, Locale::getGermany(), status); if (U_FAILURE(status)) { errln("FAIL: could not construct formatter"); @@ -1237,7 +1259,7 @@ IntlTestRBNF::doTest(RuleBasedNumberFormat* formatter, const char* testData[][2] // man, error reporting would be easier with printf-style syntax for unicode string and formattable UErrorCode status = U_ZERO_ERROR; - NumberFormat* decFmt = NumberFormat::createInstance(Locale::US, status); + NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status); if (U_FAILURE(status)) { errln("FAIL: could not create NumberFormat"); } else { @@ -1306,7 +1328,7 @@ void IntlTestRBNF::doLenientParseTest(RuleBasedNumberFormat* formatter, const char* testData[][2]) { UErrorCode status = U_ZERO_ERROR; - NumberFormat* decFmt = NumberFormat::createInstance(Locale::US, status); + NumberFormat* decFmt = NumberFormat::createInstance(Locale::getUS(), status); if (U_FAILURE(status)) { errln("FAIL: could not create NumberFormat"); } else { diff --git a/icu4c/source/test/intltest/itrbnf.h b/icu4c/source/test/intltest/itrbnf.h index 43a71b519c6..00f80c1fe5b 100644 --- a/icu4c/source/test/intltest/itrbnf.h +++ b/icu4c/source/test/intltest/itrbnf.h @@ -34,6 +34,8 @@ class IntlTestRBNF : public IntlTest { * Perform API tests on llong */ virtual void TestLLong(); + virtual void TestLLongConstructors(); + virtual void TestLLongSimpleOperators(); /** * Perform a simple spot check on the English spellout rules