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"
7 #include "testing/gtest/include/gtest/gtest.h"
11 TEST(VersionTest
, DefaultConstructor
) {
13 EXPECT_FALSE(v
.IsValid());
16 TEST(VersionTest
, ValueSemantics
) {
17 Version
v1("1.2.3.4");
18 EXPECT_TRUE(v1
.IsValid());
20 EXPECT_FALSE(v3
.IsValid());
24 EXPECT_TRUE(v2
.IsValid());
25 EXPECT_TRUE(v1
.Equals(v2
));
27 EXPECT_TRUE(v3
.Equals(v1
));
30 TEST(VersionTest
, GetVersionFromString
) {
31 static const struct version_string
{
47 {"4294967295.0", 2, 4294967295, true},
48 {"4294967296.0", 0, 0, false},
49 {"-1.0", 0, 0, false},
50 {"1.-1.0", 0, 0, false},
51 {"1,--1.0", 0, 0, false},
52 {"+1.0", 0, 0, false},
53 {"1.+1.0", 0, 0, false},
54 {"1+1.0", 0, 0, false},
55 {"++1.0", 0, 0, false},
56 {"1.0a", 0, 0, false},
57 {"1.2.3.4.5.6.7.8.9.0", 10, 1, true},
58 {"02.1", 0, 0, false},
61 {"15.007.20011", 3, 15, true},
62 {"15.5.28.130162", 4, 15, true},
65 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
66 Version
version(cases
[i
].input
);
67 EXPECT_EQ(cases
[i
].success
, version
.IsValid());
68 if (cases
[i
].success
) {
69 EXPECT_EQ(cases
[i
].parts
, version
.components().size());
70 EXPECT_EQ(cases
[i
].firstpart
, version
.components()[0]);
75 TEST(VersionTest
, Compare
) {
76 static const struct version_compare
{
90 {"1.0.3", "1.0.20", -1},
91 {"11.0.10", "15.007.20011", -1},
92 {"11.0.10", "15.5.28.130162", -1},
94 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
95 Version
lhs(cases
[i
].lhs
);
96 Version
rhs(cases
[i
].rhs
);
97 EXPECT_EQ(lhs
.CompareTo(rhs
), cases
[i
].expected
) <<
98 cases
[i
].lhs
<< " ? " << cases
[i
].rhs
;
100 EXPECT_EQ(lhs
.IsOlderThan(cases
[i
].rhs
), (cases
[i
].expected
== -1));
104 TEST(VersionTest
, CompareToWildcardString
) {
105 static const struct version_compare
{
113 {"1.2.3", "1.2.3.*", 0},
114 {"10.0", "1.0.*", 1},
115 {"1.0", "3.0.*", -1},
116 {"1.4", "1.3.0.*", 1},
117 {"1.3.9", "1.3.*", 0},
118 {"1.4.1", "1.3.*", 1},
119 {"1.3", "1.4.5.*", -1},
120 {"1.5", "1.4.5.*", 1},
121 {"1.3.9", "1.3.*", 0},
122 {"1.2.0.0.0.0", "1.2.*", 0},
124 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
125 const Version
version(cases
[i
].lhs
);
126 const int result
= version
.CompareToWildcardString(cases
[i
].rhs
);
127 EXPECT_EQ(result
, cases
[i
].expected
) << cases
[i
].lhs
<< "?" << cases
[i
].rhs
;
131 TEST(VersionTest
, IsValidWildcardString
) {
132 static const struct version_compare
{
138 {"1.2.3.4.5.6", true},
141 {"1.2.3.56*", false},
148 for (size_t i
= 0; i
< arraysize(cases
); ++i
) {
149 EXPECT_EQ(Version::IsValidWildcardString(cases
[i
].version
),
150 cases
[i
].expected
) << cases
[i
].version
<< "?" << cases
[i
].expected
;