From 706f3f5396f5579fffda9a78107f99e8ac92c02a Mon Sep 17 00:00:00 2001 From: Adrien Destugues Date: Mon, 29 Sep 2014 17:28:49 +0200 Subject: [PATCH] Convert collator test to cppunit. --- src/tests/kits/locale/CollatorTest.cpp | 80 ++++++++++++++++++++ src/tests/kits/locale/CollatorTest.h | 24 ++++++ src/tests/kits/locale/Jamfile | 1 + src/tests/kits/locale/LocaleKitTestAddon.cpp | 2 + src/tests/kits/locale/collatorTest.cpp | 11 ++- src/tests/kits/locale/localeTest.cpp | 23 ------ 6 files changed, 114 insertions(+), 27 deletions(-) create mode 100644 src/tests/kits/locale/CollatorTest.cpp create mode 100644 src/tests/kits/locale/CollatorTest.h diff --git a/src/tests/kits/locale/CollatorTest.cpp b/src/tests/kits/locale/CollatorTest.cpp new file mode 100644 index 0000000000..3dee2ac226 --- /dev/null +++ b/src/tests/kits/locale/CollatorTest.cpp @@ -0,0 +1,80 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ + + +#include "CollatorTest.h" + +#include +#include +#include + +#include +#include + + +CollatorTest::CollatorTest() +{ +} + + +CollatorTest::~CollatorTest() +{ +} + + +void +CollatorTest::TestSortKeys() +{ + struct Test { + char* first; + char* second; + int sign[3]; + }; + + BCollator collator; + BLocaleRoster::Default()->GetDefaultLocale()->GetCollator(&collator); + const Test tests[] = { + {"gehen", "géhen", {0, -1, -1}}, + {"aus", "äUß", {-1, -1, -1}}, + {"auss", "äUß", {0, -1, -1}}, + {"WO", "wÖ", {0, -1, -1}}, + {"SO", "so", {0, 0, 1}}, + {"açñ", "acn", {0, 1, 1}}, + {NULL, NULL, {0, 0, 0}} + }; + + for (int32 i = 0; tests[i].first != NULL; i++) { + NextSubTest(); + + for (int32 strength = B_COLLATE_PRIMARY; strength < 4; strength++) { + BString a, b; + collator.GetSortKey(tests[i].first, &a, strength); + collator.GetSortKey(tests[i].second, &b, strength); + + int difference = collator.Compare(tests[i].first, tests[i].second, + strength); + CPPUNIT_ASSERT_EQUAL(tests[i].sign[strength - 1], difference); + int keydiff = strcmp(a.String(), b.String()); + // Check that the keys compare the same as the strings. Either both + // are 0, or both have the same sign. + if (difference == 0) + CPPUNIT_ASSERT_EQUAL(0, keydiff); + else + CPPUNIT_ASSERT(keydiff * difference > 0); + } + } +} + + +/*static*/ void +CollatorTest::AddTests(BTestSuite& parent) +{ + CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CollatorTest"); + + suite.addTest(new CppUnit::TestCaller( + "CollatorTest::TestSortKeys", &CollatorTest::TestSortKeys)); + + parent.addTest("CollatorTest", &suite); +} diff --git a/src/tests/kits/locale/CollatorTest.h b/src/tests/kits/locale/CollatorTest.h new file mode 100644 index 0000000000..4a1765acd2 --- /dev/null +++ b/src/tests/kits/locale/CollatorTest.h @@ -0,0 +1,24 @@ +/* + * Copyright 2014 Haiku, Inc. + * Distributed under the terms of the MIT License. + */ +#ifndef COLLATOR_TEST_H +#define COLLATOR_TEST_H + + +#include +#include + + +class CollatorTest: public BTestCase { +public: + CollatorTest(); + virtual ~CollatorTest(); + + void TestSortKeys(); + + static void AddTests(BTestSuite& suite); +}; + + +#endif diff --git a/src/tests/kits/locale/Jamfile b/src/tests/kits/locale/Jamfile index f22aa9d32a..caa88e56c7 100644 --- a/src/tests/kits/locale/Jamfile +++ b/src/tests/kits/locale/Jamfile @@ -51,6 +51,7 @@ Addon catalogTestAddOn UnitTestLib localekittest.so : LocaleKitTestAddon.cpp + CollatorTest.cpp UnicodeCharTest.cpp : be [ TargetLibstdc++ ] diff --git a/src/tests/kits/locale/LocaleKitTestAddon.cpp b/src/tests/kits/locale/LocaleKitTestAddon.cpp index 81adfd6e26..33811e288a 100644 --- a/src/tests/kits/locale/LocaleKitTestAddon.cpp +++ b/src/tests/kits/locale/LocaleKitTestAddon.cpp @@ -7,6 +7,7 @@ #include #include +#include "CollatorTest.h" #include "UnicodeCharTest.h" @@ -15,6 +16,7 @@ getTestSuite() { BTestSuite* suite = new BTestSuite("LocaleKit"); + CollatorTest::AddTests(*suite); UnicodeCharTest::AddTests(*suite); return suite; diff --git a/src/tests/kits/locale/collatorTest.cpp b/src/tests/kits/locale/collatorTest.cpp index a9b96062a1..a49fb4ce18 100644 --- a/src/tests/kits/locale/collatorTest.cpp +++ b/src/tests/kits/locale/collatorTest.cpp @@ -6,6 +6,7 @@ #include #include +#include #include #include @@ -126,6 +127,9 @@ main(int argc, char **argv) bool ignorePunctuation = false; char *addon = NULL; + BCollator defaultCollator; + BLocaleRoster::Default()->GetDefaultLocale()->GetCollator(&defaultCollator); + while ((++argv)[0]) { if (argv[0][0] == '-' && argv[0][1] != '-') { char *arg = argv[0] + 1; @@ -155,13 +159,12 @@ main(int argc, char **argv) // load the collator add-on if necessary - if (addon != NULL) { + if (addon != NULL) gCollator = new BCollator(addon, strength, true); - } if (gCollator == NULL) { printf("--------- Use standard collator! -----------\n"); - gCollator = be_locale->Collator(); + gCollator = &defaultCollator; } printf("test archiving/unarchiving collator\n"); @@ -176,7 +179,7 @@ main(int argc, char **argv) fprintf(stderr, "Unarchiving failed!\n"); delete unarchived; - gCollator = be_locale->Collator(); + gCollator = &defaultCollator; } } diff --git a/src/tests/kits/locale/localeTest.cpp b/src/tests/kits/locale/localeTest.cpp index f24d985159..43600273fa 100644 --- a/src/tests/kits/locale/localeTest.cpp +++ b/src/tests/kits/locale/localeTest.cpp @@ -12,29 +12,6 @@ int main() { - // Test BCollator class - - BCollator collator; - BLocaleRoster::Default()->GetDefaultLocale()->GetCollator(&collator); - const char *strings[] = {"gehen", "géhen", "aus", "äUß", "auss", "äUß", - "WO", "wÖ", "SO", "so", "açñ", "acn", NULL}; - const char *strengths[] = {"primary: ", "secondary:", "tertiary: "}; - for (int32 i = 0; strings[i]; i += 2) { - for (int32 strength = B_COLLATE_PRIMARY; strength < 4; strength++) { - BString a, b; - collator.GetSortKey(strings[i], &a, strength); - collator.GetSortKey(strings[i + 1], &b, strength); - - printf("%s sort keys: \"%s\" -> \"%s\", \"%s\" -> \"%s\"\n", - strengths[strength-1], strings[i], a.String(), strings[i+1], - b.String()); - printf("\tcmp = %d (key compare = %d)\n", - collator.Compare(strings[i], strings[i + 1], strength), - strcmp(a.String(), b.String())); - } - putchar('\n'); - } - // Tests the BLanguage class BLanguage language;