From 2b00def7af3a694f8e7634437c8ed367a87a9a4c Mon Sep 17 00:00:00 2001 From: kai Date: Sat, 14 Jun 2025 12:58:38 +0200 Subject: [PATCH] format --- lab11/game_of_life/test.h | 75 ++++++++++++++++++--------------------- 1 file changed, 34 insertions(+), 41 deletions(-) diff --git a/lab11/game_of_life/test.h b/lab11/game_of_life/test.h index 151a70b..e87b0e4 100644 --- a/lab11/game_of_life/test.h +++ b/lab11/game_of_life/test.h @@ -77,10 +77,10 @@ * // test code * } */ -#define TEST(name) \ - struct _TestClass##name { \ - _TestClass##name(); \ - } _TestClass##name##Instance; \ +#define TEST(name) \ + struct _TestClass##name { \ + _TestClass##name(); \ + } _TestClass##name##Instance; \ _TestClass##name::_TestClass##name() // Use a namespace to hide implementation details @@ -94,17 +94,16 @@ namespace Test::Detail { * the function is not generated. */ template -std::ostream& operator<<( - typename std::enable_if::value, std::ostream>::type& stream, - const T& e) { +std::ostream &operator<<( + typename std::enable_if::value, std::ostream>::type &stream, + const T &e) { return stream << static_cast::type>(e); } /** * Convert anything to a string. */ -template -std::string toString(const T& t) { +template std::string toString(const T &t) { std::ostringstream ss; ss << std::setprecision(10); ss << t; @@ -114,16 +113,14 @@ std::string toString(const T& t) { /** * Convert bools to string "true" or "false" instead of 0 and 1 */ -template <> -inline std::string toString(const bool& b) { +template <> inline std::string toString(const bool &b) { return b ? "\"true\"" : "\"false\""; } /** * Comparison function for different types */ -template -bool isEqual(const T& t1, const T& t2) { +template bool isEqual(const T &t1, const T &t2) { return t1 == t2; } @@ -131,8 +128,8 @@ bool isEqual(const T& t1, const T& t2) { * Double values are equal if they differ no more than 1e-8 */ template <> -inline bool isEqual(const double& expectedValue, - const double& actualValue) { +inline bool isEqual(const double &expectedValue, + const double &actualValue) { const double epsilon = 1e-4; const double distance = fabs(actualValue - expectedValue); return (distance < epsilon); @@ -142,8 +139,8 @@ inline bool isEqual(const double& expectedValue, * Float values are equal if they differ no more than 1e-4 */ template <> -inline bool isEqual(const float& expectedValue, - const float& actualValue) { +inline bool isEqual(const float &expectedValue, + const float &actualValue) { const double epsilon = 1e-4; const double distance = fabs(actualValue - expectedValue); return (distance < epsilon); @@ -157,11 +154,11 @@ inline bool isEqual(const float& expectedValue, * the result of each test on the command line. */ class Test { - public: +public: /** * Test class is a Singleton */ - static Test& instance() { + static Test &instance() { static Test test; return test; } @@ -171,7 +168,7 @@ class Test { * result. */ template - bool check(const T& expectedValue, const T& actualValue) { + bool check(const T &expectedValue, const T &actualValue) { bool testResult = isEqual(expectedValue, actualValue); if (testResult == true) { registerPassingTest(); @@ -193,7 +190,7 @@ class Test { return testResult; } - private: +private: /** * Print a summary of all tests at the end of program execution. * @@ -225,9 +222,8 @@ class Test { std::atomic numFailedTests_ = 0; }; -template -class InstanceCounterHelper { - public: +template class InstanceCounterHelper { +public: ~InstanceCounterHelper() { std::cout << "The remaining number of objects of type " << typeid(T).name() << " at the end of the program is " << count; @@ -244,12 +240,12 @@ class InstanceCounterHelper { void decrement() { count--; } - private: +private: std::atomic count = 0; std::atomic total = 0; }; -} // namespace Test::Detail +} // namespace Test::Detail /** * Count the instances of a class T. @@ -257,18 +253,17 @@ class InstanceCounterHelper { * To use it, inherit T from InstanceCounter, e.g. * class MyClass : InstanceCounter */ -template -class InstanceCounter { - public: +template class InstanceCounter { +public: InstanceCounter() { counter().increment(); } - InstanceCounter(const InstanceCounter&) { counter().increment(); } + InstanceCounter(const InstanceCounter &) { counter().increment(); } - InstanceCounter(const InstanceCounter&&) { counter().increment(); } + InstanceCounter(const InstanceCounter &&) { counter().increment(); } virtual ~InstanceCounter() { counter().decrement(); } - Test::Detail::InstanceCounterHelper& counter() { + Test::Detail::InstanceCounterHelper &counter() { static Test::Detail::InstanceCounterHelper c; return c; } @@ -280,16 +275,16 @@ class InstanceCounter { * summary of all tests is printed. */ template -void check(const T1& actualValue, const T2& expectedValue) { - const T1& expectedValueCasted{ - expectedValue}; // allows conversion in general, but avoids narrowing - // conversion +void check(const T1 &actualValue, const T2 &expectedValue) { + const T1 &expectedValueCasted{ + expectedValue}; // allows conversion in general, but avoids narrowing + // conversion Test::Detail::Test::instance().check(expectedValueCasted, actualValue); } // allow conversion from int to double explicitely template <> -inline void check(const double& actualValue, const int& expectedValue) { +inline void check(const double &actualValue, const int &expectedValue) { Test::Detail::Test::instance().check(static_cast(expectedValue), actualValue); } @@ -299,11 +294,9 @@ inline void check(const double& actualValue, const int& expectedValue) { * Result is printed on the command line and at the end of the program, a * summary of all tests is printed. */ -inline void check(bool a) { - Test::Detail::Test::instance().check(true, a); -} +inline void check(bool a) { Test::Detail::Test::instance().check(true, a); } -#endif // VERY_SIMPLE_TEST_H +#endif // VERY_SIMPLE_TEST_H /** * V1.0: Creation of framework