mirror of
https://github.com/thunderbrewhq/squall.git
synced 2025-12-12 02:22:30 +00:00
fix(array): use copy constructor and call destructors appropriately
This commit is contained in:
parent
3492f8b8f2
commit
955a0bbba7
2 changed files with 31 additions and 7 deletions
|
|
@ -53,8 +53,7 @@ void TSFixedArray<T>::ReallocData(uint32_t count) {
|
||||||
|
|
||||||
if (count < this->m_count) {
|
if (count < this->m_count) {
|
||||||
for (uint32_t i = count; i < this->m_count; i++) {
|
for (uint32_t i = count; i < this->m_count; i++) {
|
||||||
T* element = &this->m_data[i];
|
(&this->m_data[i])->~T();
|
||||||
delete element;
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -70,11 +69,8 @@ void TSFixedArray<T>::ReallocData(uint32_t count) {
|
||||||
uint32_t smallestCount = count >= this->m_count ? this->m_count : count;
|
uint32_t smallestCount = count >= this->m_count ? this->m_count : count;
|
||||||
|
|
||||||
for (uint32_t i = 0; i < smallestCount; i++) {
|
for (uint32_t i = 0; i < smallestCount; i++) {
|
||||||
T* v8 = &this->m_data[i];
|
new (&this->m_data[i]) T(oldData[i]);
|
||||||
|
(&oldData[i])->~T();
|
||||||
if (v8) {
|
|
||||||
*v8 = oldData[i];
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
SMemFree(oldData, nullptr, 0, 0x0);
|
SMemFree(oldData, nullptr, 0, 0x0);
|
||||||
|
|
|
||||||
|
|
@ -2,6 +2,10 @@
|
||||||
#include "storm/String.hpp"
|
#include "storm/String.hpp"
|
||||||
#include "test/Test.hpp"
|
#include "test/Test.hpp"
|
||||||
|
|
||||||
|
struct TestArrayObject {
|
||||||
|
uint32_t index = 0;
|
||||||
|
};
|
||||||
|
|
||||||
TEST_CASE("TSBaseArray", "[array]") {
|
TEST_CASE("TSBaseArray", "[array]") {
|
||||||
SECTION("constructs correctly") {
|
SECTION("constructs correctly") {
|
||||||
TSBaseArray<uint32_t> array;
|
TSBaseArray<uint32_t> array;
|
||||||
|
|
@ -59,3 +63,27 @@ TEST_CASE("TSGrowableArray", "[array]") {
|
||||||
REQUIRE(array.Count() == 0);
|
REQUIRE(array.Count() == 0);
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
TEST_CASE("TSGrowableArray::Reserve", "[array]") {
|
||||||
|
SECTION("reserves slot when array is empty") {
|
||||||
|
TSGrowableArray<uint32_t> array;
|
||||||
|
array.Reserve(1, 1);
|
||||||
|
REQUIRE(array.Count() == 0);
|
||||||
|
}
|
||||||
|
|
||||||
|
SECTION("reserves slot when array has elements") {
|
||||||
|
TSGrowableArray<TestArrayObject> 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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue