RDFS
The Rice Comp413 2017 class' continuation on the work of the 2016 RDFS.
 All Classes Namespaces Functions Variables Typedefs Enumerations Enumerator Friends Pages
test.hpp
1 #ifndef HEADER_TEST_TEST_HPP
2 #define HEADER_TEST_TEST_HPP
3 
4 #include "../src/pugixml.hpp"
5 
6 #include <setjmp.h>
7 
8 struct test_runner
9 {
10  test_runner(const char* name)
11  {
12  _name = name;
13  _next = _tests;
14  _tests = this;
15  }
16 
17  virtual ~test_runner() {}
18 
19  virtual void run() = 0;
20 
21  const char* _name;
22  test_runner* _next;
23 
24  static test_runner* _tests;
25  static size_t _memory_fail_threshold;
26  static bool _memory_fail_triggered;
27  static jmp_buf _failure_buffer;
28  static const char* _failure_message;
29 
30  static const char* _temp_path;
31 };
32 
33 bool test_string_equal(const pugi::char_t* lhs, const pugi::char_t* rhs);
34 
35 template <typename Node> inline bool test_node_name_value(const Node& node, const pugi::char_t* name, const pugi::char_t* value)
36 {
37  return test_string_equal(node.name(), name) && test_string_equal(node.value(), value);
38 }
39 
40 bool test_node(const pugi::xml_node& node, const pugi::char_t* contents, const pugi::char_t* indent, unsigned int flags);
41 bool test_double_nan(double value);
42 
43 #ifndef PUGIXML_NO_XPATH
44 bool test_xpath_string(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, const pugi::char_t* expected);
45 bool test_xpath_boolean(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, bool expected);
46 bool test_xpath_number(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables, double expected);
47 bool test_xpath_number_nan(const pugi::xpath_node& node, const pugi::char_t* query, pugi::xpath_variable_set* variables);
48 
49 bool test_xpath_fail_compile(const pugi::char_t* query, pugi::xpath_variable_set* variables);
50 
52 {
53  pugi::xpath_node* document_order;
54  size_t document_size;
55 
56  pugi::xpath_node_set result;
57  unsigned int last;
58  const char* message;
59 
60  void check(bool condition);
61 
62  xpath_node_set_tester(const pugi::xpath_node_set& set, const char* message);
64 
65  xpath_node_set_tester& operator%(unsigned int expected);
66 };
67 
68 #endif
69 
70 struct dummy_fixture {};
71 
72 #define TEST_FIXTURE(name, fixture) \
73  struct test_runner_helper_##name: fixture \
74  { \
75  void run(); \
76  }; \
77  static struct test_runner_##name: test_runner \
78  { \
79  test_runner_##name(): test_runner(#name) {} \
80  \
81  virtual void run() \
82  { \
83  test_runner_helper_##name helper; \
84  helper.run(); \
85  } \
86  } test_runner_instance_##name; \
87  void test_runner_helper_##name::run()
88 
89 #define TEST(name) TEST_FIXTURE(name, dummy_fixture)
90 
91 #define TEST_XML_FLAGS(name, xml, flags) \
92  struct test_fixture_##name \
93  { \
94  pugi::xml_document doc; \
95  \
96  test_fixture_##name() \
97  { \
98  CHECK(doc.load_string(PUGIXML_TEXT(xml), flags)); \
99  } \
100  \
101  private: \
102  test_fixture_##name(const test_fixture_##name&); \
103  test_fixture_##name& operator=(const test_fixture_##name&); \
104  }; \
105  \
106  TEST_FIXTURE(name, test_fixture_##name)
107 
108 #define TEST_XML(name, xml) TEST_XML_FLAGS(name, xml, pugi::parse_default)
109 
110 #define CHECK_JOIN(text, file, line) text " at " file ":" #line
111 #define CHECK_JOIN2(text, file, line) CHECK_JOIN(text, file, line)
112 #define CHECK_TEXT(condition, text) if (condition) ; else test_runner::_failure_message = CHECK_JOIN2(text, __FILE__, __LINE__), longjmp(test_runner::_failure_buffer, 1)
113 #define CHECK_FORCE_FAIL(text) test_runner::_failure_message = CHECK_JOIN2(text, __FILE__, __LINE__), longjmp(test_runner::_failure_buffer, 1)
114 
115 #if (defined(_MSC_VER) && _MSC_VER == 1200) || defined(__MWERKS__) || (defined(__BORLANDC__) && __BORLANDC__ <= 0x540)
116 # define STRINGIZE(value) "??" // Some compilers have issues with stringizing expressions that contain strings w/escaping inside
117 #else
118 # define STRINGIZE(value) #value
119 #endif
120 
121 #define CHECK(condition) CHECK_TEXT(condition, STRINGIZE(condition) " is false")
122 #define CHECK_STRING(value, expected) CHECK_TEXT(test_string_equal(value, expected), STRINGIZE(value) " is not equal to " STRINGIZE(expected))
123 #define CHECK_DOUBLE(value, expected) CHECK_TEXT((value > expected ? value - expected : expected - value) < 1e-6, STRINGIZE(value) " is not equal to " STRINGIZE(expected))
124 #define CHECK_DOUBLE_NAN(value) CHECK_TEXT(test_double_nan(value), STRINGIZE(value) " is not equal to NaN")
125 #define CHECK_NAME_VALUE(node, name, value) CHECK_TEXT(test_node_name_value(node, name, value), STRINGIZE(node) " name/value do not match " STRINGIZE(name) " and " STRINGIZE(value))
126 #define CHECK_NODE_EX(node, expected, indent, flags) CHECK_TEXT(test_node(node, expected, indent, flags), STRINGIZE(node) " contents does not match " STRINGIZE(expected))
127 #define CHECK_NODE(node, expected) CHECK_NODE_EX(node, expected, PUGIXML_TEXT(""), pugi::format_raw)
128 
129 #ifndef PUGIXML_NO_XPATH
130 #define CHECK_XPATH_STRING_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_string(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node))
131 #define CHECK_XPATH_BOOLEAN_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_boolean(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node))
132 #define CHECK_XPATH_NUMBER_VAR(node, query, variables, expected) CHECK_TEXT(test_xpath_number(node, query, variables, expected), STRINGIZE(query) " does not evaluate to " STRINGIZE(expected) " in context " STRINGIZE(node))
133 #define CHECK_XPATH_NUMBER_NAN_VAR(node, query, variables) CHECK_TEXT(test_xpath_number_nan(node, query, variables), STRINGIZE(query) " does not evaluate to NaN in context " STRINGIZE(node))
134 #define CHECK_XPATH_NODESET_VAR(node, query, variables) xpath_node_set_tester(pugi::xpath_query(query, variables).evaluate_node_set(node), CHECK_JOIN2(STRINGIZE(query) " does not evaluate to expected set in context " STRINGIZE(node), __FILE__, __LINE__))
135 #define CHECK_XPATH_FAIL_VAR(query, variables) CHECK_TEXT(test_xpath_fail_compile(query, variables), STRINGIZE(query) " should not compile")
136 
137 #define CHECK_XPATH_STRING(node, query, expected) CHECK_XPATH_STRING_VAR(node, query, 0, expected)
138 #define CHECK_XPATH_BOOLEAN(node, query, expected) CHECK_XPATH_BOOLEAN_VAR(node, query, 0, expected)
139 #define CHECK_XPATH_NUMBER(node, query, expected) CHECK_XPATH_NUMBER_VAR(node, query, 0, expected)
140 #define CHECK_XPATH_NUMBER_NAN(node, query) CHECK_XPATH_NUMBER_NAN_VAR(node, query, 0)
141 #define CHECK_XPATH_NODESET(node, query) CHECK_XPATH_NODESET_VAR(node, query, 0)
142 #define CHECK_XPATH_FAIL(query) CHECK_XPATH_FAIL_VAR(query, 0)
143 #endif
144 
145 #ifdef PUGIXML_NO_EXCEPTIONS
146 #define CHECK_ALLOC_FAIL(code) do { CHECK(!test_runner::_memory_fail_triggered); code; CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false; } while (test_runner::_memory_fail_triggered)
147 #else
148 #define CHECK_ALLOC_FAIL(code) do { CHECK(!test_runner::_memory_fail_triggered); try { code; } catch (std::bad_alloc&) {} CHECK(test_runner::_memory_fail_triggered); test_runner::_memory_fail_triggered = false; } while (test_runner::_memory_fail_triggered)
149 #endif
150 
151 #define STR(text) PUGIXML_TEXT(text)
152 
153 #if defined(__DMC__) || defined(__BORLANDC__)
154 #define U_LITERALS // DMC does not understand \x01234 (it parses first three digits), but understands \u01234
155 #endif
156 
157 #if (defined(_MSC_VER) && _MSC_VER == 1200) || (defined(__INTEL_COMPILER) && __INTEL_COMPILER == 800) || defined(__BORLANDC__)
158 // NaN comparison on MSVC6 is incorrect, see http://www.nabble.com/assertDoubleEquals,-NaN---Microsoft-Visual-Studio-6-td9137859.html
159 // IC8 and BCC are also affected by the same bug
160 # define MSVC6_NAN_BUG
161 #endif
162 
163 inline wchar_t wchar_cast(unsigned int value)
164 {
165  return static_cast<wchar_t>(value); // to avoid C4310 on MSVC
166 }
167 
168 bool is_little_endian();
169 pugi::xml_encoding get_native_encoding();
170 
171 #endif
Definition: test.hpp:8
Definition: test.hpp:51
Definition: pugixml.hpp:1266
Definition: test.hpp:70
Definition: pugixml.hpp:406
Definition: pugixml.hpp:1225
Definition: pugixml.hpp:1089