diff options
-rw-r--r-- | chrome/installer/installer.gyp | 1 | ||||
-rw-r--r-- | chrome/installer/util/version_unittest.cc | 82 |
2 files changed, 83 insertions, 0 deletions
diff --git a/chrome/installer/installer.gyp b/chrome/installer/installer.gyp index 3a36eb6..588bfc6 100644 --- a/chrome/installer/installer.gyp +++ b/chrome/installer/installer.gyp @@ -274,6 +274,7 @@ 'util/run_all_unittests.cc', 'util/set_reg_value_work_item_unittest.cc', 'util/work_item_list_unittest.cc', + 'util/version_unittest.cc', ], 'msvs_settings': { 'VCManifestTool': { diff --git a/chrome/installer/util/version_unittest.cc b/chrome/installer/util/version_unittest.cc new file mode 100644 index 0000000..6a3e12c --- /dev/null +++ b/chrome/installer/util/version_unittest.cc @@ -0,0 +1,82 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/installer/util/version.h" + +#include <string> +#include <vector> + +#include "base/scoped_ptr.h" +#include "testing/gtest/include/gtest/gtest.h" + +static const installer::Version* kNoVersion = NULL; + +TEST(VersionTest, Parse) { + EXPECT_EQ(installer::Version::GetVersionFromString(L"1"), kNoVersion); + EXPECT_EQ(installer::Version::GetVersionFromString(L"1.2"), kNoVersion); + EXPECT_EQ(installer::Version::GetVersionFromString(L"1.2.3"), kNoVersion); + EXPECT_EQ(installer::Version::GetVersionFromString(L"1.2.3.4.5"), kNoVersion); + EXPECT_EQ(installer::Version::GetVersionFromString(L"hokum"), kNoVersion); + + scoped_ptr<installer::Version> v1( + installer::Version::GetVersionFromString(L"1.22.333.4444")); + EXPECT_EQ(v1->GetString(), L"1.22.333.4444"); +} + +bool Implies(bool p, bool q) { return !p | q; } + +TEST(VersionTest, Comparison) { + static const wchar_t* version_strings[] = { + L"0.0.0.0", + L"1.0.0.0", + L"0.1.0.0", + L"0.0.1.0", + L"0.0.0.1", + L"2.0.193.1", + L"3.0.183.1", + L"3.0.187.1", + L"3.0.189.0", + }; + + std::vector<const installer::Version*> versions; + + for (size_t i = 0; i < _countof(version_strings); ++i) { + std::wstring version_string(version_strings[i]); + const installer::Version* version = + installer::Version::GetVersionFromString(version_string); + EXPECT_NE(version, kNoVersion); + EXPECT_EQ(version_string, version->GetString()); + versions.push_back(version); + } + + // Compare all N*N pairs and check that IsHigherThan is antireflexive. + for (size_t i = 0; i < versions.size(); ++i) { + const installer::Version* v1 = versions[i]; + for (size_t j = 0; j < versions.size(); ++j) { + const installer::Version* v2 = versions[j]; + // Check exactly one of '>', '<', or '=' is true. + bool higher = v1->IsHigherThan(v2); + bool lower = v2->IsHigherThan(v1); + bool equal = v1->GetString() == v2->GetString(); + EXPECT_EQ(1, higher + lower + equal); + } + } + + // Compare all N*N*N triples and check that IsHigherThan is a total order. + for (size_t i = 0; i < versions.size(); ++i) { + const installer::Version* v1 = versions[i]; + for (size_t j = 0; j < versions.size(); ++j) { + const installer::Version* v2 = versions[j]; + for (size_t k = 0; k < versions.size(); ++k) { + const installer::Version* v3 = versions[j]; + EXPECT_TRUE(Implies(v2->IsHigherThan(v1) && v3->IsHigherThan(v2), + v3->IsHigherThan(v1))); + } + } + } + + for (size_t i = 0; i < versions.size(); ++i) { + delete versions[i]; + } +} |