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.
5 #include "base/version.h"
11 #include "base/macros.h"
12 #include "testing/gtest/include/gtest/gtest.h"
16 TEST(VersionTest, DefaultConstructor) {
18 EXPECT_FALSE(v.IsValid());
21 TEST(VersionTest, ValueSemantics) {
22 base::Version v1("1.2.3.4");
23 EXPECT_TRUE(v1.IsValid());
25 EXPECT_FALSE(v3.IsValid());
29 EXPECT_TRUE(v2.IsValid());
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");
43 TEST(VersionTest, GetVersionFromString) {
44 static const struct version_string {
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},
74 {"15.007.20011", 3, 15, true},
75 {"15.5.28.130162", 4, 15, true},
78 for (const auto& i : cases) {
79 base::Version version(i.input);
80 EXPECT_EQ(i.success, version.IsValid());
82 EXPECT_EQ(i.parts, version.components().size());
83 EXPECT_EQ(i.firstpart, version.components()[0]);
88 TEST(VersionTest, Compare) {
89 static const struct version_compare {
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},
108 for (const auto& i : cases) {
109 base::Version lhs(i.lhs);
110 base::Version rhs(i.rhs);
111 EXPECT_EQ(lhs.CompareTo(rhs), i.expected) << i.lhs << " ? " << i.rhs;
112 // CompareToWildcardString() should have same behavior as CompareTo() when
113 // no wildcards are present.
114 EXPECT_EQ(lhs.CompareToWildcardString(i.rhs), i.expected)
115 << i.lhs << " ? " << i.rhs;
116 EXPECT_EQ(rhs.CompareToWildcardString(i.lhs), -i.expected)
117 << i.lhs << " ? " << i.rhs;
119 // Test comparison operators
120 switch (i.expected) {
125 EXPECT_FALSE(lhs == rhs);
126 EXPECT_FALSE(lhs >= rhs);
127 EXPECT_FALSE(lhs > rhs);
130 EXPECT_FALSE(lhs < rhs);
132 EXPECT_FALSE(lhs != rhs);
135 EXPECT_FALSE(lhs > rhs);
138 EXPECT_FALSE(lhs < rhs);
139 EXPECT_FALSE(lhs <= rhs);
141 EXPECT_FALSE(lhs == rhs);
149 TEST(VersionTest, CompareToWildcardString) {
150 static const struct version_compare {
158 {"1.2.3", "1.2.3.*", 0},
159 {"10.0", "1.0.*", 1},
160 {"1.0", "3.0.*", -1},
161 {"1.4", "1.3.0.*", 1},
162 {"1.3.9", "1.3.*", 0},
163 {"1.4.1", "1.3.*", 1},
164 {"1.3", "1.4.5.*", -1},
165 {"1.5", "1.4.5.*", 1},
166 {"1.3.9", "1.3.*", 0},
167 {"1.2.0.0.0.0", "1.2.*", 0},
169 for (const auto& i : cases) {
170 const base::Version version(i.lhs);
171 const int result = version.CompareToWildcardString(i.rhs);
172 EXPECT_EQ(result, i.expected) << i.lhs << "?" << i.rhs;
176 TEST(VersionTest, IsValidWildcardString) {
177 static const struct version_compare {
183 {"1.2.3.4.5.6", true},
186 {"1.2.3.56*", false},
193 for (const auto& i : cases) {
194 EXPECT_EQ(base::Version::IsValidWildcardString(i.version), i.expected)
195 << i.version << "?" << i.expected;