diff --git a/storm/array/TSBaseArray.hpp b/storm/array/TSBaseArray.hpp index 2be5ebd..54d6aaa 100644 --- a/storm/array/TSBaseArray.hpp +++ b/storm/array/TSBaseArray.hpp @@ -21,6 +21,7 @@ class TSBaseArray { void Clear(); T* Ptr(); const T* Ptr() const; + T* Top(); }; template @@ -77,4 +78,13 @@ const T* TSBaseArray::Ptr() const { return this->m_data; } +template +T* TSBaseArray::Top() { + if (this->m_count == 0) { + return nullptr; + } + + return &this->m_data[this->m_count - 1]; +} + #endif diff --git a/test/Array.cpp b/test/Array.cpp index c0dd2c4..9771cc0 100644 --- a/test/Array.cpp +++ b/test/Array.cpp @@ -30,6 +30,29 @@ TEST_CASE("TSFixedArray", "[array]") { } } +TEST_CASE("TSFixedArray::Top", "[array]") { + SECTION("returns nullptr when array has no elements") { + TSFixedArray array; + array.SetCount(0); + REQUIRE(array.Top() == nullptr); + } + + SECTION("returns first element when array has 1 element") { + TSFixedArray array; + array.SetCount(1); + array[0] = 1000; + REQUIRE(*array.Top() == 1000); + } + + SECTION("returns last element when array has 2 elements") { + TSFixedArray array; + array.SetCount(2); + array[0] = 1000; + array[1] = 1001; + REQUIRE(*array.Top() == 1001); + } +} + TEST_CASE("TSGrowableArray", "[array]") { SECTION("constructs correctly") { TSGrowableArray array;