EventIDMatcher_t.cc
Go to the documentation of this file.
1 // vim: set sw=2 :
2 
5 
6 //#include <algorithm>
7 #include <cstdlib>
8 //#include <functional>
9 #include <iostream>
10 #include <string>
11 #include <vector>
12 
13 using namespace std::string_literals;
14 
15 using namespace art;
16 // using namespace std::placeholders;
17 using namespace std;
18 
19 void
20 dump_pats(vector<string> const& pats)
21 {
22  int i = 0;
23  for (auto const& val : pats) {
24  cout << i << ": " << val << endl;
25  }
26 }
27 
28 void
29 dump_ids(vector<EventID> const& cont)
30 {
31  int i = 0;
32  for (auto const& val : cont) {
33  cout << i << ": " << val << endl;
34  }
35 }
36 
37 void
38 find_matches(string const& pat,
39  vector<EventID> const& eids,
40  vector<EventID>& matches)
41 {
42  auto m = EventIDMatcher(pat);
43  //
44  // The for loop is equivalent to this:
45  //
46  // copy_if(eids.cbegin(), eids.cend(),
47  // back_inserter(matches),
48  // bind(&EventIDMatcher::match, &m, _1));
49  //
50  // For those addicted to functional programming, feel free
51  // to replace the for loop. I find the for loop much
52  // easier to maintain and debug.
53  //
54  for (auto const& val : eids) {
55  if (m.match(val)) {
56  matches.push_back(val);
57  }
58  }
59 }
60 
61 void
62 find_list_matches(vector<string> const& pats,
63  vector<EventID> const& eids,
64  vector<EventID>& matches)
65 {
66  auto m = EventIDMatcher(pats);
67  //
68  // The for loop is equivalent to this:
69  //
70  // copy_if(eids.cbegin(), eids.cend(),
71  // back_inserter(matches),
72  // bind(&EventIDMatcher::match, &m, _1));
73  //
74  // For those addicted to functional programming, feel free
75  // to replace the for loop. I find the for loop much
76  // easier to maintain and debug.
77  //
78  for (auto const& val : eids) {
79  if (m.match(val)) {
80  matches.push_back(val);
81  }
82  }
83 }
84 
85 void
86 check_match(string const& pat,
87  vector<EventID> const& expected,
88  vector<EventID> const& matches)
89 {
90  // cout << " eids.size(): " << eids.size() << endl;
91  // cout << "matches.size(): " << matches.size() << endl;
92  if (matches == expected) {
93  return;
94  }
95  cerr << "Match with \"" << pat << "\" failed!" << endl;
96  cerr << "expected:" << endl;
97  dump_ids(expected);
98  cerr << "matches:" << endl;
99  dump_ids(matches);
100  exit(EXIT_FAILURE);
101 }
102 
103 void
104 check_list_match(vector<string> const& pats,
105  vector<EventID> const& expected,
106  vector<EventID> const& matches)
107 {
108  // cout << " eids.size(): " << eids.size() << endl;
109  // cout << "matches.size(): " << matches.size() << endl;
110  if (matches == expected) {
111  return;
112  }
113  cerr << "Match with pattern list failed!" << endl;
114  cerr << "patterns:" << endl;
115  dump_pats(pats);
116  cerr << "expected:" << endl;
117  dump_ids(expected);
118  cerr << "matches:" << endl;
119  dump_ids(matches);
120  exit(EXIT_FAILURE);
121 }
122 
123 void
124 run_test(string const& pat,
125  vector<EventID> const& eids,
126  vector<EventID> const& expected)
127 {
128  vector<EventID> matches;
129  find_matches(pat, eids, matches);
130  check_match(pat, expected, matches);
131 }
132 
133 void
134 run_list_test(vector<string> const& pats,
135  vector<EventID> const& eids,
136  vector<EventID> const& expected)
137 {
138  vector<EventID> matches;
139  find_list_matches(pats, eids, matches);
140  check_list_match(pats, expected, matches);
141 }
142 
143 int
145 {
146  //
147  // Generate a list of EventIDs to test with.
148  //
149  vector<EventID> eids;
150  for (auto run = 1U; run < 3U; ++run) {
151  for (auto subrun = 0U; subrun < 2U; ++subrun) {
152  for (auto event = 1U; event < 3U; ++event) {
153  eids.emplace_back(run, subrun, event);
154  }
155  }
156  }
157  //
158  // Check wildcards.
159  //
160  if (1) {
161  // Match everything.
162  auto pat = "*:*:*"s;
163  vector<EventID> expected = {
164  {1, 0, 1},
165  {1, 0, 2},
166  {1, 1, 1},
167  {1, 1, 2},
168  {2, 0, 1},
169  {2, 0, 2},
170  {2, 1, 1},
171  {2, 1, 2},
172  };
173  run_test(pat, eids, expected);
174  }
175  if (1) {
176  // Match any run, any subRun.
177  auto pat = "*:*:1"s;
178  vector<EventID> expected = {
179  {1, 0, 1},
180  {1, 1, 1},
181  {2, 0, 1},
182  {2, 1, 1},
183  };
184  run_test(pat, eids, expected);
185  }
186  if (1) {
187  // Match any run.
188  auto pat = "*:0:1"s;
189  vector<EventID> expected = {
190  {1, 0, 1},
191  {2, 0, 1},
192  };
193  run_test(pat, eids, expected);
194  }
195  if (1) {
196  // Match entry 0.
197  auto pat = "1:0:1"s;
198  vector<EventID> expected = {
199  {1, 0, 1},
200  };
201  run_test(pat, eids, expected);
202  }
203  if (1) {
204  // Match entry 1.
205  auto pat = "1:0:2"s;
206  vector<EventID> expected = {
207  {1, 0, 2},
208  };
209  run_test(pat, eids, expected);
210  }
211  if (1) {
212  // Match entry 2.
213  auto pat = "1:1:1"s;
214  vector<EventID> expected = {
215  {1, 1, 1},
216  };
217  run_test(pat, eids, expected);
218  }
219  if (1) {
220  // Match entry 3.
221  auto pat = "1:1:2"s;
222  vector<EventID> expected = {
223  {1, 1, 2},
224  };
225  run_test(pat, eids, expected);
226  }
227  if (1) {
228  // Match entry 4.
229  auto pat = "2:0:1"s;
230  vector<EventID> expected = {
231  {2, 0, 1},
232  };
233  run_test(pat, eids, expected);
234  }
235  if (1) {
236  // Match entry 5.
237  auto pat = "2:0:2"s;
238  vector<EventID> expected = {
239  {2, 0, 2},
240  };
241  run_test(pat, eids, expected);
242  }
243  if (1) {
244  // Match entry 6.
245  auto pat = "2:1:1"s;
246  vector<EventID> expected = {
247  {2, 1, 1},
248  };
249  run_test(pat, eids, expected);
250  }
251  if (1) {
252  // Match entry 7.
253  auto pat = "2:1:2"s;
254  vector<EventID> expected = {
255  {2, 1, 2},
256  };
257  run_test(pat, eids, expected);
258  }
259  if (1) {
260  // Match run range.
261  auto pat = "1-2:0:1"s;
262  vector<EventID> expected = {
263  {1, 0, 1},
264  {2, 0, 1},
265  };
266  run_test(pat, eids, expected);
267  }
268  if (1) {
269  // Match subrun range.
270  auto pat = "1:0-1:1"s;
271  vector<EventID> expected = {
272  {1, 0, 1},
273  {1, 1, 1},
274  };
275  run_test(pat, eids, expected);
276  }
277  if (1) {
278  // Match event range.
279  auto pat = "1:0:1-2"s;
280  vector<EventID> expected = {
281  {1, 0, 1},
282  {1, 0, 2},
283  };
284  run_test(pat, eids, expected);
285  }
286  {
287  vector<EventID> empty;
288  eids.swap(empty);
289  }
290  for (auto run = 1U; run < 11U; ++run) {
291  for (auto subrun = 0U; subrun < 10U; ++subrun) {
292  for (auto event = 1U; event < 11U; ++event) {
293  eids.emplace_back(run, subrun, event);
294  }
295  }
296  }
297  if (1) {
298  // Match run range list.
299  auto pat = "1-2, 4, 6-9 : 0 : 1"s;
300  vector<EventID> expected = {
301  {1, 0, 1},
302  {2, 0, 1},
303  {4, 0, 1},
304  {6, 0, 1},
305  {7, 0, 1},
306  {8, 0, 1},
307  {9, 0, 1},
308  };
309  run_test(pat, eids, expected);
310  }
311  if (1) {
312  // Match subrun range list.
313  auto pat = "3: 1-3, 6, 9 : 5"s;
314  vector<EventID> expected = {
315  {3, 1, 5},
316  {3, 2, 5},
317  {3, 3, 5},
318  {3, 6, 5},
319  {3, 9, 5},
320  };
321  run_test(pat, eids, expected);
322  }
323  if (1) {
324  // Match event range list.
325  auto pat = "7: 5 : 3, 5-5, 6-7, 9-10"s;
326  vector<EventID> expected = {
327  {7, 5, 3},
328  {7, 5, 5},
329  {7, 5, 6},
330  {7, 5, 7},
331  {7, 5, 9},
332  {7, 5, 10},
333  };
334  run_test(pat, eids, expected);
335  }
336  if (1) {
337  // Match complex pattern.
338  auto pat = "1, 6 : 0, 2, 5-6 : 3, 6-7"s;
339  vector<EventID> expected = {
340  {1, 0, 3}, {1, 0, 6}, {1, 0, 7}, {1, 2, 3}, {1, 2, 6}, {1, 2, 7},
341  {1, 5, 3}, {1, 5, 6}, {1, 5, 7}, {1, 6, 3}, {1, 6, 6}, {1, 6, 7},
342  {6, 0, 3}, {6, 0, 6}, {6, 0, 7}, {6, 2, 3}, {6, 2, 6}, {6, 2, 7},
343  {6, 5, 3}, {6, 5, 6}, {6, 5, 7}, {6, 6, 3}, {6, 6, 6}, {6, 6, 7},
344  };
345  run_test(pat, eids, expected);
346  }
347  if (1) {
348  // Simple pattern list.
349  vector<string> pats = {
350  "1:0:1"s,
351  "5:3:10"s,
352  };
353  vector<EventID> expected = {
354  {1, 0, 1},
355  {5, 3, 10},
356  };
357  run_list_test(pats, eids, expected);
358  }
359  if (1) {
360  // Complex pattern list.
361  vector<string> pats = {
362  "7: 5 : 3, 5-5, 6-7, 9-10"s,
363  "1, 6 : 0, 2, 5-6 : 3, 6-7"s,
364  "1-2: 5 : 3, 5-5, 6-7, 9-10"s,
365  };
366  vector<EventID> expected = {
367  {1, 0, 3}, {1, 0, 6}, {1, 0, 7}, {1, 2, 3}, {1, 2, 6}, {1, 2, 7},
368  {1, 5, 3}, {1, 5, 5}, {1, 5, 6}, {1, 5, 7}, {1, 5, 9}, {1, 5, 10},
369  {1, 6, 3}, {1, 6, 6}, {1, 6, 7}, {2, 5, 3}, {2, 5, 5}, {2, 5, 6},
370  {2, 5, 7}, {2, 5, 9}, {2, 5, 10}, {6, 0, 3}, {6, 0, 6}, {6, 0, 7},
371  {6, 2, 3}, {6, 2, 6}, {6, 2, 7}, {6, 5, 3}, {6, 5, 6}, {6, 5, 7},
372  {6, 6, 3}, {6, 6, 6}, {6, 6, 7}, {7, 5, 3}, {7, 5, 5}, {7, 5, 6},
373  {7, 5, 7}, {7, 5, 9}, {7, 5, 10},
374  };
375  run_list_test(pats, eids, expected);
376  }
377 }
void find_matches(string const &pat, vector< EventID > const &eids, vector< EventID > &matches)
const char expected[]
Definition: Exception_t.cc:22
STL namespace.
int main()
void find_list_matches(vector< string > const &pats, vector< EventID > const &eids, vector< EventID > &matches)
void run_test(string const &pat, vector< EventID > const &eids, vector< EventID > const &expected)
void check_match(string const &pat, vector< EventID > const &expected, vector< EventID > const &matches)
void check_list_match(vector< string > const &pats, vector< EventID > const &expected, vector< EventID > const &matches)
void dump_pats(vector< string > const &pats)
void dump_ids(vector< EventID > const &cont)
void run_list_test(vector< string > const &pats, vector< EventID > const &eids, vector< EventID > const &expected)
static QCString * s
Definition: config.cpp:1042
decltype(auto) constexpr empty(T &&obj)
ADL-aware version of std::empty.
Definition: StdUtils.h:97
QTextStream & endl(QTextStream &s)
Event finding and building.