OLD | NEW |
| (Empty) |
1 /* | |
2 * Copyright 2016 The WebRTC project authors. All Rights Reserved. | |
3 * | |
4 * Use of this source code is governed by a BSD-style license | |
5 * that can be found in the LICENSE file in the root of the source | |
6 * tree. An additional intellectual property rights grant can be found | |
7 * in the file PATENTS. All contributing project authors may | |
8 * be found in the AUTHORS file in the root of the source tree. | |
9 */ | |
10 | |
11 #include <algorithm> | |
12 #include <climits> | |
13 | |
14 #include "webrtc/base/constructormagic.h" | |
15 #include "webrtc/base/numerics/percentile_filter.h" | |
16 #include "webrtc/test/gtest.h" | |
17 | |
18 namespace webrtc { | |
19 | |
20 class PercentileFilterTest : public ::testing::TestWithParam<float> { | |
21 public: | |
22 PercentileFilterTest() : filter_(GetParam()) { | |
23 // Make sure the tests are deterministic by seeding with a constant. | |
24 srand(42); | |
25 } | |
26 | |
27 protected: | |
28 PercentileFilter<int64_t> filter_; | |
29 | |
30 private: | |
31 RTC_DISALLOW_COPY_AND_ASSIGN(PercentileFilterTest); | |
32 }; | |
33 | |
34 INSTANTIATE_TEST_CASE_P(PercentileFilterTests, | |
35 PercentileFilterTest, | |
36 ::testing::Values(0.0f, 0.1f, 0.5f, 0.9f, 1.0f)); | |
37 | |
38 TEST(PercentileFilterTest, MinFilter) { | |
39 PercentileFilter<int64_t> filter(0.0f); | |
40 filter.Insert(4); | |
41 EXPECT_EQ(4, filter.GetPercentileValue()); | |
42 filter.Insert(3); | |
43 EXPECT_EQ(3, filter.GetPercentileValue()); | |
44 } | |
45 | |
46 TEST(PercentileFilterTest, MaxFilter) { | |
47 PercentileFilter<int64_t> filter(1.0f); | |
48 filter.Insert(3); | |
49 EXPECT_EQ(3, filter.GetPercentileValue()); | |
50 filter.Insert(4); | |
51 EXPECT_EQ(4, filter.GetPercentileValue()); | |
52 } | |
53 | |
54 TEST(PercentileFilterTest, MedianFilterDouble) { | |
55 PercentileFilter<double> filter(0.5f); | |
56 filter.Insert(2.71828); | |
57 filter.Insert(3.14159); | |
58 filter.Insert(1.41421); | |
59 EXPECT_EQ(2.71828, filter.GetPercentileValue()); | |
60 } | |
61 | |
62 TEST(PercentileFilterTest, MedianFilterInt) { | |
63 PercentileFilter<int> filter(0.5f); | |
64 filter.Insert(INT_MIN); | |
65 filter.Insert(1); | |
66 filter.Insert(2); | |
67 EXPECT_EQ(1, filter.GetPercentileValue()); | |
68 filter.Insert(INT_MAX); | |
69 filter.Erase(INT_MIN); | |
70 EXPECT_EQ(2, filter.GetPercentileValue()); | |
71 } | |
72 | |
73 TEST(PercentileFilterTest, MedianFilterUnsigned) { | |
74 PercentileFilter<unsigned> filter(0.5f); | |
75 filter.Insert(UINT_MAX); | |
76 filter.Insert(2u); | |
77 filter.Insert(1u); | |
78 EXPECT_EQ(2u, filter.GetPercentileValue()); | |
79 filter.Insert(0u); | |
80 filter.Erase(UINT_MAX); | |
81 EXPECT_EQ(1u, filter.GetPercentileValue()); | |
82 } | |
83 | |
84 TEST_P(PercentileFilterTest, EmptyFilter) { | |
85 EXPECT_EQ(0, filter_.GetPercentileValue()); | |
86 filter_.Insert(3); | |
87 bool success = filter_.Erase(3); | |
88 EXPECT_TRUE(success); | |
89 EXPECT_EQ(0, filter_.GetPercentileValue()); | |
90 } | |
91 | |
92 TEST_P(PercentileFilterTest, EraseNonExistingElement) { | |
93 bool success = filter_.Erase(3); | |
94 EXPECT_FALSE(success); | |
95 EXPECT_EQ(0, filter_.GetPercentileValue()); | |
96 filter_.Insert(4); | |
97 success = filter_.Erase(3); | |
98 EXPECT_FALSE(success); | |
99 EXPECT_EQ(4, filter_.GetPercentileValue()); | |
100 } | |
101 | |
102 TEST_P(PercentileFilterTest, DuplicateElements) { | |
103 filter_.Insert(3); | |
104 filter_.Insert(3); | |
105 filter_.Erase(3); | |
106 EXPECT_EQ(3, filter_.GetPercentileValue()); | |
107 } | |
108 | |
109 TEST_P(PercentileFilterTest, InsertAndEraseTenValuesInRandomOrder) { | |
110 int64_t zero_to_nine[10] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9}; | |
111 // The percentile value of the ten values above. | |
112 const int64_t expected_value = static_cast<int64_t>(GetParam() * 9); | |
113 | |
114 // Insert two sets of |zero_to_nine| in random order. | |
115 for (int i = 0; i < 2; ++i) { | |
116 std::random_shuffle(zero_to_nine, zero_to_nine + 10); | |
117 for (int64_t value : zero_to_nine) | |
118 filter_.Insert(value); | |
119 // After inserting a full set of |zero_to_nine|, the percentile should | |
120 // stay constant. | |
121 EXPECT_EQ(expected_value, filter_.GetPercentileValue()); | |
122 } | |
123 | |
124 // Insert and erase sets of |zero_to_nine| in random order a few times. | |
125 for (int i = 0; i < 3; ++i) { | |
126 std::random_shuffle(zero_to_nine, zero_to_nine + 10); | |
127 for (int64_t value : zero_to_nine) | |
128 filter_.Erase(value); | |
129 EXPECT_EQ(expected_value, filter_.GetPercentileValue()); | |
130 | |
131 std::random_shuffle(zero_to_nine, zero_to_nine + 10); | |
132 for (int64_t value : zero_to_nine) | |
133 filter_.Insert(value); | |
134 EXPECT_EQ(expected_value, filter_.GetPercentileValue()); | |
135 } | |
136 } | |
137 | |
138 } // namespace webrtc | |
OLD | NEW |