From 55f115d875bf70305ca43e93761395b4329a23a6 Mon Sep 17 00:00:00 2001 From: Karen Arutyunov Date: Thu, 27 Apr 2017 21:56:42 +0300 Subject: Add standard_version_constraint::satisfies() --- butl/standard-version | 3 ++ butl/standard-version.cxx | 20 ++++++++++ tests/standard-version/driver.cxx | 19 ++++++--- tests/standard-version/testscript | 82 +++++++++++++++++++++++++++++++++++++++ 4 files changed, 118 insertions(+), 6 deletions(-) diff --git a/butl/standard-version b/butl/standard-version index 0c7b9bc..59056ac 100644 --- a/butl/standard-version +++ b/butl/standard-version @@ -181,6 +181,9 @@ namespace butl bool empty () const noexcept {return !min_version && !max_version;} + + bool + satisfies (const standard_version&) const noexcept; }; inline bool diff --git a/butl/standard-version.cxx b/butl/standard-version.cxx index 16095c0..3e049e2 100644 --- a/butl/standard-version.cxx +++ b/butl/standard-version.cxx @@ -530,4 +530,24 @@ namespace butl return (min_open ? '(' : '[') + min_version->string () + ' ' + max_version->string () + (max_open ? ')' : ']'); } + + bool standard_version_constraint:: + satisfies (const standard_version& v) const noexcept + { + bool s (true); + + if (min_version) + { + int i (v.compare (*min_version)); + s = min_open ? i > 0 : i >= 0; + } + + if (s && max_version) + { + int i (v.compare (*max_version)); + s = max_open ? i < 0 : i <= 0; + } + + return s; + } } diff --git a/tests/standard-version/driver.cxx b/tests/standard-version/driver.cxx index e6a21ef..86d9621 100644 --- a/tests/standard-version/driver.cxx +++ b/tests/standard-version/driver.cxx @@ -76,12 +76,14 @@ version (const string& s, bool allow_earliest = true) // argv[0] -b // argv[0] -c // argv[0] -r +// argv[0] -s // argv[0] // // -a output 'y' for alpha-version, 'n' otherwise // -b output 'y' for beta-version, 'n' otherwise // -c output 0 if versions are equal, -1 if the first one is less, 1 otherwise // -r create version constraints from STDIN lines, and print them to STDOUT +// -s output 'y' if version satisfies constraint, 'n' otherwise // // If no options are specified, then create versions from STDIN lines, and // print them to STDOUT. @@ -100,18 +102,14 @@ try if (o == "-a") { assert (argc == 3); - char r (version (argv[2]).alpha () - ? 'y' - : 'n'); + char r (version (argv[2]).alpha () ? 'y' : 'n'); cout << r << endl; } else if (o == "-b") { assert (argc == 3); - char r (version (argv[2]).beta () - ? 'y' - : 'n'); + char r (version (argv[2]).beta () ? 'y' : 'n'); cout << r << endl; } @@ -130,6 +128,15 @@ try while (getline (cin, s)) cout << standard_version_constraint (s) << endl; } + else if (o == "-s") + { + assert (argc == 4); + + char r (standard_version_constraint (argv[3]).satisfies ( + version (argv[2])) ? 'y' : 'n'); + + cout << r << endl; + } else assert (false); diff --git a/tests/standard-version/testscript b/tests/standard-version/testscript index e5e3248..b6bc1a0 100644 --- a/tests/standard-version/testscript +++ b/tests/standard-version/testscript @@ -297,3 +297,85 @@ } } } + +: satisfaction +: +{ + test.options += -s + + : comparison + : + : Constraints have a single endpoint being present. + : + { + : eq + : + { + $* '1.2.3' '== 1.2.3' >y : eq + $* '1.2.3' '<= 1.2.3' >y : le + $* '1.2.3' '< 1.2.3' >n : lt + $* '1.2.3' '>= 1.2.3' >y : ge + $* '1.2.3' '> 1.2.3' >n : gt + } + + : smaller-greater + : + { + $* '1.2.3' '== 1.2.4' >n : eq + $* '1.2.3' '<= 1.2.4' >y : le + $* '1.2.3' '< 1.2.4' >y : lt + $* '1.2.3' '>= 1.2.4' >n : ge + $* '1.2.3' '> 1.2.4' >n : gt + } + + : greater-smaller + : + { + $* '1.2.4' '<= 1.2.3' >n : le + $* '1.2.4' '< 1.2.3' >n : lt + $* '1.2.4' '>= 1.2.3' >y : ge + $* '1.2.4' '> 1.2.3' >y : gt + } + } + + : range + : + : Constraints have both endpoints being present. + : + { + : left-out + : + { + $* '1.2.3' '[1.2.4 1.2.5]' >n : closed + $* '1.2.3' '(1.2.4 1.2.5]' >n : open + } + + : left-endpoint + : + { + $* '1.2.3' '[1.2.3 1.2.4]' >y : closed + $* '1.2.3' '(1.2.3 1.2.4]' >n : open + } + + : in + : + { + $* '1.2.3' '[1.2.2 1.2.4]' >y : closed + $* '1.2.3' '(1.2.2 1.2.4)' >y : open + } + + : right-endpoint + : + { + $* '1.2.3' '[1.2.2 1.2.3]' >y : closed + $* '1.2.3' '[1.2.2 1.2.3)' >n : open + } + + : right-out + : + { + $* '1.2.3' '[1.2.1 1.2.2]' >n : closed + $* '1.2.3' '[1.2.1 1.2.2)' >n : open + } + } +} -- cgit v1.1