mfbt/tests/TestRollingMean.cpp

branch
TOR_BUG_9701
changeset 15
b8a032363ba2
equal deleted inserted replaced
-1:000000000000 0:7d45bab1b929
1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
4 * You can obtain one at http://mozilla.org/MPL/2.0/. */
5
6 #include "mozilla/Assertions.h"
7 #include "mozilla/RollingMean.h"
8
9 using mozilla::RollingMean;
10
11 class MyClass
12 {
13 public:
14 uint32_t value;
15
16 MyClass(uint32_t val = 0) : value(val) {
17 }
18
19 bool operator==(const MyClass& other) const {
20 return value == other.value;
21 }
22
23 MyClass operator+(const MyClass& other) const {
24 return MyClass(value + other.value);
25 }
26
27 MyClass operator-(const MyClass& other) const {
28 return MyClass(value - other.value);
29 }
30
31 MyClass operator/(uint32_t div) const {
32 return MyClass(value / div);
33 }
34 };
35
36 class RollingMeanSuite
37 {
38 public:
39 RollingMeanSuite()
40 { }
41
42 void runTests() {
43 testZero();
44 testClear();
45 testRolling();
46 testClass();
47 testMove();
48 }
49
50 private:
51 void testZero() {
52 RollingMean<uint32_t, uint64_t> mean(3);
53 MOZ_RELEASE_ASSERT(mean.empty());
54 }
55
56 void testClear() {
57 RollingMean<uint32_t, uint64_t> mean(3);
58
59 mean.insert(4);
60 MOZ_RELEASE_ASSERT(mean.mean() == 4);
61
62 mean.clear();
63 MOZ_RELEASE_ASSERT(mean.empty());
64
65 mean.insert(3);
66 MOZ_RELEASE_ASSERT(mean.mean() == 3);
67 }
68
69 void testRolling() {
70 RollingMean<uint32_t, uint64_t> mean(3);
71
72 mean.insert(10);
73 MOZ_RELEASE_ASSERT(mean.mean() == 10);
74
75 mean.insert(20);
76 MOZ_RELEASE_ASSERT(mean.mean() == 15);
77
78 mean.insert(35);
79 MOZ_RELEASE_ASSERT(mean.mean() == 21);
80
81 mean.insert(5);
82 MOZ_RELEASE_ASSERT(mean.mean() == 20);
83
84 mean.insert(10);
85 MOZ_RELEASE_ASSERT(mean.mean() == 16);
86 }
87
88 void testClass() {
89 RollingMean<MyClass, MyClass> mean(3);
90
91 mean.insert(MyClass(4));
92 MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4));
93
94 mean.clear();
95 MOZ_RELEASE_ASSERT(mean.empty());
96 }
97
98 void testMove() {
99 RollingMean<uint32_t, uint64_t> mean(3);
100 mean = RollingMean<uint32_t, uint64_t>(4);
101 MOZ_RELEASE_ASSERT(mean.maxValues() == 4);
102
103 mean.insert(10);
104 MOZ_RELEASE_ASSERT(mean.mean() == 10);
105
106 mean = RollingMean<uint32_t, uint64_t>(3);
107 mean.insert(30);
108 mean.insert(40);
109 mean.insert(50);
110 mean.insert(60);
111 MOZ_RELEASE_ASSERT(mean.mean() == 50);
112 }
113
114 };
115
116 int main()
117 {
118 RollingMeanSuite suite;
119 suite.runTests();
120 return 0;
121 }

mercurial