| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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/dom_distiller/core/distillable_page_detector.h" | 5 #include "components/dom_distiller/core/distillable_page_detector.h" |
| 6 | 6 |
| 7 #include "base/memory/ptr_util.h" |
| 7 #include "testing/gtest/include/gtest/gtest.h" | 8 #include "testing/gtest/include/gtest/gtest.h" |
| 8 | 9 |
| 9 namespace dom_distiller { | 10 namespace dom_distiller { |
| 10 namespace { | 11 namespace { |
| 11 | 12 |
| 12 class Builder { | 13 class Builder { |
| 13 public: | 14 public: |
| 14 Builder() {} | 15 Builder() {} |
| 15 | 16 |
| 16 Builder& Stump(int feature_number, double split, double weight) { | 17 Builder& Stump(int feature_number, double split, double weight) { |
| 17 StumpProto* stump = proto_.add_stump(); | 18 StumpProto* stump = proto_.add_stump(); |
| 18 stump->set_feature_number(feature_number); | 19 stump->set_feature_number(feature_number); |
| 19 stump->set_split(split); | 20 stump->set_split(split); |
| 20 stump->set_weight(weight); | 21 stump->set_weight(weight); |
| 21 return *this; | 22 return *this; |
| 22 } | 23 } |
| 23 | 24 |
| 24 scoped_ptr<DistillablePageDetector> Build() { | 25 std::unique_ptr<DistillablePageDetector> Build() { |
| 25 int num_features = 0; | 26 int num_features = 0; |
| 26 for (int i = 0; i < proto_.stump_size(); ++i) { | 27 for (int i = 0; i < proto_.stump_size(); ++i) { |
| 27 num_features = | 28 num_features = |
| 28 std::max(num_features, proto_.stump(i).feature_number() + 1); | 29 std::max(num_features, proto_.stump(i).feature_number() + 1); |
| 29 } | 30 } |
| 30 proto_.set_num_features(num_features); | 31 proto_.set_num_features(num_features); |
| 31 proto_.set_num_stumps(proto_.stump_size()); | 32 proto_.set_num_stumps(proto_.stump_size()); |
| 32 return make_scoped_ptr(new DistillablePageDetector( | 33 return base::WrapUnique(new DistillablePageDetector( |
| 33 make_scoped_ptr(new AdaBoostProto(proto_)))); | 34 base::WrapUnique(new AdaBoostProto(proto_)))); |
| 34 } | 35 } |
| 35 | 36 |
| 36 private: | 37 private: |
| 37 AdaBoostProto proto_; | 38 AdaBoostProto proto_; |
| 38 }; | 39 }; |
| 39 | 40 |
| 40 } | 41 } |
| 41 | 42 |
| 42 TEST(DomDistillerDistillablePageDetectorTest, TestCalculateThreshold) { | 43 TEST(DomDistillerDistillablePageDetectorTest, TestCalculateThreshold) { |
| 43 scoped_ptr<DistillablePageDetector> detector = | 44 std::unique_ptr<DistillablePageDetector> detector = |
| 44 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); | 45 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); |
| 45 | 46 |
| 46 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); | 47 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); |
| 47 | 48 |
| 48 detector = Builder() | 49 detector = Builder() |
| 49 .Stump(0, 1.0, -1.0) | 50 .Stump(0, 1.0, -1.0) |
| 50 .Stump(0, 1.4, 2.0) | 51 .Stump(0, 1.4, 2.0) |
| 51 .Build(); | 52 .Build(); |
| 52 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold()); | 53 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold()); |
| 53 | 54 |
| 54 detector = Builder() | 55 detector = Builder() |
| 55 .Stump(0, 1.0, 1.0) | 56 .Stump(0, 1.0, 1.0) |
| 56 .Stump(0, 1.4, 2.0) | 57 .Stump(0, 1.4, 2.0) |
| 57 .Stump(1, 0.0, 1.0) | 58 .Stump(1, 0.0, 1.0) |
| 58 .Stump(2, 1.0, -3.0) | 59 .Stump(2, 1.0, -3.0) |
| 59 .Build(); | 60 .Build(); |
| 60 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold()); | 61 EXPECT_DOUBLE_EQ(0.5, detector->GetThreshold()); |
| 61 } | 62 } |
| 62 | 63 |
| 63 TEST(DomDistillerDistillablePageDetectorTest, TestScoreAndClassify) { | 64 TEST(DomDistillerDistillablePageDetectorTest, TestScoreAndClassify) { |
| 64 scoped_ptr<DistillablePageDetector> detector = | 65 std::unique_ptr<DistillablePageDetector> detector = |
| 65 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); | 66 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); |
| 66 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); | 67 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); |
| 67 | 68 |
| 68 std::vector<double> features; | 69 std::vector<double> features; |
| 69 features.push_back(2.0); | 70 features.push_back(2.0); |
| 70 EXPECT_DOUBLE_EQ(3.0, detector->Score(features)); | 71 EXPECT_DOUBLE_EQ(3.0, detector->Score(features)); |
| 71 EXPECT_TRUE(detector->Classify(features)); | 72 EXPECT_TRUE(detector->Classify(features)); |
| 72 | 73 |
| 73 features[0] = 1.1; | 74 features[0] = 1.1; |
| 74 EXPECT_DOUBLE_EQ(1.0, detector->Score(features)); | 75 EXPECT_DOUBLE_EQ(1.0, detector->Score(features)); |
| (...skipping 10 matching lines...) Expand all Loading... |
| 85 features.push_back(1.0); | 86 features.push_back(1.0); |
| 86 features.push_back(3.0); | 87 features.push_back(3.0); |
| 87 EXPECT_DOUBLE_EQ(-2.0, detector->Score(features)); | 88 EXPECT_DOUBLE_EQ(-2.0, detector->Score(features)); |
| 88 EXPECT_FALSE(detector->Classify(features)); | 89 EXPECT_FALSE(detector->Classify(features)); |
| 89 features[2] = 0.0; | 90 features[2] = 0.0; |
| 90 EXPECT_DOUBLE_EQ(1.0, detector->Score(features)); | 91 EXPECT_DOUBLE_EQ(1.0, detector->Score(features)); |
| 91 EXPECT_TRUE(detector->Classify(features)); | 92 EXPECT_TRUE(detector->Classify(features)); |
| 92 } | 93 } |
| 93 | 94 |
| 94 TEST(DomDistillerDistillablePageDetectorTest, TestScoreWrongNumberFeatures) { | 95 TEST(DomDistillerDistillablePageDetectorTest, TestScoreWrongNumberFeatures) { |
| 95 scoped_ptr<DistillablePageDetector> detector = | 96 std::unique_ptr<DistillablePageDetector> detector = |
| 96 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); | 97 Builder().Stump(0, 1.0, 1.0).Stump(0, 1.4, 2.0).Build(); |
| 97 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); | 98 EXPECT_DOUBLE_EQ(1.5, detector->GetThreshold()); |
| 98 | 99 |
| 99 std::vector<double> features; | 100 std::vector<double> features; |
| 100 EXPECT_DOUBLE_EQ(0.0, detector->Score(features)); | 101 EXPECT_DOUBLE_EQ(0.0, detector->Score(features)); |
| 101 features.push_back(-3.0); | 102 features.push_back(-3.0); |
| 102 features.push_back(1.0); | 103 features.push_back(1.0); |
| 103 EXPECT_DOUBLE_EQ(0.0, detector->Score(features)); | 104 EXPECT_DOUBLE_EQ(0.0, detector->Score(features)); |
| 104 } | 105 } |
| 105 | 106 |
| 106 | 107 |
| 107 } | 108 } |
| 108 | 109 |
| OLD | NEW |