| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 "components/variations/entropy_provider.h" | 5 #include "components/variations/entropy_provider.h" |
| 6 | 6 |
| 7 #include <stddef.h> | 7 #include <stddef.h> |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <cmath> | 10 #include <cmath> |
| (...skipping 252 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 263 for (int j = 0; j < 100; ++j) | 263 for (int j = 0; j < 100; ++j) |
| 264 trials[i]->AppendGroup(std::string(), 1); | 264 trials[i]->AppendGroup(std::string(), 1); |
| 265 } | 265 } |
| 266 | 266 |
| 267 // Normally, these trials should produce different groups, but if the same | 267 // Normally, these trials should produce different groups, but if the same |
| 268 // custom seed is used, they should produce the same group assignment. | 268 // custom seed is used, they should produce the same group assignment. |
| 269 EXPECT_EQ(trials[0]->group(), trials[1]->group()); | 269 EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 270 EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); | 270 EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 271 } | 271 } |
| 272 | 272 |
| 273 TEST(EntropyProviderTest, UseOneTimeRandomizationWithCustomSeedSHA1) { |
| 274 // Ensures that two trials with different names but the same custom seed used |
| 275 // for one time randomization produce the same group assignments. |
| 276 base::FieldTrialList field_trial_list( |
| 277 base::MakeUnique<SHA1EntropyProvider>("client_id")); |
| 278 const int kNoExpirationYear = base::FieldTrialList::kNoExpirationYear; |
| 279 const uint32_t kCustomSeed = 9001; |
| 280 scoped_refptr<base::FieldTrial> trials[] = { |
| 281 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 282 "one", 100, "default", kNoExpirationYear, 1, 1, |
| 283 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), |
| 284 base::FieldTrialList::FactoryGetFieldTrialWithRandomizationSeed( |
| 285 "two", 100, "default", kNoExpirationYear, 1, 1, |
| 286 base::FieldTrial::ONE_TIME_RANDOMIZED, kCustomSeed, NULL, NULL), |
| 287 }; |
| 288 |
| 289 for (size_t i = 0; i < arraysize(trials); ++i) { |
| 290 for (int j = 0; j < 100; ++j) |
| 291 trials[i]->AppendGroup(std::string(), 1); |
| 292 } |
| 293 |
| 294 // Normally, these trials should produce different groups, but if the same |
| 295 // custom seed is used, they should produce the same group assignment. |
| 296 EXPECT_EQ(trials[0]->group(), trials[1]->group()); |
| 297 EXPECT_EQ(trials[0]->group_name(), trials[1]->group_name()); |
| 298 } |
| 299 |
| 273 TEST(EntropyProviderTest, SHA1Entropy) { | 300 TEST(EntropyProviderTest, SHA1Entropy) { |
| 274 const double results[] = { GenerateSHA1Entropy("hi", "1"), | 301 const double results[] = { GenerateSHA1Entropy("hi", "1"), |
| 275 GenerateSHA1Entropy("there", "1") }; | 302 GenerateSHA1Entropy("there", "1") }; |
| 276 | 303 |
| 277 EXPECT_NE(results[0], results[1]); | 304 EXPECT_NE(results[0], results[1]); |
| 278 for (size_t i = 0; i < arraysize(results); ++i) { | 305 for (size_t i = 0; i < arraysize(results); ++i) { |
| 279 EXPECT_LE(0.0, results[i]); | 306 EXPECT_LE(0.0, results[i]); |
| 280 EXPECT_GT(1.0, results[i]); | 307 EXPECT_GT(1.0, results[i]); |
| 281 } | 308 } |
| 282 | 309 |
| (...skipping 80 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 363 ++count; | 390 ++count; |
| 364 } | 391 } |
| 365 | 392 |
| 366 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << | 393 ASSERT_LT(count, kMaxAttempts) << "Expected average was " << |
| 367 kExpectedAverage << ", average ended at " << cumulative_average << | 394 kExpectedAverage << ", average ended at " << cumulative_average << |
| 368 ", for trial " << kTestTrialNames[i]; | 395 ", for trial " << kTestTrialNames[i]; |
| 369 } | 396 } |
| 370 } | 397 } |
| 371 | 398 |
| 372 } // namespace metrics | 399 } // namespace metrics |
| OLD | NEW |