OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 // Test of FieldTrial class | 5 // Test of FieldTrial class |
6 | 6 |
7 #include "base/metrics/field_trial.h" | 7 #include "base/metrics/field_trial.h" |
8 | 8 |
9 #include "base/rand_util.h" | 9 #include "base/rand_util.h" |
10 #include "base/stringprintf.h" | 10 #include "base/stringprintf.h" |
(...skipping 203 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 name, 1000000000, default_group_name, last_year_, 1, 1); | 214 name, 1000000000, default_group_name, last_year_, 1, 1); |
215 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. | 215 trial->AppendGroup(loser, 999999999); // 99.9999999% chance of being chosen. |
216 | 216 |
217 // Because trial has expired, we should always be in the default group. | 217 // Because trial has expired, we should always be in the default group. |
218 EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); | 218 EXPECT_EQ(FieldTrial::kDefaultGroupNumber, trial->group()); |
219 | 219 |
220 // And that default_group_name should ALWAYS win. | 220 // And that default_group_name should ALWAYS win. |
221 EXPECT_EQ(default_group_name, trial->group_name()); | 221 EXPECT_EQ(default_group_name, trial->group_name()); |
222 } | 222 } |
223 | 223 |
| 224 TEST_F(FieldTrialTest, HashName) { |
| 225 // Make sure hashing is stable on all platforms. |
| 226 struct { |
| 227 const char* name; |
| 228 uint32 hash_value; |
| 229 } known_hashes[] = { |
| 230 {"a", 937752454}, |
| 231 {"1", 723085877}, |
| 232 {"Trial Name", 2713117220}, |
| 233 {"Group Name", 3201815843}, |
| 234 {"My Favorite Experiment", 3722155194}, |
| 235 {"My Awesome Group Name", 4109503236}, |
| 236 {"abcdefghijklmonpqrstuvwxyz", 787728696}, |
| 237 {"0123456789ABCDEF", 348858318} |
| 238 }; |
| 239 for (size_t i = 0; i < ARRAYSIZE_UNSAFE(known_hashes); ++i) { |
| 240 EXPECT_EQ(known_hashes[i].hash_value, |
| 241 FieldTrial::HashName(known_hashes[i].name)); |
| 242 } |
| 243 } |
| 244 |
| 245 TEST_F(FieldTrialTest, NameGroupIds) { |
| 246 std::string no_group("No Group"); |
| 247 uint32 no_group_id = FieldTrial::HashName(no_group); |
| 248 scoped_refptr<FieldTrial> trial(new FieldTrial( |
| 249 no_group, 10, "Default", next_year_, 12, 31)); |
| 250 |
| 251 // There is no winner yet, so no NameGroupId should be returned. |
| 252 FieldTrial::NameGroupId name_group_id; |
| 253 EXPECT_FALSE(trial->GetNameGroupId(&name_group_id)); |
| 254 |
| 255 // Create a single winning group. |
| 256 std::string one_winner("One Winner"); |
| 257 uint32 one_winner_id = FieldTrial::HashName(one_winner); |
| 258 trial = new FieldTrial(one_winner, 10, "Default", next_year_, 12, 31); |
| 259 std::string winner("Winner"); |
| 260 uint32 winner_group_id = FieldTrial::HashName(winner); |
| 261 trial->AppendGroup(winner, 10); |
| 262 EXPECT_TRUE(trial->GetNameGroupId(&name_group_id)); |
| 263 EXPECT_EQ(one_winner_id, name_group_id.name); |
| 264 EXPECT_EQ(winner_group_id, name_group_id.group); |
| 265 |
| 266 std::string multi_group("MultiGroup"); |
| 267 uint32 multi_group_id = FieldTrial::HashName(multi_group); |
| 268 scoped_refptr<FieldTrial> multi_group_trial = |
| 269 new FieldTrial(multi_group, 9, "Default", next_year_, 12, 31); |
| 270 |
| 271 multi_group_trial->AppendGroup("Me", 3); |
| 272 multi_group_trial->AppendGroup("You", 3); |
| 273 multi_group_trial->AppendGroup("Them", 3); |
| 274 EXPECT_TRUE(multi_group_trial->GetNameGroupId(&name_group_id)); |
| 275 EXPECT_EQ(multi_group_id, name_group_id.name); |
| 276 uint32 multi_group_winner_id = |
| 277 FieldTrial::HashName(multi_group_trial->group_name()); |
| 278 EXPECT_EQ(multi_group_winner_id, name_group_id.group); |
| 279 |
| 280 // Now check if the list is built properly... |
| 281 std::vector<FieldTrial::NameGroupId> name_group_ids; |
| 282 FieldTrialList::GetFieldTrialNameGroupIds(&name_group_ids); |
| 283 EXPECT_EQ(2U, name_group_ids.size()); |
| 284 for (size_t i = 0; i < name_group_ids.size(); ++i) { |
| 285 // Order is not guaranteed, so check all values. |
| 286 EXPECT_NE(no_group_id, name_group_ids[i].name); |
| 287 EXPECT_TRUE(one_winner_id != name_group_ids[i].name || |
| 288 winner_group_id == name_group_ids[i].group); |
| 289 EXPECT_TRUE(multi_group_id != name_group_ids[i].name || |
| 290 multi_group_winner_id == name_group_ids[i].group); |
| 291 } |
| 292 } |
| 293 |
224 TEST_F(FieldTrialTest, Save) { | 294 TEST_F(FieldTrialTest, Save) { |
225 std::string save_string; | 295 std::string save_string; |
226 | 296 |
227 FieldTrial* trial = | 297 FieldTrial* trial = |
228 new FieldTrial( | 298 new FieldTrial( |
229 "Some name", 10, "Default some name", next_year_, 12, 31); | 299 "Some name", 10, "Default some name", next_year_, 12, 31); |
230 // There is no winner yet, so no textual group name is associated with trial. | 300 // There is no winner yet, so no textual group name is associated with trial. |
231 // In this case, the trial should not be included. | 301 // In this case, the trial should not be included. |
232 EXPECT_EQ("", trial->group_name_internal()); | 302 EXPECT_EQ("", trial->group_name_internal()); |
233 FieldTrialList::StatesToString(&save_string); | 303 FieldTrialList::StatesToString(&save_string); |
(...skipping 192 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
426 TEST_F(FieldTrialTest, DisableAfterInitialization) { | 496 TEST_F(FieldTrialTest, DisableAfterInitialization) { |
427 FieldTrial* trial = | 497 FieldTrial* trial = |
428 new FieldTrial("trial", 100, "default", next_year_, 12, 31); | 498 new FieldTrial("trial", 100, "default", next_year_, 12, 31); |
429 trial->AppendGroup("non_default", 100); | 499 trial->AppendGroup("non_default", 100); |
430 ASSERT_EQ("non_default", trial->group_name()); | 500 ASSERT_EQ("non_default", trial->group_name()); |
431 trial->Disable(); | 501 trial->Disable(); |
432 ASSERT_EQ("default", trial->group_name()); | 502 ASSERT_EQ("default", trial->group_name()); |
433 } | 503 } |
434 | 504 |
435 } // namespace base | 505 } // namespace base |
OLD | NEW |