libdballe  7.6
msg/tests.h
1 #include <dballe/core/tests.h>
2 #include <dballe/message.h>
3 #include <dballe/msg/codec.h>
4 #include <vector>
5 
6 namespace wreport {
7 struct Vartable;
8 }
9 
10 namespace dballe {
11 namespace tests {
12 
13 Messages read_msgs(const char* filename, File::Encoding type, const dballe::msg::Importer::Options& opts=dballe::msg::Importer::Options());
14 Messages read_msgs_csv(const char* filename);
15 
16 struct ActualMessage : public Actual<const Message&>
17 {
18  using Actual::Actual;
19 
20  void is_undef(int shortcut) const;
21 };
22 
23 inline ActualMessage actual(const Message& message) { return ActualMessage(message); }
24 
25 std::unique_ptr<wreport::Bulletin> export_msgs(File::Encoding enctype, const Messages& in, const std::string& tag, const dballe::msg::Exporter::Options& opts=dballe::msg::Exporter::Options());
26 #define test_export_msgs(...) wcallchecked(export_msgs(__VA_ARGS__))
27 
28 void track_different_msgs(const Message& msg1, const Message& msg2, const std::string& prefix);
29 void track_different_msgs(const Messages& msgs1, const Messages& msgs2, const std::string& prefix);
30 
31 extern const char* bufr_files[];
32 extern const char* crex_files[];
33 extern const char* aof_files[];
34 
35 const wreport::Var& want_var(const Message& msg, int shortcut);
36 const wreport::Var& want_var(const Message& msg, wreport::Varcode code, const dballe::Level& lev, const dballe::Trange& tr);
37 
38 inline ActualVar actual(const Message& message, int shortcut) { return ActualVar(want_var(message, shortcut)); }
39 inline ActualVar actual(const Message& message, wreport::Varcode code, const dballe::Level& lev, const dballe::Trange& tr) { return ActualVar(want_var(message, code, lev, tr)); }
40 
41 void dump(const std::string& tag, const Message& msg, const std::string& desc="message");
42 void dump(const std::string& tag, const Messages& msgs, const std::string& desc="message");
43 void dump(const std::string& tag, const wreport::Bulletin& bul, const std::string& desc="message");
44 void dump(const std::string& tag, const BinaryMessage& msg, const std::string& desc="message");
45 void dump(const std::string& tag, const std::string& str, const std::string& desc="message");
46 
48 {
49  virtual ~MessageTweaker() {}
50  virtual void tweak(Messages&) {}
51  virtual std::string desc() const = 0;
52 };
53 
55 {
56  std::vector<MessageTweaker*> tweaks;
57 
58  ~MessageTweakers();
59  // Takes ownership of memory management
60  void add(MessageTweaker* tweak);
61  void apply(Messages& msgs);
62 };
63 
64 namespace tweaks {
65 
66 // Strip attributes from all variables in a Messages
67 struct StripAttrs : public MessageTweaker
68 {
69  std::vector<wreport::Varcode> codes;
70 
71  void tweak(Messages& msgs);
72  virtual std::string desc() const { return "StripAttrs"; }
73 };
74 
75 // Strip attributes from all variables in a Messages
76 struct StripQCAttrs : public StripAttrs
77 {
78  StripQCAttrs();
79  virtual std::string desc() const { return "StripQCAttrs"; }
80 };
81 
82 // Strip attributes with substituted values
84 {
85  void tweak(Messages& msgs);
86  virtual std::string desc() const { return "StripSubstituteAttrs"; }
87 };
88 
89 // Strip context attributes from all variables in a Messages
91 {
93  virtual std::string desc() const { return "StripContextAttrs"; }
94 };
95 
96 // Strip a user-defined list of vars from all levels
97 struct StripVars : public MessageTweaker
98 {
99  std::vector<wreport::Varcode> codes;
100 
101  StripVars() {}
102  StripVars(std::initializer_list<wreport::Varcode> codes) : codes(codes) {}
103  void tweak(Messages& msgs);
104  virtual std::string desc() const { return "StripVars"; }
105 };
106 
107 // Strip datetime variables in the station context
109 {
111  virtual std::string desc() const { return "StripDatetimeVars"; }
112 };
113 
114 // Round variables to account for a passage through legacy vars
116 {
117  const wreport::Vartable* table;
118  RoundLegacyVars();
119  void tweak(Messages& msgs);
120  virtual std::string desc() const { return "RoundLegacyVars"; }
121 };
122 
123 // Remove synop vars present in WMO templates but not in ECMWF templates
125 {
126  void tweak(Messages& msgs);
127  virtual std::string desc() const { return "RemoveSynopWMOOnlyVars"; }
128 };
129 
130 // Remove temp vars present in WMO templates but not in ECMWF templates
132 {
133  void tweak(Messages& msgs);
134  virtual std::string desc() const { return "RemoveTempWMOOnlyVars"; }
135 };
136 
137 // Remove temp vars present only in an odd temp template for which we have
138 // messages in the test suite
140 {
142  virtual std::string desc() const { return "RemoveOddTempTemplateOnlyVars"; }
143 };
144 
145 // Remove ground level with missing length of statistical processing, that
146 // cannot be encoded in ECMWF templates
148 {
149  void tweak(Messages& msgs);
150  virtual std::string desc() const { return "RemoveSynopWMOOddprec"; }
151 };
152 
153 // Truncate station name to its canonical length
155 {
156  void tweak(Messages& msgs);
157  virtual std::string desc() const { return "TruncStName"; }
158 };
159 
160 // Round geopotential with a B10003->B10008->B10009->B10008->B10003 round trip
162 {
163  const wreport::Vartable* table;
165  void tweak(Messages& msgs);
166  virtual std::string desc() const { return "RoundGeopotential"; }
167 };
168 
169 // Add B10008 GEOPOTENTIAL to all height levels, with its value taken from the height
171 {
172  const wreport::Vartable* table;
174  void tweak(Messages& msgs);
175  virtual std::string desc() const { return "HeightToGeopotential"; }
176 };
177 
178 // Round vertical sounding significance with a B08042->B08001->B08042 round trip
179 struct RoundVSS : public MessageTweaker
180 {
181  void tweak(Messages& msgs);
182  virtual std::string desc() const { return "RoundVSS"; }
183 };
184 
185 // Remove a context given its level and time range
187 {
188  Level lev;
189  Trange tr;
190  RemoveContext(const Level& lev, const Trange& tr);
191  void tweak(Messages& msgs);
192  virtual std::string desc() const { return "RemoveContext"; }
193 };
194 
195 }
196 
198 {
199  std::string name;
200  File::Encoding type;
201  BinaryMessage raw;
202  wreport::Bulletin* bulletin = 0;
203  Messages msgs;
204 
205  TestMessage(File::Encoding type, const std::string& name);
206  ~TestMessage();
207 
208  void read_from_file(const std::string& fname, const msg::Importer::Options& input_opts);
209  void read_from_raw(const BinaryMessage& msg, const msg::Importer::Options& input_opts);
210  void read_from_msgs(const Messages& msgs, const msg::Exporter::Options& export_opts);
211  void dump() const;
212 };
213 
214 struct TestCodec
215 {
216  std::string fname;
217  File::Encoding type;
218  bool verbose = false;
219  msg::Importer::Options input_opts;
220  msg::Exporter::Options output_opts;
221  std::string expected_template;
222  int expected_subsets = 1;
223  int expected_min_vars = 1;
224  int expected_data_category = MISSING_INT;
225  int expected_data_subcategory = MISSING_INT;
226  int expected_data_subcategory_local = MISSING_INT;
227  MessageTweakers after_reimport_import;
228  MessageTweakers after_reimport_reimport;
229  MessageTweakers after_convert_import;
230  MessageTweakers after_convert_reimport;
231 
232  void do_compare(const TestMessage& msg1, const TestMessage& msg2);
233 
234  TestCodec(const std::string& fname, File::Encoding type=File::BUFR);
235 
236  void configure_ecmwf_to_wmo_tweaks();
237 
238  // "import, export, import again, compare" test
239  void run_reimport();
240 
241  // "import, export as different template, import again, compare" test
242  void run_convert(const std::string& tplname);
243 };
244 
245 
246 #if 0
247 
248 /* Random message generation functions */
249 
250 class msg_generator : public generator
251 {
252 public:
253  dba_err fill_message(dba_msg msg, bool mobile);
254 };
255 
256 
257 /* Message reading functions */
258 
259 class msg_vector : public dba_raw_consumer, public std::vector<dba_msgs>
260 {
261 public:
262  virtual ~msg_vector()
263  {
264  for (iterator i = begin(); i != end(); i++)
265  dba_msgs_delete(*i);
266  }
267 
268  virtual dba_err consume(dba_rawmsg raw)
269  {
270  dba_msgs msgs;
271 
272  DBA_RUN_OR_RETURN(dba_marshal_decode(raw, &msgs));
273  push_back(msgs);
274 
275  return dba_error_ok();
276  }
277 };
278 
279 template <typename T>
280 void my_ensure_msg_equals(const char* file, int line, dba_msg msg, int id, const char* idname, const T& value)
281 {
282  dba_var var = my_want_var(file, line, msg, id, idname);
283  inner_ensure_var_equals(var, value);
284 }
285 #define gen_ensure_msg_equals(msg, id, value) my_ensure_msg_equals(__FILE__, __LINE__, (msg), (id), #id, (value))
286 #define inner_ensure_msg_equals(msg, id, value) my_ensure_msg_equals(file, line, (msg), (id), #id, (value))
287 #endif
288 
289 }
290 }
291 
292 // vim:set ts=4 sw=4:
Encoding
Supported encodings.
Definition: file.h:20
Definition: codec.h:107
Definition: msg/tests.h:108
Definition: msg/tests.h:97
Definition: msg/tests.h:154
Definition: msg/tests.h:197
Definition: msg/tests.h:214
Definition: codec.h:35
General codec options.
Information on how a value has been sampled or computed with regards to time.
Definition: types.h:565
A bulletin that has been decoded and physically interpreted.
Definition: message.h:25
Definition: msg/tests.h:67
Copyright (C) 2008–2010 ARPA-SIM urpsim@smr.arpa.emr.it
Definition: cmdline.h:17
Definition: msg/tests.h:186
Vertical level or layer.
Definition: types.h:515
Definition: msg/tests.h:179
Definition: msg/tests.h:54
Definition: msg/tests.h:47
Definition: msg/tests.h:16
Binary message.
Definition: file.h:131
Ordered collection of messages.
Definition: message.h:64
Definition: conversion.h:6
Definition: msg/tests.h:76
Definition: msg/tests.h:115
Definition: msg/tests.h:161