1 /*
2 * test-version.c
3 * Tests for the public libpkgconf version comparison API.
4 *
5 * SPDX-License-Identifier: pkgconf
6 *
7 * Copyright (c) 2026 pkgconf authors (see AUTHORS).
8 *
9 * Permission to use, copy, modify, and/or distribute this software for any
10 * purpose with or without fee is hereby granted, provided that the above
11 * copyright notice and this permission notice appear in all copies.
12 *
13 * This software is provided 'as is' and without any warranty, express or
14 * implied. In no event shall the authors be liable for any damages arising
15 * from the use of this software.
16 */
17
18 #include "test-api.h"
19
20 static void
cmp_lt(const char * a,const char * b)21 cmp_lt(const char *a, const char *b)
22 {
23 TEST_ASSERT_EQ(pkgconf_compare_version(a, b), -1);
24 TEST_ASSERT_EQ(pkgconf_compare_version(b, a), 1);
25 }
26
27 static void
cmp_eq(const char * a,const char * b)28 cmp_eq(const char *a, const char *b)
29 {
30 TEST_ASSERT_EQ(pkgconf_compare_version(a, b), 0);
31 TEST_ASSERT_EQ(pkgconf_compare_version(b, a), 0);
32 }
33
34 static void
test_version_equal(void)35 test_version_equal(void)
36 {
37 cmp_eq("1.0.0", "1.0.0");
38 cmp_eq("", "");
39 cmp_eq("abc", "abc");
40 cmp_eq("1.0a", "1.0A");
41 cmp_eq("RELEASE", "release");
42 }
43
44 static void
test_version_null(void)45 test_version_null(void)
46 {
47 TEST_ASSERT_EQ(pkgconf_compare_version(NULL, NULL), -1);
48 cmp_lt(NULL, "1.0");
49 cmp_lt(NULL, "");
50 }
51
52 static void
test_version_numeric(void)53 test_version_numeric(void)
54 {
55 cmp_lt("1.0.0", "1.0.1");
56 cmp_lt("0.9", "1.0");
57 cmp_lt("1.9.9", "2.0.0");
58 cmp_lt("1.9", "1.10");
59 cmp_lt("1.9", "1.100");
60 }
61
62 static void
test_version_leading_zeros(void)63 test_version_leading_zeros(void)
64 {
65 cmp_eq("1.0", "1.00");
66 cmp_eq("1.7", "1.007");
67 cmp_eq("01.02.03", "1.2.3");
68 cmp_lt("1.0.1", "1.0.10");
69 }
70
71 static void
test_version_component_count(void)72 test_version_component_count(void)
73 {
74 cmp_lt("1.0", "1.0.1");
75 cmp_lt("1.0", "1.0.0");
76 cmp_lt("1.2.3", "1.2.3.1");
77 }
78
79 static void
test_version_separators(void)80 test_version_separators(void)
81 {
82 cmp_eq("1.2.3", "1-2-3");
83 cmp_eq("1.2.3", "1_2_3");
84 cmp_eq("1.2.3", "1:2:3");
85 cmp_eq("1.0", "1...0");
86 }
87
88 static void
test_version_tilde(void)89 test_version_tilde(void)
90 {
91 cmp_lt("1.0~rc1", "1.0");
92 cmp_lt("1.0~rc1", "1.0~rc2");
93 cmp_lt("1.0~~", "1.0~");
94 cmp_lt("1.0~1", "1.0.1");
95 cmp_lt("1.0.0~beta", "1.0.0");
96 }
97
98 static void
test_version_alpha_numeric(void)99 test_version_alpha_numeric(void)
100 {
101 cmp_lt("1.b", "1.5");
102 cmp_lt("1.0.alpha", "1.0.beta");
103 cmp_lt("1.0alpha", "1.0alphabeta");
104 cmp_lt("1.0", "1.0pre");
105 }
106
107 static void
test_version_real_world(void)108 test_version_real_world(void)
109 {
110 cmp_lt("2.5.1", "2.9.91");
111 cmp_lt("2.9.91", "3.0.0");
112 cmp_lt("1.0.0~beta", "1.0.0~rc1");
113 cmp_lt("1.0.0~rc1", "1.0.0");
114 }
115
116 int
main(int argc,char * argv[])117 main(int argc, char *argv[])
118 {
119 (void) argc;
120 const char *basename = pkgconf_path_find_basename(argv[0]);
121
122 TEST_RUN(basename, test_version_equal);
123 TEST_RUN(basename, test_version_null);
124 TEST_RUN(basename, test_version_numeric);
125 TEST_RUN(basename, test_version_leading_zeros);
126 TEST_RUN(basename, test_version_component_count);
127 TEST_RUN(basename, test_version_separators);
128 TEST_RUN(basename, test_version_tilde);
129 TEST_RUN(basename, test_version_alpha_numeric);
130 TEST_RUN(basename, test_version_real_world);
131
132 return EXIT_SUCCESS;
133 }
134