Skip to content

Commit

Permalink
Convert collator test to cppunit.
Browse files Browse the repository at this point in the history
  • Loading branch information
pulkomandy committed Sep 29, 2014
1 parent af59f5c commit 706f3f5
Show file tree
Hide file tree
Showing 6 changed files with 114 additions and 27 deletions.
80 changes: 80 additions & 0 deletions src/tests/kits/locale/CollatorTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,80 @@
/*
* Copyright 2014 Haiku, Inc.
* Distributed under the terms of the MIT License.
*/


#include "CollatorTest.h"

#include <Collator.h>
#include <Locale.h>
#include <LocaleRoster.h>

#include <cppunit/TestCaller.h>
#include <cppunit/TestSuite.h>


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", "", {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>(
"CollatorTest::TestSortKeys", &CollatorTest::TestSortKeys));

parent.addTest("CollatorTest", &suite);
}
24 changes: 24 additions & 0 deletions src/tests/kits/locale/CollatorTest.h
Original file line number Diff line number Diff line change
@@ -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 <TestCase.h>
#include <TestSuite.h>


class CollatorTest: public BTestCase {
public:
CollatorTest();
virtual ~CollatorTest();

void TestSortKeys();

static void AddTests(BTestSuite& suite);
};


#endif
1 change: 1 addition & 0 deletions src/tests/kits/locale/Jamfile
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,7 @@ Addon catalogTestAddOn
UnitTestLib localekittest.so :
LocaleKitTestAddon.cpp

CollatorTest.cpp
UnicodeCharTest.cpp

: be [ TargetLibstdc++ ]
Expand Down
2 changes: 2 additions & 0 deletions src/tests/kits/locale/LocaleKitTestAddon.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@
#include <TestSuite.h>
#include <TestSuiteAddon.h>

#include "CollatorTest.h"
#include "UnicodeCharTest.h"


Expand All @@ -15,6 +16,7 @@ getTestSuite()
{
BTestSuite* suite = new BTestSuite("LocaleKit");

CollatorTest::AddTests(*suite);
UnicodeCharTest::AddTests(*suite);

return suite;
Expand Down
11 changes: 7 additions & 4 deletions src/tests/kits/locale/collatorTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include <Collator.h>
#include <Locale.h>
#include <LocaleRoster.h>
#include <Message.h>

#include <stdio.h>
Expand Down Expand Up @@ -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;
Expand Down Expand Up @@ -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");
Expand All @@ -176,7 +179,7 @@ main(int argc, char **argv)
fprintf(stderr, "Unarchiving failed!\n");

delete unarchived;
gCollator = be_locale->Collator();
gCollator = &defaultCollator;
}
}

Expand Down
23 changes: 0 additions & 23 deletions src/tests/kits/locale/localeTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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", "", "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;
Expand Down

0 comments on commit 706f3f5

Please sign in to comment.