From 955a0bbba7c3dadc13408c78cf7e656c270a14e7 Mon Sep 17 00:00:00 2001 From: fallenoak Date: Sun, 5 Feb 2023 21:36:49 -0600 Subject: [PATCH] fix(array): use copy constructor and call destructors appropriately --- storm/array/TSFixedArray.hpp | 10 +++------- test/Array.cpp | 28 ++++++++++++++++++++++++++++ 2 files changed, 31 insertions(+), 7 deletions(-) diff --git a/storm/array/TSFixedArray.hpp b/storm/array/TSFixedArray.hpp index d9527d3..592372f 100644 --- a/storm/array/TSFixedArray.hpp +++ b/storm/array/TSFixedArray.hpp @@ -53,8 +53,7 @@ void TSFixedArray::ReallocData(uint32_t count) { if (count < this->m_count) { for (uint32_t i = count; i < this->m_count; i++) { - T* element = &this->m_data[i]; - delete element; + (&this->m_data[i])->~T(); } } @@ -70,11 +69,8 @@ void TSFixedArray::ReallocData(uint32_t count) { uint32_t smallestCount = count >= this->m_count ? this->m_count : count; for (uint32_t i = 0; i < smallestCount; i++) { - T* v8 = &this->m_data[i]; - - if (v8) { - *v8 = oldData[i]; - } + new (&this->m_data[i]) T(oldData[i]); + (&oldData[i])->~T(); } SMemFree(oldData, nullptr, 0, 0x0); diff --git a/test/Array.cpp b/test/Array.cpp index 9771cc0..b7887d6 100644 --- a/test/Array.cpp +++ b/test/Array.cpp @@ -2,6 +2,10 @@ #include "storm/String.hpp" #include "test/Test.hpp" +struct TestArrayObject { + uint32_t index = 0; +}; + TEST_CASE("TSBaseArray", "[array]") { SECTION("constructs correctly") { TSBaseArray array; @@ -59,3 +63,27 @@ TEST_CASE("TSGrowableArray", "[array]") { REQUIRE(array.Count() == 0); } } + +TEST_CASE("TSGrowableArray::Reserve", "[array]") { + SECTION("reserves slot when array is empty") { + TSGrowableArray array; + array.Reserve(1, 1); + REQUIRE(array.Count() == 0); + } + + SECTION("reserves slot when array has elements") { + TSGrowableArray array; + + auto elementA = array.New(); + elementA->index = 10; + + array.Reserve(1, 1); + + auto elementB = array.New(); + elementB->index = 20; + + REQUIRE(array.Count() == 2); + REQUIRE(array[0].index == 10); + REQUIRE(array[1].index == 20); + } +}