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. 4 5 #include "base/version.h" 6 7 #include <stddef.h> 8 #include <stdint.h> 9 #include <utility> 10 11 #include "base/macros.h" 12 #include "testing/gtest/include/gtest/gtest.h" 13 14 namespace { 15 16 TEST(VersionTest, DefaultConstructor) { 17 base::Version v; 18 EXPECT_FALSE(v.IsValid()); 19 } 20 21 TEST(VersionTest, ValueSemantics) { 22 base::Version v1("1.2.3.4"); 23 EXPECT_TRUE(v1.IsValid()); 24 base::Version v3; 25 EXPECT_FALSE(v3.IsValid()); 26 { 27 base::Version v2(v1); 28 v3 = v2; 29 EXPECT_TRUE(v2.IsValid()); 30 EXPECT_EQ(v1, v2); 31 } 32 EXPECT_EQ(v3, v1); 33 } 34 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"); 40 EXPECT_EQ(v1, v2); 41 } 42 43 TEST(VersionTest, GetVersionFromString) { 44 static const struct version_string { 45 const char* input; 46 size_t parts; 47 uint32_t firstpart; 48 bool success; 49 } cases[] = { 50 {"", 0, 0, false}, 51 {" ", 0, 0, false}, 52 {"\t", 0, 0, false}, 53 {"\n", 0, 0, false}, 54 {" ", 0, 0, false}, 55 {".", 0, 0, false}, 56 {" . ", 0, 0, false}, 57 {"0", 1, 0, true}, 58 {"0.", 0, 0, false}, 59 {"0.0", 2, 0, true}, 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}, 72 {"0.01", 2, 0, true}, 73 {"f.1", 0, 0, false}, 74 {"15.007.20011", 3, 15, true}, 75 {"15.5.28.130162", 4, 15, true}, 76 }; 77 78 for (size_t i = 0; i < arraysize(cases); ++i) { 79 base::Version version(cases[i].input); 80 EXPECT_EQ(cases[i].success, version.IsValid()); 81 if (cases[i].success) { 82 EXPECT_EQ(cases[i].parts, version.components().size()); 83 EXPECT_EQ(cases[i].firstpart, version.components()[0]); 84 } 85 } 86 } 87 88 TEST(VersionTest, Compare) { 89 static const struct version_compare { 90 const char* lhs; 91 const char* rhs; 92 int expected; 93 } cases[] = { 94 {"1.0", "1.0", 0}, 95 {"1.0", "0.0", 1}, 96 {"1.0", "2.0", -1}, 97 {"1.0", "1.1", -1}, 98 {"1.1", "1.0", 1}, 99 {"1.0", "1.0.1", -1}, 100 {"1.1", "1.0.1", 1}, 101 {"1.1", "1.0.1", 1}, 102 {"1.0.0", "1.0", 0}, 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 }; 107 for (size_t i = 0; i < arraysize(cases); ++i) { 108 base::Version lhs(cases[i].lhs); 109 base::Version rhs(cases[i].rhs); 110 EXPECT_EQ(lhs.CompareTo(rhs), cases[i].expected) << 111 cases[i].lhs << " ? " << cases[i].rhs; 112 113 // Test comparison operators 114 switch (cases[i].expected) { 115 case -1: 116 EXPECT_LT(lhs, rhs); 117 EXPECT_LE(lhs, rhs); 118 EXPECT_NE(lhs, rhs); 119 EXPECT_FALSE(lhs == rhs); 120 EXPECT_FALSE(lhs >= rhs); 121 EXPECT_FALSE(lhs > rhs); 122 break; 123 case 0: 124 EXPECT_FALSE(lhs < rhs); 125 EXPECT_LE(lhs, rhs); 126 EXPECT_FALSE(lhs != rhs); 127 EXPECT_EQ(lhs, rhs); 128 EXPECT_GE(lhs, rhs); 129 EXPECT_FALSE(lhs > rhs); 130 break; 131 case 1: 132 EXPECT_FALSE(lhs < rhs); 133 EXPECT_FALSE(lhs <= rhs); 134 EXPECT_NE(lhs, rhs); 135 EXPECT_FALSE(lhs == rhs); 136 EXPECT_GE(lhs, rhs); 137 EXPECT_GT(lhs, rhs); 138 break; 139 } 140 } 141 } 142 143 TEST(VersionTest, CompareToWildcardString) { 144 static const struct version_compare { 145 const char* lhs; 146 const char* rhs; 147 int expected; 148 } cases[] = { 149 {"1.0", "1.*", 0}, 150 {"1.0", "0.*", 1}, 151 {"1.0", "2.*", -1}, 152 {"1.2.3", "1.2.3.*", 0}, 153 {"10.0", "1.0.*", 1}, 154 {"1.0", "3.0.*", -1}, 155 {"1.4", "1.3.0.*", 1}, 156 {"1.3.9", "1.3.*", 0}, 157 {"1.4.1", "1.3.*", 1}, 158 {"1.3", "1.4.5.*", -1}, 159 {"1.5", "1.4.5.*", 1}, 160 {"1.3.9", "1.3.*", 0}, 161 {"1.2.0.0.0.0", "1.2.*", 0}, 162 }; 163 for (size_t i = 0; i < arraysize(cases); ++i) { 164 const base::Version version(cases[i].lhs); 165 const int result = version.CompareToWildcardString(cases[i].rhs); 166 EXPECT_EQ(result, cases[i].expected) << cases[i].lhs << "?" << cases[i].rhs; 167 } 168 } 169 170 TEST(VersionTest, IsValidWildcardString) { 171 static const struct version_compare { 172 const char* version; 173 bool expected; 174 } cases[] = { 175 {"1.0", true}, 176 {"", false}, 177 {"1.2.3.4.5.6", true}, 178 {"1.2.3.*", true}, 179 {"1.2.3.5*", false}, 180 {"1.2.3.56*", false}, 181 {"1.*.3", false}, 182 {"20.*", true}, 183 {"+2.*", false}, 184 {"*", false}, 185 {"*.2", false}, 186 }; 187 for (size_t i = 0; i < arraysize(cases); ++i) { 188 EXPECT_EQ(base::Version::IsValidWildcardString(cases[i].version), 189 cases[i].expected) << cases[i].version << "?" << cases[i].expected; 190 } 191 } 192 193 } // namespace 194