| OLD | NEW |
| 1 // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2006-2008 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/field_trial.h" | 7 #include "base/field_trial.h" |
| 8 | 8 |
| 9 #include "base/logging.h" | 9 #include "base/logging.h" |
| 10 #include "base/string_util.h" |
| 10 #include "testing/gtest/include/gtest/gtest.h" | 11 #include "testing/gtest/include/gtest/gtest.h" |
| 11 | 12 |
| 12 class FieldTrialTest : public testing::Test { | 13 class FieldTrialTest : public testing::Test { |
| 13 public: | 14 public: |
| 14 FieldTrialTest() : trial_list_() { } | 15 FieldTrialTest() : trial_list_() { } |
| 15 | 16 |
| 16 private: | 17 private: |
| 17 FieldTrialList trial_list_; | 18 FieldTrialList trial_list_; |
| 18 }; | 19 }; |
| 19 | 20 |
| 20 // Test registration, and also check that destructors are called for trials | 21 // Test registration, and also check that destructors are called for trials |
| 21 // (and that Purify doesn't catch us leaking). | 22 // (and that Purify doesn't catch us leaking). |
| 22 TEST_F(FieldTrialTest, Registration) { | 23 TEST_F(FieldTrialTest, Registration) { |
| 23 const wchar_t* name1 = L"name 1 test"; | 24 const char* name1 = "name 1 test"; |
| 24 const wchar_t* name2 = L"name 2 test"; | 25 const char* name2 = "name 2 test"; |
| 25 EXPECT_FALSE(FieldTrialList::Find(name1)); | 26 EXPECT_FALSE(FieldTrialList::Find(name1)); |
| 26 EXPECT_FALSE(FieldTrialList::Find(name2)); | 27 EXPECT_FALSE(FieldTrialList::Find(name2)); |
| 27 | 28 |
| 28 FieldTrial* trial1 = new FieldTrial(name1, 0.7); | 29 FieldTrial* trial1 = new FieldTrial(name1, 10); |
| 30 EXPECT_EQ(trial1->group(), FieldTrial::kNotParticipating); |
| 31 EXPECT_EQ(trial1->name(), name1); |
| 32 EXPECT_EQ(trial1->group_name(), ""); |
| 33 |
| 34 trial1->AppendGroup("", 7); |
| 29 | 35 |
| 30 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); | 36 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); |
| 31 EXPECT_FALSE(FieldTrialList::Find(name2)); | 37 EXPECT_FALSE(FieldTrialList::Find(name2)); |
| 32 | 38 |
| 33 FieldTrial* trial2 = new FieldTrial(name2, 0.7); | 39 FieldTrial* trial2 = new FieldTrial(name2, 10); |
| 40 EXPECT_EQ(trial2->group(), FieldTrial::kNotParticipating); |
| 41 EXPECT_EQ(trial2->name(), name2); |
| 42 EXPECT_EQ(trial2->group_name(), ""); |
| 43 |
| 44 trial2->AppendGroup("a first group", 7); |
| 34 | 45 |
| 35 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); | 46 EXPECT_EQ(trial1, FieldTrialList::Find(name1)); |
| 36 EXPECT_EQ(trial2, FieldTrialList::Find(name2)); | 47 EXPECT_EQ(trial2, FieldTrialList::Find(name2)); |
| 37 // Note: FieldTrialList should delete the objects at shutdown. | 48 // Note: FieldTrialList should delete the objects at shutdown. |
| 38 } | 49 } |
| 39 | 50 |
| 40 TEST_F(FieldTrialTest, AbsoluteProbabilities) { | 51 TEST_F(FieldTrialTest, AbsoluteProbabilities) { |
| 41 wchar_t always_true[] = L" always true"; | 52 char always_true[] = " always true"; |
| 42 wchar_t always_false[] = L" always false"; | 53 char always_false[] = " always false"; |
| 43 for (int i = 1; i < 250; ++i) { | 54 for (int i = 1; i < 250; ++i) { |
| 44 // Try lots of names, by changing the first character of the name. | 55 // Try lots of names, by changing the first character of the name. |
| 45 always_true[0] = i; | 56 always_true[0] = i; |
| 46 always_false[0] = i; | 57 always_false[0] = i; |
| 47 FieldTrial* trial_true = new FieldTrial(always_true, 1.0); | 58 |
| 48 EXPECT_TRUE(trial_true->boolean_value()); | 59 FieldTrial* trial_true = new FieldTrial(always_true, 10); |
| 49 FieldTrial* trial_false = new FieldTrial(always_false, 0.0); | 60 const std::string winner = "_TheWinner"; |
| 50 EXPECT_FALSE(trial_false->boolean_value()); | 61 int winner_group = trial_true->AppendGroup(winner, 10); |
| 62 |
| 63 EXPECT_EQ(trial_true->group(), winner_group); |
| 64 EXPECT_EQ(trial_true->group_name(), winner); |
| 65 |
| 66 FieldTrial* trial_false = new FieldTrial(always_false, 10); |
| 67 int loser_group = trial_false->AppendGroup("ALoser", 0); |
| 68 |
| 69 EXPECT_NE(trial_false->group(), loser_group); |
| 51 } | 70 } |
| 52 } | 71 } |
| 53 | 72 |
| 54 TEST_F(FieldTrialTest, MiddleProbabalities) { | 73 TEST_F(FieldTrialTest, MiddleProbabilities) { |
| 55 wchar_t name[] = L" same name"; | 74 char name[] = " same name"; |
| 56 bool false_event_seen = false; | 75 bool false_event_seen = false; |
| 57 bool true_event_seen = false; | 76 bool true_event_seen = false; |
| 58 for (int i = 1; i < 250; ++i) { | 77 for (int i = 1; i < 250; ++i) { |
| 59 name[0] = i; | 78 name[0] = i; |
| 60 FieldTrial* trial = new FieldTrial(name, 0.5); | 79 FieldTrial* trial = new FieldTrial(name, 10); |
| 61 if (trial->boolean_value()) { | 80 int might_win = trial->AppendGroup("MightWin", 5); |
| 81 |
| 82 if (trial->group() == might_win) { |
| 62 true_event_seen = true; | 83 true_event_seen = true; |
| 63 } else { | 84 } else { |
| 64 false_event_seen = true; | 85 false_event_seen = true; |
| 65 } | 86 } |
| 66 if (false_event_seen && true_event_seen) | 87 if (false_event_seen && true_event_seen) |
| 67 return; // Successful test!!! | 88 return; // Successful test!!! |
| 68 } | 89 } |
| 69 // Very surprising to get here. Probability should be around 1 in 2 ** 250. | 90 // Very surprising to get here. Probability should be around 1 in 2 ** 250. |
| 70 // One of the following will fail. | 91 // One of the following will fail. |
| 71 EXPECT_TRUE(false_event_seen); | 92 EXPECT_TRUE(false_event_seen); |
| 72 EXPECT_TRUE(true_event_seen); | 93 EXPECT_TRUE(true_event_seen); |
| 73 } | 94 } |
| 95 |
| 96 TEST_F(FieldTrialTest, OneWinner) { |
| 97 char name[] = "Some name"; |
| 98 int group_count(10); |
| 99 |
| 100 FieldTrial* trial = new FieldTrial(name, group_count); |
| 101 int winner_index(-2); |
| 102 std::string winner_name; |
| 103 |
| 104 for (int i = 1; i <= group_count; ++i) { |
| 105 int might_win = trial->AppendGroup("", 1); |
| 106 |
| 107 if (trial->group() == might_win) { |
| 108 EXPECT_EQ(winner_index, -2); |
| 109 winner_index = might_win; |
| 110 StringAppendF(&winner_name, "_%d", might_win); |
| 111 EXPECT_EQ(winner_name, trial->group_name()); |
| 112 } |
| 113 } |
| 114 EXPECT_GE(winner_index, 0); |
| 115 EXPECT_EQ(trial->group(), winner_index); |
| 116 EXPECT_EQ(winner_name, trial->group_name()); |
| 117 } |
| 118 |
| OLD | NEW |