Wed, 31 Dec 2014 06:09:35 +0100
Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.
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/. */
6 #include "mozilla/Assertions.h"
7 #include "mozilla/RollingMean.h"
9 using mozilla::RollingMean;
11 class MyClass
12 {
13 public:
14 uint32_t value;
16 MyClass(uint32_t val = 0) : value(val) {
17 }
19 bool operator==(const MyClass& other) const {
20 return value == other.value;
21 }
23 MyClass operator+(const MyClass& other) const {
24 return MyClass(value + other.value);
25 }
27 MyClass operator-(const MyClass& other) const {
28 return MyClass(value - other.value);
29 }
31 MyClass operator/(uint32_t div) const {
32 return MyClass(value / div);
33 }
34 };
36 class RollingMeanSuite
37 {
38 public:
39 RollingMeanSuite()
40 { }
42 void runTests() {
43 testZero();
44 testClear();
45 testRolling();
46 testClass();
47 testMove();
48 }
50 private:
51 void testZero() {
52 RollingMean<uint32_t, uint64_t> mean(3);
53 MOZ_RELEASE_ASSERT(mean.empty());
54 }
56 void testClear() {
57 RollingMean<uint32_t, uint64_t> mean(3);
59 mean.insert(4);
60 MOZ_RELEASE_ASSERT(mean.mean() == 4);
62 mean.clear();
63 MOZ_RELEASE_ASSERT(mean.empty());
65 mean.insert(3);
66 MOZ_RELEASE_ASSERT(mean.mean() == 3);
67 }
69 void testRolling() {
70 RollingMean<uint32_t, uint64_t> mean(3);
72 mean.insert(10);
73 MOZ_RELEASE_ASSERT(mean.mean() == 10);
75 mean.insert(20);
76 MOZ_RELEASE_ASSERT(mean.mean() == 15);
78 mean.insert(35);
79 MOZ_RELEASE_ASSERT(mean.mean() == 21);
81 mean.insert(5);
82 MOZ_RELEASE_ASSERT(mean.mean() == 20);
84 mean.insert(10);
85 MOZ_RELEASE_ASSERT(mean.mean() == 16);
86 }
88 void testClass() {
89 RollingMean<MyClass, MyClass> mean(3);
91 mean.insert(MyClass(4));
92 MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4));
94 mean.clear();
95 MOZ_RELEASE_ASSERT(mean.empty());
96 }
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);
103 mean.insert(10);
104 MOZ_RELEASE_ASSERT(mean.mean() == 10);
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 }
114 };
116 int main()
117 {
118 RollingMeanSuite suite;
119 suite.runTests();
120 return 0;
121 }