From 63fc8e14be5e450df8ccc18fe76e02c5f0827660 Mon Sep 17 00:00:00 2001 From: Roland Reichwein Date: Sat, 7 Jan 2023 14:07:13 +0100 Subject: Test statistics --- tests/test-statistics.cpp | 69 +++++++++++++++++++++++++++++++++++++++++++++-- 1 file changed, 67 insertions(+), 2 deletions(-) (limited to 'tests/test-statistics.cpp') diff --git a/tests/test-statistics.cpp b/tests/test-statistics.cpp index 9ed7d06..fbc8cfb 100644 --- a/tests/test-statistics.cpp +++ b/tests/test-statistics.cpp @@ -6,23 +6,88 @@ #include #include +#include #include #include #include "statistics.h" +#include "config.h" + using namespace std::string_literals; +namespace fs = std::filesystem; class StatisticsFixture { public: StatisticsFixture(){} ~StatisticsFixture(){} - void setup(){} - void teardown(){} + void setup() + { + std::error_code ec; + fs::remove("stats.db", ec); + } + void teardown() + { + std::error_code ec; + fs::remove("stats.db", ec); + } }; BOOST_FIXTURE_TEST_CASE(statistics, StatisticsFixture) { + { + Statistics stats; + + BOOST_CHECK_EQUAL(stats.getValues(), ""); + + stats.count(10, 1000, false, true, true); + + auto v{stats.getValues()}; + BOOST_CHECK_GT(v.size(), 0); + + auto pos{v.find(",")}; + BOOST_CHECK(pos != std::string::npos); + BOOST_CHECK_GT(pos, 0); + + std::string v2{v.substr(pos)}; + + BOOST_CHECK_EQUAL(v2, ",1,0,10,1000,1,0,10,1000,1,0,10,1000\n"); + + stats.save(); + + BOOST_CHECK_EQUAL(v2, ",1,0,10,1000,1,0,10,1000,1,0,10,1000\n"); + } + + { + Statistics stats("stats.db"); + auto v{stats.getValues()}; + BOOST_CHECK_GT(v.size(), 0); + + auto pos{v.find(",")}; + BOOST_CHECK(pos != std::string::npos); + BOOST_CHECK_GT(pos, 0); + + std::string v2{v.substr(pos)}; + + BOOST_CHECK_EQUAL(v2, ",1,0,10,1000,1,0,10,1000,1,0,10,1000\n"); + + stats.count(10, 1000, false, true, true); + BOOST_CHECK_EQUAL(stats.getValues().substr(pos), ",2,0,20,2000,2,0,20,2000,2,0,20,2000\n"); + stats.count(10, 1000, true, true, true); + BOOST_CHECK_EQUAL(stats.getValues().substr(pos), ",3,1,30,3000,3,1,30,3000,3,1,30,3000\n"); + stats.count(10, 1000, false, false, false); + BOOST_CHECK_EQUAL(stats.getValues().substr(pos), ",4,1,40,4000,3,1,30,3000,3,1,30,3000\n"); + stats.count(10, 1000, false, true, false); + BOOST_CHECK_EQUAL(stats.getValues().substr(pos), ",5,1,50,5000,4,1,40,4000,3,1,30,3000\n"); + stats.count(10, 1000, false, false, true); + BOOST_CHECK_EQUAL(stats.getValues().substr(pos), ",6,1,60,6000,4,1,40,4000,4,1,40,4000\n"); + } +} + +BOOST_FIXTURE_TEST_CASE(statistics_non_existing, StatisticsFixture) +{ + Statistics stats("nonexistingpath/stats.db"); + BOOST_CHECK_EQUAL(stats.getValues(), ""); } -- cgit v1.2.3