Roll src/third_party/WebKit d9c6159:8139f33 (svn 201974:201975)
[chromium-blink-merge.git] / base / version_unittest.cc
blobf40ed27d8812630b3c1b2f4d0d9b89a68b763b46
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"
9 namespace {
11 TEST(VersionTest, DefaultConstructor) {
12 Version v;
13 EXPECT_FALSE(v.IsValid());
16 TEST(VersionTest, ValueSemantics) {
17 Version v1("1.2.3.4");
18 EXPECT_TRUE(v1.IsValid());
19 Version v3;
20 EXPECT_FALSE(v3.IsValid());
22 Version v2(v1);
23 v3 = v2;
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 {
32 const char* input;
33 size_t parts;
34 uint32_t firstpart;
35 bool success;
36 } cases[] = {
37 {"", 0, 0, false},
38 {" ", 0, 0, false},
39 {"\t", 0, 0, false},
40 {"\n", 0, 0, false},
41 {" ", 0, 0, false},
42 {".", 0, 0, false},
43 {" . ", 0, 0, false},
44 {"0", 1, 0, true},
45 {"0.", 0, 0, false},
46 {"0.0", 2, 0, true},
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},
59 {"0.01", 2, 0, true},
60 {"f.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 {
77 const char* lhs;
78 const char* rhs;
79 int expected;
80 } cases[] = {
81 {"1.0", "1.0", 0},
82 {"1.0", "0.0", 1},
83 {"1.0", "2.0", -1},
84 {"1.0", "1.1", -1},
85 {"1.1", "1.0", 1},
86 {"1.0", "1.0.1", -1},
87 {"1.1", "1.0.1", 1},
88 {"1.1", "1.0.1", 1},
89 {"1.0.0", "1.0", 0},
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 {
106 const char* lhs;
107 const char* rhs;
108 int expected;
109 } cases[] = {
110 {"1.0", "1.*", 0},
111 {"1.0", "0.*", 1},
112 {"1.0", "2.*", -1},
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 {
133 const char* version;
134 bool expected;
135 } cases[] = {
136 {"1.0", true},
137 {"", false},
138 {"1.2.3.4.5.6", true},
139 {"1.2.3.*", true},
140 {"1.2.3.5*", false},
141 {"1.2.3.56*", false},
142 {"1.*.3", false},
143 {"20.*", true},
144 {"+2.*", false},
145 {"*", false},
146 {"*.2", 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;
154 } // namespace