memory tests

v1
Brett 2023-12-10 15:15:23 -05:00
parent 319f7a3771
commit 7aae8272c3
3 changed files with 60 additions and 11 deletions

View File

@ -327,11 +327,11 @@ namespace blt
public: public:
static_vector() = default; static_vector() = default;
inline bool push_back(T copy) inline bool push_back(const T& copy)
{ {
if (size_ >= MAX_SIZE) if (size_ >= MAX_SIZE)
return false; return false;
buffer_[size_++] = std::move(copy); buffer_[size_++] = copy;
return true; return true;
} }
@ -339,10 +339,16 @@ namespace blt
{ {
if (size_ >= MAX_SIZE) if (size_ >= MAX_SIZE)
return false; return false;
buffer_[size_++] = std::forward(move); buffer_[size_++] = std::move(move);
return true; return true;
} }
inline T& at(size_t index)
{
if (index >= MAX_SIZE)
throw std::runtime_error("Array index " + std::to_string(index) + " out of bounds! (Max size: " + std::to_string(MAX_SIZE) + ')');
}
inline T& operator[](size_t index) inline T& operator[](size_t index)
{ {
return buffer_[index]; return buffer_[index];
@ -353,11 +359,23 @@ namespace blt
return buffer_[index]; return buffer_[index];
} }
inline void reserve(size_t size)
{
if (size > MAX_SIZE)
size = MAX_SIZE;
size_ = size;
}
[[nodiscard]] inline size_t size() const [[nodiscard]] inline size_t size() const
{ {
return size_; return size_;
} }
[[nodiscard]] inline size_t capacity() const
{
return MAX_SIZE;
}
inline T* data() inline T* data()
{ {
return buffer_; return buffer_;
@ -373,14 +391,14 @@ namespace blt
return buffer_; return buffer_;
} }
inline ptr_iterator<T> begin() inline T* begin()
{ {
return ptr_iterator<T>{buffer_}; return buffer_;
} }
inline ptr_iterator<T> end() inline T* end()
{ {
return ptr_iterator<T>{&buffer_[size_]}; return &buffer_[size_];
} }
}; };

View File

@ -26,14 +26,14 @@ namespace blt::test::memory
void access(); void access();
void ranges(); void static_vector_test();
static inline void run() static inline void run()
{ {
copy(); copy();
move(); move();
access(); access();
ranges(); static_vector_test();
} }
} }

View File

@ -99,7 +99,38 @@ void blt::test::memory::access()
} }
void blt::test::memory::ranges() void blt::test::memory::static_vector_test()
{ {
BLT_TRACE_STREAM << '\n'; blt::static_vector<int, 16> vec;
for (size_t i = 0; i < 16; i++)
vec[i] = static_cast<int>(i * 2);
for (size_t i = 0; i < 16; i++)
BLT_DEBUG_STREAM << vec[i] << ' ';
BLT_DEBUG_STREAM << '\n';
vec[3] = 120;
vec[7] = 230;
vec.reserve(vec.capacity());
for (auto v : vec)
BLT_DEBUG_STREAM << v << ' ';
BLT_DEBUG_STREAM << '\n';
vec.reserve(0);
for (size_t i = 0; i < vec.capacity(); i++)
{
if (!vec.push_back(static_cast<int>(i)))
BLT_INFO("Failed to insert on %d", i);
}
if (!vec.push_back(10))
BLT_INFO("Vector unable to push, current size vs capacity: %d vs %d", vec.size(), vec.capacity());
for (auto v : vec)
BLT_DEBUG_STREAM << v << ' ';
BLT_DEBUG_STREAM << '\n';
} }