michael@0: /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ michael@0: /* This Source Code Form is subject to the terms of the Mozilla Public michael@0: * License, v. 2.0. If a copy of the MPL was not distributed with this file, michael@0: * You can obtain one at http://mozilla.org/MPL/2.0/. */ michael@0: michael@0: #include "mozilla/Assertions.h" michael@0: #include "mozilla/RollingMean.h" michael@0: michael@0: using mozilla::RollingMean; michael@0: michael@0: class MyClass michael@0: { michael@0: public: michael@0: uint32_t value; michael@0: michael@0: MyClass(uint32_t val = 0) : value(val) { michael@0: } michael@0: michael@0: bool operator==(const MyClass& other) const { michael@0: return value == other.value; michael@0: } michael@0: michael@0: MyClass operator+(const MyClass& other) const { michael@0: return MyClass(value + other.value); michael@0: } michael@0: michael@0: MyClass operator-(const MyClass& other) const { michael@0: return MyClass(value - other.value); michael@0: } michael@0: michael@0: MyClass operator/(uint32_t div) const { michael@0: return MyClass(value / div); michael@0: } michael@0: }; michael@0: michael@0: class RollingMeanSuite michael@0: { michael@0: public: michael@0: RollingMeanSuite() michael@0: { } michael@0: michael@0: void runTests() { michael@0: testZero(); michael@0: testClear(); michael@0: testRolling(); michael@0: testClass(); michael@0: testMove(); michael@0: } michael@0: michael@0: private: michael@0: void testZero() { michael@0: RollingMean mean(3); michael@0: MOZ_RELEASE_ASSERT(mean.empty()); michael@0: } michael@0: michael@0: void testClear() { michael@0: RollingMean mean(3); michael@0: michael@0: mean.insert(4); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 4); michael@0: michael@0: mean.clear(); michael@0: MOZ_RELEASE_ASSERT(mean.empty()); michael@0: michael@0: mean.insert(3); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 3); michael@0: } michael@0: michael@0: void testRolling() { michael@0: RollingMean mean(3); michael@0: michael@0: mean.insert(10); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 10); michael@0: michael@0: mean.insert(20); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 15); michael@0: michael@0: mean.insert(35); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 21); michael@0: michael@0: mean.insert(5); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 20); michael@0: michael@0: mean.insert(10); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 16); michael@0: } michael@0: michael@0: void testClass() { michael@0: RollingMean mean(3); michael@0: michael@0: mean.insert(MyClass(4)); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == MyClass(4)); michael@0: michael@0: mean.clear(); michael@0: MOZ_RELEASE_ASSERT(mean.empty()); michael@0: } michael@0: michael@0: void testMove() { michael@0: RollingMean mean(3); michael@0: mean = RollingMean(4); michael@0: MOZ_RELEASE_ASSERT(mean.maxValues() == 4); michael@0: michael@0: mean.insert(10); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 10); michael@0: michael@0: mean = RollingMean(3); michael@0: mean.insert(30); michael@0: mean.insert(40); michael@0: mean.insert(50); michael@0: mean.insert(60); michael@0: MOZ_RELEASE_ASSERT(mean.mean() == 50); michael@0: } michael@0: michael@0: }; michael@0: michael@0: int main() michael@0: { michael@0: RollingMeanSuite suite; michael@0: suite.runTests(); michael@0: return 0; michael@0: }