Bumping gaia.json for 3 gaia revision(s) a=gaia-bump
[gecko.git] / mfbt / tests / TestRollingMean.cpp
blobbec77786f3c0e8afb4092594036f76f33d392bd7
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
3 /* This Source Code Form is subject to the terms of the Mozilla Public
4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
7 #include "mozilla/Assertions.h"
8 #include "mozilla/RollingMean.h"
10 using mozilla::RollingMean;
12 class MyClass
14 public:
15 uint32_t mValue;
17 explicit MyClass(uint32_t aValue = 0) : mValue(aValue) { }
19 bool operator==(const MyClass& aOther) const
21 return mValue == aOther.mValue;
24 MyClass operator+(const MyClass& aOther) const
26 return MyClass(mValue + aOther.mValue);
29 MyClass operator-(const MyClass& aOther) const
31 return MyClass(mValue - aOther.mValue);
34 MyClass operator/(uint32_t aDiv) const
36 return MyClass(mValue / aDiv);
40 class RollingMeanSuite
42 public:
43 RollingMeanSuite() { }
45 void runTests() {
46 testZero();
47 testClear();
48 testRolling();
49 testClass();
50 testMove();
53 private:
54 void testZero()
56 RollingMean<uint32_t, uint64_t> mean(3);
57 MOZ_RELEASE_ASSERT(mean.empty());
60 void testClear()
62 RollingMean<uint32_t, uint64_t> mean(3);
64 mean.insert(4);
65 MOZ_RELEASE_ASSERT(mean.mean() == 4);
67 mean.clear();
68 MOZ_RELEASE_ASSERT(mean.empty());
70 mean.insert(3);
71 MOZ_RELEASE_ASSERT(mean.mean() == 3);
74 void testRolling()
76 RollingMean<uint32_t, uint64_t> mean(3);
78 mean.insert(10);
79 MOZ_RELEASE_ASSERT(mean.mean() == 10);
81 mean.insert(20);
82 MOZ_RELEASE_ASSERT(mean.mean() == 15);
84 mean.insert(35);
85 MOZ_RELEASE_ASSERT(mean.mean() == 21);
87 mean.insert(5);
88 MOZ_RELEASE_ASSERT(mean.mean() == 20);
90 mean.insert(10);
91 MOZ_RELEASE_ASSERT(mean.mean() == 16);
94 void testClass()
96 RollingMean<MyClass, MyClass> mean(3);
98 mean.insert(MyClass(4));
99 MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4));
101 mean.clear();
102 MOZ_RELEASE_ASSERT(mean.empty());
105 void testMove()
107 RollingMean<uint32_t, uint64_t> mean(3);
108 mean = RollingMean<uint32_t, uint64_t>(4);
109 MOZ_RELEASE_ASSERT(mean.maxValues() == 4);
111 mean.insert(10);
112 MOZ_RELEASE_ASSERT(mean.mean() == 10);
114 mean = RollingMean<uint32_t, uint64_t>(3);
115 mean.insert(30);
116 mean.insert(40);
117 mean.insert(50);
118 mean.insert(60);
119 MOZ_RELEASE_ASSERT(mean.mean() == 50);
124 main()
126 RollingMeanSuite suite;
127 suite.runTests();
128 return 0;