Convert collator test to cppunit.

This commit is contained in:
Adrien Destugues
2014-09-29 18:37:49 +02:00
parent af59f5ca42
commit 706f3f5396
6 changed files with 114 additions and 27 deletions
+80
View File
@@ -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
View File
@@ -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
View File
@@ -51,6 +51,7 @@ Addon catalogTestAddOn
UnitTestLib localekittest.so :
LocaleKitTestAddon.cpp
CollatorTest.cpp
UnicodeCharTest.cpp
: be [ TargetLibstdc++ ]
@@ -7,6 +7,7 @@
#include <TestSuite.h>
#include <TestSuiteAddon.h>
#include "CollatorTest.h"
#include "UnicodeCharTest.h"
@@ -15,6 +16,7 @@ getTestSuite()
{
BTestSuite* suite = new BTestSuite("LocaleKit");
CollatorTest::AddTests(*suite);
UnicodeCharTest::AddTests(*suite);
return suite;
+7 -4
View File
@@ -6,6 +6,7 @@
#include <Collator.h>
#include <Locale.h>
#include <LocaleRoster.h>
#include <Message.h>
#include <stdio.h>
@@ -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;
}
}
-23
View File
@@ -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;