1 /* 2 * Copyright 2014 Haiku, Inc. 3 * Distributed under the terms of the MIT License. 4 */ 5 6 7 #include "CollatorTest.h" 8 9 #include <Collator.h> 10 #include <Locale.h> 11 #include <LocaleRoster.h> 12 13 #include <cppunit/TestCaller.h> 14 #include <cppunit/TestSuite.h> 15 16 17 CollatorTest::CollatorTest() 18 { 19 } 20 21 22 CollatorTest::~CollatorTest() 23 { 24 } 25 26 27 void 28 CollatorTest::TestSortKeys() 29 { 30 struct Test { 31 char* first; 32 char* second; 33 int sign[3]; 34 }; 35 36 BCollator collator; 37 BLocaleRoster::Default()->GetDefaultLocale()->GetCollator(&collator); 38 const Test tests[] = { 39 {"gehen", "géhen", {0, -1, -1}}, 40 {"aus", "äUß", {-1, -1, -1}}, 41 {"auss", "äUß", {0, -1, -1}}, 42 {"WO", "wÖ", {0, -1, -1}}, 43 {"SO", "so", {0, 0, 1}}, 44 {"açñ", "acn", {0, 1, 1}}, 45 {NULL, NULL, {0, 0, 0}} 46 }; 47 48 for (int32 i = 0; tests[i].first != NULL; i++) { 49 NextSubTest(); 50 51 for (int32 strength = B_COLLATE_PRIMARY; strength < 4; strength++) { 52 BString a, b; 53 collator.SetStrength(strength); 54 collator.GetSortKey(tests[i].first, &a); 55 collator.GetSortKey(tests[i].second, &b); 56 57 int difference = collator.Compare(tests[i].first, tests[i].second); 58 CPPUNIT_ASSERT_EQUAL(tests[i].sign[strength - 1], difference); 59 int keydiff = strcmp(a.String(), b.String()); 60 // Check that the keys compare the same as the strings. Either both 61 // are 0, or both have the same sign. 62 if (difference == 0) 63 CPPUNIT_ASSERT_EQUAL(0, keydiff); 64 else 65 CPPUNIT_ASSERT(keydiff * difference > 0); 66 } 67 } 68 } 69 70 71 /*static*/ void 72 CollatorTest::AddTests(BTestSuite& parent) 73 { 74 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CollatorTest"); 75 76 suite.addTest(new CppUnit::TestCaller<CollatorTest>( 77 "CollatorTest::TestSortKeys", &CollatorTest::TestSortKeys)); 78 79 parent.addTest("CollatorTest", &suite); 80 } 81