1 /* 2 * libjingle 3 * Copyright 2010, Google Inc. 4 * 5 * Redistribution and use in source and binary forms, with or without 6 * modification, are permitted provided that the following conditions are met: 7 * 8 * 1. Redistributions of source code must retain the above copyright notice, 9 * this list of conditions and the following disclaimer. 10 * 2. Redistributions in binary form must reproduce the above copyright notice, 11 * this list of conditions and the following disclaimer in the documentation 12 * and/or other materials provided with the distribution. 13 * 3. The name of the author may not be used to endorse or promote products 14 * derived from this software without specific prior written permission. 15 * 16 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR IMPLIED 17 * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 18 * MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO 19 * EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 20 * SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, 21 * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; 22 * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, 23 * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR 24 * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF 25 * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 26 */ 27 28 #include "talk/base/versionparsing.h" 29 30 #include "talk/base/gunit.h" 31 32 namespace talk_base { 33 34 static const int kExampleSegments = 4; 35 36 typedef int ExampleVersion[kExampleSegments]; 37 38 TEST(VersionParsing, TestGoodParse) { 39 ExampleVersion ver; 40 std::string str1("1.1.2.0"); 41 static const ExampleVersion expect1 = {1, 1, 2, 0}; 42 EXPECT_TRUE(ParseVersionString(str1, kExampleSegments, ver)); 43 EXPECT_EQ(0, CompareVersions(ver, expect1, kExampleSegments)); 44 std::string str2("2.0.0.1"); 45 static const ExampleVersion expect2 = {2, 0, 0, 1}; 46 EXPECT_TRUE(ParseVersionString(str2, kExampleSegments, ver)); 47 EXPECT_EQ(0, CompareVersions(ver, expect2, kExampleSegments)); 48 } 49 50 TEST(VersionParsing, TestBadParse) { 51 ExampleVersion ver; 52 std::string str1("1.1.2"); 53 EXPECT_FALSE(ParseVersionString(str1, kExampleSegments, ver)); 54 std::string str2(""); 55 EXPECT_FALSE(ParseVersionString(str2, kExampleSegments, ver)); 56 std::string str3("garbarge"); 57 EXPECT_FALSE(ParseVersionString(str3, kExampleSegments, ver)); 58 } 59 60 TEST(VersionParsing, TestCompare) { 61 static const ExampleVersion ver1 = {1, 0, 21, 0}; 62 static const ExampleVersion ver2 = {1, 1, 2, 0}; 63 static const ExampleVersion ver3 = {1, 1, 3, 0}; 64 static const ExampleVersion ver4 = {1, 1, 3, 9861}; 65 66 // Test that every combination of comparisons has the expected outcome. 67 EXPECT_EQ(0, CompareVersions(ver1, ver1, kExampleSegments)); 68 EXPECT_EQ(0, CompareVersions(ver2, ver2, kExampleSegments)); 69 EXPECT_EQ(0, CompareVersions(ver3, ver3, kExampleSegments)); 70 EXPECT_EQ(0, CompareVersions(ver4, ver4, kExampleSegments)); 71 72 EXPECT_GT(0, CompareVersions(ver1, ver2, kExampleSegments)); 73 EXPECT_LT(0, CompareVersions(ver2, ver1, kExampleSegments)); 74 75 EXPECT_GT(0, CompareVersions(ver1, ver3, kExampleSegments)); 76 EXPECT_LT(0, CompareVersions(ver3, ver1, kExampleSegments)); 77 78 EXPECT_GT(0, CompareVersions(ver1, ver4, kExampleSegments)); 79 EXPECT_LT(0, CompareVersions(ver4, ver1, kExampleSegments)); 80 81 EXPECT_GT(0, CompareVersions(ver2, ver3, kExampleSegments)); 82 EXPECT_LT(0, CompareVersions(ver3, ver2, kExampleSegments)); 83 84 EXPECT_GT(0, CompareVersions(ver2, ver4, kExampleSegments)); 85 EXPECT_LT(0, CompareVersions(ver4, ver2, kExampleSegments)); 86 87 EXPECT_GT(0, CompareVersions(ver3, ver4, kExampleSegments)); 88 EXPECT_LT(0, CompareVersions(ver4, ver3, kExampleSegments)); 89 } 90 91 } // namespace talk_base 92