1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4
5 #include "base/version.h"
6
7 #include <stddef.h>
8 #include <stdint.h>
9 #include <utility>
10
11 #include "base/macros.h"
12 #include "testing/gtest/include/gtest/gtest.h"
13
14 namespace {
15
TEST(VersionTest,DefaultConstructor)16 TEST(VersionTest, DefaultConstructor) {
17 base::Version v;
18 EXPECT_FALSE(v.IsValid());
19 }
20
TEST(VersionTest,ValueSemantics)21 TEST(VersionTest, ValueSemantics) {
22 base::Version v1("1.2.3.4");
23 EXPECT_TRUE(v1.IsValid());
24 base::Version v3;
25 EXPECT_FALSE(v3.IsValid());
26 {
27 base::Version v2(v1);
28 v3 = v2;
29 EXPECT_TRUE(v2.IsValid());
30 EXPECT_EQ(v1, v2);
31 }
32 EXPECT_EQ(v3, v1);
33 }
34
TEST(VersionTest,MoveSemantics)35 TEST(VersionTest, MoveSemantics) {
36 const std::vector<uint32_t> components = {1, 2, 3, 4};
37 base::Version v1(std::move(components));
38 EXPECT_TRUE(v1.IsValid());
39 base::Version v2("1.2.3.4");
40 EXPECT_EQ(v1, v2);
41 }
42
TEST(VersionTest,GetVersionFromString)43 TEST(VersionTest, GetVersionFromString) {
44 static const struct version_string {
45 const char* input;
46 size_t parts;
47 uint32_t firstpart;
48 bool success;
49 } cases[] = {
50 {"", 0, 0, false},
51 {" ", 0, 0, false},
52 {"\t", 0, 0, false},
53 {"\n", 0, 0, false},
54 {" ", 0, 0, false},
55 {".", 0, 0, false},
56 {" . ", 0, 0, false},
57 {"0", 1, 0, true},
58 {"0.", 0, 0, false},
59 {"0.0", 2, 0, true},
60 {"4294967295.0", 2, 4294967295, true},
61 {"4294967296.0", 0, 0, false},
62 {"-1.0", 0, 0, false},
63 {"1.-1.0", 0, 0, false},
64 {"1,--1.0", 0, 0, false},
65 {"+1.0", 0, 0, false},
66 {"1.+1.0", 0, 0, false},
67 {"1+1.0", 0, 0, false},
68 {"++1.0", 0, 0, false},
69 {"1.0a", 0, 0, false},
70 {"1.2.3.4.5.6.7.8.9.0", 10, 1, true},
71 {"02.1", 0, 0, false},
72 {"0.01", 2, 0, true},
73 {"f.1", 0, 0, false},
74 {"15.007.20011", 3, 15, true},
75 {"15.5.28.130162", 4, 15, true},
76 };
77
78 for (size_t i = 0; i < arraysize(cases); ++i) {
79 base::Version version(cases[i].input);
80 EXPECT_EQ(cases[i].success, version.IsValid());
81 if (cases[i].success) {
82 EXPECT_EQ(cases[i].parts, version.components().size());
83 EXPECT_EQ(cases[i].firstpart, version.components()[0]);
84 }
85 }
86 }
87
TEST(VersionTest,Compare)88 TEST(VersionTest, Compare) {
89 static const struct version_compare {
90 const char* lhs;
91 const char* rhs;
92 int expected;
93 } cases[] = {
94 {"1.0", "1.0", 0},
95 {"1.0", "0.0", 1},
96 {"1.0", "2.0", -1},
97 {"1.0", "1.1", -1},
98 {"1.1", "1.0", 1},
99 {"1.0", "1.0.1", -1},
100 {"1.1", "1.0.1", 1},
101 {"1.1", "1.0.1", 1},
102 {"1.0.0", "1.0", 0},
103 {"1.0.3", "1.0.20", -1},
104 {"11.0.10", "15.007.20011", -1},
105 {"11.0.10", "15.5.28.130162", -1},
106 {"15.5.28.130162", "15.5.28.130162", 0},
107 };
108 for (size_t i = 0; i < arraysize(cases); ++i) {
109 base::Version lhs(cases[i].lhs);
110 base::Version rhs(cases[i].rhs);
111 EXPECT_EQ(lhs.CompareTo(rhs), cases[i].expected) <<
112 cases[i].lhs << " ? " << cases[i].rhs;
113 // CompareToWildcardString() should have same behavior as CompareTo() when
114 // no wildcards are present.
115 EXPECT_EQ(lhs.CompareToWildcardString(cases[i].rhs), cases[i].expected)
116 << cases[i].lhs << " ? " << cases[i].rhs;
117 EXPECT_EQ(rhs.CompareToWildcardString(cases[i].lhs), -cases[i].expected)
118 << cases[i].lhs << " ? " << cases[i].rhs;
119
120 // Test comparison operators
121 switch (cases[i].expected) {
122 case -1:
123 EXPECT_LT(lhs, rhs);
124 EXPECT_LE(lhs, rhs);
125 EXPECT_NE(lhs, rhs);
126 EXPECT_FALSE(lhs == rhs);
127 EXPECT_FALSE(lhs >= rhs);
128 EXPECT_FALSE(lhs > rhs);
129 break;
130 case 0:
131 EXPECT_FALSE(lhs < rhs);
132 EXPECT_LE(lhs, rhs);
133 EXPECT_FALSE(lhs != rhs);
134 EXPECT_EQ(lhs, rhs);
135 EXPECT_GE(lhs, rhs);
136 EXPECT_FALSE(lhs > rhs);
137 break;
138 case 1:
139 EXPECT_FALSE(lhs < rhs);
140 EXPECT_FALSE(lhs <= rhs);
141 EXPECT_NE(lhs, rhs);
142 EXPECT_FALSE(lhs == rhs);
143 EXPECT_GE(lhs, rhs);
144 EXPECT_GT(lhs, rhs);
145 break;
146 }
147 }
148 }
149
TEST(VersionTest,CompareToWildcardString)150 TEST(VersionTest, CompareToWildcardString) {
151 static const struct version_compare {
152 const char* lhs;
153 const char* rhs;
154 int expected;
155 } cases[] = {
156 {"1.0", "1.*", 0},
157 {"1.0", "0.*", 1},
158 {"1.0", "2.*", -1},
159 {"1.2.3", "1.2.3.*", 0},
160 {"10.0", "1.0.*", 1},
161 {"1.0", "3.0.*", -1},
162 {"1.4", "1.3.0.*", 1},
163 {"1.3.9", "1.3.*", 0},
164 {"1.4.1", "1.3.*", 1},
165 {"1.3", "1.4.5.*", -1},
166 {"1.5", "1.4.5.*", 1},
167 {"1.3.9", "1.3.*", 0},
168 {"1.2.0.0.0.0", "1.2.*", 0},
169 };
170 for (size_t i = 0; i < arraysize(cases); ++i) {
171 const base::Version version(cases[i].lhs);
172 const int result = version.CompareToWildcardString(cases[i].rhs);
173 EXPECT_EQ(result, cases[i].expected) << cases[i].lhs << "?" << cases[i].rhs;
174 }
175 }
176
TEST(VersionTest,IsValidWildcardString)177 TEST(VersionTest, IsValidWildcardString) {
178 static const struct version_compare {
179 const char* version;
180 bool expected;
181 } cases[] = {
182 {"1.0", true},
183 {"", false},
184 {"1.2.3.4.5.6", true},
185 {"1.2.3.*", true},
186 {"1.2.3.5*", false},
187 {"1.2.3.56*", false},
188 {"1.*.3", false},
189 {"20.*", true},
190 {"+2.*", false},
191 {"*", false},
192 {"*.2", false},
193 };
194 for (size_t i = 0; i < arraysize(cases); ++i) {
195 EXPECT_EQ(base::Version::IsValidWildcardString(cases[i].version),
196 cases[i].expected) << cases[i].version << "?" << cases[i].expected;
197 }
198 }
199
200 } // namespace
201