libdballe  7.21
core/tests.h
1 #include <wreport/tests.h>
2 #include <dballe/file.h>
3 #include <dballe/record.h>
4 #include <dballe/core/query.h>
5 #include <dballe/core/values.h>
6 #include <dballe/core/defs.h>
7 #include <dballe/core/csv.h>
8 #include <dballe/core/ostream.h>
9 #include <cstdlib>
10 #include <climits>
11 #include <string>
12 #include <vector>
13 #include <sstream>
14 #include <iostream>
15 #include <memory>
16 
17 namespace dballe {
18 namespace tests {
19 
20 using namespace wreport::tests;
21 
22 #if 0
23 // Some utility random generator functions
24 
25 static inline int rnd(int min, int max)
26 {
27  return min + (int) ((max - min) * (rand() / (RAND_MAX + 1.0)));
28 }
29 
30 static inline double rnd(double min, double max)
31 {
32  return min + (int) ((max - min) * (rand() / (RAND_MAX + 1.0)));
33 }
34 
35 static inline std::string rnd(int len)
36 {
37  std::string res;
38  int max = rnd(1, len);
39  for (int i = 0; i < max; i++)
40  res += (char)rnd('a', 'z');
41  return res;
42 }
43 
44 static inline bool rnd(double prob)
45 {
46  return (rnd(0, 100) < prob*100) ? true : false;
47 }
48 #endif
49 
50 // Message reading functions
51 
53 std::string datafile(const std::string& fname);
54 
55 std::unique_ptr<File> open_test_data(const char* filename, File::Encoding type);
56 
57 BinaryMessage read_rawmsg(const char* filename, File::Encoding type);
58 
59 class MemoryCSVWriter : public CSVWriter
60 {
61 public:
62  std::stringstream buf;
63 
64  void flush_row() override
65  {
66  buf << row << std::endl;
67  row.clear();
68  }
69 };
70 
71 #if 0
72 struct TestRecordValEqual
74 {
75  const dballe::Record& actual;
76  const dballe::Record& expected;
77  const char* name;
78  bool with_missing_int;
79 
80  TestRecordValEqual(const dballe::Record& actual, const dballe::Record& expected, const char* name, bool with_missing_int=false)
81  : actual(actual), expected(expected), name(name), with_missing_int(with_missing_int) {}
82 
83  void check() const;
84 };
85 
86 struct TestRecordVarsEqual
87 {
88  const dballe::Record& actual;
89  dballe::Values expected;
90 
91  TestRecordVarsEqual(const dballe::Record& actual, const dballe::Record& expected) : actual(actual), expected(expected) {}
92  TestRecordVarsEqual(const dballe::Record& actual, const dballe::Values& expected) : actual(actual), expected(expected) {}
93 
94  void check() const;
95 };
96 #endif
97 
98 struct ActualRecord : public wreport::tests::Actual<const dballe::Record&>
99 {
101 
102 #if 0
103  TestRecordValEqual equals(const Record& expected, const char* name) { return TestRecordValEqual(this->actual, expected, name); }
104  TestRecordValEqual equals_with_missing_int(const Record& expected, const char* name)
105  {
106  return TestRecordValEqual(this->actual, expected, name, true);
107  }
108 #endif
109  void vars_equal(const Record& expected) const { vars_equal(Values(expected)); }
112  void vars_equal(const Values& expected) const;
113 };
114 
115 // Set a record from a ", "-separated string of assignments
116 void set_record_from_string(Record& rec, const std::string& s);
117 std::unique_ptr<Record> record_from_string(const std::string& s);
118 std::unique_ptr<Query> query_from_string(const std::string& s);
119 core::Query core_query_from_string(const std::string& s);
120 
121 struct ActualMatcherResult : public Actual<int>
122 {
123  using Actual::Actual;
124 
125  void operator==(int expected) const;
126  void operator!=(int expected) const;
127 };
128 
129 inline ActualMatcherResult actual_matcher_result(int actual) { return ActualMatcherResult(actual); }
130 
131 using wreport::tests::actual;
132 
133 inline dballe::tests::ActualRecord actual(const dballe::Record& actual) { return dballe::tests::ActualRecord(actual); }
134 
135 inline ActualCString actual(const dballe::Ident& ident) { return ActualCString(ident); }
136 
137 }
138 }
Definition: csv.h:140
Encoding
Supported encodings.
Definition: file.h:20
Key/value store where keys are strings and values are wreport variables.
Definition: record.h:16
Ostream output for dballe/core objects.
Standard dballe::Query implementation.
Definition: core/query.h:29
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
A station identifier, that can be any string (including the empty string) or a missing value...
Definition: core/defs.h:19
Routines to parse data in CSV format.
Definition: core/tests.h:98
Definition: core/tests.h:121
Common definitions.
Structures used as input to database insert functions.
Collection of Value objects, indexed by wreport::Varcode.
Definition: values.h:202
void flush_row() override
Write the current line to the output file, and start a new one.
Definition: core/tests.h:64
Definition: core/tests.h:59