File: test_semanticversion.cc

package info (click to toggle)
eckit 1.32.4-2
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 600,644 kB
  • sloc: cpp: 111,654; ansic: 2,826; yacc: 590; lex: 361; python: 237; sh: 202; makefile: 41
file content (123 lines) | stat: -rw-r--r-- 3,808 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
/*
 * (C) Copyright 1996- ECMWF.
 *
 * This software is licensed under the terms of the Apache Licence Version 2.0
 * which can be obtained at http://www.apache.org/licenses/LICENSE-2.0.
 * In applying this licence, ECMWF does not waive the privileges and immunities
 * granted to it by virtue of its status as an intergovernmental organisation nor
 * does it submit to any jurisdiction.
 */

#include "eckit/log/Log.h"
#include "eckit/runtime/Tool.h"
#include "eckit/types/SemanticVersion.h"
#include "eckit/types/Types.h"

#include "eckit/testing/Test.h"

using namespace std;
using namespace eckit;
using namespace eckit::testing;

namespace eckit::test {

//----------------------------------------------------------------------------------------------------------------------

CASE("Empty versions") {

    SemanticVersion v;
    EXPECT(std::string(v) == "0.0.0");

    SemanticVersion v0("0.0.0");
    SemanticVersion v1("1.0.0");
    SemanticVersion v2("0.1.0");
    SemanticVersion v3("0.0.1");

    EXPECT(v == v0);
    EXPECT(v < v1);
    EXPECT(v < v2);
    EXPECT(v < v3);
}

CASE("Construct with integers") {
    EXPECT(SemanticVersion() == SemanticVersion(0, 0, 0));
    EXPECT(SemanticVersion("0.0.0") == SemanticVersion(0, 0, 0));
    EXPECT(SemanticVersion("27.125.22") == SemanticVersion(27, 125, 22));
    EXPECT(SemanticVersion("2.1.3") != SemanticVersion(27, 125, 22));
}

CASE("Failed construction") {
    EXPECT_THROWS_AS(SemanticVersion("1"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("2.3"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.2.3.4"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.2-4"), eckit::BadValue);

    EXPECT_THROWS_AS(SemanticVersion("1.2.4-r1"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.-2.4"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.2.456abc"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.aaa.56"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("v1.2.4"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("x.y.z"), eckit::BadValue);
    EXPECT_THROWS_AS(SemanticVersion("1.2.999999999999999999999999999999"), eckit::OutOfRange);
}

CASE("Version comparison") {
    std::ostringstream ss;
    ss << SemanticVersion("27.125.22");
    EXPECT(ss.str() == "27.125.22");
}

CASE("Version comparison") {

    SemanticVersion v1("1.0.0");
    SemanticVersion v2("0.1.0");
    SemanticVersion v3("0.0.1");

    EXPECT(std::string(v1) == "1.0.0");
    EXPECT(std::string(v2) == "0.1.0");
    EXPECT(std::string(v3) == "0.0.1");

    EXPECT(v1 > v2);
    EXPECT(v1 > v3);

    EXPECT(v2 < v1);
    EXPECT(v2 > v3);

    EXPECT(v3 < v1);
    EXPECT(v3 < v2);
}

CASE("Multidigit versions") {

    SemanticVersion v("27.125.22");

    EXPECT(std::string(v) == "27.125.22");

    EXPECT(v == SemanticVersion("27.125.22"));
    EXPECT(v != SemanticVersion("27.125.33"));
    EXPECT(v != SemanticVersion("27.25.22"));
    EXPECT(v != SemanticVersion("7.125.22"));

    EXPECT(v > SemanticVersion("27.125.11"));
    EXPECT(v > SemanticVersion("27.1.33"));
    EXPECT(v > SemanticVersion("2.125.2"));

    EXPECT(v < SemanticVersion("27.125.33"));
    EXPECT(v < SemanticVersion("27.777.33"));
    EXPECT(v < SemanticVersion("42.125.22"));

    EXPECT(v >= SemanticVersion("27.125.22"));
    EXPECT(v >= SemanticVersion("27.125.12"));
    EXPECT(v <= SemanticVersion("27.125.22"));
    EXPECT(v <= SemanticVersion("27.125.33"));
}

//----------------------------------------------------------------------------------------------------------------------

}  // namespace eckit::test

//----------------------------------------------------------------------------------------------------------------------

int main(int argc, char** argv) {
    return run_tests(argc, argv);
}