wibble  0.1.28
tests.h
Go to the documentation of this file.
1 #ifndef WIBBLE_TESTS_H
2 #define WIBBLE_TESTS_H
3 
10 #include <string>
11 #include <sstream>
12 
13 #include <wibble/tests/tut.h>
15 
16 
17 #define TESTGRP(name) \
18 typedef test_group<name ## _shar> tg; \
19 typedef tg::object to; \
20 tg name ## _tg (#name);
21 
22 
23 namespace wibble {
24 namespace tests {
25 
26 class Location
27 {
28  std::string file;
29  int line;
30  std::string str;
31  std::string testfile;
32  int testline;
33  std::string teststr;
34 
35 public:
36 
37  Location(const std::string& file, int line, const std::string& str)
38  : file(file), line(line), str(str) {}
39  Location(const Location& loc,
40  const std::string& testfile, int testline, const std::string& str) :
41  file(loc.file), line(loc.line), str(loc.str),
42  testfile(testfile), testline(testline), teststr(str) {}
43 
44  std::string locstr() const;
45  std::string msg(const std::string m) const;
46 };
47 
48 #define ensure(x) wibble::tests::impl_ensure(wibble::tests::Location(__FILE__, __LINE__, #x), (x))
49 #define inner_ensure(x) wibble::tests::impl_ensure(wibble::tests::Location(loc, __FILE__, __LINE__, #x), (x))
50 void impl_ensure(const Location& loc, bool res);
51 
52 #define ensure_equals(x, y) wibble::tests::impl_ensure_equals(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y))
53 #define inner_ensure_equals(x, y) wibble::tests::impl_ensure_equals(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y))
54 
55 template <class Actual,class Expected>
56 void impl_ensure_equals(const Location& loc, const Actual& actual, const Expected& expected)
57 {
58  if( expected != actual )
59  {
60  std::stringstream ss;
61  ss << "expected '" << expected << "' actual '" << actual << "'";
62  throw tut::failure(loc.msg(ss.str()));
63  }
64 }
65 
66 #define ensure_similar(x, y, prec) wibble::tests::impl_ensure_similar(wibble::tests::Location(__FILE__, __LINE__, #x " == " #y), (x), (y), (prec))
67 #define inner_ensure_similar(x, y, prec) wibble::tests::impl_ensure_similar(wibble::tests::Location(loc, __FILE__, __LINE__, #x " == " #y), (x), (y), (prec))
68 
69 template <class Actual, class Expected, class Precision>
70 void impl_ensure_similar(const Location& loc, const Actual& actual, const Expected& expected, const Precision& precision)
71 {
72  if( actual < expected - precision || expected + precision < actual )
73  {
74  std::stringstream ss;
75  ss << "expected '" << expected << "' actual '" << actual << "'";
76  throw tut::failure(loc.msg(ss.str()));
77  }
78 }
79 
80 
81 }
82 }
83 
84 #endif