mfbt/tests/TestRollingMean.cpp

Thu, 22 Jan 2015 13:21:57 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Thu, 22 Jan 2015 13:21:57 +0100
branch
TOR_BUG_9701
changeset 15
b8a032363ba2
permissions
-rw-r--r--

Incorporate requested changes from Mozilla in review:
https://bugzilla.mozilla.org/show_bug.cgi?id=1123480#c6

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

mercurial