2023-11-21 14:22:02 -05:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2023 Brett Terpstra
|
|
|
|
*
|
|
|
|
* This program is free software: you can redistribute it and/or modify
|
|
|
|
* it under the terms of the GNU General Public License as published by
|
|
|
|
* the Free Software Foundation, either version 3 of the License, or
|
|
|
|
* (at your option) any later version.
|
|
|
|
*
|
|
|
|
* This program is distributed in the hope that it will be useful,
|
|
|
|
* but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
* MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
* GNU General Public License for more details.
|
|
|
|
*
|
|
|
|
* You should have received a copy of the GNU General Public License
|
|
|
|
* along with this program. If not, see <https://www.gnu.org/licenses/>.
|
|
|
|
*/
|
|
|
|
#include <memory_test.h>
|
|
|
|
|
|
|
|
#include <blt/std/logging.h>
|
|
|
|
#include <blt/std/memory.h>
|
|
|
|
#include <blt/std/assert.h>
|
|
|
|
#include <blt/std/random.h>
|
|
|
|
#include <type_traits>
|
2023-12-18 02:02:35 -05:00
|
|
|
#include "blt/std/utility.h"
|
2023-11-21 14:22:02 -05:00
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
blt::scoped_buffer<T> create_scoped_buffer(size_t size)
|
|
|
|
{
|
|
|
|
static std::random_device dev;
|
|
|
|
static std::mt19937_64 engine(dev());
|
|
|
|
blt::scoped_buffer<T> data(size);
|
|
|
|
if constexpr (std::is_floating_point_v<T>)
|
|
|
|
{
|
|
|
|
static std::uniform_real_distribution<T> dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
|
|
|
|
|
|
|
for (auto& v : data)
|
|
|
|
v = dist(engine);
|
|
|
|
} else if (std::is_integral_v<T>)
|
|
|
|
{
|
|
|
|
static std::uniform_int_distribution<T> dist(std::numeric_limits<T>::min(), std::numeric_limits<T>::max());
|
|
|
|
|
|
|
|
for (auto& v : data)
|
|
|
|
v = dist(engine);
|
|
|
|
}
|
|
|
|
return data;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
blt::scoped_buffer<T> modify_copy(blt::scoped_buffer<T> fill)
|
|
|
|
{
|
|
|
|
for (size_t i = 0; i < size_t(fill.size() / 2); i++)
|
|
|
|
{
|
|
|
|
std::swap(fill[i], fill[fill.size() - i - 1]);
|
|
|
|
}
|
|
|
|
return fill;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
T collect(blt::scoped_buffer<T> buff)
|
|
|
|
{
|
|
|
|
T val = 0;
|
|
|
|
for (auto v : buff)
|
|
|
|
val = std::max(v, val);
|
|
|
|
return val;
|
|
|
|
}
|
|
|
|
|
|
|
|
void blt::test::memory::copy()
|
|
|
|
{
|
|
|
|
BLT_INFO("Running memory copy tests");
|
|
|
|
|
|
|
|
auto int_buffer_small = create_scoped_buffer<int32_t>(16);
|
|
|
|
auto int_buffer_medium = create_scoped_buffer<int32_t>(512);
|
|
|
|
auto int_buffer_large = create_scoped_buffer<int32_t>(8192);
|
|
|
|
|
|
|
|
auto float_buffer_small = create_scoped_buffer<float>(16);
|
|
|
|
auto float_buffer_medium = create_scoped_buffer<float>(512);
|
|
|
|
auto float_buffer_large = create_scoped_buffer<float>(8192);
|
|
|
|
|
|
|
|
auto int_small = collect(modify_copy(int_buffer_small));
|
|
|
|
auto int_medium = collect(modify_copy(int_buffer_medium));
|
|
|
|
auto int_large = collect(modify_copy(int_buffer_large));
|
|
|
|
|
|
|
|
auto float_small = collect(modify_copy(float_buffer_small));
|
|
|
|
auto float_medium = collect(modify_copy(float_buffer_medium));
|
|
|
|
auto float_large = collect(modify_copy(float_buffer_large));
|
|
|
|
|
|
|
|
BLT_TRACE("We collected values [%d, %d, %d]; [%f.0, %f.0, %f.0]", int_small, int_medium, int_large, float_small, float_medium, float_large);
|
|
|
|
}
|
|
|
|
|
|
|
|
void blt::test::memory::move()
|
|
|
|
{
|
|
|
|
BLT_INFO("Running memory move tests");
|
|
|
|
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
void blt::test::memory::access()
|
|
|
|
{
|
|
|
|
BLT_INFO("Running memory construction tests");
|
|
|
|
|
|
|
|
}
|
|
|
|
|
2023-12-10 15:15:23 -05:00
|
|
|
void blt::test::memory::static_vector_test()
|
2023-11-21 14:22:02 -05:00
|
|
|
{
|
2023-12-10 15:15:23 -05:00
|
|
|
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';
|
2023-11-21 14:22:02 -05:00
|
|
|
}
|
2023-12-18 02:02:35 -05:00
|
|
|
|
|
|
|
struct fucked_type1
|
|
|
|
{
|
|
|
|
private:
|
|
|
|
int T = 0;
|
|
|
|
public:
|
|
|
|
fucked_type1() = default;
|
|
|
|
};
|
|
|
|
|
|
|
|
struct fucked_type2
|
|
|
|
{
|
|
|
|
public:
|
|
|
|
int T = 0;
|
|
|
|
public:
|
|
|
|
fucked_type2()
|
|
|
|
{
|
|
|
|
T = 50;
|
|
|
|
BLT_DEBUG("I HAVE BEEN CONSTRUCTED");
|
|
|
|
}
|
|
|
|
|
|
|
|
~fucked_type2()
|
|
|
|
{
|
|
|
|
BLT_DEBUG("I HAVE BEEN DESTRUCTED!");
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
void blt::test::memory::test()
|
|
|
|
{
|
|
|
|
area_allocator<fucked_type2, 20> int_test{};
|
|
|
|
//auto arr = int_test.allocate(10);
|
|
|
|
auto arr2 = int_test.allocate(15);
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
blt::black_box(int_test.allocate(1));
|
|
|
|
//blt::black_box(arr4);
|
|
|
|
BLT_INFO("CUM");
|
|
|
|
// arr3 = int_test.allocate(2);
|
|
|
|
// blt::black_box(arr3);
|
|
|
|
// arr3 = int_test.allocate(5);
|
|
|
|
// blt::black_box(arr3);
|
|
|
|
// arr3 = int_test.allocate(10);
|
|
|
|
// blt::black_box(arr3);
|
|
|
|
//auto arr3 = int_test.allocate(20);
|
|
|
|
|
|
|
|
//std::memset(arr, 0, 10);
|
|
|
|
//std::memset(arr2, 0, 15);
|
|
|
|
//std::memset(arr3, 0, 20);
|
|
|
|
|
|
|
|
for (int i = 0; i < 15; i++)
|
|
|
|
{
|
|
|
|
BLT_TRACE_STREAM << arr2[i].T << ' ';
|
|
|
|
}
|
|
|
|
BLT_TRACE_STREAM << "\n";
|
|
|
|
|
|
|
|
int_test.deallocate(arr2, 15);
|
|
|
|
BLT_INFO("-----------------");
|
|
|
|
}
|