Bug 1637473 [wpt PR 23557] - De-flaky pointerevents/pointerevent_capture_mouse.html...
[gecko.git] / mfbt / tests / TestRollingMean.cpp
blob001d827c4fb307a508d4960dd4c440f7ba9ee402
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 {
13 public:
14 uint32_t mValue;
16 explicit MyClass(uint32_t aValue = 0) : mValue(aValue) {}
18 bool operator==(const MyClass& aOther) const {
19 return mValue == aOther.mValue;
22 MyClass operator+(const MyClass& aOther) const {
23 return MyClass(mValue + aOther.mValue);
26 MyClass operator-(const MyClass& aOther) const {
27 return MyClass(mValue - aOther.mValue);
30 MyClass operator/(uint32_t aDiv) const { return MyClass(mValue / aDiv); }
33 class RollingMeanSuite {
34 public:
35 RollingMeanSuite() = default;
37 void runTests() {
38 testZero();
39 testClear();
40 testRolling();
41 testClass();
42 testMove();
45 private:
46 void testZero() {
47 RollingMean<uint32_t, uint64_t> mean(3);
48 MOZ_RELEASE_ASSERT(mean.empty());
51 void testClear() {
52 RollingMean<uint32_t, uint64_t> mean(3);
54 mean.insert(4);
55 MOZ_RELEASE_ASSERT(mean.mean() == 4);
57 mean.clear();
58 MOZ_RELEASE_ASSERT(mean.empty());
60 mean.insert(3);
61 MOZ_RELEASE_ASSERT(mean.mean() == 3);
64 void testRolling() {
65 RollingMean<uint32_t, uint64_t> mean(3);
67 mean.insert(10);
68 MOZ_RELEASE_ASSERT(mean.mean() == 10);
70 mean.insert(20);
71 MOZ_RELEASE_ASSERT(mean.mean() == 15);
73 mean.insert(35);
74 MOZ_RELEASE_ASSERT(mean.mean() == 21);
76 mean.insert(5);
77 MOZ_RELEASE_ASSERT(mean.mean() == 20);
79 mean.insert(10);
80 MOZ_RELEASE_ASSERT(mean.mean() == 16);
83 void testClass() {
84 RollingMean<MyClass, MyClass> mean(3);
86 mean.insert(MyClass(4));
87 MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4));
89 mean.clear();
90 MOZ_RELEASE_ASSERT(mean.empty());
93 void testMove() {
94 RollingMean<uint32_t, uint64_t> mean(3);
95 mean = RollingMean<uint32_t, uint64_t>(4);
96 MOZ_RELEASE_ASSERT(mean.maxValues() == 4);
98 mean.insert(10);
99 MOZ_RELEASE_ASSERT(mean.mean() == 10);
101 mean = RollingMean<uint32_t, uint64_t>(3);
102 mean.insert(30);
103 mean.insert(40);
104 mean.insert(50);
105 mean.insert(60);
106 MOZ_RELEASE_ASSERT(mean.mean() == 50);
110 int main() {
111 RollingMeanSuite suite;
112 suite.runTests();
113 return 0;