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
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
|
// file : tests/standard-version/driver.cxx -*- C++ -*-
// license : MIT; see accompanying LICENSE file
#ifndef __cpp_lib_modules_ts
#include <ios> // ios::failbit, ios::badbit
#include <string>
#include <cstdint> // uint*_t
#include <iostream>
#include <stdexcept> // invalid_argument
#endif
// Other includes.
#ifdef __cpp_modules_ts
#ifdef __cpp_lib_modules_ts
import std.core;
import std.io;
#endif
import butl.utility; // operator<<(ostream,exception), eof()
import butl.optional;
import butl.standard_version;
#else
#include <libbutl/utility.mxx>
#include <libbutl/optional.mxx>
#include <libbutl/standard-version.mxx>
#endif
#undef NDEBUG
#include <cassert>
using namespace std;
using namespace butl;
// Return the standard version created from a string, and also perform some
// resulting version tests (check other constructors, invariants, etc.).
//
static standard_version
version (const string& s,
standard_version::flags f =
standard_version::allow_earliest | standard_version::allow_stub)
{
standard_version r (s, f);
// Test the other constructors.
//
try
{
standard_version v (r.epoch,
r.version,
r.snapshot ()
? r.string_snapshot ()
: string (),
r.revision,
f);
assert (r == v);
if (r.epoch == 1 && r.revision == 0)
{
standard_version v (r.version,
r.snapshot ()
? r.string_snapshot ()
: string (),
f);
assert (r == v);
if (!r.snapshot ())
{
standard_version v (r.version, f);
assert (r == v);
}
}
if (r.snapshot ())
{
standard_version v (r.epoch,
r.version,
r.snapshot_sn,
r.snapshot_id,
r.revision,
f);
assert (r == v);
}
// Test using the resulting version with shortcut operators.
//
if (!r.stub ())
{
auto max_ver = [&v] (char c) -> string
{
string e (v.epoch != 1 ? '+' + to_string (v.epoch) + '-' : string ());
return c == '~' || v.major () == 0
? e + to_string (v.major ()) + '.' + to_string (v.minor () + 1) + ".0-"
: e + to_string (v.major () + 1) + ".0.0-";
};
if (v.minor () != 99999)
{
standard_version_constraint c1 ("~" + s);
standard_version_constraint c2 ('[' + s + ' ' + max_ver ('~') + ')');
assert (c1 == c2);
}
if ((v.major () == 0 && v.minor () != 99999) ||
(v.major () != 0 && v.major () != 99999))
{
standard_version_constraint c1 ("^" + s);
standard_version_constraint c2 ('[' + s + ' ' + max_ver ('^') + ')');
assert (c1 == c2);
}
}
// Check some invariants for the resulting version.
//
// Stub is not a final (pre-)release nor snapshot.
//
assert (!r.stub () || !(r.final () || r.snapshot ()));
// Earliest is a final alpha.
//
assert (!r.earliest () || (r.final () && r.alpha ()));
// Final is a release or a pre-release but not a snapshot.
//
assert (r.final () ==
(r.release () || (r.pre_release () && !r.snapshot ())));
// Snapshot is a pre-release.
//
assert (!r.snapshot () || r.pre_release ());
// Pre-release is either alpha or beta.
//
assert (r.pre_release ().has_value () == (r.alpha () || r.beta ()));
}
catch (const invalid_argument& e)
{
cerr << e << endl;
assert (false);
}
return r;
}
// Usages:
//
// argv[0] (-rl|-pr|-al|-bt|-st|-el|-sn|-fn) <version>
// argv[0] -cm <version> <version>
// argv[0] -cr [<dependent-version>]
// argv[0] -sf <version> <constraint>
// argv[0]
//
// -rl output 'y' for release, 'n' otherwise
// -pr output DDD version part for pre-release, '-' otherwise
// -al output alpha version number for alpha-version, '-' otherwise
// -bt output beta version number for beta-version, '-' otherwise
// -st output 'y' for stub, 'n' otherwise
// -el output 'y' for earliest, 'n' otherwise
// -sn output 'y' for snapshot, 'n' otherwise
// -fn output 'y' for final, 'n' otherwise
//
// -cm output 0 if versions are equal, -1 if the first one is less, 1
// otherwise
//
// -cr create version constraints from stdin lines, optionally using the
// dependent version, and print them to stdout
//
// -sf output 'y' if version satisfies constraint, 'n' otherwise
//
// If no options are specified, then create versions from stdin lines, and
// print them to stdout.
//
int
main (int argc, char* argv[])
try
{
using butl::optional;
cin.exceptions (ios::badbit);
cout.exceptions (ios::failbit | ios::badbit);
if (argc > 1)
{
string o (argv[1]);
if (o == "-rl")
{
assert (argc == 3);
cout << (version (argv[2]).release () ? 'y' : 'n') << endl;
}
else if (o == "-pr")
{
assert (argc == 3);
if (optional<uint16_t> n = version (argv[2]).pre_release ())
cout << *n << endl;
else
cout << '-' << endl;
}
else if (o == "-al")
{
assert (argc == 3);
if (optional<uint16_t> n = version (argv[2]).alpha ())
cout << *n << endl;
else
cout << '-' << endl;
}
else if (o == "-bt")
{
assert (argc == 3);
if (optional<uint16_t> n = version (argv[2]).beta ())
cout << *n << endl;
else
cout << '-' << endl;
}
else if (o == "-st")
{
assert (argc == 3);
cout << (version (argv[2]).stub () ? 'y' : 'n') << endl;
}
else if (o == "-el")
{
assert (argc == 3);
cout << (version (argv[2]).earliest () ? 'y' : 'n') << endl;
}
else if (o == "-sn")
{
assert (argc == 3);
cout << (version (argv[2]).snapshot () ? 'y' : 'n') << endl;
}
else if (o == "-fn")
{
assert (argc == 3);
cout << (version (argv[2]).final () ? 'y' : 'n') << endl;
}
else if (o == "-cm")
{
assert (argc == 4);
int r (version (argv[2]).compare (version (argv[3])));
cout << r << endl;
}
else if (o == "-cr")
{
assert (argc <= 3);
optional<standard_version> dv;
if (argc == 3)
{
string s (argv[2]);
dv = s.empty ()
? standard_version ()
: standard_version (s,
standard_version::allow_stub |
standard_version::allow_earliest);
}
string s;
while (getline (cin, s))
cout << (dv
? standard_version_constraint (s, *dv)
: standard_version_constraint (s)) << endl;
}
else if (o == "-sf")
{
assert (argc == 4);
char r (standard_version_constraint (argv[3]).satisfies (
version (argv[2])) ? 'y' : 'n');
cout << r << endl;
}
else
assert (false);
return 0;
}
string s;
while (!eof (getline (cin, s)))
cout << (s.empty () ? standard_version () : version (s)) << endl;
return 0;
}
catch (const invalid_argument& e)
{
cerr << e << endl;
return 1;
}
|