From 6d5aaf851445b4bde65f9597b37c1786ffc3b771 Mon Sep 17 00:00:00 2001 From: Alan Liu Date: Thu, 12 Feb 2004 01:09:13 +0000 Subject: [PATCH] ICU-3090 add JUnit-like assert methods and ctou() X-SVN-Rev: 14488 --- icu4c/source/test/intltest/intltest.cpp | 55 +++++++++++++++++++++++++ icu4c/source/test/intltest/intltest.h | 10 +++++ 2 files changed, 65 insertions(+) diff --git a/icu4c/source/test/intltest/intltest.cpp b/icu4c/source/test/intltest/intltest.cpp index cbaf0d496b0..42c9af0c728 100644 --- a/icu4c/source/test/intltest/intltest.cpp +++ b/icu4c/source/test/intltest/intltest.cpp @@ -1275,6 +1275,10 @@ UnicodeString CharsToUnicodeString(const char* chars) return str.unescape(); } +UnicodeString ctou(const char* chars) { + return CharsToUnicodeString(chars); +} + #define RAND_M (714025) #define RAND_IA (1366) #define RAND_IC (150889) @@ -1319,6 +1323,57 @@ float IntlTest::random() { return random(&RAND_SEED); } +#define VERBOSE_ASSERTIONS + +UBool IntlTest::assertTrue(const char* message, UBool condition) { + if (!condition) { + errln("FAIL: assertTrue() failed: %s", message); + } +#ifdef VERBOSE_ASSERTIONS + else { + logln("Ok: %s", message); + } +#endif + return condition; +} + +UBool IntlTest::assertFalse(const char* message, UBool condition) { + if (condition) { + errln("FAIL: assertFalse() failed: %s", message); + } +#ifdef VERBOSE_ASSERTIONS + else { + logln("Ok: %s", message); + } +#endif + return !condition; +} + +UBool IntlTest::assertSuccess(const char* message, UErrorCode ec) { + if (U_FAILURE(ec)) { + errln("FAIL: %s (%s)", message, u_errorName(ec)); + return FALSE; + } + return TRUE; +} + +UBool IntlTest::assertEquals(const char* message, + const UnicodeString& expected, + const UnicodeString& actual) { + if (expected != actual) { + errln((UnicodeString)"FAIL: " + message + "; got " + + prettify(actual) + + "; expected " + prettify(expected)); + return FALSE; + } +#ifdef VERBOSE_ASSERTIONS + else { + logln((UnicodeString)"Ok: " + message + "; got " + prettify(actual)); + } +#endif + return TRUE; +} + /* * Hey, Emacs, please set the following: * diff --git a/icu4c/source/test/intltest/intltest.h b/icu4c/source/test/intltest/intltest.h index d53655e3b43..425c9091641 100644 --- a/icu4c/source/test/intltest/intltest.h +++ b/icu4c/source/test/intltest/intltest.h @@ -130,6 +130,13 @@ public: static float random(); protected: + /* JUnit-like assertions. Each returns TRUE if it succeeds. */ + UBool assertTrue(const char* message, UBool condition); + UBool assertFalse(const char* message, UBool condition); + UBool assertSuccess(const char* message, UErrorCode ec); + UBool assertEquals(const char* message, const UnicodeString& expected, + const UnicodeString& actual); + virtual void runIndexedTest( int32_t index, UBool exec, const char* &name, char* par = NULL ); // overide ! virtual UBool runTestLoop( char* testname, char* par ); @@ -202,4 +209,7 @@ void it_errln( UnicodeString message ); */ extern UnicodeString CharsToUnicodeString(const char* chars); +/* alias for CharsToUnicodeString */ +extern UnicodeString ctou(const char* chars); + #endif // _INTLTEST