Home | History | Annotate | Download | only in base
      1 // Copyright (c) 2011 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/memory/scoped_ptr.h"
      6 #include "base/version.h"
      7 #include "testing/gtest/include/gtest/gtest.h"
      8 
      9 class VersionTest : public testing::Test {
     10 };
     11 
     12 TEST_F(VersionTest, DefaultConstructor) {
     13   Version v;
     14   EXPECT_FALSE(v.is_valid_);
     15 }
     16 
     17 TEST_F(VersionTest, GetVersionFromString) {
     18   static const struct version_string {
     19     const char* input;
     20     size_t parts;
     21     bool success;
     22   } cases[] = {
     23     {"", 0, false},
     24     {" ", 0, false},
     25     {"\t", 0, false},
     26     {"\n", 0, false},
     27     {"  ", 0, false},
     28     {".", 0, false},
     29     {" . ", 0, false},
     30     {"0", 1, true},
     31     {"0.0", 2, true},
     32     {"65537.0", 0, false},
     33     {"-1.0", 0, false},
     34     {"1.-1.0", 0, false},
     35     {"+1.0", 0, false},
     36     {"1.+1.0", 0, false},
     37     {"1.0a", 0, false},
     38     {"1.2.3.4.5.6.7.8.9.0", 10, true},
     39     {"02.1", 0, false},
     40     {"f.1", 0, false},
     41   };
     42   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
     43     scoped_ptr<Version> vers(Version::GetVersionFromString(cases[i].input));
     44     EXPECT_EQ(cases[i].success, vers.get() != NULL);
     45     if (cases[i].success) {
     46       EXPECT_TRUE(vers->is_valid_);
     47       EXPECT_EQ(cases[i].parts, vers->components().size());
     48     }
     49   }
     50 }
     51 
     52 TEST_F(VersionTest, Compare) {
     53   static const struct version_compare {
     54     const char* lhs;
     55     const char* rhs;
     56     int expected;
     57   } cases[] = {
     58     {"1.0", "1.0", 0},
     59     {"1.0", "0.0", 1},
     60     {"1.0", "2.0", -1},
     61     {"1.0", "1.1", -1},
     62     {"1.1", "1.0", 1},
     63     {"1.0", "1.0.1", -1},
     64     {"1.1", "1.0.1", 1},
     65     {"1.1", "1.0.1", 1},
     66     {"1.0.0", "1.0", 0},
     67     {"1.0.3", "1.0.20", -1},
     68   };
     69   for (size_t i = 0; i < ARRAYSIZE_UNSAFE(cases); ++i) {
     70     scoped_ptr<Version> lhs(Version::GetVersionFromString(cases[i].lhs));
     71     scoped_ptr<Version> rhs(Version::GetVersionFromString(cases[i].rhs));
     72     EXPECT_EQ(lhs->CompareTo(*rhs), cases[i].expected) <<
     73           cases[i].lhs << " ? " << cases[i].rhs;
     74   }
     75 }
     76