| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "net/base/lookup_string_in_fixed_set.h" | 5 #include "net/base/lookup_string_in_fixed_set.h" |
| 6 | 6 |
| 7 #include <string.h> | |
| 8 | |
| 9 #include <algorithm> | 7 #include <algorithm> |
| 10 #include <limits> | 8 #include <limits> |
| 11 #include <ostream> | 9 #include <ostream> |
| 12 #include <utility> | 10 #include <utility> |
| 13 #include <vector> | 11 #include <vector> |
| 14 | 12 |
| 15 #include "base/base_paths.h" | 13 #include "base/base_paths.h" |
| 16 #include "base/files/file_path.h" | 14 #include "base/files/file_path.h" |
| 17 #include "base/files/file_util.h" | 15 #include "base/files/file_util.h" |
| 18 #include "base/path_service.h" | 16 #include "base/path_service.h" |
| 17 #include "base/strings/string_piece.h" |
| 19 #include "base/strings/string_util.h" | 18 #include "base/strings/string_util.h" |
| 20 #include "base/strings/stringprintf.h" | 19 #include "base/strings/stringprintf.h" |
| 21 #include "testing/gtest/include/gtest/gtest.h" | 20 #include "testing/gtest/include/gtest/gtest.h" |
| 22 | 21 |
| 23 namespace net { | 22 namespace net { |
| 24 namespace { | 23 namespace { |
| 25 namespace effective_tld_names { | 24 namespace effective_tld_names { |
| 26 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" | 25 #include "net/base/registry_controlled_domains/effective_tld_names-inc.cc" |
| 27 } | 26 } |
| 28 namespace test1 { | 27 namespace test1 { |
| (...skipping 17 matching lines...) Expand all Loading... |
| 46 int value; | 45 int value; |
| 47 }; | 46 }; |
| 48 | 47 |
| 49 void PrintTo(const Expectation& expectation, std::ostream* os) { | 48 void PrintTo(const Expectation& expectation, std::ostream* os) { |
| 50 *os << "{\"" << expectation.key << "\", " << expectation.value << "}"; | 49 *os << "{\"" << expectation.key << "\", " << expectation.value << "}"; |
| 51 } | 50 } |
| 52 | 51 |
| 53 class LookupStringInFixedSetTest : public testing::TestWithParam<Expectation> { | 52 class LookupStringInFixedSetTest : public testing::TestWithParam<Expectation> { |
| 54 protected: | 53 protected: |
| 55 template <size_t N> | 54 template <size_t N> |
| 56 int LookupInGraph(const unsigned char(&graph)[N], const char* key) { | 55 int LookupInGraph(const unsigned char (&graph)[N], base::StringPiece key) { |
| 57 return LookupStringInFixedSet(graph, N, key, strlen(key)); | 56 std::string rev_key(key.rbegin(), key.rend()); |
| 57 return LookupStringInFixedSet(graph, N, rev_key.data(), rev_key.length()); |
| 58 } | 58 } |
| 59 }; | 59 }; |
| 60 | 60 |
| 61 class Dafsa1Test : public LookupStringInFixedSetTest {}; | 61 class Dafsa1Test : public LookupStringInFixedSetTest {}; |
| 62 | 62 |
| 63 TEST_P(Dafsa1Test, BasicTest) { | 63 TEST_P(Dafsa1Test, BasicTest) { |
| 64 const Expectation& param = GetParam(); | 64 const Expectation& param = GetParam(); |
| 65 EXPECT_EQ(param.value, LookupInGraph(test1::kDafsa, param.key)); | 65 EXPECT_EQ(param.value, LookupInGraph(test1::kDafsa, param.key)); |
| 66 } | 66 } |
| 67 | 67 |
| 68 const Expectation kBasicTestCases[] = { | 68 const Expectation kBasicTestCases[] = { |
| 69 {"", -1}, {"j", -1}, {"jp", 0}, {"jjp", -1}, {"jpp", -1}, | 69 {"", -1}, {"j", -1}, {"jp", 0}, {"jjp", -1}, {"jpp", -1}, |
| 70 {"bar.jp", 2}, {"pref.bar.jp", 1}, {"c", 2}, {"b.c", 1}, {"priv.no", 4}, | 70 {"bar.jp", 2}, {"pref.bar.jp", 1}, {"c", 2}, {"b.c", 1}, {"priv.no", 4}, |
| 71 }; | 71 }; |
| 72 | 72 |
| 73 // Helper function for EnumerateDafsaLanaguage. | 73 // Helper function for EnumerateDafsaLanaguage. |
| 74 void RecursivelyEnumerateDafsaLanguage( | 74 void RecursivelyEnumerateDafsaLanguage( |
| 75 const FixedSetIncrementalLookup& lookup, | 75 const FixedSetIncrementalLookup& lookup, |
| 76 std::string* sequence, | 76 std::string* sequence, |
| 77 std::vector<std::pair<std::string, int>>* language) { | 77 std::vector<std::pair<std::string, int>>* language) { |
| 78 int result = lookup.GetResultForCurrentSequence(); | 78 int result = lookup.GetResultForCurrentSequence(); |
| 79 if (result != kDafsaNotFound) { | 79 if (result != kDafsaNotFound) { |
| 80 language->emplace_back(std::string(sequence->begin(), sequence->end()), | 80 language->emplace_back(std::string(sequence->rbegin(), sequence->rend()), |
| 81 result); | 81 result); |
| 82 } | 82 } |
| 83 // Try appending each char value. | 83 // Try appending each char value. |
| 84 for (char c = std::numeric_limits<char>::min();; ++c) { | 84 for (char c = std::numeric_limits<char>::min();; ++c) { |
| 85 FixedSetIncrementalLookup continued_lookup = lookup; | 85 FixedSetIncrementalLookup continued_lookup = lookup; |
| 86 if (continued_lookup.Advance(c)) { | 86 if (continued_lookup.Advance(c)) { |
| 87 sequence->push_back(c); | 87 sequence->push_back(c); |
| 88 size_t saved_language_size = language->size(); | 88 size_t saved_language_size = language->size(); |
| 89 RecursivelyEnumerateDafsaLanguage(continued_lookup, sequence, language); | 89 RecursivelyEnumerateDafsaLanguage(continued_lookup, sequence, language); |
| 90 CHECK_LT(saved_language_size, language->size()) | 90 CHECK_LT(saved_language_size, language->size()) |
| (...skipping 155 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 246 | 246 |
| 247 // |regenerated_gperf_text| should match the body of the .gperf file exactly. | 247 // |regenerated_gperf_text| should match the body of the .gperf file exactly. |
| 248 EXPECT_EQ(actual_gperf_text, regenerated_gperf_text); | 248 EXPECT_EQ(actual_gperf_text, regenerated_gperf_text); |
| 249 | 249 |
| 250 // Sanity check to prevent trivial success. | 250 // Sanity check to prevent trivial success. |
| 251 EXPECT_GT(actual_gperf_text.length(), 30000U); | 251 EXPECT_GT(actual_gperf_text.length(), 30000U); |
| 252 } | 252 } |
| 253 | 253 |
| 254 } // namespace | 254 } // namespace |
| 255 } // namespace net | 255 } // namespace net |
| OLD | NEW |