2 * Copyright 2012, Axel Dörfler, axeld@pinc-software.de.
3 * Distributed under the terms of the MIT License.
7 #include "NaturalCompareTest.h"
9 #include <NaturalCompare.h>
11 #include <cppunit/TestCaller.h>
12 #include <cppunit/TestSuite.h>
15 using namespace BPrivate
;
25 NaturalCompareTest::NaturalCompareTest()
30 NaturalCompareTest::~NaturalCompareTest()
36 NaturalCompareTest::TestSome()
38 static const Sample samples
[] = {
39 // NULL in either side of the comparison
46 // Handling of accented characters
48 // Natural number ordering
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
));
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
);
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
);
94 NaturalCompareTest::_RunTest(const char* a
, const char* b
, int expectedResult
)
96 int result
= _Normalize(NaturalCompare(a
, b
));
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
)