From af59f5ca42e5107e22da9a9abd67826e6028ccf0 Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 29 Sep 2014 16:08:45 +0200 Subject: [PATCH] Convert BUnicodeChar tests to cppunit. --- src/tests/kits/locale/Jamfile | 11 +- src/tests/kits/locale/LocaleKitTestAddon.cpp | 21 +++ src/tests/kits/locale/UnicodeCharTest.cpp | 161 +++++++++++++++++++ src/tests/kits/locale/UnicodeCharTest.h | 41 +++++ src/tests/kits/locale/localeTest.cpp | 54 ------- 5 files changed, 233 insertions(+), 55 deletions(-) create mode 100644 src/tests/kits/locale/LocaleKitTestAddon.cpp create mode 100644 src/tests/kits/locale/UnicodeCharTest.cpp create mode 100644 src/tests/kits/locale/UnicodeCharTest.h diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile index a3752b2e74..f22aa9d32a 100644 --- a/src/tests/kits/locale/Jamfile +++ b/src/tests/kits/locale/Jamfile @@ -15,7 +15,8 @@ Depends LocaleKitTests collatorTest localeTest ICUTest - libNumberFormatTests.so +# libNumberFormatTests.so + localekittest.so ; rule LocaleTest @@ -47,4 +48,12 @@ Addon catalogTestAddOn : be ; +UnitTestLib localekittest.so : + LocaleKitTestAddon.cpp + + UnicodeCharTest.cpp + + : be [ TargetLibstdc++ ] + ; + # SubInclude HAIKU_TOP src tests kits locale number_format ; diff --git a/src/tests/kits/locale/LocaleKitTestAddon.cpp b/src/tests/kits/locale/LocaleKitTestAddon.cpp new file mode 100644 index 0000000000..81adfd6e26 --- /dev/null +++ b/src/tests/kits/locale/LocaleKitTestAddon.cpp @@ -0,0 +1,21 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include +#include + +#include "UnicodeCharTest.h" + + +BTestSuite* +getTestSuite() +{ + BTestSuite* suite = new BTestSuite("LocaleKit"); + + UnicodeCharTest::AddTests(*suite); + + return suite; +} diff --git a/src/tests/kits/locale/UnicodeCharTest.cpp b/src/tests/kits/locale/UnicodeCharTest.cpp new file mode 100644 index 0000000000..a65d7dc4d1 --- /dev/null +++ b/src/tests/kits/locale/UnicodeCharTest.cpp @@ -0,0 +1,161 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include "UnicodeCharTest.h" + +#include +#include + +#include +#include + + +UnicodeCharTest::UnicodeCharTest() +{ +} + + +UnicodeCharTest::~UnicodeCharTest() +{ +} + + +void +UnicodeCharTest::TestAscii() +{ + Result results[] = { + {"\x1e", 0, 0, 0, 0, 1, 15, '\x1e', '\x1e'}, + {"\x1f", 0, 0, 0, 0, 1, 15, '\x1f', '\x1f'}, + {" ", 0, 0, 0, 0, 1, 12, ' ', ' '}, + {"!", 0, 0, 0, 0, 1, 23, '!', '!'}, + {"\"", 0, 0, 0, 0, 1, 23, '"', '"'}, + {"#", 0, 0, 0, 0, 1, 23, '#', '#'}, + {"$", 0, 0, 0, 0, 1, 25, '$', '$'}, + {"%", 0, 0, 0, 0, 1, 23, '%', '%'}, + {"&", 0, 0, 0, 0, 1, 23, '&', '&'}, + {"'", 0, 0, 0, 0, 1, 23, '\'', '\''}, + {"(", 0, 0, 0, 0, 1, 20, '(', '('}, + {")", 0, 0, 0, 0, 1, 21, ')', ')'}, + {"*", 0, 0, 0, 0, 1, 23, '*', '*'}, + {"+", 0, 0, 0, 0, 1, 24, '+', '+'}, + {",", 0, 0, 0, 0, 1, 23, ',', ','}, + {"-", 0, 0, 0, 0, 1, 19, '-', '-'}, + {".", 0, 0, 0, 0, 1, 23, '.', '.'}, + {"/", 0, 0, 0, 0, 1, 23, '/', '/'}, + {"0", 0, 1, 0, 0, 1, 9, '0', '0'}, + {"1", 0, 1, 0, 0, 1, 9, '1', '1'}, + {"2", 0, 1, 0, 0, 1, 9, '2', '2'}, + {"3", 0, 1, 0, 0, 1, 9, '3', '3'}, + {"4", 0, 1, 0, 0, 1, 9, '4', '4'}, + {"5", 0, 1, 0, 0, 1, 9, '5', '5'}, + {"6", 0, 1, 0, 0, 1, 9, '6', '6'}, + {"7", 0, 1, 0, 0, 1, 9, '7', '7'}, + {"8", 0, 1, 0, 0, 1, 9, '8', '8'}, + {"9", 0, 1, 0, 0, 1, 9, '9', '9'}, + {":", 0, 0, 0, 0, 1, 23, ':', ':'}, + {";", 0, 0, 0, 0, 1, 23, ';', ';'}, + {"<", 0, 0, 0, 0, 1, 24, '<', '<'}, + {"=", 0, 0, 0, 0, 1, 24, '=', '='}, + {">", 0, 0, 0, 0, 1, 24, '>', '>'}, + {"?", 0, 0, 0, 0, 1, 23, '?', '?'}, + {"@", 0, 0, 0, 0, 1, 23, '@', '@'}, + {"A", 1, 1, 0, 1, 1, 1, 'A', 'a'}, + {"B", 1, 1, 0, 1, 1, 1, 'B', 'b'}, + {"C", 1, 1, 0, 1, 1, 1, 'C', 'c'}, + {"D", 1, 1, 0, 1, 1, 1, 'D', 'd'}, + {"E", 1, 1, 0, 1, 1, 1, 'E', 'e'} + }; + + for (int32 i = 30; i < 70; i++) { + NextSubTest(); + _TestChar(i, results[i - 30]); + } +} + + +void +UnicodeCharTest::TestISO8859() +{ + uint32 chars[] = {(uint8)'\xe4', (uint8)'\xd6', (uint8)'\xdf', + (uint8)'\xe8', (uint8)'\xe1', (uint8)'\xe9', 0}; + + Result results[] = { + {"ä", 1, 1, 1, 0, 1, 2, 196, 228}, + {"Ö", 1, 1, 0, 1, 1, 1, 214, 246}, + {"ß", 1, 1, 1, 0, 1, 2, 223, 223}, + {"è", 1, 1, 1, 0, 1, 2, 200, 232}, + {"á", 1, 1, 1, 0, 1, 2, 193, 225}, + {"é", 1, 1, 1, 0, 1, 2, 201, 233} + }; + + for(int i = 0; chars[i] != 0; i++) { + NextSubTest(); + _TestChar(chars[i], results[i]); + } +} + + +void +UnicodeCharTest::TestUTF8() +{ + const char *utf8chars[] = {"à", "ß", "ñ", "é", "ç", "ä", NULL}; + + Result results[] = { + {"à", 1, 1, 1, 0, 1, 2, 192, 224}, + {"ß", 1, 1, 1, 0, 1, 2, 223, 223}, + {"ñ", 1, 1, 1, 0, 1, 2, 209, 241}, + {"é", 1, 1, 1, 0, 1, 2, 201, 233}, + {"ç", 1, 1, 1, 0, 1, 2, 199, 231}, + {"ä", 1, 1, 1, 0, 1, 2, 196, 228} + }; + + for(int i = 0; utf8chars[i] != 0; i++) { + NextSubTest(); + _TestChar(BUnicodeChar::FromUTF8(utf8chars[i]), results[i]); + } +} + + +/*static*/ void +UnicodeCharTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("UnicodeCharTest"); + + suite.addTest(new CppUnit::TestCaller( + "UnicodeCharTest::TestAscii", &UnicodeCharTest::TestAscii)); + suite.addTest(new CppUnit::TestCaller( + "UnicodeCharTest::TestISO8859", &UnicodeCharTest::TestISO8859)); + suite.addTest(new CppUnit::TestCaller( + "UnicodeCharTest::TestUTF8", &UnicodeCharTest::TestUTF8)); + + parent.addTest("UnicodeCharTest", &suite); +} + + +void +UnicodeCharTest::_ToString(uint32 c, char *text) +{ + BUnicodeChar::ToUTF8(c, &text); + text[0] = '\0'; +} + + +void +UnicodeCharTest::_TestChar(uint32 i, Result result) +{ + char text[16]; + + _ToString(i, text); + CPPUNIT_ASSERT_EQUAL(BString(result.value), text); + CPPUNIT_ASSERT_EQUAL(result.isAlpha, BUnicodeChar::IsAlpha(i)); + CPPUNIT_ASSERT_EQUAL(result.isAlNum, BUnicodeChar::IsAlNum(i)); + CPPUNIT_ASSERT_EQUAL(result.isLower, BUnicodeChar::IsLower(i)); + CPPUNIT_ASSERT_EQUAL(result.isUpper, BUnicodeChar::IsUpper(i)); + CPPUNIT_ASSERT_EQUAL(result.isDefined, BUnicodeChar::IsDefined(i)); + CPPUNIT_ASSERT_EQUAL(result.type, BUnicodeChar::Type(i)); + CPPUNIT_ASSERT_EQUAL(result.toUpper, BUnicodeChar::ToUpper(i)); + CPPUNIT_ASSERT_EQUAL(result.toLower, BUnicodeChar::ToLower(i)); +} diff --git a/src/tests/kits/locale/UnicodeCharTest.h b/src/tests/kits/locale/UnicodeCharTest.h new file mode 100644 index 0000000000..31fa7952fe --- /dev/null +++ b/src/tests/kits/locale/UnicodeCharTest.h @@ -0,0 +1,41 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef UNICODE_CHAR_TEST_H +#define UNICODE_CHAR_TEST_H + + +#include +#include + + +class UnicodeCharTest: public BTestCase { +public: + UnicodeCharTest(); + virtual ~UnicodeCharTest(); + + void TestAscii(); + void TestISO8859(); + void TestUTF8(); + + static void AddTests(BTestSuite& suite); + +private: + struct Result { + const char* value; + bool isAlpha; + bool isAlNum; + bool isLower; + bool isUpper; + bool isDefined; + int type; + int32 toUpper; + int32 toLower; + }; + + void _ToString(uint32 c, char* text); + void _TestChar(uint32 c, Result result); +}; + +#endif diff --git a/src/tests/kits/locale/localeTest.cpp b/src/tests/kits/locale/localeTest.cpp index 02f0e50c7e..f24d985159 100644 --- a/src/tests/kits/locale/localeTest.cpp +++ b/src/tests/kits/locale/localeTest.cpp @@ -9,63 +9,9 @@ #include -void -unicode_char_to_string(uint32 c, char *text) -{ - BUnicodeChar::ToUTF8(c, &text); - text[0] = '\0'; -} - - int main() { - // Test BUnicodeChar class - - char text[16]; - - for (int32 i = 30; i < 70; i++) { - unicode_char_to_string(i, text); - printf("%s: alpha == %d, alNum == %d, lower == %d, upper == %d, " - "defined == %d, charType == %d\n", text, - BUnicodeChar::IsAlpha(i), BUnicodeChar::IsAlNum(i), - BUnicodeChar::IsLower(i), BUnicodeChar::IsUpper(i), - BUnicodeChar::IsDefined(i), BUnicodeChar::Type(i)); - } - - uint32 chars[] = {(uint8)'\xe4', (uint8)'\xd6', (uint8)'\xdf', - (uint8)'\xe8', (uint8)'\xe1', (uint8)'\xe9', 0}; - for (int32 j = 0, i; (i = chars[j]) != 0; j++) { - unicode_char_to_string(i, text); - printf("%s: alpha == %d, alNum == %d, lower == %d, upper == %d, " - "defined == %d, charType == %d\n", text, - BUnicodeChar::IsAlpha(i), BUnicodeChar::IsAlNum(i), - BUnicodeChar::IsLower(i), BUnicodeChar::IsUpper(i), - BUnicodeChar::IsDefined(i), BUnicodeChar::Type(i)); - - unicode_char_to_string(BUnicodeChar::ToUpper(i), text); - printf("toUpper == %s, ", text); - unicode_char_to_string(BUnicodeChar::ToLower(i), text); - printf("toLower == %s\n", text); - } - - const char *utf8chars[] = {"à", "ß", "ñ", "é", "ç", "ä", NULL}; - for (int32 j = 0; utf8chars[j] != 0; j++) { - int32 i = BUnicodeChar::FromUTF8(utf8chars[j]); - unicode_char_to_string(i, text); - printf("%s: alpha == %d, alNum == %d, lower == %d, upper == %d, " - "defined == %d, charType == %d\n", text, - BUnicodeChar::IsAlpha(i), BUnicodeChar::IsAlNum(i), - BUnicodeChar::IsLower(i), BUnicodeChar::IsUpper(i), - BUnicodeChar::IsDefined(i), BUnicodeChar::Type(i)); - - unicode_char_to_string(BUnicodeChar::ToUpper(i), text); - printf("toUpper == %s, ", text); - unicode_char_to_string(BUnicodeChar::ToLower(i), text); - printf("toLower == %s\n", text); - } - printf("%c: digitValue == %ld\n", '8', BUnicodeChar::DigitValue('8')); - // Test BCollator class BCollator collator;