vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / locale / CollatorTest.cpp
blob2fb36da8ebefcc1145ef4123b5b956e2eeb2259a
1 /*
2 * Copyright 2014 Haiku, Inc.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "CollatorTest.h"
9 #include <Collator.h>
10 #include <Locale.h>
11 #include <LocaleRoster.h>
13 #include <cppunit/TestCaller.h>
14 #include <cppunit/TestSuite.h>
17 CollatorTest::CollatorTest()
22 CollatorTest::~CollatorTest()
27 void
28 CollatorTest::TestSortKeys()
30 struct Test {
31 char* first;
32 char* second;
33 int sign[3];
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}}
48 for (int32 i = 0; tests[i].first != NULL; i++) {
49 NextSubTest();
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);
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);
71 /*static*/ void
72 CollatorTest::AddTests(BTestSuite& parent)
74 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("CollatorTest");
76 suite.addTest(new CppUnit::TestCaller<CollatorTest>(
77 "CollatorTest::TestSortKeys", &CollatorTest::TestSortKeys));
79 parent.addTest("CollatorTest", &suite);