1 // Copyright 2012 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #include "base/version.h"
11 #include "testing/gtest/include/gtest/gtest.h"
15 TEST(VersionTest, DefaultConstructor) {
17 EXPECT_FALSE(v.IsValid());
20 TEST(VersionTest, ValueSemantics) {
21 base::Version v1("1.2.3.4");
22 EXPECT_TRUE(v1.IsValid());
24 EXPECT_FALSE(v3.IsValid());
28 EXPECT_TRUE(v2.IsValid());
34 TEST(VersionTest, MoveSemantics) {
35 const std::vector<uint32_t> components = {1, 2, 3, 4};
36 base::Version v1(std::move(components));
37 EXPECT_TRUE(v1.IsValid());
38 base::Version v2("1.2.3.4");
42 TEST(VersionTest, GetVersionFromString) {
43 static const struct version_string {
59 {"4294967295.0", 2, 4294967295, true},
60 {"4294967296.0", 0, 0, false},
61 {"-1.0", 0, 0, false},
62 {"1.-1.0", 0, 0, false},
63 {"1,--1.0", 0, 0, false},
64 {"+1.0", 0, 0, false},
65 {"1.+1.0", 0, 0, false},
66 {"1+1.0", 0, 0, false},
67 {"++1.0", 0, 0, false},
68 {"1.0a", 0, 0, false},
69 {"1.2.3.4.5.6.7.8.9.0", 10, 1, true},
70 {"02.1", 0, 0, false},
73 {"15.007.20011", 3, 15, true},
74 {"15.5.28.130162", 4, 15, true},
77 for (const auto& i : cases) {
78 base::Version version(i.input);
79 EXPECT_EQ(i.success, version.IsValid());
81 EXPECT_EQ(i.parts, version.components().size());
82 EXPECT_EQ(i.firstpart, version.components()[0]);
87 TEST(VersionTest, Compare) {
88 static const struct version_compare {
102 {"1.0.3", "1.0.20", -1},
103 {"11.0.10", "15.007.20011", -1},
104 {"11.0.10", "15.5.28.130162", -1},
105 {"15.5.28.130162", "15.5.28.130162", 0},
107 for (const auto& i : cases) {
108 base::Version lhs(i.lhs);
109 base::Version rhs(i.rhs);
110 EXPECT_EQ(lhs.CompareTo(rhs), i.expected) << i.lhs << " ? " << i.rhs;
111 // CompareToWildcardString() should have same behavior as CompareTo() when
112 // no wildcards are present.
113 EXPECT_EQ(lhs.CompareToWildcardString(i.rhs), i.expected)
114 << i.lhs << " ? " << i.rhs;
115 EXPECT_EQ(rhs.CompareToWildcardString(i.lhs), -i.expected)
116 << i.lhs << " ? " << i.rhs;
118 // Test comparison operators
119 switch (i.expected) {
124 EXPECT_FALSE(lhs == rhs);
125 EXPECT_FALSE(lhs >= rhs);
126 EXPECT_FALSE(lhs > rhs);
129 EXPECT_FALSE(lhs < rhs);
131 EXPECT_FALSE(lhs != rhs);
134 EXPECT_FALSE(lhs > rhs);
137 EXPECT_FALSE(lhs < rhs);
138 EXPECT_FALSE(lhs <= rhs);
140 EXPECT_FALSE(lhs == rhs);
148 TEST(VersionTest, CompareToWildcardString) {
149 static const struct version_compare {
157 {"1.2.3", "1.2.3.*", 0},
158 {"10.0", "1.0.*", 1},
159 {"1.0", "3.0.*", -1},
160 {"1.4", "1.3.0.*", 1},
161 {"1.3.9", "1.3.*", 0},
162 {"1.4.1", "1.3.*", 1},
163 {"1.3", "1.4.5.*", -1},
164 {"1.5", "1.4.5.*", 1},
165 {"1.3.9", "1.3.*", 0},
166 {"1.2.0.0.0.0", "1.2.*", 0},
168 for (const auto& i : cases) {
169 const base::Version version(i.lhs);
170 const int result = version.CompareToWildcardString(i.rhs);
171 EXPECT_EQ(result, i.expected) << i.lhs << "?" << i.rhs;
175 TEST(VersionTest, IsValidWildcardString) {
176 static const struct version_compare {
182 {"1.2.3.4.5.6", true},
185 {"1.2.3.56*", false},
192 for (const auto& i : cases) {
193 EXPECT_EQ(base::Version::IsValidWildcardString(i.version), i.expected)
194 << i.version << "?" << i.expected;
198 TEST(VersionTest, LeadingZeros) {
200 // Leading zeros in the first component are not allowed.
201 base::Version v("01.1");
202 EXPECT_FALSE(v.IsValid());
206 // Leading zeros in subsequent components are allowed (and this behavior is
207 // now important for compatibility with existing modules, like extensions),
208 // but are ignored because the value is parsed as an integer...
209 base::Version v1("1.01");
210 EXPECT_TRUE(v1.IsValid());
211 // ...and as a result, v1.01 == v1.1.
212 EXPECT_EQ("1.1", v1.GetString());
213 base::Version v2("1.1");
217 // Similarly, since leading zeros are ignored, v1.02 > v1.1 (because
218 // v1.02 is translated to 1.2).
219 EXPECT_GT(base::Version("1.02"), base::Version("1.1"));
222 TEST(VersionTest, GetString) {
223 static const struct version_compare {
228 {"", false, "invalid"},
230 {"1.0", true, "1.0"},
231 {"0.0.1.0", true, "0.0.1.0"},
232 {"1.2.3.4.5.6", true, "1.2.3.4.5.6"},
233 {"1.*.3", false, "invalid"},
236 for (const auto& i : cases) {
237 base::Version v(i.version);
238 EXPECT_EQ(v.IsValid(), i.valid);
239 EXPECT_EQ(v.GetString(), i.string);