Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "components/contextual_search/browser/ctr_aggregator.h" | |
| 6 | |
| 7 #include <stddef.h> | |
| 8 #include <unordered_map> | |
| 9 | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/logging.h" | |
| 12 #include "components/contextual_search/browser/native_int_storage.h" | |
| 13 #include "testing/gtest/include/gtest/gtest.h" | |
| 14 | |
| 15 namespace { | |
| 16 const int kTestWeek = 2500; | |
| 17 } | |
| 18 | |
| 19 namespace contextual_search { | |
| 20 | |
| 21 class CTRAggregatorTest : public testing::Test { | |
| 22 public: | |
| 23 CTRAggregatorTest() {} | |
| 24 ~CTRAggregatorTest() override {} | |
| 25 | |
| 26 class NativeIntStorageStub : public NativeIntStorage { | |
| 27 int ReadInt(std::string storage_key) override; | |
| 28 void WriteInt(std::string storage_key, int value) override; | |
| 29 | |
| 30 typedef std::unordered_map<std::string, int> hashmap; | |
| 31 hashmap weeks_; | |
| 32 }; | |
| 33 | |
| 34 // Test helpers | |
| 35 void Fill4Weeks(); // Fill 4 weeks with 2 impressions, 1 click. | |
| 36 | |
| 37 // The class under test. | |
| 38 std::unique_ptr<CTRAggregator> aggregator_; | |
| 39 | |
| 40 protected: | |
| 41 // The storage stub. | |
| 42 std::unique_ptr<NativeIntStorage> storage_; | |
| 43 | |
| 44 void SetUp() override { | |
| 45 storage_.reset(new NativeIntStorageStub()); | |
| 46 aggregator_.reset(new CTRAggregator(*storage_.get(), kTestWeek)); | |
| 47 } | |
| 48 | |
| 49 void TearDown() override {} | |
| 50 | |
| 51 private: | |
| 52 DISALLOW_COPY_AND_ASSIGN(CTRAggregatorTest); | |
| 53 }; | |
| 54 | |
| 55 int CTRAggregatorTest::NativeIntStorageStub::ReadInt(std::string storage_key) { | |
| 56 return weeks_[storage_key]; | |
| 57 } | |
| 58 | |
| 59 void CTRAggregatorTest::NativeIntStorageStub::WriteInt(std::string storage_key, | |
| 60 int value) { | |
| 61 weeks_[storage_key] = value; | |
| 62 } | |
| 63 | |
| 64 void CTRAggregatorTest::Fill4Weeks() { | |
| 65 int weeks_to_record = 4; | |
| 66 for (int i = 0; i < weeks_to_record; i++) { | |
| 67 aggregator_->RecordImpression(true); | |
| 68 aggregator_->RecordImpression(false); | |
| 69 EXPECT_FALSE(aggregator_->HasPrevious28DayData()); | |
| 70 aggregator_->IncrementWeek(1); | |
| 71 } | |
| 72 } | |
| 73 | |
| 74 // NaN has the property that it is not equal to itself. | |
| 75 #define EXPECT_NAN(x) EXPECT_NE(x, x) | |
| 76 | |
| 77 TEST_F(CTRAggregatorTest, SimpleOperationTest) { | |
| 78 aggregator_->RecordImpression(true); | |
| 79 aggregator_->RecordImpression(false); | |
| 80 EXPECT_FALSE(aggregator_->HasPreviousWeekData()); | |
| 81 EXPECT_EQ(0, aggregator_->GetPreviousWeekImpressions()); | |
| 82 EXPECT_NAN(aggregator_->GetPreviousWeekCTR()); | |
| 83 | |
| 84 aggregator_->IncrementWeek(1); | |
| 85 EXPECT_TRUE(aggregator_->HasPreviousWeekData()); | |
| 86 EXPECT_EQ(2, aggregator_->GetPreviousWeekImpressions()); | |
| 87 EXPECT_FLOAT_EQ(0.5, aggregator_->GetPreviousWeekCTR()); | |
| 88 } | |
| 89 | |
| 90 TEST_F(CTRAggregatorTest, MultiWeekTest) { | |
| 91 Fill4Weeks(); | |
| 92 aggregator_->RecordImpression(false); | |
| 93 aggregator_->IncrementWeek(1); | |
| 94 EXPECT_TRUE(aggregator_->HasPrevious28DayData()); | |
| 95 EXPECT_FLOAT_EQ(3.0 / 7, aggregator_->GetPrevious28DayCTR()); | |
| 96 aggregator_->RecordImpression(false); | |
| 97 aggregator_->IncrementWeek(1); | |
| 98 EXPECT_TRUE(aggregator_->HasPrevious28DayData()); | |
| 99 EXPECT_FLOAT_EQ(2.0 / 6, aggregator_->GetPrevious28DayCTR()); | |
| 100 } | |
| 101 | |
| 102 TEST_F(CTRAggregatorTest, SkipOneWeekTest) { | |
| 103 Fill4Weeks(); | |
| 104 aggregator_->IncrementWeek(1); | |
| 105 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); | |
| 106 EXPECT_FLOAT_EQ(3.0 / 6, aggregator_->GetPrevious28DayCTR()); | |
| 107 } | |
| 108 | |
| 109 TEST_F(CTRAggregatorTest, SkipThreeWeekTest) { | |
|
Theresa
2016/08/26 23:32:08
Week -> Weeks?
Donn Denman
2016/08/29 22:58:06
Done.
| |
| 110 Fill4Weeks(); | |
| 111 aggregator_->IncrementWeek(3); | |
| 112 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); | |
| 113 EXPECT_FLOAT_EQ(1.0 / 2, aggregator_->GetPrevious28DayCTR()); | |
| 114 } | |
| 115 | |
| 116 TEST_F(CTRAggregatorTest, SkipFourWeekTest) { | |
| 117 Fill4Weeks(); | |
| 118 aggregator_->IncrementWeek(4); | |
| 119 EXPECT_EQ(0, aggregator_->GetPreviousWeekCTR()); | |
| 120 EXPECT_EQ(0, aggregator_->GetPrevious28DayCTR()); | |
| 121 } | |
| 122 | |
| 123 } // namespace contextual_search | |
| OLD | NEW |