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/unit_test.h>
20 #include <ripple/beast/core/SemanticVersion.h>
21 namespace beast {
22 
23 class SemanticVersion_test : public unit_test::suite
24 {
26 
27 public:
28  void checkPass(std::string const& input, bool shouldPass = true)
29  {
31 
32  if (shouldPass)
33  {
34  BEAST_EXPECT(v.parse(input));
35  BEAST_EXPECT(v.print() == input);
36  }
37  else
38  {
39  BEAST_EXPECT(!v.parse(input));
40  }
41  }
42 
43  void checkFail(std::string const& input)
44  {
45  checkPass(input, false);
46  }
47 
48  // check input and input with appended metadata
49  void checkMeta(std::string const& input, bool shouldPass)
50  {
51  checkPass(input, shouldPass);
52 
53  checkPass(input + "+a", shouldPass);
54  checkPass(input + "+1", shouldPass);
55  checkPass(input + "+a.b", shouldPass);
56  checkPass(input + "+ab.cd", shouldPass);
57 
58  checkFail(input + "!");
59  checkFail(input + "+");
60  checkFail(input + "++");
61  checkFail(input + "+!");
62  checkFail(input + "+.");
63  checkFail(input + "+a.!");
64  }
65 
66  void checkMetaFail(std::string const& input)
67  {
68  checkMeta(input, false);
69  }
70 
71  // check input, input with appended release data,
72  // input with appended metadata, and input with both
73  // appended release data and appended metadata
74  //
75  void checkRelease(std::string const& input, bool shouldPass = true)
76  {
77  checkMeta(input, shouldPass);
78 
79  checkMeta(input + "-1", shouldPass);
80  checkMeta(input + "-a", shouldPass);
81  checkMeta(input + "-a1", shouldPass);
82  checkMeta(input + "-a1.b1", shouldPass);
83  checkMeta(input + "-ab.cd", shouldPass);
84  checkMeta(input + "--", shouldPass);
85 
86  checkMetaFail(input + "+");
87  checkMetaFail(input + "!");
88  checkMetaFail(input + "-");
89  checkMetaFail(input + "-!");
90  checkMetaFail(input + "-.");
91  checkMetaFail(input + "-a.!");
92  checkMetaFail(input + "-0.a");
93  }
94 
95  // Checks the major.minor.version string alone and with all
96  // possible combinations of release identifiers and metadata.
97  //
98  void check(std::string const& input, bool shouldPass = true)
99  {
100  checkRelease(input, shouldPass);
101  }
102 
103  void negcheck(std::string const& input)
104  {
105  check(input, false);
106  }
107 
108  void testParse()
109  {
110  testcase("parsing");
111 
112  check("0.0.0");
113  check("1.2.3");
114  check("2147483647.2147483647.2147483647"); // max int
115 
116  // negative values
117  negcheck("-1.2.3");
118  negcheck("1.-2.3");
119  negcheck("1.2.-3");
120 
121  // missing parts
122  negcheck("");
123  negcheck("1");
124  negcheck("1.");
125  negcheck("1.2");
126  negcheck("1.2.");
127  negcheck(".2.3");
128 
129  // whitespace
130  negcheck(" 1.2.3");
131  negcheck("1 .2.3");
132  negcheck("1.2 .3");
133  negcheck("1.2.3 ");
134 
135  // leading zeroes
136  negcheck("01.2.3");
137  negcheck("1.02.3");
138  negcheck("1.2.03");
139  }
140 
142  {
143  return identifier_list();
144  }
145 
147  std::string const& s1)
148  {
149  identifier_list v;
150  v.push_back(s1);
151  return v;
152  }
153 
155  std::string const& s1, std::string const& s2)
156  {
157  identifier_list v;
158  v.push_back(s1);
159  v.push_back(s2);
160  return v;
161  }
162 
164  std::string const& s1, std::string const& s2, std::string const& s3)
165  {
166  identifier_list v;
167  v.push_back(s1);
168  v.push_back(s2);
169  v.push_back(s3);
170  return v;
171  }
172 
173  // Checks the decomposition of the input into appropriate values
174  void checkValues(std::string const& input,
175  int majorVersion,
176  int minorVersion,
177  int patchVersion,
178  identifier_list const& preReleaseIdentifiers = identifier_list(),
179  identifier_list const& metaData = identifier_list())
180  {
181  SemanticVersion v;
182 
183  BEAST_EXPECT(v.parse(input));
184 
185  BEAST_EXPECT(v.majorVersion == majorVersion);
186  BEAST_EXPECT(v.minorVersion == minorVersion);
187  BEAST_EXPECT(v.patchVersion == patchVersion);
188 
189  BEAST_EXPECT(v.preReleaseIdentifiers == preReleaseIdentifiers);
190  BEAST_EXPECT(v.metaData == metaData);
191  }
192 
193  void testValues()
194  {
195  testcase("values");
196 
197  checkValues("0.1.2", 0, 1, 2);
198  checkValues("1.2.3", 1, 2, 3);
199  checkValues("1.2.3-rc1", 1, 2, 3, ids("rc1"));
200  checkValues("1.2.3-rc1.debug", 1, 2, 3, ids("rc1", "debug"));
201  checkValues("1.2.3-rc1.debug.asm", 1, 2, 3, ids("rc1", "debug", "asm"));
202  checkValues("1.2.3+full", 1, 2, 3, ids(), ids("full"));
203  checkValues("1.2.3+full.prod", 1, 2, 3, ids(), ids("full", "prod"));
204  checkValues("1.2.3+full.prod.x86", 1, 2, 3, ids(), ids("full", "prod", "x86"));
205  checkValues("1.2.3-rc1.debug.asm+full.prod.x86", 1, 2, 3,
206  ids("rc1", "debug", "asm"), ids("full", "prod", "x86"));
207  }
208 
209  // makes sure the left version is less than the right
210  void checkLessInternal(std::string const& lhs, std::string const& rhs)
211  {
212  SemanticVersion left;
213  SemanticVersion right;
214 
215  BEAST_EXPECT(left.parse(lhs));
216  BEAST_EXPECT(right.parse(rhs));
217 
218  BEAST_EXPECT(compare(left, left) == 0);
219  BEAST_EXPECT(compare(right, right) == 0);
220  BEAST_EXPECT(compare(left, right) < 0);
221  BEAST_EXPECT(compare(right, left) > 0);
222 
223  BEAST_EXPECT(left < right);
224  BEAST_EXPECT(right > left);
225  BEAST_EXPECT(left == left);
226  BEAST_EXPECT(right == right);
227  }
228 
229  void checkLess(std::string const& lhs, std::string const& rhs)
230  {
231  checkLessInternal(lhs, rhs);
232  checkLessInternal(lhs + "+meta", rhs);
233  checkLessInternal(lhs, rhs + "+meta");
234  checkLessInternal(lhs + "+meta", rhs + "+meta");
235  }
236 
237  void testCompare()
238  {
239  testcase("comparisons");
240 
241  checkLess("1.0.0-alpha", "1.0.0-alpha.1");
242  checkLess("1.0.0-alpha.1", "1.0.0-alpha.beta");
243  checkLess("1.0.0-alpha.beta", "1.0.0-beta");
244  checkLess("1.0.0-beta", "1.0.0-beta.2");
245  checkLess("1.0.0-beta.2", "1.0.0-beta.11");
246  checkLess("1.0.0-beta.11", "1.0.0-rc.1");
247  checkLess("1.0.0-rc.1", "1.0.0");
248  checkLess("0.9.9", "1.0.0");
249  }
250 
251  void run() override
252  {
253  testParse();
254  testValues();
255  testCompare();
256  }
257 };
258 
259 BEAST_DEFINE_TESTSUITE(SemanticVersion, beast_core, beast);
260 }
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:103
beast::SemanticVersion_test::ids
static identifier_list ids(std::string const &s1)
Definition: SemanticVersion_test.cpp:146
beast::SemanticVersion_test::checkLessInternal
void checkLessInternal(std::string const &lhs, std::string const &rhs)
Definition: SemanticVersion_test.cpp:210
beast::SemanticVersion_test::testValues
void testValues()
Definition: SemanticVersion_test.cpp:193
beast::SemanticVersion_test::ids
static identifier_list ids()
Definition: SemanticVersion_test.cpp:141
beast::SemanticVersion_test::checkRelease
void checkRelease(std::string const &input, bool shouldPass=true)
Definition: SemanticVersion_test.cpp:75
std::vector< std::string >
beast::SemanticVersion::print
std::string print() const
Produce a string from semantic version components.
Definition: SemanticVersion.cpp:228
beast::compare
int compare(SemanticVersion const &lhs, SemanticVersion const &rhs)
Compare two SemanticVersions against each other.
Definition: SemanticVersion.cpp:251
beast::SemanticVersion_test::check
void check(std::string const &input, bool shouldPass=true)
Definition: SemanticVersion_test.cpp:98
beast::SemanticVersion_test::ids
static identifier_list ids(std::string const &s1, std::string const &s2)
Definition: SemanticVersion_test.cpp:154
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:229
beast::SemanticVersion_test::checkMetaFail
void checkMetaFail(std::string const &input)
Definition: SemanticVersion_test.cpp:66
beast::SemanticVersion_test::run
void run() override
Definition: SemanticVersion_test.cpp:251
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:43
beast::SemanticVersion::parse
bool parse(std::string const &input)
Parse a semantic version string.
Definition: SemanticVersion.cpp:162
beast::SemanticVersion_test::testParse
void testParse()
Definition: SemanticVersion_test.cpp:108
beast::SemanticVersion_test::checkMeta
void checkMeta(std::string const &input, bool shouldPass)
Definition: SemanticVersion_test.cpp:49
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:174
beast::BEAST_DEFINE_TESTSUITE
BEAST_DEFINE_TESTSUITE(aged_set, container, beast)
beast::SemanticVersion_test::testCompare
void testCompare()
Definition: SemanticVersion_test.cpp:237
beast::SemanticVersion_test::checkPass
void checkPass(std::string const &input, bool shouldPass=true)
Definition: SemanticVersion_test.cpp:28
beast::SemanticVersion_test::ids
static identifier_list ids(std::string const &s1, std::string const &s2, std::string const &s3)
Definition: SemanticVersion_test.cpp:163
beast
Definition: base_uint.h:582