rippled
Loading...
Searching...
No Matches
SemanticVersion_test.cpp
1//------------------------------------------------------------------------------
2/*
3This file is part of rippled: https://github.com/ripple/rippled
4Copyright (c) 2012, 2013 Ripple Labs Inc.
5
6Permission to use, copy, modify, and/or distribute this software for any
7purpose with or without fee is hereby granted, provided that the above
8copyright notice and this permission notice appear in all copies.
9
10THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
11WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
12MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
13ANY SPECIAL , DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
14WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
15ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
16OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
17*/
18//==============================================================================
19
20#include <xrpl/beast/core/SemanticVersion.h>
21#include <xrpl/beast/unit_test.h>
22
23namespace beast {
24
26{
28
29public:
30 void
31 checkPass(std::string const& input, bool shouldPass = true)
32 {
34
35 if (shouldPass)
36 {
37 BEAST_EXPECT(v.parse(input));
38 BEAST_EXPECT(v.print() == input);
39 }
40 else
41 {
42 BEAST_EXPECT(!v.parse(input));
43 }
44 }
45
46 void
47 checkFail(std::string const& input)
48 {
49 checkPass(input, false);
50 }
51
52 // check input and input with appended metadata
53 void
54 checkMeta(std::string const& input, bool shouldPass)
55 {
56 checkPass(input, shouldPass);
57
58 checkPass(input + "+a", shouldPass);
59 checkPass(input + "+1", shouldPass);
60 checkPass(input + "+a.b", shouldPass);
61 checkPass(input + "+ab.cd", shouldPass);
62
63 checkFail(input + "!");
64 checkFail(input + "+");
65 checkFail(input + "++");
66 checkFail(input + "+!");
67 checkFail(input + "+.");
68 checkFail(input + "+a.!");
69 }
70
71 void
73 {
74 checkMeta(input, false);
75 }
76
77 // check input, input with appended release data,
78 // input with appended metadata, and input with both
79 // appended release data and appended metadata
80 //
81 void
82 checkRelease(std::string const& input, bool shouldPass = true)
83 {
84 checkMeta(input, shouldPass);
85
86 checkMeta(input + "-1", shouldPass);
87 checkMeta(input + "-a", shouldPass);
88 checkMeta(input + "-a1", shouldPass);
89 checkMeta(input + "-a1.b1", shouldPass);
90 checkMeta(input + "-ab.cd", shouldPass);
91 checkMeta(input + "--", shouldPass);
92
93 checkMetaFail(input + "+");
94 checkMetaFail(input + "!");
95 checkMetaFail(input + "-");
96 checkMetaFail(input + "-!");
97 checkMetaFail(input + "-.");
98 checkMetaFail(input + "-a.!");
99 checkMetaFail(input + "-0.a");
100 }
101
102 // Checks the major.minor.version string alone and with all
103 // possible combinations of release identifiers and metadata.
104 //
105 void
106 check(std::string const& input, bool shouldPass = true)
107 {
108 checkRelease(input, shouldPass);
109 }
110
111 void
112 negcheck(std::string const& input)
113 {
114 check(input, false);
115 }
116
117 void
119 {
120 testcase("parsing");
121
122 check("0.0.0");
123 check("1.2.3");
124 check("2147483647.2147483647.2147483647"); // max int
125
126 // negative values
127 negcheck("-1.2.3");
128 negcheck("1.-2.3");
129 negcheck("1.2.-3");
130
131 // missing parts
132 negcheck("");
133 negcheck("1");
134 negcheck("1.");
135 negcheck("1.2");
136 negcheck("1.2.");
137 negcheck(".2.3");
138
139 // whitespace
140 negcheck(" 1.2.3");
141 negcheck("1 .2.3");
142 negcheck("1.2 .3");
143 negcheck("1.2.3 ");
144
145 // leading zeroes
146 negcheck("01.2.3");
147 negcheck("1.02.3");
148 negcheck("1.2.03");
149 }
150
151 static identifier_list
153 {
154 return identifier_list();
155 }
156
157 static identifier_list
158 ids(std::string const& s1)
159 {
161 v.push_back(s1);
162 return v;
163 }
164
165 static identifier_list
166 ids(std::string const& s1, std::string const& s2)
167 {
169 v.push_back(s1);
170 v.push_back(s2);
171 return v;
172 }
173
174 static identifier_list
175 ids(std::string const& s1, std::string const& s2, std::string const& s3)
176 {
178 v.push_back(s1);
179 v.push_back(s2);
180 v.push_back(s3);
181 return v;
182 }
183
184 // Checks the decomposition of the input into appropriate values
185 void
187 std::string const& input,
188 int majorVersion,
189 int minorVersion,
190 int patchVersion,
191 identifier_list const& preReleaseIdentifiers = identifier_list(),
192 identifier_list const& metaData = identifier_list())
193 {
195
196 BEAST_EXPECT(v.parse(input));
197
198 BEAST_EXPECT(v.majorVersion == majorVersion);
199 BEAST_EXPECT(v.minorVersion == minorVersion);
200 BEAST_EXPECT(v.patchVersion == patchVersion);
201
202 BEAST_EXPECT(v.preReleaseIdentifiers == preReleaseIdentifiers);
203 BEAST_EXPECT(v.metaData == metaData);
204 }
205
206 void
208 {
209 testcase("values");
210
211 checkValues("0.1.2", 0, 1, 2);
212 checkValues("1.2.3", 1, 2, 3);
213 checkValues("1.2.3-rc1", 1, 2, 3, ids("rc1"));
214 checkValues("1.2.3-rc1.debug", 1, 2, 3, ids("rc1", "debug"));
215 checkValues("1.2.3-rc1.debug.asm", 1, 2, 3, ids("rc1", "debug", "asm"));
216 checkValues("1.2.3+full", 1, 2, 3, ids(), ids("full"));
217 checkValues("1.2.3+full.prod", 1, 2, 3, ids(), ids("full", "prod"));
219 "1.2.3+full.prod.x86", 1, 2, 3, ids(), ids("full", "prod", "x86"));
221 "1.2.3-rc1.debug.asm+full.prod.x86",
222 1,
223 2,
224 3,
225 ids("rc1", "debug", "asm"),
226 ids("full", "prod", "x86"));
227 }
228
229 // makes sure the left version is less than the right
230 void
232 {
233 SemanticVersion left;
234 SemanticVersion right;
235
236 BEAST_EXPECT(left.parse(lhs));
237 BEAST_EXPECT(right.parse(rhs));
238
239 BEAST_EXPECT(compare(left, left) == 0);
240 BEAST_EXPECT(compare(right, right) == 0);
241 BEAST_EXPECT(compare(left, right) < 0);
242 BEAST_EXPECT(compare(right, left) > 0);
243
244 BEAST_EXPECT(left < right);
245 BEAST_EXPECT(right > left);
246 BEAST_EXPECT(left == left);
247 BEAST_EXPECT(right == right);
248 }
249
250 void
251 checkLess(std::string const& lhs, std::string const& rhs)
252 {
253 checkLessInternal(lhs, rhs);
254 checkLessInternal(lhs + "+meta", rhs);
255 checkLessInternal(lhs, rhs + "+meta");
256 checkLessInternal(lhs + "+meta", rhs + "+meta");
257 }
258
259 void
261 {
262 testcase("comparisons");
263
264 checkLess("1.0.0-alpha", "1.0.0-alpha.1");
265 checkLess("1.0.0-alpha.1", "1.0.0-alpha.beta");
266 checkLess("1.0.0-alpha.beta", "1.0.0-beta");
267 checkLess("1.0.0-beta", "1.0.0-beta.2");
268 checkLess("1.0.0-beta.2", "1.0.0-beta.11");
269 checkLess("1.0.0-beta.11", "1.0.0-rc.1");
270 checkLess("1.0.0-rc.1", "1.0.0");
271 checkLess("0.9.9", "1.0.0");
272 }
273
274 void
275 run() override
276 {
277 testParse();
278 testValues();
279 testCompare();
280 }
281};
282
283BEAST_DEFINE_TESTSUITE(SemanticVersion, beast, beast);
284} // namespace beast
SemanticVersion::identifier_list identifier_list
void checkFail(std::string const &input)
void checkPass(std::string const &input, bool shouldPass=true)
void check(std::string const &input, bool shouldPass=true)
void checkMeta(std::string const &input, bool shouldPass)
static identifier_list ids(std::string const &s1, std::string const &s2, std::string const &s3)
void checkLess(std::string const &lhs, std::string const &rhs)
void checkLessInternal(std::string const &lhs, std::string const &rhs)
void checkMetaFail(std::string const &input)
static identifier_list ids(std::string const &s1, std::string const &s2)
void checkRelease(std::string const &input, bool shouldPass=true)
static identifier_list ids(std::string const &s1)
void checkValues(std::string const &input, int majorVersion, int minorVersion, int patchVersion, identifier_list const &preReleaseIdentifiers=identifier_list(), identifier_list const &metaData=identifier_list())
void run() override
Runs the suite.
void negcheck(std::string const &input)
A Semantic Version number.
bool parse(std::string const &input)
Parse a semantic version string.
identifier_list preReleaseIdentifiers
std::string print() const
Produce a string from semantic version components.
std::vector< std::string > identifier_list
identifier_list metaData
A testsuite class.
Definition suite.h:55
testcase_t testcase
Memberspace for declaring test cases.
Definition suite.h:155
int compare(SemanticVersion const &lhs, SemanticVersion const &rhs)
Compare two SemanticVersions against each other.
T push_back(T... args)