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 "chrome/browser/metrics/perf/random_selector.h" | 5 #include "chrome/browser/metrics/perf/random_selector.h" |
6 | 6 |
7 #include <cmath> | 7 #include <cmath> |
8 #include <map> | 8 #include <map> |
9 #include <string> | 9 #include <string> |
10 | 10 |
(...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
77 } | 77 } |
78 | 78 |
79 TEST(RandomSelector, SimpleAccessors) { | 79 TEST(RandomSelector, SimpleAccessors) { |
80 using WeightAndValue = RandomSelector::WeightAndValue; | 80 using WeightAndValue = RandomSelector::WeightAndValue; |
81 std::vector<WeightAndValue> odds; | 81 std::vector<WeightAndValue> odds; |
82 odds.push_back(WeightAndValue(1, "a 1")); | 82 odds.push_back(WeightAndValue(1, "a 1")); |
83 odds.push_back(WeightAndValue(3, "b --help")); | 83 odds.push_back(WeightAndValue(3, "b --help")); |
84 odds.push_back(WeightAndValue(107, "c bar")); | 84 odds.push_back(WeightAndValue(107, "c bar")); |
85 EXPECT_EQ(111.0L, RandomSelector::SumWeights(odds)); | 85 EXPECT_EQ(111.0L, RandomSelector::SumWeights(odds)); |
86 RandomSelector random_selector; | 86 RandomSelector random_selector; |
87 random_selector.SetOdds(odds); | 87 EXPECT_TRUE(random_selector.SetOdds(odds)); |
88 EXPECT_EQ(3UL, random_selector.num_values()); | 88 EXPECT_EQ(3UL, random_selector.num_values()); |
| 89 EXPECT_EQ(odds, random_selector.odds()); |
89 } | 90 } |
90 | 91 |
91 // Ensure RandomSelector is able to generate results from given odds. | 92 // Ensure RandomSelector is able to generate results from given odds. |
92 TEST(RandomSelector, GenerateTest) { | 93 TEST(RandomSelector, GenerateTest) { |
93 using WeightAndValue = RandomSelector::WeightAndValue; | 94 using WeightAndValue = RandomSelector::WeightAndValue; |
94 const int kLargeNumber = 2000; | 95 const int kLargeNumber = 2000; |
95 std::vector<RandomSelector::WeightAndValue> odds; | 96 std::vector<RandomSelector::WeightAndValue> odds; |
96 odds.push_back(WeightAndValue(1, "a 1")); | 97 odds.push_back(WeightAndValue(1, "a 1")); |
97 odds.push_back(WeightAndValue(2, "b --help")); | 98 odds.push_back(WeightAndValue(2, "b --help")); |
98 odds.push_back(WeightAndValue(3, "c bar")); | 99 odds.push_back(WeightAndValue(3, "c bar")); |
99 RandomSelectorWithCustomRNG random_selector(kLargeNumber); | 100 RandomSelectorWithCustomRNG random_selector(kLargeNumber); |
100 random_selector.SetOdds(odds); | 101 EXPECT_TRUE(random_selector.SetOdds(odds)); |
101 // Generate a lot of values. | 102 // Generate a lot of values. |
102 std::map<std::string, int> results; | 103 std::map<std::string, int> results; |
103 GenerateResults(kLargeNumber, &random_selector, &results); | 104 GenerateResults(kLargeNumber, &random_selector, &results); |
104 // Ensure the values and odds are related. | 105 // Ensure the values and odds are related. |
105 CheckResultsAgainstOdds(odds, results); | 106 CheckResultsAgainstOdds(odds, results); |
106 } | 107 } |
| 108 |
| 109 TEST(RandomSelector, InvalidWeights) { |
| 110 using WeightAndValue = RandomSelector::WeightAndValue; |
| 111 std::vector<RandomSelector::WeightAndValue> good_odds; |
| 112 good_odds.push_back(WeightAndValue(1, "a 1")); |
| 113 good_odds.push_back(WeightAndValue(2, "b --help")); |
| 114 good_odds.push_back(WeightAndValue(3, "c bar")); |
| 115 RandomSelector random_selector; |
| 116 EXPECT_TRUE(random_selector.SetOdds(good_odds)); |
| 117 EXPECT_EQ(good_odds, random_selector.odds()); |
| 118 |
| 119 std::vector<RandomSelector::WeightAndValue> bad_odds; |
| 120 bad_odds.push_back(WeightAndValue(1, "a 1")); |
| 121 bad_odds.push_back(WeightAndValue(2, "b --help")); |
| 122 bad_odds.push_back(WeightAndValue(-3.5, "c bar")); |
| 123 EXPECT_FALSE(random_selector.SetOdds(bad_odds)); |
| 124 EXPECT_EQ(good_odds, random_selector.odds()); |
| 125 |
| 126 bad_odds[2].weight = 0.0; |
| 127 EXPECT_FALSE(random_selector.SetOdds(bad_odds)); |
| 128 EXPECT_EQ(good_odds, random_selector.odds()); |
| 129 } |
| 130 |
| 131 TEST(RandomSelector, EmptyWeights) { |
| 132 using WeightAndValue = RandomSelector::WeightAndValue; |
| 133 std::vector<RandomSelector::WeightAndValue> good_odds; |
| 134 good_odds.push_back(WeightAndValue(1, "a 1")); |
| 135 good_odds.push_back(WeightAndValue(2, "b --help")); |
| 136 good_odds.push_back(WeightAndValue(3, "c bar")); |
| 137 RandomSelector random_selector; |
| 138 EXPECT_TRUE(random_selector.SetOdds(good_odds)); |
| 139 EXPECT_EQ(good_odds, random_selector.odds()); |
| 140 |
| 141 std::vector<RandomSelector::WeightAndValue> empty_odds; |
| 142 EXPECT_FALSE(random_selector.SetOdds(empty_odds)); |
| 143 EXPECT_EQ(good_odds, random_selector.odds()); |
| 144 } |
OLD | NEW |