testValidatrix.cc
Go to the documentation of this file.
1 
11 #include <fstream>
12 #include <stdlib.h>
13 #include <boost/format.hpp>
14 #include <cppunit/extensions/HelperMacros.h>
15 #include <utl/System.h>
16 #include <tst/Verify.h>
17 #include <tst/Validatrix.h>
18 #include <utl/Trace.h>
19 #include <utl/AugerUnits.h>
20 
21 using namespace tst::Validatrix;
22 using namespace std;
23 using namespace boost;
24 //using utl::TraceD;
25 using utl::nanosecond;
26 
27 
28 
29 
33 class TestValidatrix : public CppUnit::TestFixture {
34 
35  CPPUNIT_TEST_SUITE(TestValidatrix);
36  CPPUNIT_TEST(TestBuiltin);
37  CPPUNIT_TEST(TestBuiltinFail);
38  CPPUNIT_TEST(TestTraceD);
39  CPPUNIT_TEST(TestVectorUnsignedShort);
40  CPPUNIT_TEST(TestVectorUnsignedShortFail);
41  CPPUNIT_TEST(TestZeroLengthVector);
42  CPPUNIT_TEST_EXCEPTION(TestEmptyStreams, utl::DoesNotComputeException);
43  CPPUNIT_TEST_SUITE_END();
44 
45  template<int n, class FStream>
46  class Streams {
47  public:
48  Streams(const string& prefix)
49  {
50  for (int i = 0; i < n; ++i) {
51  fFilename[i] = (format("%1%%2%.trx") % prefix % i).str();
52  fStream[i].open(fFilename[i].c_str());
53  CPPUNIT_ASSERT(fStream[i].is_open());
54  }
55  }
56 
57  ~Streams() { Close(); }
58 
59  template<int i>
60  FStream& Get() { return fStream[i]; }
61 
62  void
64  {
65  Close();
66  string command = "rm";
67  for (int i = 0; i < n; ++i)
68  command.append(string(" ") + fFilename[i]);
69  utl::System(command);
70  }
71 
72  private:
73  void
75  {
76  for (int i = 0; i < n; ++i)
77  if (fStream[i].is_open())
78  fStream[i].close();
79  }
80 
81  Streams(const Streams&);
82  Streams& operator=(const Streams&);
83 
84  FStream fStream[n];
85  string fFilename[n];
86  };
87 
88 public:
89  void setUp() { }
90 
91  void tearDown() { }
92 
93  void
95  {
96  const string prefix = "foo_1_";
97  {
98  Streams<2, ofstream> f(prefix);
99 
100  int i = 13;
101  unsigned u = 100;
102  double d = 3.14;
103  double dd = 137;
104 
105  f.Get<0>() << BeLabel("test") << BeEqual(i) << BeEqual(u) << BeCloseAbs(d) << BeCloseRel(dd);
106  f.Get<1>() << BeLabel("test") << BeEqual(i) << BeEqual(u) << BeCloseAbs(d) << BeCloseRel(dd);
107  }
108 
109  {
110  Streams<2, ifstream> f(prefix);
111 
112  const bool test = Compare(f.Get<0>(), f.Get<1>());
113  CPPUNIT_ASSERT(test);
114 
115  if (test)
116  f.Delete();
117  }
118  }
119 
120  void
122  {
123  const string prefix = "foo_2_";
124  {
125  Streams<2, ofstream> f(prefix);
126 
127  int i = 13;
128  unsigned u = 100;
129  double d = 3.14;
130  double dd = 137;
131 
132  f.Get<0>()
133  << BeLabel("test")
134  << BeEqual(i) << BeEqual(u) << BeCloseAbs(d) << BeCloseRel(dd);
135  f.Get<1>()
136  << BeLabel("tast")
137  << BeEqual(i-1) << BeEqual(u+1) << BeCloseAbs(d+0.1) << BeCloseRel(dd*1.01);
138  }
139 
140  {
141  Streams<2, ifstream> f(prefix);
142 
143  tst::Expected();
144  const bool test = Compare(f.Get<0>(), f.Get<1>(), /*failOnFirst=*/false);
145  CPPUNIT_ASSERT(!test);
146 
147  if (!test)
148  f.Delete();
149  }
150  }
151 
152  void
154  {
155  const string prefix = "foo_3_";
156  {
157  Streams<2, ofstream> f(prefix);
158 
159  const int size = 768;
160  utl::TraceD trace(size, 25*nanosecond);
161 
162  for (int i = 0; i < size; ++i)
163  trace[i] = 3.14*(i+1);
164 
165  f.Get<0>()
166  << BeLabel("some trace shit")
167  << BeCloseRel(trace) << endl
168  << BeLabel("again")
169  << BeCloseAbs(trace, /*tolerance*/1);
170 
171  // this should not cross the default tolerance
172  for (int i = 0; i < size; ++i)
173  trace[i] += 1e-8;
174 
175  f.Get<1>()
176  << BeLabel("some trace shit")
177  << BeCloseRel(trace) << endl;
178 
179  // this should not cross the absolute tolerance of 1
180  for (int i = 0; i < size; ++i)
181  trace[i] += 0.5;
182 
183  f.Get<1>()
184  << BeLabel("again")
185  << BeCloseAbs(trace, 1);
186  }
187  {
188  Streams<2, ifstream> f(prefix);
189 
190  const bool test = Compare(f.Get<0>(), f.Get<1>());
191  CPPUNIT_ASSERT(test);
192 
193  if (test)
194  f.Delete();
195  }
196  }
197 
198  void
200  {
201  const string prefix = "foo_4_";
202  {
203  Streams<2, ofstream> f(prefix);
204 
205  vector<unsigned short> vec(100, 13U);
206  for (int i = 50; i < 60; ++i)
207  vec[i] = (unsigned short)(i);
208 
209  f.Get<0>() << BeLabel("some vector shit") << BeEqual(vec);
210  f.Get<1>() << BeLabel("some vector shit") << BeEqual(vec);
211  }
212  {
213  Streams<2, ifstream> f(prefix);
214 
215  const bool test = Compare(f.Get<0>(), f.Get<1>());
216  CPPUNIT_ASSERT(test);
217 
218  if (test)
219  f.Delete();
220  }
221  }
222 
223  void
225  {
226  const string prefix = "foo_5_";
227  {
228  Streams<2, ofstream> f(prefix);
229 
230  vector<unsigned short> vec1(100, 13U);
231  for (int i = 50; i < 60; ++i)
232  vec1[i] = (unsigned short)(i);
233  vector<unsigned short> vec2 = vec1;
234  vec2[99] = 0U;
235 
236  f.Get<0>() << BeLabel("some vector shit") << BeEqual(vec1);
237  f.Get<1>() << BeLabel("some vector shit") << BeEqual(vec2);
238  }
239  {
240  Streams<2, ifstream> f(prefix);
241 
242  const bool test = Compare(f.Get<0>(), f.Get<1>());
243  CPPUNIT_ASSERT(!test);
244 
245  if (!test)
246  f.Delete();
247  }
248  }
249 
250  void
252  {
253  const string prefix = "foo_6_";
254  {
255  Streams<2, ofstream> f(prefix);
256 
257  vector<double> vec;
258 
259  f.Get<0>() << BeLabel("empty vector") << BeEqual(vec);
260  f.Get<1>() << BeLabel("empty vector") << BeEqual(vec);
261  }
262  {
263  Streams<2, ifstream> f(prefix);
264 
265  const bool test = Compare(f.Get<0>(), f.Get<1>());
266  CPPUNIT_ASSERT(test);
267 
268  if (test)
269  f.Delete();
270  }
271  }
272 
273  void
275  {
276  {
277  ofstream f1("f1.trx");
278  f1 << BeEqual(1);
279  }
280 
281  ifstream f1("f1.trx");
282  ifstream f2("f2.trx");
283  const bool test = Compare(f1, f2);
284  CPPUNIT_ASSERT(!test);
285  f2.close();
286  utl::System("rm f1.trx f2.trx");
287  }
288 
289 };
290 
291 
293 
294 
295 // Configure (x)emacs for this file ...
296 // Local Variables:
297 // mode: c++
298 // compile-command: "make -C .. -k testTesting && ../testTesting"
299 // End:
void TestZeroLengthVector()
std::string BeCloseRel(const T &value, const double tolerance=kDefaultTolerance)
Definition: Validatrix.h:153
void System(const char *const command, const bool throwOnError, const bool notify)
Definition: System.cc:10
void TestVectorUnsignedShortFail()
CPPUNIT_TEST_SUITE_REGISTRATION(testAiresShowerFile)
#define U
std::string BeCloseAbs(const T &value, const double tolerance=kDefaultTolerance)
Definition: Validatrix.h:141
bool Compare(const string &oldFilename, const string &newFilename, const bool failOnFirst)
constexpr double nanosecond
Definition: AugerUnits.h:143
std::string BeLabel(const string &tag)
void Expected()
Print `Expected&#39; for expected failures.
Definition: Verify.h:85
Base class for inconsistency/illogicality exceptions.
Streams(const string &prefix)
std::string BeEqual(const T &value)
Definition: Validatrix.h:131
void TestVectorUnsignedShort()

, generated on Tue Sep 26 2023.