171171a7dSAxel Dörfler /*
271171a7dSAxel Dörfler * Copyright 2012, Axel Dörfler, axeld@pinc-software.de.
371171a7dSAxel Dörfler * Distributed under the terms of the MIT License.
471171a7dSAxel Dörfler */
571171a7dSAxel Dörfler
671171a7dSAxel Dörfler
771171a7dSAxel Dörfler #include "NaturalCompareTest.h"
871171a7dSAxel Dörfler
971171a7dSAxel Dörfler #include <NaturalCompare.h>
1071171a7dSAxel Dörfler
1171171a7dSAxel Dörfler #include <cppunit/TestCaller.h>
1271171a7dSAxel Dörfler #include <cppunit/TestSuite.h>
1371171a7dSAxel Dörfler
1471171a7dSAxel Dörfler
1571171a7dSAxel Dörfler using namespace BPrivate;
1671171a7dSAxel Dörfler
1771171a7dSAxel Dörfler
1871171a7dSAxel Dörfler struct Sample {
1971171a7dSAxel Dörfler const char* a;
2071171a7dSAxel Dörfler const char* b;
2171171a7dSAxel Dörfler int expectedResult;
2271171a7dSAxel Dörfler };
2371171a7dSAxel Dörfler
2471171a7dSAxel Dörfler
NaturalCompareTest()2571171a7dSAxel Dörfler NaturalCompareTest::NaturalCompareTest()
2671171a7dSAxel Dörfler {
2771171a7dSAxel Dörfler }
2871171a7dSAxel Dörfler
2971171a7dSAxel Dörfler
~NaturalCompareTest()3071171a7dSAxel Dörfler NaturalCompareTest::~NaturalCompareTest()
3171171a7dSAxel Dörfler {
3271171a7dSAxel Dörfler }
3371171a7dSAxel Dörfler
3471171a7dSAxel Dörfler
3571171a7dSAxel Dörfler void
TestSome()3671171a7dSAxel Dörfler NaturalCompareTest::TestSome()
3771171a7dSAxel Dörfler {
3871171a7dSAxel Dörfler static const Sample samples[] = {
39*3ec77017SAdrien Destugues // NULL in either side of the comparison
4071171a7dSAxel Dörfler {NULL, NULL, 0},
4171171a7dSAxel Dörfler {NULL, "A", -1},
42*3ec77017SAdrien Destugues {"A", NULL, 1},
43*3ec77017SAdrien Destugues // Case insensitive
4471171a7dSAxel Dörfler {"a", "A", 0},
45*3ec77017SAdrien Destugues {"é", "É", 0},
46*3ec77017SAdrien Destugues // Handling of accented characters
4771171a7dSAxel Dörfler {"ä", "a", 1},
48*3ec77017SAdrien Destugues // Natural number ordering
4971171a7dSAxel Dörfler {"3", "99", -1},
5071171a7dSAxel Dörfler {"9", "19", -1},
5171171a7dSAxel Dörfler {"13", "99", -1},
5271171a7dSAxel Dörfler {"9", "111", -1},
5371171a7dSAxel Dörfler {"00000009", "111", -1},
54*3ec77017SAdrien Destugues // Natural number ordering, ignoring leading space
5571171a7dSAxel Dörfler {"Hallo2", "hallo12", -1},
5671171a7dSAxel Dörfler {"Hallo 2", "hallo12", -1},
5771171a7dSAxel Dörfler {"Hallo 2", "hallo12", -1},
5871171a7dSAxel Dörfler {"Hallo 2 ", "hallo12", -1},
59*3ec77017SAdrien Destugues // A mix of everything
60*3ec77017SAdrien Destugues {"12 äber 42", "12aber42", -1},
61*3ec77017SAdrien Destugues {"12 äber 42", "12aber43", -1},
62*3ec77017SAdrien Destugues {"12 äber 44", "12aber43", -1},
63*3ec77017SAdrien Destugues {"12 äber 44", "12 aber45", -1},
6471171a7dSAxel Dörfler };
6571171a7dSAxel Dörfler _RunTests(samples, sizeof(samples) / sizeof(Sample));
6671171a7dSAxel Dörfler }
6771171a7dSAxel Dörfler
6871171a7dSAxel Dörfler
6971171a7dSAxel Dörfler /*static*/ void
AddTests(BTestSuite & parent)7071171a7dSAxel Dörfler NaturalCompareTest::AddTests(BTestSuite& parent)
7171171a7dSAxel Dörfler {
7271171a7dSAxel Dörfler CppUnit::TestSuite& suite = *new CppUnit::TestSuite("NaturalCompareTest");
7371171a7dSAxel Dörfler
7471171a7dSAxel Dörfler suite.addTest(new CppUnit::TestCaller<NaturalCompareTest>(
7571171a7dSAxel Dörfler "NaturalCompareTest::TestSome", &NaturalCompareTest::TestSome));
7671171a7dSAxel Dörfler
7771171a7dSAxel Dörfler parent.addTest("NaturalCompareTest", &suite);
7871171a7dSAxel Dörfler }
7971171a7dSAxel Dörfler
8071171a7dSAxel Dörfler
8171171a7dSAxel Dörfler void
_RunTests(const Sample * samples,int count)8271171a7dSAxel Dörfler NaturalCompareTest::_RunTests(const Sample* samples, int count)
8371171a7dSAxel Dörfler {
8471171a7dSAxel Dörfler for (int i = 0; i < count; i++) {
8571171a7dSAxel Dörfler const Sample& sample = samples[i];
8671171a7dSAxel Dörfler
8771171a7dSAxel Dörfler _RunTest(sample.a, sample.b, sample.expectedResult);
8871171a7dSAxel Dörfler _RunTest(sample.b, sample.a, -sample.expectedResult);
8971171a7dSAxel Dörfler }
9071171a7dSAxel Dörfler }
9171171a7dSAxel Dörfler
9271171a7dSAxel Dörfler
9371171a7dSAxel Dörfler void
_RunTest(const char * a,const char * b,int expectedResult)9471171a7dSAxel Dörfler NaturalCompareTest::_RunTest(const char* a, const char* b, int expectedResult)
9571171a7dSAxel Dörfler {
9671171a7dSAxel Dörfler int result = _Normalize(NaturalCompare(a, b));
9771171a7dSAxel Dörfler
9871171a7dSAxel Dörfler char message[256];
9971171a7dSAxel Dörfler snprintf(message, sizeof(message), "\"%s\" vs. \"%s\" == %d, expected %d",
10071171a7dSAxel Dörfler a, b, result, expectedResult);
10171171a7dSAxel Dörfler
10271171a7dSAxel Dörfler CppUnit::Asserter::failIf(result != expectedResult, message);
10371171a7dSAxel Dörfler }
10471171a7dSAxel Dörfler
10571171a7dSAxel Dörfler
10671171a7dSAxel Dörfler int
_Normalize(int result)10771171a7dSAxel Dörfler NaturalCompareTest::_Normalize(int result)
10871171a7dSAxel Dörfler {
10971171a7dSAxel Dörfler if (result > 0)
11071171a7dSAxel Dörfler return 1;
11171171a7dSAxel Dörfler if (result < 0)
11271171a7dSAxel Dörfler return -1;
11371171a7dSAxel Dörfler return 0;
11471171a7dSAxel Dörfler }
115