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