25
#include "precompiled.hpp"
26
#include "memory/allocation.inline.hpp"
27
#include "unittest.hpp"
29
using Element = struct TestArrayAllocatorElement {
34
static void fill(Element* elements, int start, int size) {
35
for (int i = 0; i < size; i++) {
36
new (&elements[start + i]) Element{0.0, start + i};
40
static Element* allocate_and_fill(int size) {
41
Element* const elements = MallocArrayAllocator<Element>::allocate(size, mtTest);
43
fill(elements, 0, size);
48
TEST_VM(ArrayAllocator, allocate) {
51
Element* const elements = allocate_and_fill(size);
53
for (int i = 0; i < size; i++) {
54
ASSERT_EQ(elements[i].b, i);
57
MallocArrayAllocator<Element>::free(elements);
60
TEST_VM(ArrayAllocator, reallocate_0) {
63
Element* const elements = allocate_and_fill(size);
65
Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, 0, mtTest);
66
ASSERT_NE(ret, nullptr) << "We've chosen to NOT return nullptr when reallcting with 0";
68
MallocArrayAllocator<Element>::free(ret);
71
TEST_VM(ArrayAllocator, reallocate_shrink) {
74
Element* const elements = allocate_and_fill(size);
76
Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, size / 2, mtTest);
78
for (int i = 0; i < size / 2; i++) {
79
ASSERT_EQ(ret[i].b, i);
82
MallocArrayAllocator<Element>::free(ret);
85
TEST_VM(ArrayAllocator, reallocate_grow) {
88
Element* const elements = allocate_and_fill(size);
90
Element* const ret = MallocArrayAllocator<Element>::reallocate(elements, size * 2, mtTest);
92
fill(ret, size, size);
94
for (int i = 0; i < size * 2; i++) {
95
ASSERT_EQ(ret[i].b, i);
98
MallocArrayAllocator<Element>::free(ret);