Now have a simple stats test.
This commit is contained in:
parent
d3158291f7
commit
0efb17371b
4 changed files with 37 additions and 10 deletions
|
@ -137,6 +137,7 @@ executable('runtests', sources + [
|
|||
'tests/rituals.cpp',
|
||||
'tests/sound.cpp',
|
||||
'tests/spatialmap.cpp',
|
||||
'tests/stats.cpp',
|
||||
'tests/textures.cpp',
|
||||
'tests/tilemap.cpp',
|
||||
],
|
||||
|
|
4
rand.hpp
4
rand.hpp
|
@ -20,8 +20,8 @@ namespace Random {
|
|||
}
|
||||
|
||||
template<typename T>
|
||||
T normal(T from, T to) {
|
||||
std::normal_distribution<T> rand(from, to);
|
||||
T normal(T mean, T stddev) {
|
||||
std::normal_distribution<T> rand(mean, stddev);
|
||||
|
||||
return rand(GENERATOR);
|
||||
}
|
||||
|
|
15
stats.hpp
15
stats.hpp
|
@ -7,16 +7,16 @@ struct Stats {
|
|||
|
||||
double sum = 0.0;
|
||||
double sumsq = 0.0;
|
||||
unsigned long n = 0L;
|
||||
double n = 0.0;
|
||||
double min = 0.0;
|
||||
double max = 0.0;
|
||||
|
||||
inline void reset() {
|
||||
sum = 0;
|
||||
sumsq = 0;
|
||||
n = 0L;
|
||||
min = 0;
|
||||
max = 0;
|
||||
sum = 0.0;
|
||||
sumsq = 0.0;
|
||||
n = 0.0;
|
||||
min = 0.0;
|
||||
max = 0.0;
|
||||
}
|
||||
|
||||
inline double mean() {
|
||||
|
@ -24,8 +24,7 @@ struct Stats {
|
|||
}
|
||||
|
||||
inline double stddev() {
|
||||
return std::sqrt((sumsq - (sum * sum / n)) /
|
||||
(n - 1));
|
||||
return std::sqrt((sumsq - (sum * sum / n)) / (n - 1));
|
||||
}
|
||||
|
||||
inline void sample(double s) {
|
||||
|
|
27
tests/stats.cpp
Normal file
27
tests/stats.cpp
Normal file
|
@ -0,0 +1,27 @@
|
|||
#include <catch2/catch_test_macros.hpp>
|
||||
#include "stats.hpp"
|
||||
#include "rand.hpp"
|
||||
#include <cmath>
|
||||
#include <fmt/core.h>
|
||||
|
||||
TEST_CASE("basic stats tests", "[stats]") {
|
||||
Stats stat1;
|
||||
stat1.sample(1.0);
|
||||
|
||||
for(int i = 0; i < 20; i++) {
|
||||
double x = Random::normal(20.0,5.0);
|
||||
stat1.sample(x);
|
||||
REQUIRE(!std::isnan(stat1.stddev()));
|
||||
REQUIRE(stat1.mean() < stat1.mean() + stat1.stddev() * 4.0);
|
||||
}
|
||||
|
||||
stat1.dump();
|
||||
|
||||
stat1.reset();
|
||||
REQUIRE(stat1.n == 0.0);
|
||||
|
||||
auto timer = stat1.time_start();
|
||||
for(int i = 0; i < 20; i++) {
|
||||
stat1.sample_time(timer);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue