Debian Codegraph @

test/fpm_test/test_versioning.f90

raw
1 !> Test implementation of version data type
2 module test_versioning
3 use testsuite, only : new_unittest, unittest_t, error_t, test_failed
4 use fpm_versioning
5 use fpm_release, only: fpm_version
6 implicit none
7 private
8
9 public :: collect_versioning
10
11
12 contains
13
14
15 !> Collect all exported unit tests
16 subroutine collect_versioning(tests)
17
18 !> Collection of tests
19 type(unittest_t), allocatable, intent(out) :: tests(:)
20
21 tests = [ &
22 & new_unittest("fpm-version", test_fpm_version), &
23 & new_unittest("valid-version", test_valid_version), &
24 & new_unittest("valid-equals", test_valid_equals), &
25 & new_unittest("valid-notequals", test_valid_notequals), &
26 & new_unittest("valid-compare", test_valid_compare), &
27 & new_unittest("valid-match", test_valid_match), &
28 & new_unittest("valid-string", test_valid_string), &
29 & new_unittest("invalid-empty", test_invalid_empty, should_fail=.true.), &
30 & new_unittest("invalid-version1", test_invalid_version1, should_fail=.true.), &
31 & new_unittest("invalid-version2", test_invalid_version2, should_fail=.true.), &
32 & new_unittest("invalid-version3", test_invalid_version3, should_fail=.true.), &
33 & new_unittest("invalid-overflow", test_invalid_overflow, should_fail=.true.)]
34
35 end subroutine collect_versioning
36
37 !> Test fpm self-version query
38 subroutine test_fpm_version(error)
39
40 !> Error handling
41 type(error_t), allocatable, intent(out) :: error
42
43 type(version_t) :: self_version
44
45 self_version = fpm_version()
46
47 end subroutine test_fpm_version
48
49 !> Read valid version strings
50 subroutine test_valid_version(error)
51
52 !> Error handling
53 type(error_t), allocatable, intent(out) :: error
54
55 type(version_t) :: version
56
57 call new_version(version, "8.9.0", error)
58 if (allocated(error)) return
59
60 call new_version(version, "2020.10.003", error)
61
62 end subroutine test_valid_version
63
64
65 !> Compare versions for equality
66 subroutine test_valid_equals(error)
67
68 !> Error handling
69 type(error_t), allocatable, intent(out) :: error
70
71 type(version_t) :: v1, v2
72 type(version_t) :: varray(4)
73
74 call new_version(v1, [1, 2, 0])
75 call new_version(v2, [1, 2])
76
77 if (.not. v1 == v2) then
78 call test_failed(error, "Version comparison failed")
79 return
80 end if
81
82 if (.not. v2 == v1) then
83 call test_failed(error, "Version comparison failed")
84 return
85 end if
86
87 call new_version(v1, [0, 9, 0])
88 call new_version(v2, [0, 9])
89
90 if (.not. v1==v2) then
91 call test_failed(error, "Version comparison failed")
92 return
93 end if
94
95 if (.not. v2==v1) then
96 call test_failed(error, "Version comparison failed")
97 return
98 end if
99
100 call new_version(v1, [2020])
101 call new_version(v2, [2020, 0])
102
103 if (.not. v1 == v2) then
104 call test_failed(error, "Version comparison failed")
105 return
106 end if
107
108 if (.not. v2 == v1) then
109 call test_failed(error, "Version comparison failed")
110 return
111 end if
112
113 call new_version(v1, [20, 1])
114 call new_version(varray(1), [19])
115 call new_version(varray(2), [18, 2])
116 call new_version(varray(3), [20, 1])
117 call new_version(varray(4), [1, 3, 1])
118
119 if (.not. any(v1 == varray)) then
120 call test_failed(error, "Version comparison failed")
121 return
122 end if
123
124 end subroutine test_valid_equals
125
126
127 !> Compare versions for mismatch
128 subroutine test_valid_notequals(error)
129
130 !> Error handling
131 type(error_t), allocatable, intent(out) :: error
132
133 type(version_t) :: v1, v2
134 type(version_t) :: varray(4)
135
136 call new_version(v1, [2020, 3, 1])
137 call new_version(v2, [2020, 3])
138
139 if (.not. v1 /= v2) then
140 call test_failed(error, "Version comparison failed")
141 return
142 end if
143
144 if (.not. v2 /= v1) then
145 call test_failed(error, "Version comparison failed")
146 return
147 end if
148
149 call new_version(v1, [0, 9, 1])
150 call new_version(v2, [0, 9])
151
152 if (.not. v1/=v2) then
153 call test_failed(error, "Version comparison failed")
154 return
155 end if
156
157 if (.not. v2/=v1) then
158 call test_failed(error, "Version comparison failed")
159 return
160 end if
161
162 call new_version(v1, [2020])
163 call new_version(v2, [0, 2020])
164
165 if (.not. v2 /= v1) then
166 call test_failed(error, "Version comparison failed")
167 return
168 end if
169
170 if (.not. v1 /= v2) then
171 call test_failed(error, "Version comparison failed")
172 return
173 end if
174
175 call new_version(v1, [20, 1])
176 call new_version(varray(1), [19])
177 call new_version(varray(2), [18, 2])
178 call new_version(varray(3), [18, 1])
179 call new_version(varray(4), [1, 3, 1])
180
181 if (.not. any(v1 /= varray)) then
182 call test_failed(error, "Version comparison failed")
183 return
184 end if
185
186 end subroutine test_valid_notequals
187
188
189 !> Relative comparison of versions
190 subroutine test_valid_compare(error)
191
192 !> Error handling
193 type(error_t), allocatable, intent(out) :: error
194
195 type(version_t) :: v1, v2
196 type(version_t) :: varray(4)
197
198 call new_version(v1, [10])
199 call new_version(v2, [1])
200
201 if (.not. v1 > v2) then
202 call test_failed(error, "Version comparison failed (gt)")
203 return
204 end if
205
206 if (.not. v1 >= v2) then
207 call test_failed(error, "Version comparison failed (ge)")
208 return
209 end if
210
211 if (.not. v2 < v1) then
212 call test_failed(error, "Version comparison failed (lt)")
213 return
214 end if
215
216 if (.not. v2 <= v1) then
217 call test_failed(error, "Version comparison failed (le)")
218 return
219 end if
220
221 call new_version(v1, [1, 0, 8])
222 call new_version(v2, [1, 0])
223
224 if (.not. v1 > v2) then
225 call test_failed(error, "Version comparison failed (gt)")
226 return
227 end if
228
229 if (.not. v1 >= v2) then
230 call test_failed(error, "Version comparison failed (ge)")
231 return
232 end if
233
234 if (.not. v2 < v1) then
235 call test_failed(error, "Version comparison failed (lt)")
236 return
237 end if
238
239 if (.not. v2 <= v1) then
240 call test_failed(error, "Version comparison failed (le)")
241 return
242 end if
243
244 call new_version(v1, [1, 2])
245 call new_version(v2, [1, 2, 0])
246
247 if (v1 > v2) then
248 call test_failed(error, "Version comparison failed (gt)")
249 return
250 end if
251
252 if (.not. v1 >= v2) then
253 call test_failed(error, "Version comparison failed (ge)")
254 return
255 end if
256
257 if (v2 < v1) then
258 call test_failed(error, "Version comparison failed (lt)")
259 return
260 end if
261
262 if (.not. v2 <= v1) then
263 call test_failed(error, "Version comparison failed (le)")
264 return
265 end if
266
267 call new_version(v1, [20, 1])
268 call new_version(varray(1), [19])
269 call new_version(varray(2), [18, 2])
270 call new_version(varray(3), [18, 1])
271 call new_version(varray(4), [1, 3, 1])
272
273 if (.not. all(v1 > varray)) then
274 call test_failed(error, "Version comparison failed (gt)")
275 return
276 end if
277
278 call new_version(v1, [1, 2, 3])
279 call new_version(v2, [2, 0, 0])
280
281 if (v1 > v2) then
282 call test_failed(error, "Version comparison failed (gt)")
283 return
284 end if
285
286 if (v1 >= v2) then
287 call test_failed(error, "Version comparison failed (ge)")
288 return
289 end if
290
291 if (v2 < v1) then
292 call test_failed(error, "Version comparison failed (lt)")
293 return
294 end if
295
296 if (v2 <= v1) then
297 call test_failed(error, "Version comparison failed (le)")
298 return
299 end if
300
301 call new_version(v1, [1, 2, 3])
302 call new_version(v2, [1, 0, 4])
303
304 if (v2 > v1) then
305 call test_failed(error, "Version comparison failed (gt)")
306 return
307 end if
308
309 if (v2 >= v1) then
310 call test_failed(error, "Version comparison failed (ge)")
311 return
312 end if
313
314 if (v1 < v2) then
315 call test_failed(error, "Version comparison failed (lt)")
316 return
317 end if
318
319 if (v1 <= v2) then
320 call test_failed(error, "Version comparison failed (le)")
321 return
322 end if
323
324 call new_version(v1, [1, 0, 8])
325 call new_version(v2, [1])
326
327 if (.not. v1 > v2) then
328 call test_failed(error, "Version comparison failed (gt)")
329 return
330 end if
331
332 if (.not. v1 >= v2) then
333 call test_failed(error, "Version comparison failed (ge)")
334 return
335 end if
336
337 if (.not. v2 < v1) then
338 call test_failed(error, "Version comparison failed (lt)")
339 return
340 end if
341
342 if (.not. v2 <= v1) then
343 call test_failed(error, "Version comparison failed (le)")
344 return
345 end if
346
347 call new_version(v1, [1])
348 call new_version(v2, [1, 0, 8])
349
350 if (v1 > v2) then
351 call test_failed(error, "Version comparison failed (gt)")
352 return
353 end if
354
355 if (v1 >= v2) then
356 call test_failed(error, "Version comparison failed (ge)")
357 return
358 end if
359
360 if (v2 < v1) then
361 call test_failed(error, "Version comparison failed (lt)")
362 return
363 end if
364
365 if (v2 <= v1) then
366 call test_failed(error, "Version comparison failed (le)")
367 return
368 end if
369
370 end subroutine test_valid_compare
371
372
373 !> Semantic version matching
374 subroutine test_valid_match(error)
375
376 !> Error handling
377 type(error_t), allocatable, intent(out) :: error
378
379 type(version_t) :: v1, v2
380
381 call new_version(v1, [1, 1, 0])
382 call new_version(v2, [1])
383
384 if (.not. (v1 .match. v2)) then
385 call test_failed(error, "Version comparison failed (match)")
386 return
387 end if
388
389 if (v2 .match. v1) then
390 call test_failed(error, "Version comparison failed (match)")
391 return
392 end if
393
394 call new_version(v1, [0, 5, 8])
395 call new_version(v2, [0, 5])
396
397 if (.not. (v1 .match. v2)) then
398 call test_failed(error, "Version comparison failed (match)")
399 return
400 end if
401
402 if (v2 .match. v1) then
403 call test_failed(error, "Version comparison failed (match)")
404 return
405 end if
406
407 call new_version(v1, [1, 2])
408 call new_version(v2, [1, 2, 0])
409
410 if (.not. (v1 .match. v2)) then
411 call test_failed(error, "Version comparison failed (match)")
412 return
413 end if
414
415 if (.not. (v2 .match. v1)) then
416 call test_failed(error, "Version comparison failed (match)")
417 return
418 end if
419
420 end subroutine test_valid_match
421
422
423 !> Test if version string is preserved
424 subroutine test_valid_string(error)
425
426 !> Error handling
427 type(error_t), allocatable, intent(out) :: error
428
429 character(len=*), parameter :: str_in = "20.1.100"
430 type(version_t) :: version
431
432 call new_version(version, str_in, error)
433 if (allocated(error)) return
434
435 if (str_in /= version%s()) then
436 call test_failed(error, "Expected "//str_in//" but got "//version%s())
437 end if
438
439 end subroutine test_valid_string
440
441
442 !> Empty string does not represent a version
443 subroutine test_invalid_empty(error)
444
445 !> Error handling
446 type(error_t), allocatable, intent(out) :: error
447
448 type(version_t) :: version
449
450 call new_version(version, "", error)
451
452 end subroutine test_invalid_empty
453
454
455 !> Version is invalid with trailing dots
456 subroutine test_invalid_version1(error)
457
458 !> Error handling
459 type(error_t), allocatable, intent(out) :: error
460
461 type(version_t) :: version
462
463 call new_version(version, "1.", error)
464
465 end subroutine test_invalid_version1
466
467
468 !> Version is invalid with multiple dots
469 subroutine test_invalid_version2(error)
470
471 !> Error handling
472 type(error_t), allocatable, intent(out) :: error
473
474 type(version_t) :: version
475
476 call new_version(version, "1..1", error)
477
478 end subroutine test_invalid_version2
479
480
481 !> Version is invalid if it is not a version
482 subroutine test_invalid_version3(error)
483
484 !> Error handling
485 type(error_t), allocatable, intent(out) :: error
486
487 type(version_t) :: version
488
489 call new_version(version, "one", error)
490
491 end subroutine test_invalid_version3
492
493
494 !> Check if overflows of the internal size constraint are handled gracefully
495 subroutine test_invalid_overflow(error)
496
497 !> Error handling
498 type(error_t), allocatable, intent(out) :: error
499
500 type(version_t) :: version
501
502 call new_version(version, "0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0.0", error)
503
504 end subroutine test_invalid_overflow
505
506
507 end module test_versioning