1 /*-
2 * SPDX-License-Identifier: BSD-2-Clause
3 * Copyright (c) 2022 Aymeric Wibo <obiwac@gmail.com>
4 */
5
6 #include <atf-c.h>
7 #include <string.h>
8
9 static void
check_all(size_t len,const char * ordered[len])10 check_all(size_t len, const char *ordered[len])
11 {
12 const char *a, *b;
13
14 for (size_t i = 0; i < len; i++) {
15 for (size_t j = 0; j < len; j++) {
16 a = ordered[i];
17 b = ordered[j];
18
19 if (i == j)
20 ATF_CHECK_MSG(
21 strverscmp(a, b) == 0,
22 "strverscmp(\"%s\", \"%s\") == 0",
23 a, b
24 );
25 else if (i < j)
26 ATF_CHECK_MSG(
27 strverscmp(a, b) < 0,
28 "strverscmp(\"%s\", \"%s\") < 0",
29 a, b
30 );
31 else if (i > j)
32 ATF_CHECK_MSG(
33 strverscmp(a, b) > 0,
34 "strverscmp(\"%s\", \"%s\") > 0",
35 a, b
36 );
37 }
38 }
39 }
40
41 #define CHECK_ALL(...) do { \
42 const char *ordered[] = { __VA_ARGS__ }; \
43 check_all(sizeof(ordered) / sizeof(*ordered), ordered); \
44 } while (0)
45
46 ATF_TC_WITHOUT_HEAD(strcmp_functionality);
ATF_TC_BODY(strcmp_functionality,tc)47 ATF_TC_BODY(strcmp_functionality, tc)
48 {
49 CHECK_ALL("", "a", "b");
50 }
51
52 /* from Linux man page strverscmp(3) */
53
54 ATF_TC_WITHOUT_HEAD(vers_ordering);
ATF_TC_BODY(vers_ordering,tc)55 ATF_TC_BODY(vers_ordering, tc)
56 {
57 CHECK_ALL("000", "00", "01", "010", "09", "0", "1", "9", "10");
58 }
59
60 ATF_TC_WITHOUT_HEAD(natural_ordering);
ATF_TC_BODY(natural_ordering,tc)61 ATF_TC_BODY(natural_ordering, tc)
62 {
63 CHECK_ALL("jan1", "jan2", "jan9", "jan10", "jan11", "jan19", "jan20");
64 }
65
66 /* https://sourceware.org/bugzilla/show_bug.cgi?id=9913 */
67
68 ATF_TC_WITHOUT_HEAD(glibc_bug_9913);
ATF_TC_BODY(glibc_bug_9913,tc)69 ATF_TC_BODY(glibc_bug_9913, tc)
70 {
71 CHECK_ALL(
72 "B0075022800016.gbp.corp.com",
73 "B007502280067.gbp.corp.com",
74 "B007502357019.GBP.CORP.COM"
75 );
76 }
77
78 ATF_TC_WITHOUT_HEAD(semver_ordering);
ATF_TC_BODY(semver_ordering,tc)79 ATF_TC_BODY(semver_ordering, tc)
80 {
81 CHECK_ALL("2.6.20", "2.6.21");
82 }
83
ATF_TP_ADD_TCS(tp)84 ATF_TP_ADD_TCS(tp)
85 {
86 ATF_TP_ADD_TC(tp, strcmp_functionality);
87 ATF_TP_ADD_TC(tp, vers_ordering);
88 ATF_TP_ADD_TC(tp, natural_ordering);
89 ATF_TP_ADD_TC(tp, glibc_bug_9913);
90 ATF_TP_ADD_TC(tp, semver_ordering);
91
92 return (atf_no_error());
93 }
94