aboutsummaryrefslogtreecommitdiff
path: root/tests/include
diff options
context:
space:
mode:
authorMark Haines <mjark@negativecurvature.net>2015-02-24 11:28:07 +0000
committerMark Haines <mjark@negativecurvature.net>2015-02-24 11:28:07 +0000
commit0e13cd35626e58fee436b390a523c53effab8d8c (patch)
treedee167b2c8bbde23c0f300e2ed7fe9dad995549d /tests/include
parent7c1da4896fe216cad5337996e11e8023512dcc69 (diff)
Move unit test code into a separate header
Diffstat (limited to 'tests/include')
-rw-r--r--tests/include/unittest.hh55
1 files changed, 55 insertions, 0 deletions
diff --git a/tests/include/unittest.hh b/tests/include/unittest.hh
new file mode 100644
index 0000000..65ccc34
--- /dev/null
+++ b/tests/include/unittest.hh
@@ -0,0 +1,55 @@
+#include <cstring>
+#include <iostream>
+#include <iomanip>
+#include <cstdlib>
+
+
+std::ostream & print_hex(
+ std::ostream & os,
+ std::uint8_t const * data,
+ std::size_t length
+) {
+ for (std::size_t i = 0; i < length; i++) {
+ os << std::setw(2) << std::setfill('0') << std::right
+ << std::hex << (int) data[i];
+ }
+ return os;
+}
+
+
+char const * TEST_CASE;
+
+
+template<typename T>
+void assert_equals(
+ const T & expected,
+ const T & actual
+) {
+ if (expected != actual) {
+ std::cout << "FAILED: " << TEST_CASE << std::endl;
+ std::cout << "Expected: " << expected << std::endl;
+ std::cout << "Actual: " << actual << std::endl;
+ std::exit(1);
+ }
+}
+
+
+void assert_equals(
+ std::uint8_t *expected,
+ std::uint8_t *actual,
+ std::size_t length
+) {
+ if (std::memcmp(expected, actual, length)) {
+ std::cout << "FAILED: " << TEST_CASE << std::endl;
+ print_hex(std::cout << "Expected: ", expected, length) << std::endl;
+ print_hex(std::cout << "Actual: ", actual, length) << std::endl;
+ std::exit(1);
+ }
+}
+
+
+class TestCase {
+public:
+ TestCase(const char *name) { TEST_CASE = name; }
+ ~TestCase() { std::cout << "PASSED: " << TEST_CASE << std::endl; }
+};