X-Git-Url: https://git.saurik.com/apple/icu.git/blobdiff_plain/729e4ab9bc6618bc3d8a898e575df7f4019e29ca..4f1e1a09ce4daed860e35d359ce2fceccb0764e8:/icuSources/test/intltest/itutil.cpp diff --git a/icuSources/test/intltest/itutil.cpp b/icuSources/test/intltest/itutil.cpp index bd5e392c..7b2995f7 100644 --- a/icuSources/test/intltest/itutil.cpp +++ b/icuSources/test/intltest/itutil.cpp @@ -1,6 +1,8 @@ +// © 2016 and later: Unicode, Inc. and others. +// License & terms of use: http://www.unicode.org/copyright.html /******************************************************************** * COPYRIGHT: - * Copyright (c) 1997-2010, International Business Machines Corporation and + * Copyright (c) 1997-2016, International Business Machines Corporation and * others. All Rights Reserved. ********************************************************************/ @@ -12,6 +14,7 @@ #include "unicode/utypes.h" #include "unicode/errorcode.h" #include "unicode/localpointer.h" +#include "charstr.h" #include "itutil.h" #include "strtest.h" #include "loctest.h" @@ -29,7 +32,18 @@ #include "aliastst.h" #include "usettest.h" +extern IntlTest *createBytesTrieTest(); static IntlTest *createLocalPointerTest(); +extern IntlTest *createUCharsTrieTest(); +static IntlTest *createEnumSetTest(); +extern IntlTest *createSimpleFormatterTest(); +extern IntlTest *createUnifiedCacheTest(); +extern IntlTest *createQuantityFormatterTest(); +extern IntlTest *createPluralMapTest(); +#if !UCONFIG_NO_FORMATTING +extern IntlTest *createStaticUnicodeSetsTest(); +#endif + #define CASE(id, test) case id: \ name = #test; \ @@ -68,6 +82,72 @@ void IntlTestUtilities::runIndexedTest( int32_t index, UBool exec, const char* & callTest(*test, par); } break; + case 17: + name = "BytesTrieTest"; + if (exec) { + logln("TestSuite BytesTrieTest---"); logln(); + LocalPointer test(createBytesTrieTest()); + callTest(*test, par); + } + break; + case 18: + name = "UCharsTrieTest"; + if (exec) { + logln("TestSuite UCharsTrieTest---"); logln(); + LocalPointer test(createUCharsTrieTest()); + callTest(*test, par); + } + break; + case 19: + name = "EnumSetTest"; + if (exec) { + logln("TestSuite EnumSetTest---"); logln(); + LocalPointer test(createEnumSetTest()); + callTest(*test, par); + } + break; + case 20: + name = "SimpleFormatterTest"; + if (exec) { + logln("TestSuite SimpleFormatterTest---"); logln(); + LocalPointer test(createSimpleFormatterTest()); + callTest(*test, par); + } + break; + case 21: + name = "UnifiedCacheTest"; + if (exec) { + logln("TestSuite UnifiedCacheTest---"); logln(); + LocalPointer test(createUnifiedCacheTest()); + callTest(*test, par); + } + break; + case 22: + name = "QuantityFormatterTest"; + if (exec) { + logln("TestSuite QuantityFormatterTest---"); logln(); + LocalPointer test(createQuantityFormatterTest()); + callTest(*test, par); + } + break; + case 23: + name = "PluralMapTest"; + if (exec) { + logln("TestSuite PluralMapTest---"); logln(); + LocalPointer test(createPluralMapTest()); + callTest(*test, par); + } + break; + case 24: + name = "StaticUnicodeSetsTest"; +#if !UCONFIG_NO_FORMATTING + if (exec) { + logln("TestSuite StaticUnicodeSetsTest---"); logln(); + LocalPointer test(createStaticUnicodeSetsTest()); + callTest(*test, par); + } +#endif + break; default: name = ""; break; //needed to end loop } } @@ -77,6 +157,7 @@ void ErrorCodeTest::runIndexedTest(int32_t index, UBool exec, const char* &name, switch (index) { case 0: name = "TestErrorCode"; if (exec) TestErrorCode(); break; case 1: name = "TestSubclass"; if (exec) TestSubclass(); break; + case 2: name = "TestIcuTestErrorCode"; if (exec) TestIcuTestErrorCode(); break; default: name = ""; break; //needed to end loop } } @@ -188,6 +269,92 @@ void ErrorCodeTest::TestSubclass() { } } +class IcuTestErrorCodeTestHelper : public IntlTest { + public: + void errln( const UnicodeString &message ) U_OVERRIDE { + test->assertFalse("Already saw an error", seenError); + seenError = TRUE; + test->assertEquals("Message for Error", expectedErrln, message); + if (expectedDataErr) { + test->errln("Got non-data error, but expected data error"); + } + } + + void dataerrln( const UnicodeString &message ) U_OVERRIDE { + test->assertFalse("Already saw an error", seenError); + seenError = TRUE; + test->assertEquals("Message for Error", expectedErrln, message); + if (!expectedDataErr) { + test->errln("Got data error, but expected non-data error"); + } + } + + IntlTest* test; + UBool expectedDataErr; + UnicodeString expectedErrln; + UBool seenError; +}; + +void ErrorCodeTest::TestIcuTestErrorCode() { + IcuTestErrorCodeTestHelper helper; + helper.test = this; + + // Test destructor message + helper.expectedErrln = u"AAA failure: U_ILLEGAL_PAD_POSITION"; + helper.expectedDataErr = FALSE; + helper.seenError = FALSE; + { + IcuTestErrorCode testStatus(helper, "AAA"); + testStatus.set(U_ILLEGAL_PAD_POSITION); + } + assertTrue("Should have seen an error", helper.seenError); + + // Test destructor message with scope + helper.expectedErrln = u"BBB failure: U_ILLEGAL_PAD_POSITION scope: foo"; + helper.expectedDataErr = FALSE; + helper.seenError = FALSE; + { + IcuTestErrorCode testStatus(helper, "BBB"); + testStatus.setScope("foo"); + testStatus.set(U_ILLEGAL_PAD_POSITION); + } + assertTrue("Should have seen an error", helper.seenError); + + // Check errIfFailure message with scope + helper.expectedErrln = u"CCC failure: U_ILLEGAL_PAD_POSITION scope: foo"; + helper.expectedDataErr = FALSE; + helper.seenError = FALSE; + { + IcuTestErrorCode testStatus(helper, "CCC"); + testStatus.setScope("foo"); + testStatus.set(U_ILLEGAL_PAD_POSITION); + testStatus.errIfFailureAndReset(); + assertTrue("Should have seen an error", helper.seenError); + helper.seenError = FALSE; + helper.expectedErrln = u"CCC failure: U_ILLEGAL_CHAR_FOUND scope: foo - 5.4300"; + testStatus.set(U_ILLEGAL_CHAR_FOUND); + testStatus.errIfFailureAndReset("%6.4f", 5.43); + assertTrue("Should have seen an error", helper.seenError); + } + + // Check errDataIfFailure message without scope + helper.expectedErrln = u"DDD failure: U_ILLEGAL_PAD_POSITION"; + helper.expectedDataErr = TRUE; + helper.seenError = FALSE; + { + IcuTestErrorCode testStatus(helper, "DDD"); + testStatus.set(U_ILLEGAL_PAD_POSITION); + testStatus.errDataIfFailureAndReset(); + assertTrue("Should have seen an error", helper.seenError); + helper.seenError = FALSE; + helper.expectedErrln = u"DDD failure: U_ILLEGAL_CHAR_FOUND - 5.4300"; + testStatus.set(U_ILLEGAL_CHAR_FOUND); + testStatus.errDataIfFailureAndReset("%6.4f", 5.43); + assertTrue("Should have seen an error", helper.seenError); + } +} + + class LocalPointerTest : public IntlTest { public: LocalPointerTest() {} @@ -195,8 +362,11 @@ public: void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL); void TestLocalPointer(); + void TestLocalPointerMoveSwap(); void TestLocalArray(); + void TestLocalArrayMoveSwap(); void TestLocalXyzPointer(); + void TestLocalXyzPointerMoveSwap(); void TestLocalXyzPointerNull(); }; @@ -208,18 +378,18 @@ void LocalPointerTest::runIndexedTest(int32_t index, UBool exec, const char *&na if(exec) { logln("TestSuite LocalPointerTest: "); } - switch (index) { - TESTCASE(0, TestLocalPointer); - TESTCASE(1, TestLocalArray); - TESTCASE(2, TestLocalXyzPointer); - TESTCASE(3, TestLocalXyzPointerNull); - default: - name=""; - break; // needed to end the loop - } + TESTCASE_AUTO_BEGIN; + TESTCASE_AUTO(TestLocalPointer); + TESTCASE_AUTO(TestLocalPointerMoveSwap); + TESTCASE_AUTO(TestLocalArray); + TESTCASE_AUTO(TestLocalArrayMoveSwap); + TESTCASE_AUTO(TestLocalXyzPointer); + TESTCASE_AUTO(TestLocalXyzPointerMoveSwap); + TESTCASE_AUTO(TestLocalXyzPointerNull); + TESTCASE_AUTO_END; } -// Exercise every LocalPointer and LocalPointerBase method. +// Exercise almost every LocalPointer and LocalPointerBase method. void LocalPointerTest::TestLocalPointer() { // constructor LocalPointer s(new UnicodeString((UChar32)0x50005)); @@ -242,14 +412,87 @@ void LocalPointerTest::TestLocalPointer() { errln("LocalPointer orphan() failure"); } delete orphan; - // destructor s.adoptInstead(new UnicodeString()); if(s->length()!=0) { errln("LocalPointer adoptInstead(empty) failure"); } + + // LocalPointer(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL. + UErrorCode errorCode = U_ZERO_ERROR; + LocalPointer csx(new CharString("some chars", errorCode), errorCode); + if(csx.isNull() && U_SUCCESS(errorCode)) { + errln("LocalPointer(p, errorCode) failure"); + return; + } + errorCode = U_ZERO_ERROR; + csx.adoptInsteadAndCheckErrorCode(new CharString("different chars", errorCode), errorCode); + if(csx.isNull() && U_SUCCESS(errorCode)) { + errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure"); + return; + } + // Incoming failure: Keep the current object and delete the input object. + errorCode = U_ILLEGAL_ARGUMENT_ERROR; + csx.adoptInsteadAndCheckErrorCode(new CharString("unused", errorCode), errorCode); + if(csx.isValid() && strcmp(csx->data(), "different chars") != 0) { + errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old object"); + return; + } + errorCode = U_ZERO_ERROR; + csx.adoptInsteadAndCheckErrorCode(NULL, errorCode); + if(errorCode != U_MEMORY_ALLOCATION_ERROR) { + errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR"); + return; + } + if(csx.isValid()) { + errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the object"); + return; + } + errorCode = U_ZERO_ERROR; + LocalPointer null(NULL, errorCode); + if(errorCode != U_MEMORY_ALLOCATION_ERROR) { + errln("LocalPointer(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR"); + return; + } + + // destructor +} + +void LocalPointerTest::TestLocalPointerMoveSwap() { + UnicodeString *p1 = new UnicodeString((UChar)0x61); + UnicodeString *p2 = new UnicodeString((UChar)0x62); + LocalPointer s1(p1); + LocalPointer s2(p2); + s1.swap(s2); + if(s1.getAlias() != p2 || s2.getAlias() != p1) { + errln("LocalPointer.swap() did not swap"); + } + swap(s1, s2); + if(s1.getAlias() != p1 || s2.getAlias() != p2) { + errln("swap(LocalPointer) did not swap back"); + } + LocalPointer s3; + s3.moveFrom(s1); + if(s3.getAlias() != p1 || s1.isValid()) { + errln("LocalPointer.moveFrom() did not move"); + } + infoln("TestLocalPointerMoveSwap() with rvalue references"); + s1 = static_cast &&>(s3); + if(s1.getAlias() != p1 || s3.isValid()) { + errln("LocalPointer move assignment operator did not move"); + } + LocalPointer s4(static_cast &&>(s2)); + if(s4.getAlias() != p2 || s2.isValid()) { + errln("LocalPointer move constructor did not move"); + } + + // Move self assignment leaves the object valid but in an undefined state. + // Do it to make sure there is no crash, + // but do not check for any particular resulting value. + s1.moveFrom(s1); + s3.moveFrom(s3); } -// Exercise every LocalArray method (but not LocalPointerBase). +// Exercise almost every LocalArray method (but not LocalPointerBase). void LocalPointerTest::TestLocalArray() { // constructor LocalArray a(new UnicodeString[2]); @@ -265,9 +508,83 @@ void LocalPointerTest::TestLocalArray() { if(a[3].length()!=2 || a[3][1]!=0x62) { errln("LocalArray adoptInstead() failure"); } + + // LocalArray(p, errorCode) sets U_MEMORY_ALLOCATION_ERROR if p==NULL. + UErrorCode errorCode = U_ZERO_ERROR; + LocalArray ua(new UnicodeString[3], errorCode); + if(ua.isNull() && U_SUCCESS(errorCode)) { + errln("LocalArray(p, errorCode) failure"); + return; + } + errorCode = U_ZERO_ERROR; + UnicodeString *u4 = new UnicodeString[4]; + ua.adoptInsteadAndCheckErrorCode(u4, errorCode); + if(ua.isNull() && U_SUCCESS(errorCode)) { + errln("adoptInsteadAndCheckErrorCode(p, errorCode) failure"); + return; + } + // Incoming failure: Keep the current object and delete the input object. + errorCode = U_ILLEGAL_ARGUMENT_ERROR; + ua.adoptInsteadAndCheckErrorCode(new UnicodeString[5], errorCode); + if(ua.isValid() && ua.getAlias() != u4) { + errln("adoptInsteadAndCheckErrorCode(p, U_FAILURE) did not retain the old array"); + return; + } + errorCode = U_ZERO_ERROR; + ua.adoptInsteadAndCheckErrorCode(NULL, errorCode); + if(errorCode != U_MEMORY_ALLOCATION_ERROR) { + errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR"); + return; + } + if(ua.isValid()) { + errln("adoptInsteadAndCheckErrorCode(NULL, errorCode) kept the array"); + return; + } + errorCode = U_ZERO_ERROR; + LocalArray null(NULL, errorCode); + if(errorCode != U_MEMORY_ALLOCATION_ERROR) { + errln("LocalArray(NULL, errorCode) did not set U_MEMORY_ALLOCATION_ERROR"); + return; + } + // destructor } +void LocalPointerTest::TestLocalArrayMoveSwap() { + UnicodeString *p1 = new UnicodeString[2]; + UnicodeString *p2 = new UnicodeString[3]; + LocalArray a1(p1); + LocalArray a2(p2); + a1.swap(a2); + if(a1.getAlias() != p2 || a2.getAlias() != p1) { + errln("LocalArray.swap() did not swap"); + } + swap(a1, a2); + if(a1.getAlias() != p1 || a2.getAlias() != p2) { + errln("swap(LocalArray) did not swap back"); + } + LocalArray a3; + a3.moveFrom(a1); + if(a3.getAlias() != p1 || a1.isValid()) { + errln("LocalArray.moveFrom() did not move"); + } + infoln("TestLocalArrayMoveSwap() with rvalue references"); + a1 = static_cast &&>(a3); + if(a1.getAlias() != p1 || a3.isValid()) { + errln("LocalArray move assignment operator did not move"); + } + LocalArray a4(static_cast &&>(a2)); + if(a4.getAlias() != p2 || a2.isValid()) { + errln("LocalArray move constructor did not move"); + } + + // Move self assignment leaves the object valid but in an undefined state. + // Do it to make sure there is no crash, + // but do not check for any particular resulting value. + a1.moveFrom(a1); + a3.moveFrom(a3); +} + #include "unicode/ucnvsel.h" #include "unicode/ucal.h" #include "unicode/udatpg.h" @@ -285,7 +602,7 @@ void LocalPointerTest::TestLocalXyzPointer() { static const char *const encoding="ISO-8859-1"; LocalUConverterSelectorPointer sel( ucnvsel_open(&encoding, 1, NULL, UCNV_ROUNDTRIP_SET, errorCode)); - if(errorCode.logIfFailureAndReset("ucnvsel_open()")) { + if(errorCode.errIfFailureAndReset("ucnvsel_open()")) { return; } if(sel.isNull()) { @@ -295,7 +612,7 @@ void LocalPointerTest::TestLocalXyzPointer() { #if !UCONFIG_NO_FORMATTING LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode)); - if(errorCode.logDataIfFailureAndReset("ucal_open()")) { + if(errorCode.errDataIfFailureAndReset("ucal_open()")) { return; } if(cal.isNull()) { @@ -304,7 +621,7 @@ void LocalPointerTest::TestLocalXyzPointer() { } LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode)); - if(errorCode.logIfFailureAndReset("udatpg_open()")) { + if(errorCode.errDataIfFailureAndReset("udatpg_open()")) { return; } if(patgen.isNull()) { @@ -313,7 +630,7 @@ void LocalPointerTest::TestLocalXyzPointer() { } LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode)); - if(errorCode.logIfFailureAndReset("uldn_open()")) { + if(errorCode.errIfFailureAndReset("uldn_open()")) { return; } if(ldn.isNull()) { @@ -324,7 +641,7 @@ void LocalPointerTest::TestLocalXyzPointer() { UnicodeString hello=UNICODE_STRING_SIMPLE("Hello {0}!"); LocalUMessageFormatPointer msg( umsg_open(hello.getBuffer(), hello.length(), "root", NULL, errorCode)); - if(errorCode.logIfFailureAndReset("umsg_open()")) { + if(errorCode.errIfFailureAndReset("umsg_open()")) { return; } if(msg.isNull()) { @@ -334,10 +651,10 @@ void LocalPointerTest::TestLocalXyzPointer() { #endif /* UCONFIG_NO_FORMATTING */ #if !UCONFIG_NO_NORMALIZATION - const UNormalizer2 *nfc=unorm2_getInstance(NULL, "nfc", UNORM2_COMPOSE, errorCode); + const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode); UnicodeSet emptySet; LocalUNormalizer2Pointer fn2(unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode)); - if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) { + if(errorCode.errIfFailureAndReset("unorm2_openFiltered()")) { return; } if(fn2.isNull()) { @@ -348,7 +665,7 @@ void LocalPointerTest::TestLocalXyzPointer() { #if !UCONFIG_NO_IDNA LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode)); - if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) { + if(errorCode.errIfFailureAndReset("uidna_openUTS46()")) { return; } if(idna.isNull()) { @@ -361,7 +678,7 @@ void LocalPointerTest::TestLocalXyzPointer() { UnicodeString pattern=UNICODE_STRING_SIMPLE("abc|xy+z"); LocalURegularExpressionPointer regex( uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode)); - if(errorCode.logIfFailureAndReset("uregex_open()")) { + if(errorCode.errIfFailureAndReset("uregex_open()")) { return; } if(regex.isNull()) { @@ -374,7 +691,7 @@ void LocalPointerTest::TestLocalXyzPointer() { UnicodeString id=UNICODE_STRING_SIMPLE("Grek-Latn"); LocalUTransliteratorPointer trans( utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode)); - if(errorCode.logIfFailureAndReset("utrans_open()")) { + if(errorCode.errIfFailureAndReset("utrans_open()")) { return; } if(trans.isNull()) { @@ -386,6 +703,56 @@ void LocalPointerTest::TestLocalXyzPointer() { // destructors } +void LocalPointerTest::TestLocalXyzPointerMoveSwap() { +#if !UCONFIG_NO_NORMALIZATION + IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerMoveSwap"); + const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode); + const UNormalizer2 *nfd=unorm2_getNFDInstance(errorCode); + if(errorCode.errIfFailureAndReset("unorm2_getNF[CD]Instance()")) { + return; + } + UnicodeSet emptySet; + UNormalizer2 *p1 = unorm2_openFiltered(nfc, emptySet.toUSet(), errorCode); + UNormalizer2 *p2 = unorm2_openFiltered(nfd, emptySet.toUSet(), errorCode); + LocalUNormalizer2Pointer f1(p1); + LocalUNormalizer2Pointer f2(p2); + if(errorCode.errIfFailureAndReset("unorm2_openFiltered()")) { + return; + } + if(f1.isNull() || f2.isNull()) { + errln("LocalUNormalizer2Pointer failure"); + return; + } + f1.swap(f2); + if(f1.getAlias() != p2 || f2.getAlias() != p1) { + errln("LocalUNormalizer2Pointer.swap() did not swap"); + } + swap(f1, f2); + if(f1.getAlias() != p1 || f2.getAlias() != p2) { + errln("swap(LocalUNormalizer2Pointer) did not swap back"); + } + LocalUNormalizer2Pointer f3; + f3.moveFrom(f1); + if(f3.getAlias() != p1 || f1.isValid()) { + errln("LocalUNormalizer2Pointer.moveFrom() did not move"); + } + infoln("TestLocalXyzPointerMoveSwap() with rvalue references"); + f1 = static_cast(f3); + if(f1.getAlias() != p1 || f3.isValid()) { + errln("LocalUNormalizer2Pointer move assignment operator did not move"); + } + LocalUNormalizer2Pointer f4(static_cast(f2)); + if(f4.getAlias() != p2 || f2.isValid()) { + errln("LocalUNormalizer2Pointer move constructor did not move"); + } + // Move self assignment leaves the object valid but in an undefined state. + // Do it to make sure there is no crash, + // but do not check for any particular resulting value. + f1.moveFrom(f1); + f3.moveFrom(f3); +#endif /* !UCONFIG_NO_NORMALIZATION */ +} + // Try LocalXyzPointer types with NULL pointers. void LocalPointerTest::TestLocalXyzPointerNull() { { @@ -401,7 +768,7 @@ void LocalPointerTest::TestLocalXyzPointerNull() { IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerNull/LocalUCalendarPointer"); LocalUCalendarPointer null; LocalUCalendarPointer cal(ucal_open(NULL, 0, "root", UCAL_GREGORIAN, errorCode)); - if(!errorCode.logDataIfFailureAndReset("ucal_open()")) { + if(!errorCode.errDataIfFailureAndReset("ucal_open()")) { cal.adoptInstead(NULL); } } @@ -428,7 +795,7 @@ void LocalPointerTest::TestLocalXyzPointerNull() { LocalURegularExpressionPointer null; LocalURegularExpressionPointer regex( uregex_open(pattern.getBuffer(), pattern.length(), 0, NULL, errorCode)); - if(!errorCode.logDataIfFailureAndReset("urege_open()")) { + if(!errorCode.errDataIfFailureAndReset("urege_open()")) { regex.adoptInstead(NULL); } } @@ -441,10 +808,91 @@ void LocalPointerTest::TestLocalXyzPointerNull() { LocalUTransliteratorPointer null; LocalUTransliteratorPointer trans( utrans_openU(id.getBuffer(), id.length(), UTRANS_FORWARD, NULL, 0, NULL, errorCode)); - if(!errorCode.logDataIfFailureAndReset("utrans_openU()")) { + if(!errorCode.errDataIfFailureAndReset("utrans_openU()")) { trans.adoptInstead(NULL); } } #endif /* !UCONFIG_NO_TRANSLITERATION */ } + +/** EnumSet test **/ +#include "unicode/enumset.h" + +class EnumSetTest : public IntlTest { +public: + EnumSetTest() {} + virtual void runIndexedTest(int32_t index, UBool exec, const char *&name, char *par=NULL); + void TestEnumSet(); +}; + +static IntlTest *createEnumSetTest() { + return new EnumSetTest(); +} + +void EnumSetTest::runIndexedTest(int32_t index, UBool exec, const char *&name, char * /*par*/) { + TESTCASE_AUTO_BEGIN; + TESTCASE_AUTO(TestEnumSet); + TESTCASE_AUTO_END; +} +enum myEnum { + MAX_NONBOOLEAN=-1, + THING1, + THING2, + THING3, + LIMIT_BOOLEAN +}; + +void EnumSetTest::TestEnumSet() { + EnumSet + flags; + + logln("Enum is from [%d..%d]\n", MAX_NONBOOLEAN+1, + LIMIT_BOOLEAN); + + TEST_ASSERT_TRUE(flags.get(THING1) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == FALSE); + + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + logln("Value now: %d\n", flags.getAll()); + flags.clear(); + logln("clear -Value now: %d\n", flags.getAll()); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + TEST_ASSERT_TRUE(flags.get(THING1) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == FALSE); + flags.add(THING1); + logln("set THING1 -Value now: %d\n", flags.getAll()); + TEST_ASSERT_TRUE(flags.get(THING1) == TRUE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == FALSE); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + flags.add(THING3); + logln("set THING3 -Value now: %d\n", flags.getAll()); + TEST_ASSERT_TRUE(flags.get(THING1) == TRUE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == TRUE); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + flags.remove(THING2); + TEST_ASSERT_TRUE(flags.get(THING1) == TRUE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == TRUE); + logln("remove THING2 -Value now: %d\n", flags.getAll()); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + flags.remove(THING1); + TEST_ASSERT_TRUE(flags.get(THING1) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == TRUE); + logln("remove THING1 -Value now: %d\n", flags.getAll()); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + + flags.clear(); + logln("clear -Value now: %d\n", flags.getAll()); + logln("get(thing1)=%d, get(thing2)=%d, get(thing3)=%d\n", flags.get(THING1), flags.get(THING2), flags.get(THING3)); + TEST_ASSERT_TRUE(flags.get(THING1) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING2) == FALSE); + TEST_ASSERT_TRUE(flags.get(THING3) == FALSE); +}