From e46cf7b5489213ac0f8941c870121b04cf6091ac Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Sep 2020 14:21:22 +0200 Subject: Import uunit from drumgizmo project - renamed and relicensed. --- uunit.cc | 16 +++++ uunit.h | 227 +++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++++ 2 files changed, 243 insertions(+) create mode 100644 uunit.cc create mode 100644 uunit.h diff --git a/uunit.cc b/uunit.cc new file mode 100644 index 0000000..609310a --- /dev/null +++ b/uunit.cc @@ -0,0 +1,16 @@ +// -*- c++ -*- +// This is the uUnit main file. +// Copyright 2020 Bent Bisballe Nyeng (deva@aasimon.org) +// This file released under the CC0-1.0 license. See CC0-1.0 file for details. + +#define uUNIT_MAIN +#include "uunit.h" + +#include + +int main(int argc, char* argv[]) +{ + std::ofstream xmlfile; + xmlfile.open("result_" OUTPUT ".xml"); + return uUnit::runTests(xmlfile); +} diff --git a/uunit.h b/uunit.h new file mode 100644 index 0000000..de9c93e --- /dev/null +++ b/uunit.h @@ -0,0 +1,227 @@ +// -*- c++ -*- +// This is the header-only implementation of the uUnit unit-test framework. +// Copyright 2020 Bent Bisballe Nyeng (deva@aasimon.org) +// This file released under the CC0-1.0 license. See CC0-1.0 file for details. +#pragma once + +#include +#include +#include +#include +#include +#include +#include +#include +#include + +class uUnit +{ +public: + uUnit() + { + if(uUnit::suite_list == nullptr) + { + uUnit::suite_list = this; + return; + } + + auto unit = uUnit::suite_list; + while(unit->next_unit) + { + unit = unit->next_unit; + } + unit->next_unit = this; + } + + virtual ~uUnit() = default; + + //! Overload to prepare stuff for each of the tests. + virtual void setup() {} + + //! Overload to tear down stuff for each of the tests. + virtual void teardown() {} + + struct test_result + { + std::string func; + std::string file; + std::size_t line; + std::string msg; + int id; + }; + + //! Run test + //! \param test_suite the name of a test suite or null for all. + //! \param test_name the name of a test name inside a test suite. Only valid + //! if test_suite is non-null. nullptr for all tests. + static int runTests(std::ofstream& out) + { + std::size_t test_num{0}; + std::size_t failed{0}; + + std::list failed_tests; + std::list successful_tests; + + for(auto suite = uUnit::suite_list; suite; suite = suite->next_unit) + { + for(auto test : suite->tests) + { + ++test_num; + try + { + suite->setup(); + test.first(); + suite->teardown(); + } + catch(test_result& result) + { + std::cout << "F"; + fflush(stdout); + result.id = test_num; + result.func = test.second; + failed_tests.push_back(result); + ++failed; + continue; + } + catch(...) + { + break; // Uncaught exception. Do not proceed with this test. + } + std::cout << "."; + fflush(stdout); + test_result result{test.second}; + result.id = test_num; + successful_tests.push_back(result); + } + } + + out << "" << + std::endl; + out << "" << std::endl; + out << " " << std::endl; + for(auto test : failed_tests) + { + out << " " << std::endl; + out << " " << sanitize(test.func) << "" << std::endl; + out << " Assertion" << std::endl; + out << " " << std::endl; + out << " " << sanitize(test.file) << "" << std::endl; + out << " " << test.line << "" << std::endl; + out << " " << std::endl; + out << " " << sanitize(test.msg) << "" << + std::endl; + out << " " << std::endl; + } + out << " " << std::endl; + out << " " << std::endl; + for(auto test : successful_tests) + { + out << " " << std::endl; + out << " " << sanitize(test.func) << "" << std::endl; + out << " " << std::endl; + + } + out << " " << std::endl; + out << " " << std::endl; + out << " " << (successful_tests.size() + failed_tests.size()) << + "" << std::endl; + out << " " << failed_tests.size() << "" << + std::endl; + out << " 0" << std::endl; + out << " " << failed_tests.size() << "" << + std::endl; + out << " " << std::endl; + out << "" << std::endl; + + return failed == 0 ? 0 : 1; + } + +protected: + template + void registerTest(O* obj, const F& fn, const char* name) + { + tests.emplace_back(std::make_pair(std::bind(fn, obj), name)); + } + #define uUNIT_TEST(func) \ + registerTest(this, &func, #func) + + void u_assert(bool value, const char* expr, + const char* file, std::size_t line) + { + if(!value) + { + std::stringstream ss; + ss << "assertion failed" << std::endl << + "- Expression: " << expr << "" << std::endl; + throw test_result{"", file, line, ss.str()}; + } + } + //! Convenience macro to pass along filename and linenumber + #define uUNIT_ASSERT(value) \ + u_assert(value, #value, __FILE__, __LINE__) + + void assert_equal(double expected, double value, + const char* file, std::size_t line) + { + if(std::fabs(expected - value) > 0.0000001) + { + std::stringstream ss; + ss << "equality assertion failed" << std::endl << + "- Expected: " << expected << "" << std::endl << + "- Actual : " << value << "" << std::endl; + throw test_result{"", file, line, ss.str()}; + } + } + template + void assert_equal(T expected, T value, + const char* file, std::size_t line) + { + if(expected != value) + { + std::stringstream ss; + ss << "equality assertion failed" << std::endl << + "- Expected: " << expected << "" << std::endl << + "- Actual : " << value << "" << std::endl; + throw test_result{"", file, line, ss.str()}; + } + } + //! Convenience macro to pass along filename and linenumber + #define uUNIT_ASSERT_EQUAL(expected, value) \ + assert_equal(expected, value, __FILE__, __LINE__) + +private: + static std::string sanitize(const std::string& input) + { + std::string output; + for(auto c : input) + { + switch(c) + { + case '"': + output += """; + break; + case '&': + output += "&"; + break; + case '<': + output += "<"; + break; + case '>': + output += ">"; + break; + default: + output += c; + break; + } + } + return output; + } + + static uUnit* suite_list; + uUnit* next_unit{nullptr}; + std::vector, const char*>> tests; +}; + +#ifdef uUNIT_MAIN +uUnit* uUnit::suite_list{nullptr}; +#endif -- cgit v1.2.3