libdballe  9.11
core/tests.h
1 #include <wreport/tests.h>
2 #include <dballe/file.h>
3 #include <dballe/values.h>
4 #include <dballe/core/query.h>
5 #include <dballe/core/defs.h>
6 #include <dballe/core/csv.h>
7 #include <cstdlib>
8 #include <climits>
9 #include <filesystem>
10 #include <string>
11 #include <vector>
12 #include <sstream>
13 #include <iostream>
14 #include <memory>
15 
16 namespace dballe {
17 namespace tests {
18 
19 using namespace wreport::tests;
20 
21 #if 0
22 // Some utility random generator functions
23 
24 static inline int rnd(int min, int max)
25 {
26  return min + (int) ((max - min) * (rand() / (RAND_MAX + 1.0)));
27 }
28 
29 static inline double rnd(double min, double max)
30 {
31  return min + (int) ((max - min) * (rand() / (RAND_MAX + 1.0)));
32 }
33 
34 static inline std::string rnd(int len)
35 {
36  std::string res;
37  int max = rnd(1, len);
38  for (int i = 0; i < max; i++)
39  res += (char)rnd('a', 'z');
40  return res;
41 }
42 
43 static inline bool rnd(double prob)
44 {
45  return (rnd(0, 100) < prob*100) ? true : false;
46 }
47 #endif
48 
49 // Message reading functions
50 
52 std::filesystem::path datafile(const std::string& fname);
53 
54 std::unique_ptr<File> open_test_data(const char* filename, Encoding type);
55 
56 BinaryMessage read_rawmsg(const char* filename, Encoding type);
57 
58 class MemoryCSVWriter : public CSVWriter
59 {
60 public:
61  std::stringstream buf;
62 
63  void flush_row() override
64  {
65  buf << row << std::endl;
66  row.clear();
67  }
68 };
69 
70 #if 0
71 struct TestRecordValEqual
73 {
74  const dballe::Record& actual;
75  const dballe::Record& expected;
76  const char* name;
77  bool with_missing_int;
78 
79  TestRecordValEqual(const dballe::Record& actual, const dballe::Record& expected, const char* name, bool with_missing_int=false)
80  : actual(actual), expected(expected), name(name), with_missing_int(with_missing_int) {}
81 
82  void check() const;
83 };
84 
85 struct TestRecordVarsEqual
86 {
87  const dballe::Record& actual;
88  dballe::Values expected;
89 
90  TestRecordVarsEqual(const dballe::Record& actual, const dballe::Record& expected) : actual(actual), expected(expected) {}
91  TestRecordVarsEqual(const dballe::Record& actual, const dballe::Values& expected) : actual(actual), expected(expected) {}
92 
93  void check() const;
94 };
95 #endif
96 
97 // Set a query from a ", "-separated string of assignments
98 std::unique_ptr<Query> query_from_string(const std::string& s);
99 core::Query core_query_from_string(const std::string& s);
100 
101 struct ActualMatcherResult : public Actual<int>
102 {
103  using Actual::Actual;
104 
105  void operator==(int expected) const;
106  void operator!=(int expected) const;
107 };
108 
109 inline ActualMatcherResult actual_matcher_result(int actual) { return ActualMatcherResult(actual); }
110 
111 using wreport::tests::actual;
112 
113 inline ActualCString actual(const dballe::Ident& ident) { return ActualCString(ident); }
114 
115 }
116 }
Definition: csv.h:140
Definition: cmdline.h:18
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: types.h:747
Routines to parse data in CSV format.
Definition: core/tests.h:101
Common definitions.
Structures used as input to database insert functions.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:176
void flush_row() override
Write the current line to the output file, and start a new one.
Definition: core/tests.h:63
Definition: core/tests.h:58