+// © 2016 and later: Unicode, Inc. and others.
+// License & terms of use: http://www.unicode.org/copyright.html
/********************************************************************
* COPYRIGHT:
* Copyright (c) 1997-2016, International Business Machines Corporation and
* others. All Rights Reserved.
********************************************************************/
+#include <utility>
/**
* IntlTestUtilities is the medium level test class for everything in the directory "utility".
#include "itutil.h"
#include "strtest.h"
#include "loctest.h"
+#include "localebuildertest.h"
#include "citrtest.h"
#include "ustrtest.h"
#include "ucdtest.h"
extern IntlTest *createSimpleFormatterTest();
extern IntlTest *createUnifiedCacheTest();
extern IntlTest *createQuantityFormatterTest();
-extern IntlTest *createPluralMapTest();
+extern IntlTest *createPluralMapTest();
+#if !UCONFIG_NO_FORMATTING
+extern IntlTest *createStaticUnicodeSetsTest();
+#endif
#define CASE(id, test) case id: \
callTest(*test, par);
}
break;
+ case 24:
+ name = "StaticUnicodeSetsTest";
+#if !UCONFIG_NO_FORMATTING
+ if (exec) {
+ logln("TestSuite StaticUnicodeSetsTest---"); logln();
+ LocalPointer<IntlTest> test(createStaticUnicodeSetsTest());
+ callTest(*test, par);
+ }
+#endif
+ break;
+ CASE(25, LocaleBuilderTest);
default: name = ""; break; //needed to end loop
}
}
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
}
}
}
}
+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 destructor: expected success but got error: 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 destructor: expected success but got error: 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 expected success but got error: 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 expected success but got error: 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 data: expected success but got error: 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 data: expected success but got error: 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);
+ }
+
+ // Check expectFailure
+ helper.expectedErrln = u"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ILLEGAL_PAD_POSITION";
+ helper.expectedDataErr = FALSE;
+ helper.seenError = FALSE;
+ {
+ IcuTestErrorCode testStatus(helper, "EEE");
+ testStatus.set(U_ILLEGAL_PAD_POSITION);
+ testStatus.expectErrorAndReset(U_ILLEGAL_PAD_POSITION);
+ assertFalse("Should NOT have seen an error", helper.seenError);
+ testStatus.set(U_ILLEGAL_PAD_POSITION);
+ testStatus.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND);
+ assertTrue("Should have seen an error", helper.seenError);
+ helper.seenError = FALSE;
+ helper.expectedErrln = u"EEE expected: U_ILLEGAL_CHAR_FOUND but got error: U_ZERO_ERROR scope: scopety scope - 5.4300";
+ testStatus.setScope("scopety scope");
+ testStatus.set(U_ILLEGAL_PAD_POSITION);
+ testStatus.expectErrorAndReset(U_ILLEGAL_PAD_POSITION, "%6.4f", 5.43);
+ assertFalse("Should NOT have seen an error", helper.seenError);
+ testStatus.expectErrorAndReset(U_ILLEGAL_CHAR_FOUND, "%6.4f", 5.43);
+ assertTrue("Should have seen an error", helper.seenError);
+ }
+}
+
+
class LocalPointerTest : public IntlTest {
public:
LocalPointerTest() {}
void TestLocalPointer();
void TestLocalPointerMoveSwap();
+ void TestLocalPointerStdUniquePtr();
void TestLocalArray();
void TestLocalArrayMoveSwap();
+ void TestLocalArrayStdUniquePtr();
void TestLocalXyzPointer();
void TestLocalXyzPointerMoveSwap();
void TestLocalXyzPointerNull();
+ void TestLocalXyzStdUniquePtr();
};
static IntlTest *createLocalPointerTest() {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestLocalPointer);
TESTCASE_AUTO(TestLocalPointerMoveSwap);
+ TESTCASE_AUTO(TestLocalPointerStdUniquePtr);
TESTCASE_AUTO(TestLocalArray);
TESTCASE_AUTO(TestLocalArrayMoveSwap);
+ TESTCASE_AUTO(TestLocalArrayStdUniquePtr);
TESTCASE_AUTO(TestLocalXyzPointer);
TESTCASE_AUTO(TestLocalXyzPointerMoveSwap);
TESTCASE_AUTO(TestLocalXyzPointerNull);
+ TESTCASE_AUTO(TestLocalXyzStdUniquePtr);
TESTCASE_AUTO_END;
}
// destructor
}
+// Try to avoid clang -Wself-move warnings from s1 = std::move(s1);
+template<typename T>
+void moveFrom(T &dest, T &src) {
+ dest = std::move(src);
+}
+
void LocalPointerTest::TestLocalPointerMoveSwap() {
UnicodeString *p1 = new UnicodeString((UChar)0x61);
UnicodeString *p2 = new UnicodeString((UChar)0x62);
errln("swap(LocalPointer) did not swap back");
}
LocalPointer<UnicodeString> s3;
- s3.moveFrom(s1);
+ s3 = std::move(s1);
if(s3.getAlias() != p1 || s1.isValid()) {
- errln("LocalPointer.moveFrom() did not move");
+ errln("LocalPointer = std::move() did not move");
}
-#if U_HAVE_RVALUE_REFERENCES
infoln("TestLocalPointerMoveSwap() with rvalue references");
s1 = static_cast<LocalPointer<UnicodeString> &&>(s3);
if(s1.getAlias() != p1 || s3.isValid()) {
if(s4.getAlias() != p2 || s2.isValid()) {
errln("LocalPointer move constructor did not move");
}
-#else
- infoln("TestLocalPointerMoveSwap() without rvalue references");
-#endif
// 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);
+ moveFrom(s1, s1);
+ moveFrom(s3, s3);
+}
+
+void LocalPointerTest::TestLocalPointerStdUniquePtr() {
+ auto* ptr = new UnicodeString((UChar32)0x50005);
+ // Implicit conversion operator
+ std::unique_ptr<UnicodeString> s = LocalPointer<UnicodeString>(ptr);
+ // Explicit move constructor
+ LocalPointer<UnicodeString> s2(std::move(s));
+ // Conversion operator should also work with std::move
+ s = std::move(s2);
+ // Back again with move assignment
+ s2 = std::move(s);
+ assertTrue("Pointer should remain the same", ptr == s2.getAlias());
}
// Exercise almost every LocalArray method (but not LocalPointerBase).
errln("swap(LocalArray) did not swap back");
}
LocalArray<UnicodeString> a3;
- a3.moveFrom(a1);
+ a3 = std::move(a1);
if(a3.getAlias() != p1 || a1.isValid()) {
- errln("LocalArray.moveFrom() did not move");
+ errln("LocalArray = std::move() did not move");
}
-#if U_HAVE_RVALUE_REFERENCES
infoln("TestLocalArrayMoveSwap() with rvalue references");
a1 = static_cast<LocalArray<UnicodeString> &&>(a3);
if(a1.getAlias() != p1 || a3.isValid()) {
if(a4.getAlias() != p2 || a2.isValid()) {
errln("LocalArray move constructor did not move");
}
-#else
- infoln("TestLocalArrayMoveSwap() without rvalue references");
-#endif
// 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);
+ moveFrom(a1, a1);
+ moveFrom(a3, a3);
+}
+
+void LocalPointerTest::TestLocalArrayStdUniquePtr() {
+ auto* ptr = new UnicodeString[2];
+ // Implicit conversion operator
+ std::unique_ptr<UnicodeString[]> a = LocalArray<UnicodeString>(ptr);
+ // Explicit move constructor
+ LocalArray<UnicodeString> a2(std::move(a));
+ // Conversion operator should also work with std::move
+ a = std::move(a2);
+ // Back again with move assignment
+ a2 = std::move(a);
+ assertTrue("Pointer should remain the same", ptr == a2.getAlias());
}
#include "unicode/ucnvsel.h"
#include "unicode/unorm2.h"
#include "unicode/uregex.h"
#include "unicode/utrans.h"
+#include "unicode/uformattedvalue.h"
// Use LocalXyzPointer types that are not covered elsewhere in the intltest suite.
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()) {
#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()) {
}
LocalUDateTimePatternGeneratorPointer patgen(udatpg_open("root", errorCode));
- if(errorCode.logDataIfFailureAndReset("udatpg_open()")) {
+ if(errorCode.errDataIfFailureAndReset("udatpg_open()")) {
return;
}
if(patgen.isNull()) {
}
LocalULocaleDisplayNamesPointer ldn(uldn_open("de-CH", ULDN_STANDARD_NAMES, errorCode));
- if(errorCode.logIfFailureAndReset("uldn_open()")) {
+ if(errorCode.errIfFailureAndReset("uldn_open()")) {
return;
}
if(ldn.isNull()) {
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()) {
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()) {
#if !UCONFIG_NO_IDNA
LocalUIDNAPointer idna(uidna_openUTS46(0, errorCode));
- if(errorCode.logIfFailureAndReset("uidna_openUTS46()")) {
+ if(errorCode.errIfFailureAndReset("uidna_openUTS46()")) {
return;
}
if(idna.isNull()) {
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()) {
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()) {
IcuTestErrorCode errorCode(*this, "TestLocalXyzPointerMoveSwap");
const UNormalizer2 *nfc=unorm2_getNFCInstance(errorCode);
const UNormalizer2 *nfd=unorm2_getNFDInstance(errorCode);
- if(errorCode.logIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
+ if(errorCode.errIfFailureAndReset("unorm2_getNF[CD]Instance()")) {
return;
}
UnicodeSet emptySet;
UNormalizer2 *p2 = unorm2_openFiltered(nfd, emptySet.toUSet(), errorCode);
LocalUNormalizer2Pointer f1(p1);
LocalUNormalizer2Pointer f2(p2);
- if(errorCode.logIfFailureAndReset("unorm2_openFiltered()")) {
+ if(errorCode.errIfFailureAndReset("unorm2_openFiltered()")) {
return;
}
if(f1.isNull() || f2.isNull()) {
errln("swap(LocalUNormalizer2Pointer) did not swap back");
}
LocalUNormalizer2Pointer f3;
- f3.moveFrom(f1);
+ f3 = std::move(f1);
if(f3.getAlias() != p1 || f1.isValid()) {
- errln("LocalUNormalizer2Pointer.moveFrom() did not move");
+ errln("LocalUNormalizer2Pointer = std::move() did not move");
}
-#if U_HAVE_RVALUE_REFERENCES
infoln("TestLocalXyzPointerMoveSwap() with rvalue references");
f1 = static_cast<LocalUNormalizer2Pointer &&>(f3);
if(f1.getAlias() != p1 || f3.isValid()) {
if(f4.getAlias() != p2 || f2.isValid()) {
errln("LocalUNormalizer2Pointer move constructor did not move");
}
-#else
- infoln("TestLocalXyzPointerMoveSwap() without rvalue references");
-#endif
// 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);
+ moveFrom(f1, f1);
+ moveFrom(f3, f3);
#endif /* !UCONFIG_NO_NORMALIZATION */
}
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);
}
}
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);
}
}
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);
}
}
}
+void LocalPointerTest::TestLocalXyzStdUniquePtr() {
+ IcuTestErrorCode status(*this, "TestLocalXyzStdUniquePtr");
+#if !UCONFIG_NO_FORMATTING
+ auto* ptr = ucfpos_open(status);
+ // Implicit conversion operator
+ std::unique_ptr<UConstrainedFieldPosition, void(*)(UConstrainedFieldPosition*)> a =
+ LocalUConstrainedFieldPositionPointer(ptr);
+ // Explicit move constructor
+ LocalUConstrainedFieldPositionPointer a2(std::move(a));
+ // Conversion operator should also work with std::move
+ a = std::move(a2);
+ // Back again with move assignment
+ a2 = std::move(a);
+ assertTrue("Pointer should remain the same", ptr == a2.getAlias());
+#endif // UCONFIG_NO_FORMATTING
+}
+
/** EnumSet test **/
#include "unicode/enumset.h"
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);
+ assertFalse(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertFalse(WHERE, flags.get(THING3));
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);
+ assertFalse(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertFalse(WHERE, flags.get(THING3));
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);
+ assertTrue(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertFalse(WHERE, flags.get(THING3));
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);
+ assertTrue(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertTrue(WHERE, flags.get(THING3));
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);
+ assertTrue(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertTrue(WHERE, flags.get(THING3));
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);
+ assertFalse(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertTrue(WHERE, flags.get(THING3));
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);
+ assertFalse(WHERE, flags.get(THING1));
+ assertFalse(WHERE, flags.get(THING2));
+ assertFalse(WHERE, flags.get(THING3));
}