From eab7e9aeee7e63345f43c472b7b50aed8159ef42 Mon Sep 17 00:00:00 2001 From: szdytom Date: Sun, 28 May 2023 18:54:24 +0800 Subject: [PATCH] add tests for 2d --- .clang-format | 2 +- .gitignore | 4 +++- 2d.hpp | 20 ++++++++++++++------ test/cases/2d.cpp | 20 +++++++++++++++----- test/cases/provider.cpp | 18 ++++++++++++------ test/u.hpp | 27 ++++++++++++++++----------- 6 files changed, 61 insertions(+), 30 deletions(-) diff --git a/.clang-format b/.clang-format index f555efa..a0dfa39 100644 --- a/.clang-format +++ b/.clang-format @@ -15,7 +15,7 @@ AllowShortEnumsOnASingleLine: false AllowShortBlocksOnASingleLine: Empty AllowShortCaseLabelsOnASingleLine: false AllowShortFunctionsOnASingleLine: Empty -AllowShortLambdasOnASingleLine: Empty +AllowShortLambdasOnASingleLine: All AllowShortIfStatementsOnASingleLine: Never AllowShortLoopsOnASingleLine: false AlwaysBreakAfterDefinitionReturnType: None diff --git a/.gitignore b/.gitignore index aec5fd4..e65a3ad 100644 --- a/.gitignore +++ b/.gitignore @@ -9,4 +9,6 @@ # Build .xmake/ -build/ \ No newline at end of file +build/ +*.ilk +*.pdb \ No newline at end of file diff --git a/2d.hpp b/2d.hpp index 731b304..444f967 100644 --- a/2d.hpp +++ b/2d.hpp @@ -25,7 +25,7 @@ struct Circle { Circle() = delete; Circle(const Vec2& o, Float r): o(o), r(r) {} - bool inside(const Vec2& a) const { + bool contain(const Vec2& a) const { return abs(o - a) <= r; } }; @@ -36,6 +36,14 @@ struct Polygon { Polygon() = delete; Polygon(const std::array& v): vertex(v) {} + Polygon(std::initializer_list&& v) { + int i = 0; + for (auto &&x : v) { + if (i >= n) break; + vertex[i] = x; + i += 1; + } + } Vec2& operator[](int i) { return vertex[i]; @@ -45,7 +53,7 @@ struct Polygon { return vertex[i]; } - bool inside(const Vec2& a) const { + bool contain(const Vec2& a) const { Float s = 0; for (int i = 0; i < n; i++) { Vec2 u = vertex[i] - a, w = vertex[(i + 1) % n] - a; @@ -62,13 +70,13 @@ bool intersect(const Circle& a, const Circle& b) { template bool intersect(const Polygon& a, const Polygon& b) { for (int i = 0; i < n; i++) { - if (b.inside(a[i])) { + if (b.contain(a[i])) { return true; } } for (int i = 0; i < m; i++) { - if (a.inside(b[i])) { + if (a.contain(b[i])) { return true; } } @@ -90,12 +98,12 @@ bool intersect(const Polygon& a, const Polygon& b) { template bool intersect(const Polygon& a, const Circle& b) { - if (a.inside(b.o)) { + if (a.contain(b.o)) { return true; } for (int i = 0; i < n; i++) { - if (b.inside(a[i])) { + if (b.contain(a[i])) { return true; } } diff --git a/test/cases/2d.cpp b/test/cases/2d.cpp index 365247b..109d562 100644 --- a/test/cases/2d.cpp +++ b/test/cases/2d.cpp @@ -1,13 +1,23 @@ #include "2d.hpp" #include "test/u.hpp" - #include namespace { -AddTestCase _("2d.hpp", [](TestCase &t) { - t.expectEq([]{ return cross({1, 2}, {4, 1}); }, -7); - t.expectEq([]{ return dot({1, 2}, {4, 1}); }, 6); +AddTestCase _(1, "2d.hpp", [](TestCase& t) { + t.expectEq([] { return cross({1, 2}, {4, 1}); }, -7); + t.expectEq([] { return cross({.5, 2.5}, {4, 1.5}); }, -9.25); + + t.expectEq([] { return dot({1, 2}, {4, 1}); }, 6); + + t.expectTrue([] { return Circle({0, 0}, 2).contain({1, 1}); }); + t.expectTrue([] { return Circle({0, 0}, 1).contain({0, 1}); }); + t.expectFalse([] { return Circle({1, 0}, 1.4).contain({0, -1}); }); + t.expectTrue([] { return Circle({1, 1}, 1.42).contain({0, 0}); }); + + t.expectEq([] { return Polygon<3>{{0, 0}, {1, 0}, {1, 1}}.vertex[0]; }, {0, 0}); + t.expectTrue([] { return Polygon<3>{{0, 0}, {1, 0}, {1, 1}}.contain({.1, .05}); }); + t.expectFalse([] { return Polygon<3>{{0, 0}, {1, 0}, {1, 1}}.contain({.1, .2}); }); }); -} \ No newline at end of file +} diff --git a/test/cases/provider.cpp b/test/cases/provider.cpp index b04e562..a916088 100644 --- a/test/cases/provider.cpp +++ b/test/cases/provider.cpp @@ -1,13 +1,19 @@ #include "test/u.hpp" +#include +#include namespace { -AddTestCase _("tester", [](TestCase &t) { - t.expectTrue([]{ return true; }); - t.expectFalse([]{ return false; }); - t.expectEq([]{ return 42; }, 42); +AddTestCase _(0, "tester", [](TestCase& t) { + t.expectTrue([] { return true; }); + t.expectFalse([] { return false; }); + t.expectTrue([] { + std::this_thread::sleep_for(std::chrono::milliseconds(200)); + return true; + }); + + t.expectEq([] { return 42; }, 42); t.expectEq([] { return 10 + 1e-7; }, 10); t.expectEq([] { return 10 + 1e-10; }, 10); }); - -} \ No newline at end of file +} diff --git a/test/u.hpp b/test/u.hpp index 6106953..059c278 100644 --- a/test/u.hpp +++ b/test/u.hpp @@ -7,8 +7,9 @@ #include #include #include -#include +#include #include +#include template struct TestDefaultEps {}; @@ -30,8 +31,8 @@ struct TestDefaultEps { class TestCase { public: - TestCase(const char* title): ok(0), fail(0), n(0), ended(false) { - std::printf("Testing %s:\n", title); + TestCase(int id, const char* title): ok(0), fail(0), n(0), ended(false), time_used(0) { + std::printf("[%02d]Testing %s:\n", id, title); } void expectTrue(std::function func) noexcept { @@ -97,7 +98,7 @@ public: } else { std::printf("Ok. %d tests passed", ok); } - std::printf(" in %dms.\n", time_used.count()); + std::printf(" in %lldms.\n", static_cast(time_used.count())); } } @@ -160,13 +161,17 @@ public: return instance; } - void addTestCase(const char* title, std::function f) { - tc.emplace_back(title, f); + void addTestCase(int id, const char* title, std::function f) { + tc.emplace_back(id, title, f); } int evalTestCases() { - for (auto [title, func] : tc) { - TestCase t(title); + std::sort(tc.begin(), tc.end(), [](const auto &x, const auto &y) { + return std::get<0>(x) < std::get<0>(y); + }); + + for (auto [id, title, func] : tc) { + TestCase t(id, title); func(t); t.end(); std::putchar('\n'); @@ -182,7 +187,7 @@ private: static inline TestSuit* instance = nullptr; TestSuit() {} - std::vector>> tc; + std::vector>> tc; }; class AddTestCase { @@ -191,8 +196,8 @@ class AddTestCase { AddTestCase(AddTestCase&&) = delete; public: - AddTestCase(const char* tilte, std::function func) { - TestSuit::getInstance()->addTestCase(tilte, func); + AddTestCase(int id, const char* tilte, std::function func) { + TestSuit::getInstance()->addTestCase(id, tilte, func); } };