statistics_t.cc
Go to the documentation of this file.
1 // vim: set sw=2 expandtab :
2 
5 #include "cetlib/sqlite/Ntuple.h"
7 
8 #include <array>
9 #include <cassert>
10 #include <memory>
11 #include <string>
12 
13 namespace {
14 
15  enum quantity : std::size_t { min = 0, max, mean, median, rms, n };
16  using quantities_t = std::array<unsigned, quantity::n>;
17 
18  auto
19  test_statistics_quantities(sqlite3* const db,
20  std::string const& table_name,
21  std::string const& column_name,
22  quantities_t const& expected_values)
23  {
24  assert(cet::sqlite::min<unsigned>(db, table_name, column_name) ==
25  expected_values[quantity::min]);
26  assert(cet::sqlite::max<unsigned>(db, table_name, column_name) ==
27  expected_values[quantity::max]);
28  // Don't want to worry about floating-point tolerance now, so just cast
29  // everything to unsigned.
30  assert(static_cast<unsigned>(cet::sqlite::mean(
31  db, table_name, column_name)) == expected_values[quantity::mean]);
32  assert(
33  static_cast<unsigned>(cet::sqlite::median(db, table_name, column_name)) ==
34  expected_values[quantity::median]);
35  assert(static_cast<unsigned>(cet::sqlite::rms(
36  db, table_name, column_name)) == expected_values[quantity::rms]);
37  }
38 }
39 
40 using namespace std;
41 using namespace cet::sqlite;
42 
43 int
45 {
47  unique_ptr<Connection> c{cf.make_connection(":memory:")};
48  std::string const table_name{"workers"};
49  {
51  *c, table_name, {{"Name", "Age", "Experience"}}};
52  workers.insert("Abby", 27, 5);
53  workers.insert("Benny", 48, 10);
54  workers.insert("Cassie", 52, 27);
55  workers.insert("David", 65, 14);
56  workers.insert("Emily", 38, 19);
57  }
58  test_statistics_quantities(
59  *c, table_name, "Age", {{27, 65, 46, 48, 12 /*.649...*/}});
60  test_statistics_quantities(
61  *c, table_name, "Experience", {{5, 27, 15, 14, 7 /*.563...*/}});
62 }
double rms(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:40
std::string string
Definition: nybbler.cc:12
STL namespace.
auto make_connection(std::string const &file_name, PolicyArgs &&...) -> Connection *
quantity
Definition: statistics_t.cc:15
std::void_t< T > n
int main()
Definition: statistics_t.cc:44
static int max(int a, int b)
struct sqlite3 sqlite3
T min(sqlite3 *const db, std::string const &table_name, std::string const &column_name)
Definition: statistics.h:55
workers
Definition: train.py:479
double mean(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:16
double median(sqlite3 *db, std::string const &table_name, std::string const &column_name)
Definition: statistics.cc:26