added tests for MovingPercentile

This commit is contained in:
wangyix 2014-06-04 17:23:44 -07:00
parent 5251057ed6
commit 124bd88b01
4 changed files with 245 additions and 0 deletions

View file

@ -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)

View file

@ -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 <qqueue.h>
float MovingPercentileTests::random() {
return rand() / (float)RAND_MAX;
}
void MovingPercentileTests::runAllTests() {
QVector<int> valuesForN;
valuesForN.append(1);
valuesForN.append(2);
valuesForN.append(3);
valuesForN.append(4);
valuesForN.append(5);
valuesForN.append(10);
valuesForN.append(100);
QQueue<float> lastNSamples;
for (int i=0; i<valuesForN.size(); i++) {
int N = valuesForN.at(i);
qDebug() << "testing moving percentile with N =" << N << "...";
{
bool fail = false;
qDebug() << "\t testing running min...";
lastNSamples.clear();
MovingPercentile movingMin(N, 0.0f);
for (int s = 0; s < 3*N; s++) {
float sample = random();
lastNSamples.push_back(sample);
if (lastNSamples.size() > 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<lastNSamples.size(); j++) {
if (lastNSamples.at(j) < experimentMedian) {
samplesLessThan++;
} else if (lastNSamples.at(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";
}
}
}
}

View file

@ -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

16
tests/shared/src/main.cpp Normal file
View file

@ -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;
}