• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "testing/gtest/include/gtest/gtest.h"
8 
9 namespace {
10 
TEST(VersionTest,DefaultConstructor)11 TEST(VersionTest, DefaultConstructor) {
12   Version v;
13   EXPECT_FALSE(v.IsValid());
14 }
15 
TEST(VersionTest,ValueSemantics)16 TEST(VersionTest, ValueSemantics) {
17   Version v1("1.2.3.4");
18   EXPECT_TRUE(v1.IsValid());
19   Version v3;
20   EXPECT_FALSE(v3.IsValid());
21   {
22     Version v2(v1);
23     v3 = v2;
24     EXPECT_TRUE(v2.IsValid());
25     EXPECT_TRUE(v1.Equals(v2));
26   }
27   EXPECT_TRUE(v3.Equals(v1));
28 }
29 
TEST(VersionTest,GetVersionFromString)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},
54   };
55 
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());
61   }
62 }
63 
TEST(VersionTest,Compare)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},
80   };
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;
86 
87     EXPECT_EQ(lhs.IsOlderThan(cases[i].rhs), (cases[i].expected == -1));
88   }
89 }
90 
TEST(VersionTest,CompareToWildcardString)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},
110   };
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;
115   }
116 }
117 
TEST(VersionTest,IsValidWildcardString)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},
134   };
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;
138   }
139 }
140 
141 }  // namespace
142