Require types used with construct are trivially destructible
This commit is contained in:
@@ -1,5 +1,6 @@
|
||||
#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
|
||||
#include "arena_allocator.hpp"
|
||||
#include <cstring>
|
||||
#include <doctest/doctest.h>
|
||||
#include <string>
|
||||
#include <vector>
|
||||
@@ -106,10 +107,17 @@ TEST_CASE("ArenaAllocator construct template") {
|
||||
CHECK(*ptr == 42);
|
||||
}
|
||||
|
||||
SUBCASE("construct string") {
|
||||
std::string *ptr = arena.construct<std::string>("hello world");
|
||||
SUBCASE("construct array") {
|
||||
struct FixedString {
|
||||
char data[16];
|
||||
FixedString(const char *str) {
|
||||
std::strncpy(data, str, sizeof(data) - 1);
|
||||
data[sizeof(data) - 1] = '\0';
|
||||
}
|
||||
};
|
||||
FixedString *ptr = arena.construct<FixedString>("hello world");
|
||||
CHECK(ptr != nullptr);
|
||||
CHECK(*ptr == "hello world");
|
||||
CHECK(std::strcmp(ptr->data, "hello world") == 0);
|
||||
}
|
||||
|
||||
SUBCASE("construct multiple objects") {
|
||||
@@ -122,10 +130,15 @@ TEST_CASE("ArenaAllocator construct template") {
|
||||
}
|
||||
|
||||
SUBCASE("construct with multiple arguments") {
|
||||
auto *ptr = arena.construct<std::pair<int, std::string>>(42, "test");
|
||||
struct IntPair {
|
||||
int first;
|
||||
int second;
|
||||
IntPair(int a, int b) : first(a), second(b) {}
|
||||
};
|
||||
auto *ptr = arena.construct<IntPair>(42, 24);
|
||||
CHECK(ptr != nullptr);
|
||||
CHECK(ptr->first == 42);
|
||||
CHECK(ptr->second == "test");
|
||||
CHECK(ptr->second == 24);
|
||||
}
|
||||
}
|
||||
|
||||
@@ -266,27 +279,29 @@ TEST_CASE("ArenaAllocator edge cases") {
|
||||
}
|
||||
}
|
||||
|
||||
struct TestObject {
|
||||
struct TestPOD {
|
||||
int value;
|
||||
std::string name;
|
||||
char name[16];
|
||||
|
||||
TestObject(int v, const std::string &n) : value(v), name(n) {}
|
||||
~TestObject() = default;
|
||||
TestPOD(int v, const char *n) : value(v) {
|
||||
std::strncpy(name, n, sizeof(name) - 1);
|
||||
name[sizeof(name) - 1] = '\0';
|
||||
}
|
||||
};
|
||||
|
||||
TEST_CASE("ArenaAllocator with custom objects") {
|
||||
ArenaAllocator arena;
|
||||
|
||||
TestObject *obj1 = arena.construct<TestObject>(42, "first");
|
||||
TestObject *obj2 = arena.construct<TestObject>(84, "second");
|
||||
TestPOD *obj1 = arena.construct<TestPOD>(42, "first");
|
||||
TestPOD *obj2 = arena.construct<TestPOD>(84, "second");
|
||||
|
||||
CHECK(obj1 != nullptr);
|
||||
CHECK(obj2 != nullptr);
|
||||
CHECK(obj1 != obj2);
|
||||
CHECK(obj1->value == 42);
|
||||
CHECK(obj1->name == "first");
|
||||
CHECK(std::strcmp(obj1->name, "first") == 0);
|
||||
CHECK(obj2->value == 84);
|
||||
CHECK(obj2->name == "second");
|
||||
CHECK(std::strcmp(obj2->name, "second") == 0);
|
||||
}
|
||||
|
||||
TEST_CASE("ArenaAllocator geometric growth policy") {
|
||||
|
||||
Reference in New Issue
Block a user