1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mfbt/tests/TestRollingMean.cpp Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,121 @@ 1.4 +/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 1.5 +/* This Source Code Form is subject to the terms of the Mozilla Public 1.6 + * License, v. 2.0. If a copy of the MPL was not distributed with this file, 1.7 + * You can obtain one at http://mozilla.org/MPL/2.0/. */ 1.8 + 1.9 +#include "mozilla/Assertions.h" 1.10 +#include "mozilla/RollingMean.h" 1.11 + 1.12 +using mozilla::RollingMean; 1.13 + 1.14 +class MyClass 1.15 +{ 1.16 + public: 1.17 + uint32_t value; 1.18 + 1.19 + MyClass(uint32_t val = 0) : value(val) { 1.20 + } 1.21 + 1.22 + bool operator==(const MyClass& other) const { 1.23 + return value == other.value; 1.24 + } 1.25 + 1.26 + MyClass operator+(const MyClass& other) const { 1.27 + return MyClass(value + other.value); 1.28 + } 1.29 + 1.30 + MyClass operator-(const MyClass& other) const { 1.31 + return MyClass(value - other.value); 1.32 + } 1.33 + 1.34 + MyClass operator/(uint32_t div) const { 1.35 + return MyClass(value / div); 1.36 + } 1.37 +}; 1.38 + 1.39 +class RollingMeanSuite 1.40 +{ 1.41 + public: 1.42 + RollingMeanSuite() 1.43 + { } 1.44 + 1.45 + void runTests() { 1.46 + testZero(); 1.47 + testClear(); 1.48 + testRolling(); 1.49 + testClass(); 1.50 + testMove(); 1.51 + } 1.52 + 1.53 + private: 1.54 + void testZero() { 1.55 + RollingMean<uint32_t, uint64_t> mean(3); 1.56 + MOZ_RELEASE_ASSERT(mean.empty()); 1.57 + } 1.58 + 1.59 + void testClear() { 1.60 + RollingMean<uint32_t, uint64_t> mean(3); 1.61 + 1.62 + mean.insert(4); 1.63 + MOZ_RELEASE_ASSERT(mean.mean() == 4); 1.64 + 1.65 + mean.clear(); 1.66 + MOZ_RELEASE_ASSERT(mean.empty()); 1.67 + 1.68 + mean.insert(3); 1.69 + MOZ_RELEASE_ASSERT(mean.mean() == 3); 1.70 + } 1.71 + 1.72 + void testRolling() { 1.73 + RollingMean<uint32_t, uint64_t> mean(3); 1.74 + 1.75 + mean.insert(10); 1.76 + MOZ_RELEASE_ASSERT(mean.mean() == 10); 1.77 + 1.78 + mean.insert(20); 1.79 + MOZ_RELEASE_ASSERT(mean.mean() == 15); 1.80 + 1.81 + mean.insert(35); 1.82 + MOZ_RELEASE_ASSERT(mean.mean() == 21); 1.83 + 1.84 + mean.insert(5); 1.85 + MOZ_RELEASE_ASSERT(mean.mean() == 20); 1.86 + 1.87 + mean.insert(10); 1.88 + MOZ_RELEASE_ASSERT(mean.mean() == 16); 1.89 + } 1.90 + 1.91 + void testClass() { 1.92 + RollingMean<MyClass, MyClass> mean(3); 1.93 + 1.94 + mean.insert(MyClass(4)); 1.95 + MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4)); 1.96 + 1.97 + mean.clear(); 1.98 + MOZ_RELEASE_ASSERT(mean.empty()); 1.99 + } 1.100 + 1.101 + void testMove() { 1.102 + RollingMean<uint32_t, uint64_t> mean(3); 1.103 + mean = RollingMean<uint32_t, uint64_t>(4); 1.104 + MOZ_RELEASE_ASSERT(mean.maxValues() == 4); 1.105 + 1.106 + mean.insert(10); 1.107 + MOZ_RELEASE_ASSERT(mean.mean() == 10); 1.108 + 1.109 + mean = RollingMean<uint32_t, uint64_t>(3); 1.110 + mean.insert(30); 1.111 + mean.insert(40); 1.112 + mean.insert(50); 1.113 + mean.insert(60); 1.114 + MOZ_RELEASE_ASSERT(mean.mean() == 50); 1.115 + } 1.116 + 1.117 +}; 1.118 + 1.119 +int main() 1.120 +{ 1.121 + RollingMeanSuite suite; 1.122 + suite.runTests(); 1.123 + return 0; 1.124 +}