OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 "chrome/browser/predictors/resource_prefetch_predictor_test_util.h" |
| 6 |
| 7 #include <sstream> |
| 8 |
| 9 #include "chrome/browser/predictors/resource_prefetch_predictor.h" |
| 10 #include "chrome/browser/predictors/resource_prefetch_predictor_tables.h" |
| 11 |
| 12 namespace predictors { |
| 13 |
| 14 using ::chrome_browser_predictors::ResourceData; |
| 15 |
| 16 ResourceData MakeResourceData(std::string resource_url, |
| 17 content::ResourceType resource_type, |
| 18 int number_of_hits, |
| 19 int number_of_misses, |
| 20 int consecutive_misses, |
| 21 double average_position, |
| 22 net::RequestPriority priority, |
| 23 bool has_validators, |
| 24 bool always_revalidate) { |
| 25 using ResourceType = ::chrome_browser_predictors::ResourceData_ResourceType; |
| 26 using Priority = ::chrome_browser_predictors::ResourceData_Priority; |
| 27 |
| 28 ResourceData resource; |
| 29 resource.set_resource_url(resource_url); |
| 30 resource.set_resource_type(static_cast<ResourceType>(resource_type)); |
| 31 resource.set_number_of_hits(number_of_hits); |
| 32 resource.set_number_of_misses(number_of_misses); |
| 33 resource.set_consecutive_misses(consecutive_misses); |
| 34 resource.set_average_position(average_position); |
| 35 resource.set_priority(static_cast<Priority>(priority)); |
| 36 resource.set_has_validators(has_validators); |
| 37 resource.set_always_revalidate(always_revalidate); |
| 38 |
| 39 ResourcePrefetchPredictorTables::UpdateResourceScore(&resource); |
| 40 |
| 41 return resource; |
| 42 } |
| 43 |
| 44 // For printing failures nicely. |
| 45 void PrintTo(const ResourceData& resource, ::std::ostream* os) { |
| 46 *os << "[" << resource.primary_key() << "," << resource.resource_url() << "," |
| 47 << resource.resource_type() << "," << resource.number_of_hits() << "," |
| 48 << resource.number_of_misses() << "," << resource.consecutive_misses() |
| 49 << "," << resource.average_position() << "," << resource.score() << "]"; |
| 50 } |
| 51 |
| 52 void PrintTo(const ResourcePrefetchPredictorTables::PrefetchData& data, |
| 53 ::std::ostream* os) { |
| 54 *os << "[" << data.key_type << "," << data.primary_key << "," |
| 55 << data.last_visit.ToInternalValue() << "]\n"; |
| 56 for (const ResourceData& resource : data.resources) { |
| 57 *os << "\t\t"; |
| 58 PrintTo(resource, os); |
| 59 *os << "\n"; |
| 60 } |
| 61 } |
| 62 |
| 63 bool operator==(const ResourceData& lhs, const ResourceData& rhs) { |
| 64 // The primary key is not set. |
| 65 return !lhs.has_primary_key() && !rhs.has_primary_key() && |
| 66 lhs.resource_url() == rhs.resource_url() && |
| 67 lhs.resource_type() == rhs.resource_type() && |
| 68 lhs.number_of_misses() == rhs.number_of_misses() && |
| 69 lhs.consecutive_misses() == rhs.consecutive_misses() && |
| 70 lhs.average_position() == rhs.average_position() && |
| 71 lhs.priority() == rhs.priority() && |
| 72 lhs.has_validators() == rhs.has_validators() && |
| 73 lhs.always_revalidate() == rhs.always_revalidate() && |
| 74 lhs.score() == rhs.score(); |
| 75 } |
| 76 |
| 77 bool operator==(const ResourcePrefetchPredictorTables::PrefetchData& lhs, |
| 78 const ResourcePrefetchPredictorTables::PrefetchData& rhs) { |
| 79 bool equal = lhs.key_type == rhs.key_type && |
| 80 lhs.primary_key == rhs.primary_key && |
| 81 lhs.resources.size() == rhs.resources.size(); |
| 82 if (!equal) |
| 83 return false; |
| 84 for (size_t i = 0; i < lhs.resources.size(); ++i) { |
| 85 if (!(lhs.resources[i] == rhs.resources[i])) |
| 86 return false; |
| 87 } |
| 88 return true; |
| 89 } |
| 90 |
| 91 } // namespace predictors |
OLD | NEW |