summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2023-01-26 20:38:52 +0100
committerBent Bisballe Nyeng <deva@aasimon.org>2023-01-26 20:38:52 +0100
commit8c54d41a063a595b8663490b2bbd1d06bac209f7 (patch)
tree96bba4965486d4ed798a6648894e24cd2a61c937
parent33b40f71fcea36987cc30b0f57f85aff1b8e54ff (diff)
Use std::source_location instead of __FILE__ and __LINE__ and add new functions for use instead of macros.source_location
-rw-r--r--examples/ExampleTest.cc107
-rw-r--r--examples/Makefile2
-rw-r--r--uunit.h63
3 files changed, 152 insertions, 20 deletions
diff --git a/examples/ExampleTest.cc b/examples/ExampleTest.cc
index aebe712..09162c7 100644
--- a/examples/ExampleTest.cc
+++ b/examples/ExampleTest.cc
@@ -30,14 +30,115 @@ std::ostream& operator<<(std::ostream& stream, const Speed& speed)
return stream;
}
+
+// New-style (non-macro based) test
class ExampleTest
: public uUnit
{
public:
ExampleTest()
{
- uTEST(ExampleTest::boundaryTests);
- uTEST(ExampleTest::exceptionTests);
+ test(&ExampleTest::boundaryTests, "ExampleTest::boundaryTests");
+ test(&ExampleTest::exceptionTests, "ExampleTest::exceptionTests");
+ }
+
+ void boundaryTests()
+ {
+ assert_equal(Speed::B0, getBaud(0));
+ assert_equal(Speed::B0, getBaud(1));
+ assert_equal(Speed::B0, getBaud(49));
+
+ assert_equal(Speed::B50, getBaud(50));
+ assert_equal(Speed::B50, getBaud(51));
+ assert_equal(Speed::B50, getBaud(74));
+
+ assert_equal(Speed::B75, getBaud(75));
+ assert_equal(Speed::B75, getBaud(76));
+ assert_equal(Speed::B75, getBaud(109));
+
+ assert_equal(Speed::B110, getBaud(110));
+ assert_equal(Speed::B110, getBaud(111));
+ assert_equal(Speed::B110, getBaud(133));
+
+ assert_equal(Speed::B134, getBaud(134));
+ assert_equal(Speed::B134, getBaud(135));
+ assert_equal(Speed::B134, getBaud(149));
+
+ assert_equal(Speed::B150, getBaud(150));
+ assert_equal(Speed::B150, getBaud(151));
+ assert_equal(Speed::B150, getBaud(199));
+
+ assert_equal(Speed::B200, getBaud(200));
+ assert_equal(Speed::B200, getBaud(201));
+ assert_equal(Speed::B200, getBaud(299));
+
+ assert_equal(Speed::B300, getBaud(300));
+ assert_equal(Speed::B300, getBaud(301));
+ assert_equal(Speed::B300, getBaud(599));
+
+ assert_equal(Speed::B600, getBaud(600));
+ assert_equal(Speed::B600, getBaud(601));
+ assert_equal(Speed::B600, getBaud(1199));
+
+ assert_equal(Speed::B1200, getBaud(1200));
+ assert_equal(Speed::B1200, getBaud(1201));
+ assert_equal(Speed::B1200, getBaud(1799));
+
+ assert_equal(Speed::B1800, getBaud(1800));
+ assert_equal(Speed::B1800, getBaud(1801));
+ assert_equal(Speed::B1800, getBaud(2399));
+
+ assert_equal(Speed::B2400, getBaud(2400));
+ assert_equal(Speed::B2400, getBaud(2401));
+ assert_equal(Speed::B2400, getBaud(4799));
+
+ assert_equal(Speed::B4800, getBaud(4800));
+ assert_equal(Speed::B4800, getBaud(4801));
+ assert_equal(Speed::B4800, getBaud(9599));
+
+ assert_equal(Speed::B9600, getBaud(9600));
+ assert_equal(Speed::B9600, getBaud(9601));
+ assert_equal(Speed::B9600, getBaud(19199));
+
+ assert_equal(Speed::B19200, getBaud(19200));
+ assert_equal(Speed::B19200, getBaud(19201));
+ assert_equal(Speed::B19200, getBaud(38399));
+
+ assert_equal(Speed::B38400, getBaud(38400));
+ assert_equal(Speed::B38400, getBaud(38401));
+ assert_equal(Speed::B38400, getBaud(57599));
+
+ assert_equal(Speed::B57600, getBaud(57600));
+ assert_equal(Speed::B57600, getBaud(57601));
+ assert_equal(Speed::B57600, getBaud(115199));
+
+ assert_equal(Speed::B115200, getBaud(115200));
+ assert_equal(Speed::B115200, getBaud(115201));
+ assert_equal(Speed::B115200, getBaud(230399));
+
+ assert_equal(Speed::B230400, getBaud(230400));
+ assert_equal(Speed::B230400, getBaud(230401));
+ }
+
+ void exceptionTests()
+ {
+ assert_throws<bad_speed>([](){getBaud(-1);});
+ }
+};
+
+// Registers the fixture into the 'registry'
+static ExampleTest test;
+
+
+// Old-style (macro-based) test example
+class ExampleTestOldStyle
+ : public uUnit
+{
+public:
+ ExampleTestOldStyle()
+ {
+ uTEST(ExampleTestOldStyle::boundaryTests);
+ uTEST(ExampleTestOldStyle::exceptionTests);
}
void boundaryTests()
@@ -125,4 +226,4 @@ public:
};
// Registers the fixture into the 'registry'
-static ExampleTest test;
+static ExampleTestOldStyle oldStyleTest;
diff --git a/examples/Makefile b/examples/Makefile
index 7ab11fa..2f00198 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -3,7 +3,7 @@ TESTS=\
test_ExampleTest
CXX ?= g++
-CXXFLAGS ?= -g -std=c++17 -Wall -Werror -Wextra -Wconversion -I..
+CXXFLAGS ?= -g -std=c++20 -Wall -Werror -Wextra -Wconversion -I..
all: ${TESTS}
diff --git a/uunit.h b/uunit.h
index ec88873..ec182a8 100644
--- a/uunit.h
+++ b/uunit.h
@@ -15,6 +15,7 @@
#include <cmath>
#include <exception>
#include <typeinfo>
+#include <source_location>
class uUnit
{
@@ -168,8 +169,8 @@ public:
static std::function<void(const char*, const char*, bool)> status_cb;
- static void u_assert(bool value, const char* expr,
- const char* file, std::size_t line)
+ static void assert(bool value, const char* expr,
+ const char* file, std::size_t line)
{
if(!value)
{
@@ -179,10 +180,16 @@ public:
throw test_result{"", file, line, ss.str(), {}, {}};
}
}
- //! Convenience macro to pass along filename and linenumber
- #define uUNIT_ASSERT(value) \
- uUnit::u_assert(value, #value, __FILE__, __LINE__)
- #define uASSERT(...) uUNIT_ASSERT(__VA_ARGS__)
+
+ static void assert(bool value, const char* expr,
+ const std::source_location location =
+ std::source_location::current())
+ {
+ assert(value, expr, location.file_name(), location.line());
+ }
+
+#define uUNIT_ASSERT assert
+#define uASSERT assert
static void assert_equal(double expected, double value,
const char* file, std::size_t line)
@@ -210,10 +217,17 @@ public:
throw test_result{"", file, line, ss.str(), {}, {}};
}
}
- //! Convenience macro to pass along filename and linenumber
- #define uUNIT_ASSERT_EQUAL(expected, value) \
- uUnit::assert_equal(expected, value, __FILE__, __LINE__)
- #define uASSERT_EQUAL(...) uUNIT_ASSERT_EQUAL(__VA_ARGS__)
+
+ template<typename T, typename U>
+ static void assert_equal(const T& expected, const U& value,
+ const std::source_location location =
+ std::source_location::current())
+ {
+ assert_equal(expected, value, location.file_name(), location.line());
+ }
+
+#define uUNIT_ASSERT_EQUAL assert_equal
+#define uASSERT_EQUAL assert_equal
template<typename T>
static void assert_throws(const char* expected, std::function<void()> expr,
@@ -241,9 +255,17 @@ public:
throw test_result{"", file, line, ss.str(), {}, {}};
}
}
- #define uUNIT_ASSERT_THROWS(expected, expr) \
- uUnit::assert_throws<expected>(#expected, [&](){expr;}, __FILE__, __LINE__)
- #define uASSERT_THROWS(...) uUNIT_ASSERT_THROWS(__VA_ARGS__)
+
+ template<typename T>
+ static void assert_throws(std::function<void()> expr,
+ const std::source_location location =
+ std::source_location::current())
+ {
+ assert_throws<T>(typeid(T).name(), expr, location.file_name(), location.line());
+ }
+
+#define uUNIT_ASSERT_THROWS(Exc, Expr) assert_throws<Exc>([](){Expr;})
+#define uASSERT_THROWS(Exc, Expr) assert_throws<Exc>([](){Expr;})
protected:
template<typename O, typename F>
@@ -251,9 +273,18 @@ protected:
{
tests.emplace_back(std::bind(fn, obj), name, file);
}
- #define uUNIT_TEST(func) \
- registerTest(this, &func, #func, __FILE__)
- #define uTEST(...) uUNIT_TEST(__VA_ARGS__)
+
+ template<typename F>
+ void test(void (F::*fn)(), const char* name,
+ const std::source_location location =
+ std::source_location::current())
+ {
+ registerTest((F*)(this), fn, name, location.file_name());
+ }
+
+#define uUNIT_TEST(func) \
+ registerTest(this, &func, #func, __FILE__)
+#define uTEST(...) uUNIT_TEST(__VA_ARGS__)
private:
static std::string sanitize(const std::string& input)