From 124bd88b01d3264df7d13f96eef6cfa85539d280 Mon Sep 17 00:00:00 2001 From: wangyix Date: Wed, 4 Jun 2014 17:23:44 -0700 Subject: [PATCH] added tests for MovingPercentile --- tests/shared/CMakeLists.txt | 38 +++++ tests/shared/src/MovingPercentileTests.cpp | 169 +++++++++++++++++++++ tests/shared/src/MovingPercentileTests.h | 22 +++ tests/shared/src/main.cpp | 16 ++ 4 files changed, 245 insertions(+) create mode 100644 tests/shared/CMakeLists.txt create mode 100644 tests/shared/src/MovingPercentileTests.cpp create mode 100644 tests/shared/src/MovingPercentileTests.h create mode 100644 tests/shared/src/main.cpp diff --git a/tests/shared/CMakeLists.txt b/tests/shared/CMakeLists.txt new file mode 100644 index 0000000000..b9513e3f26 --- /dev/null +++ b/tests/shared/CMakeLists.txt @@ -0,0 +1,38 @@ +cmake_minimum_required(VERSION 2.8) + +if (WIN32) + cmake_policy (SET CMP0020 NEW) +endif (WIN32) + +set(TARGET_NAME shared-tests) + +set(ROOT_DIR ../..) +set(MACRO_DIR ${ROOT_DIR}/cmake/macros) + +# setup for find modules +set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_CURRENT_SOURCE_DIR}/../../cmake/modules/") + +#find_package(Qt5Network REQUIRED) +#find_package(Qt5Script REQUIRED) +#find_package(Qt5Widgets REQUIRED) + +include(${MACRO_DIR}/SetupHifiProject.cmake) +setup_hifi_project(${TARGET_NAME} TRUE) + +include(${MACRO_DIR}/AutoMTC.cmake) +auto_mtc(${TARGET_NAME} ${ROOT_DIR}) + +#qt5_use_modules(${TARGET_NAME} Network Script Widgets) + +#include glm +include(${MACRO_DIR}/IncludeGLM.cmake) +include_glm(${TARGET_NAME} ${ROOT_DIR}) + +# link in the shared libraries +include(${MACRO_DIR}/LinkHifiLibrary.cmake) +link_hifi_library(shared ${TARGET_NAME} ${ROOT_DIR}) + +IF (WIN32) + #target_link_libraries(${TARGET_NAME} Winmm Ws2_32) +ENDIF(WIN32) + diff --git a/tests/shared/src/MovingPercentileTests.cpp b/tests/shared/src/MovingPercentileTests.cpp new file mode 100644 index 0000000000..26870717ca --- /dev/null +++ b/tests/shared/src/MovingPercentileTests.cpp @@ -0,0 +1,169 @@ +// +// MovingPercentileTests.cpp +// tests/shared/src +// +// Created by Yixin Wang on 6/4/2014 +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "MovingPercentileTests.h" + +#include "SharedUtil.h" +#include "MovingPercentile.h" + +#include + +float MovingPercentileTests::random() { + return rand() / (float)RAND_MAX; +} + +void MovingPercentileTests::runAllTests() { + + QVector valuesForN; + + valuesForN.append(1); + valuesForN.append(2); + valuesForN.append(3); + valuesForN.append(4); + valuesForN.append(5); + valuesForN.append(10); + valuesForN.append(100); + + + QQueue lastNSamples; + + for (int i=0; i N) { + lastNSamples.pop_front(); + } + + movingMin.updatePercentile(sample); + + float experimentMin = movingMin.getValueAtPercentile(); + + float actualMin = lastNSamples[0]; + for (int j = 0; j < lastNSamples.size(); j++) { + if (lastNSamples.at(j) < actualMin) { + actualMin = lastNSamples.at(j); + } + } + + if (experimentMin != actualMin) { + qDebug() << "\t\t FAIL at sample" << s; + fail = true; + break; + } + } + if (!fail) { + qDebug() << "\t\t PASS"; + } + } + + + { + bool fail = false; + + qDebug() << "\t testing running max..."; + + lastNSamples.clear(); + MovingPercentile movingMax(N, 1.0f); + + for (int s = 0; s < 10000; s++) { + + float sample = random(); + + lastNSamples.push_back(sample); + if (lastNSamples.size() > N) { + lastNSamples.pop_front(); + } + + movingMax.updatePercentile(sample); + + float experimentMax = movingMax.getValueAtPercentile(); + + float actualMax = lastNSamples[0]; + for (int j = 0; j < lastNSamples.size(); j++) { + if (lastNSamples.at(j) > actualMax) { + actualMax = lastNSamples.at(j); + } + } + + if (experimentMax != actualMax) { + qDebug() << "\t\t FAIL at sample" << s; + fail = true; + break; + } + } + if (!fail) { + qDebug() << "\t\t PASS"; + } + } + + + { + bool fail = false; + + qDebug() << "\t testing running median..."; + + lastNSamples.clear(); + MovingPercentile movingMedian(N, 0.5f); + + for (int s = 0; s < 10000; s++) { + + float sample = random(); + + lastNSamples.push_back(sample); + if (lastNSamples.size() > N) { + lastNSamples.pop_front(); + } + + movingMedian.updatePercentile(sample); + + float experimentMedian = movingMedian.getValueAtPercentile(); + + int samplesLessThan = 0; + int samplesMoreThan = 0; + + for (int j=0; j experimentMedian) { + samplesMoreThan++; + } + } + + + if (!(samplesLessThan <= N/2 && samplesMoreThan <= N-1/2)) { + qDebug() << "\t\t FAIL at sample" << s; + fail = true; + break; + } + } + if (!fail) { + qDebug() << "\t\t PASS"; + } + } + } +} + diff --git a/tests/shared/src/MovingPercentileTests.h b/tests/shared/src/MovingPercentileTests.h new file mode 100644 index 0000000000..34460880fb --- /dev/null +++ b/tests/shared/src/MovingPercentileTests.h @@ -0,0 +1,22 @@ +// +// MovingPercentileTests.h +// tests/shared/src +// +// Created by Yixin Wang on 6/4/2014 +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#ifndef hifi_MovingPercentileTests_h +#define hifi_MovingPercentileTests_h + +namespace MovingPercentileTests { + + float random(); + + void runAllTests(); +} + +#endif // hifi_MovingPercentileTests_h diff --git a/tests/shared/src/main.cpp b/tests/shared/src/main.cpp new file mode 100644 index 0000000000..3ae1b7b34d --- /dev/null +++ b/tests/shared/src/main.cpp @@ -0,0 +1,16 @@ +// +// main.cpp +// tests/physics/src +// +// Copyright 2014 High Fidelity, Inc. +// +// Distributed under the Apache License, Version 2.0. +// See the accompanying file LICENSE or http://www.apache.org/licenses/LICENSE-2.0.html +// + +#include "MovingPercentileTests.h" + +int main(int argc, char** argv) { + MovingPercentileTests::runAllTests(); + return 0; +}