| 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/. */ |
| 6 |
|
| 7 |
#include "mozilla/Assertions.h" |
| 8 |
#include "mozilla/RollingMean.h" |
| 9 |
|
| 10 |
using mozilla::RollingMean; |
| 11 |
|
| 12 |
class MyClass { |
| 13 |
public: |
| 14 |
uint32_t mValue; |
| 15 |
|
| 16 |
explicit MyClass(uint32_t aValue = 0) : mValue(aValue) {} |
| 17 |
|
| 18 |
bool operator==(const MyClass& aOther) const { |
| 19 |
return mValue == aOther.mValue; |
| 20 |
} |
| 21 |
|
| 22 |
MyClass operator+(const MyClass& aOther) const { |
| 23 |
return MyClass(mValue + aOther.mValue); |
| 24 |
} |
| 25 |
|
| 26 |
MyClass operator-(const MyClass& aOther) const { |
| 27 |
return MyClass(mValue - aOther.mValue); |
| 28 |
} |
| 29 |
|
| 30 |
MyClass operator/(uint32_t aDiv) const { return MyClass(mValue / aDiv); } |
| 31 |
}; |
| 32 |
|
| 33 |
class RollingMeanSuite { |
| 34 |
public: |
| 35 |
RollingMeanSuite() = default; |
| 36 |
|
| 37 |
void runTests() { |
| 38 |
testZero(); |
| 39 |
testClear(); |
| 40 |
testRolling(); |
| 41 |
testClass(); |
| 42 |
testMove(); |
| 43 |
} |
| 44 |
|
| 45 |
private: |
| 46 |
void testZero() { |
| 47 |
RollingMean<uint32_t, uint64_t> mean(3); |
| 48 |
MOZ_RELEASE_ASSERT(mean.empty()); |
| 49 |
} |
| 50 |
|
| 51 |
void testClear() { |
| 52 |
RollingMean<uint32_t, uint64_t> mean(3); |
| 53 |
|
| 54 |
mean.insert(4); |
| 55 |
MOZ_RELEASE_ASSERT(mean.mean() == 4); |
| 56 |
|
| 57 |
mean.clear(); |
| 58 |
MOZ_RELEASE_ASSERT(mean.empty()); |
| 59 |
|
| 60 |
mean.insert(3); |
| 61 |
MOZ_RELEASE_ASSERT(mean.mean() == 3); |
| 62 |
} |
| 63 |
|
| 64 |
void testRolling() { |
| 65 |
RollingMean<uint32_t, uint64_t> mean(3); |
| 66 |
|
| 67 |
mean.insert(10); |
| 68 |
MOZ_RELEASE_ASSERT(mean.mean() == 10); |
| 69 |
|
| 70 |
mean.insert(20); |
| 71 |
MOZ_RELEASE_ASSERT(mean.mean() == 15); |
| 72 |
|
| 73 |
mean.insert(35); |
| 74 |
MOZ_RELEASE_ASSERT(mean.mean() == 21); |
| 75 |
|
| 76 |
mean.insert(5); |
| 77 |
MOZ_RELEASE_ASSERT(mean.mean() == 20); |
| 78 |
|
| 79 |
mean.insert(10); |
| 80 |
MOZ_RELEASE_ASSERT(mean.mean() == 16); |
| 81 |
} |
| 82 |
|
| 83 |
void testClass() { |
| 84 |
RollingMean<MyClass, MyClass> mean(3); |
| 85 |
|
| 86 |
mean.insert(MyClass(4)); |
| 87 |
MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4)); |
| 88 |
|
| 89 |
mean.clear(); |
| 90 |
MOZ_RELEASE_ASSERT(mean.empty()); |
| 91 |
} |
| 92 |
|
| 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); |
| 97 |
|
| 98 |
mean.insert(10); |
| 99 |
MOZ_RELEASE_ASSERT(mean.mean() == 10); |
| 100 |
|
| 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); |
| 107 |
} |
| 108 |
}; |
| 109 |
|
| 110 |
int main() { |
| 111 |
RollingMeanSuite suite; |
| 112 |
suite.runTests(); |
| 113 |
return 0; |
| 114 |
} |