1// Copyright (C) 2013 Google Inc. 2// 3// Licensed under the Apache License, Version 2.0 (the "License"); 4// you may not use this file except in compliance with the License. 5// You may obtain a copy of the License at 6// 7// http://www.apache.org/licenses/LICENSE-2.0 8// 9// Unless required by applicable law or agreed to in writing, software 10// distributed under the License is distributed on an "AS IS" BASIS, 11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12// See the License for the specific language governing permissions and 13// limitations under the License. 14 15#include "util/string_compare.h" 16 17#include <libaddressinput/util/basictypes.h> 18 19#include <string> 20 21#include <gtest/gtest.h> 22 23namespace { 24 25using i18n::addressinput::StringCompare; 26 27struct TestCase { 28 TestCase(const std::string& left, 29 const std::string& right, 30 bool should_be_equal, 31 bool should_be_less) 32 : left(left), 33 right(right), 34 should_be_equal(should_be_equal), 35 should_be_less(should_be_less) {} 36 37 ~TestCase() {} 38 39 std::string left; 40 std::string right; 41 bool should_be_equal; 42 bool should_be_less; 43}; 44 45class StringCompareTest : public testing::TestWithParam<TestCase> { 46 protected: 47 StringCompareTest() {} 48 StringCompare compare_; 49 50 private: 51 DISALLOW_COPY_AND_ASSIGN(StringCompareTest); 52}; 53 54TEST_P(StringCompareTest, CorrectComparison) { 55 if (GetParam().should_be_equal) { 56 EXPECT_TRUE(compare_.NaturalEquals(GetParam().left, GetParam().right)); 57 } else { 58 EXPECT_FALSE(compare_.NaturalEquals(GetParam().left, GetParam().right)); 59 } 60} 61 62TEST_P(StringCompareTest, CorrectLess) { 63 if (GetParam().should_be_less) { 64 EXPECT_TRUE(compare_.NaturalLess(GetParam().left, GetParam().right)); 65 } else { 66 EXPECT_FALSE(compare_.NaturalLess(GetParam().left, GetParam().right)); 67 } 68} 69 70INSTANTIATE_TEST_CASE_P( 71 Comparisons, StringCompareTest, 72 testing::Values( 73 TestCase("foo", "foo", true, false), 74 TestCase("foo", "FOO", true, false), 75 TestCase("bar", "foo", false, true), 76 TestCase( 77 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */ 78 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", /* "강원도" */ 79 true, false), 80 TestCase( 81 /* "강원도" */ 82 "\xEA\xB0\x95\xEC\x9B\x90\xEB\x8F\x84", 83 /* "대구광역시" */ 84 "\xEB\x8C\x80\xEA\xB5\xAC\xEA\xB4\x91\xEC\x97\xAD\xEC\x8B\x9C", 85 false, true), 86 TestCase( 87 "Z\xC3\x9CRICH", /* "ZÜRICH" */ 88 "z\xC3\xBCrich", /* "zürich" */ 89 true, false), 90 TestCase( 91 "\xD0\xB0\xD0\xB1\xD0\xB2", /* "абв" */ 92 "\xD0\xB3\xD0\xB4\xD0\xB5", /* "где" */ 93 false, true), 94 TestCase( 95 "\xD0\xB0\xD0\xB1\xD0\xB2", /* "абв" */ 96 "\xD0\x93\xD0\x94\xD0\x95", /* "ГДЕ" */ 97 false, true), 98 TestCase( 99 "\xD0\xB3\xD0\xB4\xD0\xB5", /* "где" */ 100 "\xD0\xB0\xD0\xB1\xD0\xB2", /* "абв" */ 101 false, false), 102 TestCase( 103 "\xD0\xB3\xD0\xB4\xD0\xB5", /* "где" */ 104 "\xD0\x90\xD0\x91\xD0\x92", /* "АБВ" */ 105 false, false))); 106 107} // namespace 108