summaryrefslogtreecommitdiffhomepage
path: root/tests/test-statistics.cpp
blob: fbc8cfbaaf72bc6cc063fd0bc9964fc0f4490cca (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
#include <boost/test/unit_test.hpp>
#include <boost/test/data/dataset.hpp>
#include <boost/test/data/monomorphic.hpp>
#include <boost/test/data/test_case.hpp>

#include <boost/property_tree/ptree.hpp>
#include <boost/property_tree/xml_parser.hpp>

#include <filesystem>
#include <sstream>
#include <string>

#include "statistics.h"

#include "config.h"

using namespace std::string_literals;
namespace fs = std::filesystem;

class StatisticsFixture
{
public:
 StatisticsFixture(){}
 ~StatisticsFixture(){}
 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(), "");
}