vfs: check userland buffers before reading them.
[haiku.git] / src / tests / kits / shared / NaturalCompareTest.cpp
blob3bf17cfb01c3d7f8736c25678e1497e9f8ceae41
1 /*
2 * Copyright 2012, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
4 */
7 #include "NaturalCompareTest.h"
9 #include <NaturalCompare.h>
11 #include <cppunit/TestCaller.h>
12 #include <cppunit/TestSuite.h>
15 using namespace BPrivate;
18 struct Sample {
19 const char* a;
20 const char* b;
21 int expectedResult;
25 NaturalCompareTest::NaturalCompareTest()
30 NaturalCompareTest::~NaturalCompareTest()
35 void
36 NaturalCompareTest::TestSome()
38 static const Sample samples[] = {
39 // NULL in either side of the comparison
40 {NULL, NULL, 0},
41 {NULL, "A", -1},
42 {"A", NULL, 1},
43 // Case insensitive
44 {"a", "A", 0},
45 {"é", "É", 0},
46 // Handling of accented characters
47 {"ä", "a", 1},
48 // Natural number ordering
49 {"3", "99", -1},
50 {"9", "19", -1},
51 {"13", "99", -1},
52 {"9", "111", -1},
53 {"00000009", "111", -1},
54 // Natural number ordering, ignoring leading space
55 {"Hallo2", "hallo12", -1},
56 {"Hallo 2", "hallo12", -1},
57 {"Hallo 2", "hallo12", -1},
58 {"Hallo 2 ", "hallo12", -1},
59 // A mix of everything
60 {"12 äber 42", "12aber42", -1},
61 {"12 äber 42", "12aber43", -1},
62 {"12 äber 44", "12aber43", -1},
63 {"12 äber 44", "12 aber45", -1},
65 _RunTests(samples, sizeof(samples) / sizeof(Sample));
69 /*static*/ void
70 NaturalCompareTest::AddTests(BTestSuite& parent)
72 CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NaturalCompareTest");
74 suite.addTest(new CppUnit::TestCaller<NaturalCompareTest>(
75 "NaturalCompareTest::TestSome", &NaturalCompareTest::TestSome));
77 parent.addTest("NaturalCompareTest", &suite);
81 void
82 NaturalCompareTest::_RunTests(const Sample* samples, int count)
84 for (int i = 0; i < count; i++) {
85 const Sample& sample = samples[i];
87 _RunTest(sample.a, sample.b, sample.expectedResult);
88 _RunTest(sample.b, sample.a, -sample.expectedResult);
93 void
94 NaturalCompareTest::_RunTest(const char* a, const char* b, int expectedResult)
96 int result = _Normalize(NaturalCompare(a, b));
98 char message[256];
99 snprintf(message, sizeof(message), "\"%s\" vs. \"%s\" == %d, expected %d",
100 a, b, result, expectedResult);
102 CppUnit::Asserter::failIf(result != expectedResult, message);
107 NaturalCompareTest::_Normalize(int result)
109 if (result > 0)
110 return 1;
111 if (result < 0)
112 return -1;
113 return 0;