From 1bea889fd59b4ac3a32232e8f7a9ba34506717dc Mon Sep 17 00:00:00 2001 From: Boris Kolpackov Date: Tue, 25 Apr 2017 11:53:11 +0200 Subject: Add standard_version class --- tests/standard-version/driver.cxx | 136 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 136 insertions(+) create mode 100644 tests/standard-version/driver.cxx (limited to 'tests/standard-version/driver.cxx') diff --git a/tests/standard-version/driver.cxx b/tests/standard-version/driver.cxx new file mode 100644 index 0000000..1299090 --- /dev/null +++ b/tests/standard-version/driver.cxx @@ -0,0 +1,136 @@ +// file : tests/standard-version/driver.cxx -*- C++ -*- +// copyright : Copyright (c) 2014-2017 Code Synthesis Ltd +// license : MIT; see accompanying LICENSE file + +#include // ios::failbit, ios::badbit +#include +#include +#include +#include // invalid_argument + +#include // operator<<(ostream,exception) +#include + +using namespace std; +using namespace butl; + +// Create standard version from string, and also test another ctors. +// +static standard_version +version (const string& s) +{ + standard_version r (s); + + try + { + standard_version v (r.epoch, + r.version, + r.snapshot () + ? r.string_snapshot () + : string (), + r.revision); + + assert (r == v); + + if (r.epoch == 0 && r.revision == 0) + { + standard_version v (r.version, + r.snapshot () + ? r.string_snapshot () + : string ()); + assert (r == v); + + if (!r.snapshot ()) + { + standard_version v (r.version); + assert (r == v); + } + } + + if (r.snapshot ()) + { + standard_version v (r.epoch, + r.version, + r.snapshot_sn, + r.snapshot_id, + r.revision); + assert (r == v); + } + + } + catch (const invalid_argument& e) + { + cerr << e << endl; + assert (false); + } + + return r; +} + +// Usages: +// +// argv[0] -a +// argv[0] -b +// argv[0] -c +// 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 +// +// If no options are specified, then create versions from STDIN lines, and +// print them to STDOUT. +// +int +main (int argc, char* argv[]) +try +{ + cin.exceptions (ios::badbit); + cout.exceptions (ios::failbit | ios::badbit); + + if (argc > 1) + { + string o (argv[1]); + + if (o == "-a") + { + assert (argc == 3); + 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'); + + cout << r << endl; + } + else if (o == "-c") + { + assert (argc == 4); + + int r (version (argv[2]).compare (version (argv[3]))); + cout << r << endl; + } + else + assert (false); + + return 0; + } + + string s; + while (getline (cin, s)) + cout << version (s) << endl; + + return 0; +} +catch (const invalid_argument& e) +{ + cerr << e << endl; + return 1; +} -- cgit v1.1