From 4b01f999c40790a4044487c3a9c05e5f0889bb13 Mon Sep 17 00:00:00 2001 From: Danny Robson Date: Mon, 20 Jun 2016 13:08:36 +1000 Subject: [PATCH] version: add comparator tests --- test/version.cpp | 49 +++++++++++++++++++++++++++++++++++++----------- 1 file changed, 38 insertions(+), 11 deletions(-) diff --git a/test/version.cpp b/test/version.cpp index bf4235ff..f54247ad 100644 --- a/test/version.cpp +++ b/test/version.cpp @@ -2,21 +2,23 @@ #include "tap.hpp" + +/////////////////////////////////////////////////////////////////////////////// static const struct { const char *msg; const char *str; unsigned parts[4]; util::version::release_t release; -} TESTS[] = { - { "1-component", "1", { 1u }, util::version::PRODUCTION }, +} PARSE_TESTS[] = { + { "parse 1 component", "1", { 1u }, util::version::PRODUCTION }, - { "2-component", "1.2", { 1u, 2u }, util::version::PRODUCTION }, - { "3-component", "1.2.3", { 1u, 2u, 3u }, util::version::PRODUCTION }, - { "4-component", "1.2.3.4", { 1u, 2u, 3u, 4u }, util::version::PRODUCTION }, + { "parse 2component", "1.2", { 1u, 2u }, util::version::PRODUCTION }, + { "parse 3component", "1.2.3", { 1u, 2u, 3u }, util::version::PRODUCTION }, + { "parse 4component", "1.2.3.4", { 1u, 2u, 3u, 4u }, util::version::PRODUCTION }, - { "2-component alpha", "9.5a", { 9u, 5u }, util::version::ALPHA }, - { "3-component beta", "8.2.5b", { 8u, 2u, 5u }, util::version::BETA }, + { "parse 2 component alpha", "9.5a", { 9u, 5u }, util::version::ALPHA }, + { "parse 3 component beta", "8.2.5b", { 8u, 2u, 5u }, util::version::BETA }, /* { "1.4.1-p8", { 1, 4, 1 } }, @@ -26,17 +28,42 @@ static const struct { */ }; -//----------------------------------------------------------------------------- + +/////////////////////////////////////////////////////////////////////////////// +static const struct { + util::version a, b; + bool eq, lt, gt; + const char *msg; +} CMP_TESTS[] = { + { { 1u }, { 1u }, true, false, false, "v1 cmp v1" }, + { { 1u }, { 1u, 0u }, true, false, false, "v1 cmp v1.0" }, + { { 1u, 0u }, { 1u }, true, false, false, "v1.0 cmp v1" }, + { { 1u, 1u }, { 1u }, false, false, true, "v1.1 cmp v1" }, + { { 1u, }, { 1u, 1u }, false, true, false, "v1 cmp v1.1" }, +}; + + +/////////////////////////////////////////////////////////////////////////////// int main () { util::TAP::logger tap; - for (const auto &i: TESTS) { + for (const auto &i: PARSE_TESTS) { util::version v (i.str); - tap.expect (std::equal (v.begin (), v.end (), i.parts) && v.release == i.release, - i.msg); + tap.expect (std::equal (v.begin (), v.end (), i.parts) && v.release == i.release, i.msg); + } + + + for (const auto &t: CMP_TESTS) { + bool eq = t.a == t.b, + lt = t.a < t.b, + gt = t.a > t.b; + + tap.expect (t.eq == eq, "%s: equality", t.msg); + tap.expect (t.lt == lt, "%s: less-than", t.msg); + tap.expect (t.gt == gt, "%s: greater-than", t.msg); } return tap.status ();