Blink roll 168661:148720
[chromium-blink-merge.git] / base / version_unittest.cc
blob2a2309ea31a4f00974fb891938f39ee969597418
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 bool success;
35 } cases[] = {
36 {"", 0, false},
37 {" ", 0, false},
38 {"\t", 0, false},
39 {"\n", 0, false},
40 {" ", 0, false},
41 {".", 0, false},
42 {" . ", 0, false},
43 {"0", 1, true},
44 {"0.0", 2, true},
45 {"65537.0", 0, false},
46 {"-1.0", 0, false},
47 {"1.-1.0", 0, false},
48 {"+1.0", 0, false},
49 {"1.+1.0", 0, false},
50 {"1.0a", 0, false},
51 {"1.2.3.4.5.6.7.8.9.0", 10, true},
52 {"02.1", 0, false},
53 {"f.1", 0, false},
56 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
57 Version version(cases[i].input);
58 EXPECT_EQ(cases[i].success, version.IsValid());
59 if (cases[i].success)
60 EXPECT_EQ(cases[i].parts, version.components().size());
64 TEST(VersionTest, Compare) {
65 static const struct version_compare {
66 const char* lhs;
67 const char* rhs;
68 int expected;
69 } cases[] = {
70 {"1.0", "1.0", 0},
71 {"1.0", "0.0", 1},
72 {"1.0", "2.0", -1},
73 {"1.0", "1.1", -1},
74 {"1.1", "1.0", 1},
75 {"1.0", "1.0.1", -1},
76 {"1.1", "1.0.1", 1},
77 {"1.1", "1.0.1", 1},
78 {"1.0.0", "1.0", 0},
79 {"1.0.3", "1.0.20", -1},
81 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
82 Version lhs(cases[i].lhs);
83 Version rhs(cases[i].rhs);
84 EXPECT_EQ(lhs.CompareTo(rhs), cases[i].expected) <<
85 cases[i].lhs << " ? " << cases[i].rhs;
87 EXPECT_EQ(lhs.IsOlderThan(cases[i].rhs), (cases[i].expected == -1));
91 TEST(VersionTest, CompareToWildcardString) {
92 static const struct version_compare {
93 const char* lhs;
94 const char* rhs;
95 int expected;
96 } cases[] = {
97 {"1.0", "1.*", 0},
98 {"1.0", "0.*", 1},
99 {"1.0", "2.*", -1},
100 {"1.2.3", "1.2.3.*", 0},
101 {"10.0", "1.0.*", 1},
102 {"1.0", "3.0.*", -1},
103 {"1.4", "1.3.0.*", 1},
104 {"1.3.9", "1.3.*", 0},
105 {"1.4.1", "1.3.*", 1},
106 {"1.3", "1.4.5.*", -1},
107 {"1.5", "1.4.5.*", 1},
108 {"1.3.9", "1.3.*", 0},
109 {"1.2.0.0.0.0", "1.2.*", 0},
111 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
112 const Version version(cases[i].lhs);
113 const int result = version.CompareToWildcardString(cases[i].rhs);
114 EXPECT_EQ(result, cases[i].expected) << cases[i].lhs << "?" << cases[i].rhs;
118 TEST(VersionTest, IsValidWildcardString) {
119 static const struct version_compare {
120 const char* version;
121 bool expected;
122 } cases[] = {
123 {"1.0", true},
124 {"", false},
125 {"1.2.3.4.5.6", true},
126 {"1.2.3.*", true},
127 {"1.2.3.5*", false},
128 {"1.2.3.56*", false},
129 {"1.*.3", false},
130 {"20.*", true},
131 {"+2.*", false},
132 {"*", false},
133 {"*.2", false},
135 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
136 EXPECT_EQ(Version::IsValidWildcardString(cases[i].version),
137 cases[i].expected) << cases[i].version << "?" << cases[i].expected;
141 } // namespace