xref: /haiku/src/tests/kits/locale/CollatorTest.cpp (revision a19a18f5536ace3505eee3687e9d5b292391a6f3)
1706f3f53SAdrien Destugues /*
2706f3f53SAdrien Destugues  * Copyright 2014 Haiku, Inc.
3706f3f53SAdrien Destugues  * Distributed under the terms of the MIT License.
4706f3f53SAdrien Destugues  */
5706f3f53SAdrien Destugues 
6706f3f53SAdrien Destugues 
7706f3f53SAdrien Destugues #include "CollatorTest.h"
8706f3f53SAdrien Destugues 
9706f3f53SAdrien Destugues #include <Collator.h>
10706f3f53SAdrien Destugues #include <Locale.h>
11706f3f53SAdrien Destugues #include <LocaleRoster.h>
12706f3f53SAdrien Destugues 
13706f3f53SAdrien Destugues #include <cppunit/TestCaller.h>
14706f3f53SAdrien Destugues #include <cppunit/TestSuite.h>
15706f3f53SAdrien Destugues 
16706f3f53SAdrien Destugues 
17706f3f53SAdrien Destugues CollatorTest::CollatorTest()
18706f3f53SAdrien Destugues {
19706f3f53SAdrien Destugues }
20706f3f53SAdrien Destugues 
21706f3f53SAdrien Destugues 
22706f3f53SAdrien Destugues CollatorTest::~CollatorTest()
23706f3f53SAdrien Destugues {
24706f3f53SAdrien Destugues }
25706f3f53SAdrien Destugues 
26706f3f53SAdrien Destugues 
27706f3f53SAdrien Destugues void
28706f3f53SAdrien Destugues CollatorTest::TestSortKeys()
29706f3f53SAdrien Destugues {
30706f3f53SAdrien Destugues 	struct Test {
31706f3f53SAdrien Destugues 		char* first;
32706f3f53SAdrien Destugues 		char* second;
33706f3f53SAdrien Destugues 		int sign[3];
34706f3f53SAdrien Destugues 	};
35706f3f53SAdrien Destugues 
36706f3f53SAdrien Destugues 	BCollator collator;
37706f3f53SAdrien Destugues 	BLocaleRoster::Default()->GetDefaultLocale()->GetCollator(&collator);
38706f3f53SAdrien Destugues 	const Test tests[] = {
39706f3f53SAdrien Destugues 		{"gehen", "géhen", {0, -1, -1}},
40706f3f53SAdrien Destugues 		{"aus", "äUß", {-1, -1, -1}},
41706f3f53SAdrien Destugues 		{"auss", "äUß", {0, -1, -1}},
42706f3f53SAdrien Destugues 		{"WO", "wÖ", {0, -1, -1}},
43706f3f53SAdrien Destugues 		{"SO", "so", {0, 0, 1}},
44706f3f53SAdrien Destugues 		{"açñ", "acn", {0, 1, 1}},
45706f3f53SAdrien Destugues 		{NULL, NULL, {0, 0, 0}}
46706f3f53SAdrien Destugues 	};
47706f3f53SAdrien Destugues 
48706f3f53SAdrien Destugues 	for (int32 i = 0; tests[i].first != NULL; i++) {
49706f3f53SAdrien Destugues 		NextSubTest();
50706f3f53SAdrien Destugues 
51706f3f53SAdrien Destugues 		for (int32 strength = B_COLLATE_PRIMARY; strength < 4; strength++) {
52706f3f53SAdrien Destugues 			BString a, b;
53*a19a18f5SAdrien Destugues 			collator.SetStrength(strength);
54*a19a18f5SAdrien Destugues 			collator.GetSortKey(tests[i].first, &a);
55*a19a18f5SAdrien Destugues 			collator.GetSortKey(tests[i].second, &b);
56706f3f53SAdrien Destugues 
57*a19a18f5SAdrien Destugues 			int difference = collator.Compare(tests[i].first, tests[i].second);
58706f3f53SAdrien Destugues 			CPPUNIT_ASSERT_EQUAL(tests[i].sign[strength - 1], difference);
59706f3f53SAdrien Destugues 			int keydiff = strcmp(a.String(), b.String());
60706f3f53SAdrien Destugues 			// Check that the keys compare the same as the strings. Either both
61706f3f53SAdrien Destugues 			// are 0, or both have the same sign.
62706f3f53SAdrien Destugues 			if (difference == 0)
63706f3f53SAdrien Destugues 				CPPUNIT_ASSERT_EQUAL(0, keydiff);
64706f3f53SAdrien Destugues 			else
65706f3f53SAdrien Destugues 				CPPUNIT_ASSERT(keydiff * difference > 0);
66706f3f53SAdrien Destugues 		}
67706f3f53SAdrien Destugues 	}
68706f3f53SAdrien Destugues }
69706f3f53SAdrien Destugues 
70706f3f53SAdrien Destugues 
71706f3f53SAdrien Destugues /*static*/ void
72706f3f53SAdrien Destugues CollatorTest::AddTests(BTestSuite& parent)
73706f3f53SAdrien Destugues {
74706f3f53SAdrien Destugues 	CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CollatorTest");
75706f3f53SAdrien Destugues 
76706f3f53SAdrien Destugues 	suite.addTest(new CppUnit::TestCaller<CollatorTest>(
77706f3f53SAdrien Destugues 		"CollatorTest::TestSortKeys", &CollatorTest::TestSortKeys));
78706f3f53SAdrien Destugues 
79706f3f53SAdrien Destugues 	parent.addTest("CollatorTest", &suite);
80706f3f53SAdrien Destugues }
81