1 | /* |
2 | * Copyright 2018-present Facebook, Inc. |
3 | * |
4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
5 | * you may not use this file except in compliance with the License. |
6 | * You may obtain a copy of the License at |
7 | * |
8 | * http://www.apache.org/licenses/LICENSE-2.0 |
9 | * |
10 | * Unless required by applicable law or agreed to in writing, software |
11 | * distributed under the License is distributed on an "AS IS" BASIS, |
12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
13 | * See the License for the specific language governing permissions and |
14 | * limitations under the License. |
15 | */ |
16 | |
17 | #include <folly/stats/detail/SlidingWindow-defs.h> |
18 | |
19 | #include <folly/portability/GTest.h> |
20 | |
21 | using namespace folly; |
22 | using namespace folly::detail; |
23 | |
24 | class SlidingWindowTest : public ::testing::Test { |
25 | protected: |
26 | std::unique_ptr<SlidingWindow<size_t>> slidingWindow; |
27 | size_t curWindow = 0; |
28 | |
29 | void SetUp() override { |
30 | slidingWindow = std::make_unique<SlidingWindow<size_t>>( |
31 | [&]() { return curWindow++; }, 60); |
32 | } |
33 | }; |
34 | |
35 | TEST_F(SlidingWindowTest, Constructor) { |
36 | auto buckets = slidingWindow->get(); |
37 | EXPECT_EQ(60, buckets.size()); |
38 | |
39 | for (size_t i = 0; i < 60; ++i) { |
40 | EXPECT_EQ(60 - i - 1, buckets[i]); |
41 | } |
42 | } |
43 | |
44 | TEST_F(SlidingWindowTest, SlideZero) { |
45 | slidingWindow->slide(0); |
46 | auto buckets = slidingWindow->get(); |
47 | EXPECT_EQ(60, buckets.size()); |
48 | |
49 | for (size_t i = 0; i < 60; ++i) { |
50 | EXPECT_EQ(60 - i - 1, buckets[i]); |
51 | } |
52 | } |
53 | |
54 | TEST_F(SlidingWindowTest, SlideLessThanFullAmount) { |
55 | slidingWindow->slide(5); |
56 | auto buckets = slidingWindow->get(); |
57 | EXPECT_EQ(60, buckets.size()); |
58 | |
59 | for (size_t i = 0; i < 60; ++i) { |
60 | EXPECT_EQ(65 - i - 1, buckets[i]); |
61 | } |
62 | } |
63 | |
64 | TEST_F(SlidingWindowTest, SlideMoreThanFullAmount) { |
65 | slidingWindow->slide(60); |
66 | auto buckets = slidingWindow->get(); |
67 | EXPECT_EQ(60, buckets.size()); |
68 | |
69 | for (size_t i = 0; i < 60; ++i) { |
70 | EXPECT_EQ(120 - i - 1, buckets[i]); |
71 | } |
72 | } |
73 | |