summaryrefslogtreecommitdiffstats
path: root/base/field_trial_unittest.cc
blob: f678b0bd5b91ad2b30b7444630000a4b6fc623d8 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

// Test of FieldTrial class

#include "base/field_trial.h"

#include "base/string_util.h"
#include "testing/gtest/include/gtest/gtest.h"

class FieldTrialTest : public testing::Test {
 public:
  FieldTrialTest() : trial_list_() { }

 private:
  FieldTrialList trial_list_;
};

// Test registration, and also check that destructors are called for trials
// (and that Purify doesn't catch us leaking).
TEST_F(FieldTrialTest, Registration) {
  const char* name1 = "name 1 test";
  const char* name2 = "name 2 test";
  EXPECT_FALSE(FieldTrialList::Find(name1));
  EXPECT_FALSE(FieldTrialList::Find(name2));

  FieldTrial* trial1 = new FieldTrial(name1, 10);
  EXPECT_EQ(trial1->group(), FieldTrial::kNotParticipating);
  EXPECT_EQ(trial1->name(), name1);
  EXPECT_EQ(trial1->group_name(), "");

  trial1->AppendGroup("", 7);

  EXPECT_EQ(trial1, FieldTrialList::Find(name1));
  EXPECT_FALSE(FieldTrialList::Find(name2));

  FieldTrial* trial2 = new FieldTrial(name2, 10);
  EXPECT_EQ(trial2->group(), FieldTrial::kNotParticipating);
  EXPECT_EQ(trial2->name(), name2);
  EXPECT_EQ(trial2->group_name(), "");

  trial2->AppendGroup("a first group", 7);

  EXPECT_EQ(trial1, FieldTrialList::Find(name1));
  EXPECT_EQ(trial2, FieldTrialList::Find(name2));
  // Note: FieldTrialList should delete the objects at shutdown.
}

TEST_F(FieldTrialTest, AbsoluteProbabilities) {
  char always_true[] = " always true";
  char always_false[] = " always false";
  for (int i = 1; i < 250; ++i) {
    // Try lots of names, by changing the first character of the name.
    always_true[0] = i;
    always_false[0] = i;

    FieldTrial* trial_true = new FieldTrial(always_true, 10);
    const std::string winner = "_TheWinner";
    int winner_group = trial_true->AppendGroup(winner, 10);

    EXPECT_EQ(trial_true->group(), winner_group);
    EXPECT_EQ(trial_true->group_name(), winner);

    FieldTrial* trial_false = new FieldTrial(always_false, 10);
    int loser_group = trial_false->AppendGroup("ALoser", 0);

    EXPECT_NE(trial_false->group(), loser_group);
  }
}

TEST_F(FieldTrialTest, MiddleProbabilities) {
  char name[] = " same name";
  bool false_event_seen = false;
  bool true_event_seen = false;
  for (int i = 1; i < 250; ++i) {
    name[0] = i;
    FieldTrial* trial = new FieldTrial(name, 10);
    int might_win = trial->AppendGroup("MightWin", 5);

    if (trial->group() == might_win) {
      true_event_seen = true;
    } else {
      false_event_seen = true;
    }
    if (false_event_seen && true_event_seen)
      return;  // Successful test!!!
  }
  // Very surprising to get here. Probability should be around 1 in 2 ** 250.
  // One of the following will fail.
  EXPECT_TRUE(false_event_seen);
  EXPECT_TRUE(true_event_seen);
}

TEST_F(FieldTrialTest, OneWinner) {
  char name[] = "Some name";
  int group_count(10);

  FieldTrial* trial = new FieldTrial(name, group_count);
  int winner_index(-2);
  std::string winner_name;

  for (int i = 1; i <= group_count; ++i) {
    int might_win = trial->AppendGroup("", 1);

    if (trial->group() == might_win) {
      EXPECT_EQ(winner_index, -2);
      winner_index = might_win;
      StringAppendF(&winner_name, "_%d", might_win);
      EXPECT_EQ(winner_name, trial->group_name());
    }
  }
  EXPECT_GE(winner_index, 0);
  EXPECT_EQ(trial->group(), winner_index);
  EXPECT_EQ(winner_name, trial->group_name());
}

TEST_F(FieldTrialTest, Save) {
  FieldTrial* trial = new FieldTrial("Some name", 10);
  // There is no winner yet, so no textual group name is associated with trial.
  EXPECT_EQ(trial->group_name(), "");
  EXPECT_EQ(trial->MakePersistentString(), "Some name/");

  // Create a winning group.
  trial->AppendGroup("Winner", 10);
  EXPECT_EQ(trial->MakePersistentString(), "Some name/Winner");
}

TEST_F(FieldTrialTest, Restore) {
  FieldTrial* trial = FieldTrial::RestorePersistentString("Some name/winner");
  EXPECT_EQ(trial->group_name(), "winner");
  EXPECT_EQ(trial->name(), "Some name");
}

TEST_F(FieldTrialTest, BogusRestore) {
  const FieldTrial *trial = FieldTrial::RestorePersistentString("MissingSlash");
  EXPECT_EQ(trial, static_cast<FieldTrial *>(NULL));

  trial = FieldTrial::RestorePersistentString("MissingGroupName/");
  EXPECT_EQ(trial, static_cast<FieldTrial *>(NULL));

  trial = FieldTrial::RestorePersistentString("/MissingName");
  EXPECT_EQ(trial, static_cast<FieldTrial *>(NULL));
}

TEST_F(FieldTrialTest, DuplicateRestore) {
  FieldTrial* trial = new FieldTrial("Some name", 10);
  trial->AppendGroup("Winner", 10);
  EXPECT_EQ(trial->MakePersistentString(), "Some name/Winner");

  // It is OK if we redundantly specify a winner.
  EXPECT_EQ(trial, FieldTrial::RestorePersistentString("Some name/Winner"));

  // But it is an error to try to change to a different winner.
  EXPECT_EQ(FieldTrial::RestorePersistentString("Some name/Loser"),
            static_cast<FieldTrial *>(NULL));
}