diff options
author | Tor Lillqvist <tml@collabora.com> | 2014-11-11 18:50:11 +0200 |
---|---|---|
committer | Tor Lillqvist <tml@collabora.com> | 2014-11-11 19:01:58 +0200 |
commit | ca2fefb8796559b592d1ffd50c8346dcbcb33402 (patch) | |
tree | a991ba779dd0cef2820971552e7c12c3683e2c32 /comphelper/qa/string | |
parent | 6914b17246f73595c1061502e432d55d9b47d744 (diff) |
Add a function to compare version number strings
Change-Id: I88d3d9040f70e84752ade19001a699f60e9e7636
Diffstat (limited to 'comphelper/qa/string')
-rw-r--r-- | comphelper/qa/string/test_string.cxx | 40 |
1 files changed, 40 insertions, 0 deletions
diff --git a/comphelper/qa/string/test_string.cxx b/comphelper/qa/string/test_string.cxx index 78137a1052fa..5e9f23ffb087 100644 --- a/comphelper/qa/string/test_string.cxx +++ b/comphelper/qa/string/test_string.cxx @@ -44,6 +44,7 @@ public: void testIsdigitAsciiString(); void testReverseString(); void testEqualsString(); + void testCompareVersionStrings(); CPPUNIT_TEST_SUITE(TestString); CPPUNIT_TEST(testNatural); @@ -57,6 +58,7 @@ public: CPPUNIT_TEST(testIsdigitAsciiString); CPPUNIT_TEST(testReverseString); CPPUNIT_TEST(testEqualsString); + CPPUNIT_TEST(testCompareVersionStrings); CPPUNIT_TEST_SUITE_END(); }; @@ -407,6 +409,44 @@ void TestString::testEqualsString() CPPUNIT_ASSERT(!::comphelper::string::equals(aIn, 'A')); } +int sign(int n) +{ + if (n == 0) + return 0; + if (n < 0) + return -1; + else + return 1; +} + +void TestString::testCompareVersionStrings() +{ +#ifdef TEST +#error TEST already defined +#endif +#define TEST(a,b,result) \ + CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(a, b)) == result); \ + if ( result != 0 ) \ + CPPUNIT_ASSERT(sign(::comphelper::string::compareVersionStrings(b, a)) == -(result)) + + TEST("", "", 0); + TEST("", "0", -1); + TEST("", "a", -1); + TEST("0", "1", -1); + TEST("1", "2", -1); + TEST("2", "10", -1); + TEST("01", "1", -1); + TEST("01", "001", 1); + TEST("1.00", "1", 1); + TEST("1.2", "1", 1); + TEST("1.01", "1.1", -1); + TEST("1.001", "1.1", -1); + TEST("1.001", "1.010", -1); + TEST("1.2.a", "1.2.b", -1); + +#undef TEST +} + CPPUNIT_TEST_SUITE_REGISTRATION(TestString); } |