add tests for 2d
This commit is contained in:
parent
678ceb72bb
commit
eab7e9aeee
@ -15,7 +15,7 @@ AllowShortEnumsOnASingleLine: false
|
|||||||
AllowShortBlocksOnASingleLine: Empty
|
AllowShortBlocksOnASingleLine: Empty
|
||||||
AllowShortCaseLabelsOnASingleLine: false
|
AllowShortCaseLabelsOnASingleLine: false
|
||||||
AllowShortFunctionsOnASingleLine: Empty
|
AllowShortFunctionsOnASingleLine: Empty
|
||||||
AllowShortLambdasOnASingleLine: Empty
|
AllowShortLambdasOnASingleLine: All
|
||||||
AllowShortIfStatementsOnASingleLine: Never
|
AllowShortIfStatementsOnASingleLine: Never
|
||||||
AllowShortLoopsOnASingleLine: false
|
AllowShortLoopsOnASingleLine: false
|
||||||
AlwaysBreakAfterDefinitionReturnType: None
|
AlwaysBreakAfterDefinitionReturnType: None
|
||||||
|
2
.gitignore
vendored
2
.gitignore
vendored
@ -10,3 +10,5 @@
|
|||||||
# Build
|
# Build
|
||||||
.xmake/
|
.xmake/
|
||||||
build/
|
build/
|
||||||
|
*.ilk
|
||||||
|
*.pdb
|
20
2d.hpp
20
2d.hpp
@ -25,7 +25,7 @@ struct Circle {
|
|||||||
Circle() = delete;
|
Circle() = delete;
|
||||||
Circle(const Vec2& o, Float r): o(o), r(r) {}
|
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;
|
return abs(o - a) <= r;
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
@ -36,6 +36,14 @@ struct Polygon {
|
|||||||
|
|
||||||
Polygon() = delete;
|
Polygon() = delete;
|
||||||
Polygon(const std::array<Vec2, n>& v): vertex(v) {}
|
Polygon(const std::array<Vec2, n>& v): vertex(v) {}
|
||||||
|
Polygon(std::initializer_list<Vec2>&& v) {
|
||||||
|
int i = 0;
|
||||||
|
for (auto &&x : v) {
|
||||||
|
if (i >= n) break;
|
||||||
|
vertex[i] = x;
|
||||||
|
i += 1;
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
Vec2& operator[](int i) {
|
Vec2& operator[](int i) {
|
||||||
return vertex[i];
|
return vertex[i];
|
||||||
@ -45,7 +53,7 @@ struct Polygon {
|
|||||||
return vertex[i];
|
return vertex[i];
|
||||||
}
|
}
|
||||||
|
|
||||||
bool inside(const Vec2& a) const {
|
bool contain(const Vec2& a) const {
|
||||||
Float s = 0;
|
Float s = 0;
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
Vec2 u = vertex[i] - a, w = vertex[(i + 1) % n] - a;
|
Vec2 u = vertex[i] - a, w = vertex[(i + 1) % n] - a;
|
||||||
@ -62,13 +70,13 @@ bool intersect(const Circle& a, const Circle& b) {
|
|||||||
template<int n, int m>
|
template<int n, int m>
|
||||||
bool intersect(const Polygon<n>& a, const Polygon<m>& b) {
|
bool intersect(const Polygon<n>& a, const Polygon<m>& b) {
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
if (b.inside(a[i])) {
|
if (b.contain(a[i])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < m; i++) {
|
for (int i = 0; i < m; i++) {
|
||||||
if (a.inside(b[i])) {
|
if (a.contain(b[i])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -90,12 +98,12 @@ bool intersect(const Polygon<n>& a, const Polygon<m>& b) {
|
|||||||
|
|
||||||
template<int n>
|
template<int n>
|
||||||
bool intersect(const Polygon<n>& a, const Circle& b) {
|
bool intersect(const Polygon<n>& a, const Circle& b) {
|
||||||
if (a.inside(b.o)) {
|
if (a.contain(b.o)) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
for (int i = 0; i < n; i++) {
|
for (int i = 0; i < n; i++) {
|
||||||
if (b.inside(a[i])) {
|
if (b.contain(a[i])) {
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -1,13 +1,23 @@
|
|||||||
#include "2d.hpp"
|
#include "2d.hpp"
|
||||||
#include "test/u.hpp"
|
#include "test/u.hpp"
|
||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
AddTestCase _("2d.hpp", [](TestCase &t) {
|
AddTestCase _(1, "2d.hpp", [](TestCase& t) {
|
||||||
t.expectEq<Float>([] { return cross({1, 2}, {4, 1}); }, -7);
|
t.expectEq<Float>([] { return cross({1, 2}, {4, 1}); }, -7);
|
||||||
|
t.expectEq<Float>([] { return cross({.5, 2.5}, {4, 1.5}); }, -9.25);
|
||||||
|
|
||||||
t.expectEq<Float>([] { return dot({1, 2}, {4, 1}); }, 6);
|
t.expectEq<Float>([] { 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<Vec2>([] { 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}); });
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
@ -1,13 +1,19 @@
|
|||||||
#include "test/u.hpp"
|
#include "test/u.hpp"
|
||||||
|
#include <chrono>
|
||||||
|
#include <thread>
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
AddTestCase _("tester", [](TestCase &t) {
|
AddTestCase _(0, "tester", [](TestCase& t) {
|
||||||
t.expectTrue([] { return true; });
|
t.expectTrue([] { return true; });
|
||||||
t.expectFalse([] { return false; });
|
t.expectFalse([] { return false; });
|
||||||
|
t.expectTrue([] {
|
||||||
|
std::this_thread::sleep_for(std::chrono::milliseconds(200));
|
||||||
|
return true;
|
||||||
|
});
|
||||||
|
|
||||||
t.expectEq<int>([] { return 42; }, 42);
|
t.expectEq<int>([] { return 42; }, 42);
|
||||||
t.expectEq<float>([] { return 10 + 1e-7; }, 10);
|
t.expectEq<float>([] { return 10 + 1e-7; }, 10);
|
||||||
t.expectEq<double>([] { return 10 + 1e-10; }, 10);
|
t.expectEq<double>([] { return 10 + 1e-10; }, 10);
|
||||||
});
|
});
|
||||||
|
|
||||||
}
|
}
|
27
test/u.hpp
27
test/u.hpp
@ -7,8 +7,9 @@
|
|||||||
#include <functional>
|
#include <functional>
|
||||||
#include <optional>
|
#include <optional>
|
||||||
#include <type_traits>
|
#include <type_traits>
|
||||||
#include <utility>
|
#include <tuple>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
|
#include <algorithm>
|
||||||
|
|
||||||
template<typename T>
|
template<typename T>
|
||||||
struct TestDefaultEps {};
|
struct TestDefaultEps {};
|
||||||
@ -30,8 +31,8 @@ struct TestDefaultEps<long double> {
|
|||||||
|
|
||||||
class TestCase {
|
class TestCase {
|
||||||
public:
|
public:
|
||||||
TestCase(const char* title): ok(0), fail(0), n(0), ended(false) {
|
TestCase(int id, const char* title): ok(0), fail(0), n(0), ended(false), time_used(0) {
|
||||||
std::printf("Testing %s:\n", title);
|
std::printf("[%02d]Testing %s:\n", id, title);
|
||||||
}
|
}
|
||||||
|
|
||||||
void expectTrue(std::function<bool()> func) noexcept {
|
void expectTrue(std::function<bool()> func) noexcept {
|
||||||
@ -97,7 +98,7 @@ public:
|
|||||||
} else {
|
} else {
|
||||||
std::printf("Ok. %d tests passed", ok);
|
std::printf("Ok. %d tests passed", ok);
|
||||||
}
|
}
|
||||||
std::printf(" in %dms.\n", time_used.count());
|
std::printf(" in %lldms.\n", static_cast<long long>(time_used.count()));
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@ -160,13 +161,17 @@ public:
|
|||||||
return instance;
|
return instance;
|
||||||
}
|
}
|
||||||
|
|
||||||
void addTestCase(const char* title, std::function<void(TestCase&)> f) {
|
void addTestCase(int id, const char* title, std::function<void(TestCase&)> f) {
|
||||||
tc.emplace_back(title, f);
|
tc.emplace_back(id, title, f);
|
||||||
}
|
}
|
||||||
|
|
||||||
int evalTestCases() {
|
int evalTestCases() {
|
||||||
for (auto [title, func] : tc) {
|
std::sort(tc.begin(), tc.end(), [](const auto &x, const auto &y) {
|
||||||
TestCase t(title);
|
return std::get<0>(x) < std::get<0>(y);
|
||||||
|
});
|
||||||
|
|
||||||
|
for (auto [id, title, func] : tc) {
|
||||||
|
TestCase t(id, title);
|
||||||
func(t);
|
func(t);
|
||||||
t.end();
|
t.end();
|
||||||
std::putchar('\n');
|
std::putchar('\n');
|
||||||
@ -182,7 +187,7 @@ private:
|
|||||||
static inline TestSuit* instance = nullptr;
|
static inline TestSuit* instance = nullptr;
|
||||||
TestSuit() {}
|
TestSuit() {}
|
||||||
|
|
||||||
std::vector<std::pair<const char*, std::function<void(TestCase&)>>> tc;
|
std::vector<std::tuple<int, const char*, std::function<void(TestCase&)>>> tc;
|
||||||
};
|
};
|
||||||
|
|
||||||
class AddTestCase {
|
class AddTestCase {
|
||||||
@ -191,8 +196,8 @@ class AddTestCase {
|
|||||||
AddTestCase(AddTestCase&&) = delete;
|
AddTestCase(AddTestCase&&) = delete;
|
||||||
|
|
||||||
public:
|
public:
|
||||||
AddTestCase(const char* tilte, std::function<void(TestCase&)> func) {
|
AddTestCase(int id, const char* tilte, std::function<void(TestCase&)> func) {
|
||||||
TestSuit::getInstance()->addTestCase(tilte, func);
|
TestSuit::getInstance()->addTestCase(id, tilte, func);
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
Loading…
x
Reference in New Issue
Block a user