mirror of
https://github.com/mmueller41/mxtasking.git
synced 2026-01-21 12:42:57 +01:00
Initial commit
This commit is contained in:
30
test/mx/memory/alignment_helper.test.cpp
Normal file
30
test/mx/memory/alignment_helper.test.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/memory/alignment_helper.h>
|
||||
|
||||
TEST(MxTasking, alignment_helper)
|
||||
{
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(4U, 64U), 64U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(64U, 64U), 64U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(65U, 64U), 128U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(128U, 64U), 128U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(129U, 64U), 192U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_multiple(180U, 64U), 192U);
|
||||
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(4U));
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(8U));
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(16U));
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(32U));
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(64U));
|
||||
EXPECT_TRUE(mx::memory::alignment_helper::is_power_of_two(128U));
|
||||
EXPECT_FALSE(mx::memory::alignment_helper::is_power_of_two(3U));
|
||||
EXPECT_FALSE(mx::memory::alignment_helper::is_power_of_two(6U));
|
||||
EXPECT_FALSE(mx::memory::alignment_helper::is_power_of_two(15U));
|
||||
EXPECT_FALSE(mx::memory::alignment_helper::is_power_of_two(17U));
|
||||
EXPECT_FALSE(mx::memory::alignment_helper::is_power_of_two(100U));
|
||||
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_power_of_two(3U), 4U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_power_of_two(17U), 32U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_power_of_two(64U), 64U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_power_of_two(132U), 256U);
|
||||
EXPECT_EQ(mx::memory::alignment_helper::next_power_of_two(255U), 256U);
|
||||
}
|
||||
23
test/mx/memory/dynamic_size_allocator.test.cpp
Normal file
23
test/mx/memory/dynamic_size_allocator.test.cpp
Normal file
@@ -0,0 +1,23 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/memory/dynamic_size_allocator.h>
|
||||
|
||||
TEST(MxTasking, DynamicSizeAllocator)
|
||||
{
|
||||
|
||||
auto allocator = mx::memory::dynamic::Allocator{};
|
||||
|
||||
// Allocation success
|
||||
auto *m1 = allocator.allocate(0U, 64U, sizeof(std::uint32_t));
|
||||
EXPECT_NE(m1, nullptr);
|
||||
|
||||
// Alignment
|
||||
EXPECT_TRUE((std::uintptr_t(m1) & 0x3F) == 0U);
|
||||
|
||||
// is free
|
||||
EXPECT_FALSE(allocator.is_free());
|
||||
allocator.free(m1);
|
||||
EXPECT_TRUE(allocator.is_free());
|
||||
|
||||
// Different allocations, different blocks
|
||||
EXPECT_NE(allocator.allocate(0U, 64U, sizeof(std::uint32_t)), allocator.allocate(0U, 64U, sizeof(std::uint32_t)));
|
||||
}
|
||||
55
test/mx/memory/fixed_size_allocator.test.cpp
Normal file
55
test/mx/memory/fixed_size_allocator.test.cpp
Normal file
@@ -0,0 +1,55 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/memory/fixed_size_allocator.h>
|
||||
|
||||
TEST(MxTasking, FixedSizeAllocator)
|
||||
{
|
||||
// Single core
|
||||
{
|
||||
auto core_set = mx::util::core_set{};
|
||||
core_set.emplace_back(0U);
|
||||
|
||||
auto allocator = mx::memory::fixed::Allocator<64U>{core_set};
|
||||
|
||||
// Allocation success
|
||||
auto *m1 = allocator.allocate(0U);
|
||||
EXPECT_NE(m1, nullptr);
|
||||
|
||||
// Alignment
|
||||
EXPECT_TRUE((std::uintptr_t(m1) & 0x3F) == 0U);
|
||||
|
||||
// Allocate same block after free
|
||||
allocator.free(0U, m1);
|
||||
EXPECT_EQ(allocator.allocate(0U), m1);
|
||||
|
||||
// Different allocations, different blocks
|
||||
EXPECT_NE(allocator.allocate(0U), allocator.allocate(0U));
|
||||
}
|
||||
|
||||
// Different cores
|
||||
{
|
||||
auto core_set = mx::util::core_set{};
|
||||
core_set.emplace_back(0U);
|
||||
core_set.emplace_back(1U);
|
||||
|
||||
auto allocator = mx::memory::fixed::Allocator<64U>{core_set};
|
||||
|
||||
// Allocation success
|
||||
auto *m1 = allocator.allocate(1U);
|
||||
EXPECT_NE(m1, nullptr);
|
||||
|
||||
// Alignment
|
||||
EXPECT_TRUE((std::uintptr_t(m1) & 0x3F) == 0);
|
||||
|
||||
// Allocate same block after free
|
||||
allocator.free(1U, m1);
|
||||
EXPECT_EQ(allocator.allocate(1U), m1);
|
||||
|
||||
// Different blocks of different cores
|
||||
EXPECT_NE(allocator.allocate(0U), allocator.allocate(1U));
|
||||
|
||||
// Move block to different core
|
||||
auto *m2 = allocator.allocate(0U);
|
||||
allocator.free(1U, m2);
|
||||
EXPECT_EQ(allocator.allocate(1U), m2);
|
||||
}
|
||||
}
|
||||
29
test/mx/memory/tagged_ptr.test.cpp
Normal file
29
test/mx/memory/tagged_ptr.test.cpp
Normal file
@@ -0,0 +1,29 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/memory/tagged_ptr.h>
|
||||
|
||||
TEST(MxTasking, tagged_ptr)
|
||||
{
|
||||
auto p = std::uint32_t{42U};
|
||||
auto i = std::uint16_t{1337U};
|
||||
auto ptr = mx::memory::tagged_ptr<std::uint32_t, std::uint16_t>{};
|
||||
|
||||
EXPECT_EQ(ptr.get(), nullptr);
|
||||
|
||||
ptr.reset(&p);
|
||||
EXPECT_NE(ptr, nullptr);
|
||||
EXPECT_EQ(ptr.get(), &p);
|
||||
EXPECT_EQ(*ptr.get(), p);
|
||||
|
||||
ptr.reset(i);
|
||||
EXPECT_EQ(ptr.info(), i);
|
||||
|
||||
auto ptr2 = mx::memory::tagged_ptr<std::uint32_t, std::uint16_t>{&p};
|
||||
EXPECT_EQ(ptr, ptr2);
|
||||
ptr2.reset(i);
|
||||
EXPECT_EQ(ptr, ptr2);
|
||||
|
||||
ptr.reset();
|
||||
EXPECT_EQ(ptr.get(), nullptr);
|
||||
EXPECT_EQ(ptr, nullptr);
|
||||
EXPECT_NE(ptr, ptr2);
|
||||
}
|
||||
14
test/mx/util/aligned_t.test.cpp
Normal file
14
test/mx/util/aligned_t.test.cpp
Normal file
@@ -0,0 +1,14 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/util/aligned_t.h>
|
||||
|
||||
TEST(MxTasking, aligned_t)
|
||||
{
|
||||
EXPECT_EQ(sizeof(mx::util::aligned_t<std::uint8_t>), 64U);
|
||||
EXPECT_EQ(sizeof(mx::util::aligned_t<std::uint64_t>), 64U);
|
||||
|
||||
auto aligned_value = mx::util::aligned_t<std::uint64_t>{42U};
|
||||
EXPECT_EQ(aligned_value.value(), 42U);
|
||||
|
||||
aligned_value = 1337U;
|
||||
EXPECT_EQ(aligned_value.value(), 1337U);
|
||||
}
|
||||
30
test/mx/util/core_set.test.cpp
Normal file
30
test/mx/util/core_set.test.cpp
Normal file
@@ -0,0 +1,30 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/util/core_set.h>
|
||||
|
||||
TEST(MxTasking, core_set)
|
||||
{
|
||||
auto core_set = mx::util::core_set{};
|
||||
EXPECT_FALSE(static_cast<bool>(core_set));
|
||||
EXPECT_EQ(core_set.size(), 0U);
|
||||
|
||||
core_set.emplace_back(0U);
|
||||
EXPECT_TRUE(static_cast<bool>(core_set));
|
||||
EXPECT_EQ(core_set.size(), 1U);
|
||||
EXPECT_EQ(core_set[0], 0U);
|
||||
EXPECT_EQ(core_set.max_core_id(), 0U);
|
||||
|
||||
core_set.emplace_back(2U);
|
||||
EXPECT_TRUE(static_cast<bool>(core_set));
|
||||
EXPECT_EQ(core_set.size(), 2U);
|
||||
EXPECT_EQ(core_set[0], 0U);
|
||||
EXPECT_EQ(core_set[1], 2U);
|
||||
EXPECT_EQ(core_set.max_core_id(), 2U);
|
||||
|
||||
core_set.emplace_back(1U);
|
||||
EXPECT_TRUE(static_cast<bool>(core_set));
|
||||
EXPECT_EQ(core_set.size(), 3U);
|
||||
EXPECT_EQ(core_set[0], 0U);
|
||||
EXPECT_EQ(core_set[1], 2U);
|
||||
EXPECT_EQ(core_set[2], 1U);
|
||||
EXPECT_EQ(core_set.max_core_id(), 2U);
|
||||
}
|
||||
17
test/mx/util/mpsc_queue.test.cpp
Normal file
17
test/mx/util/mpsc_queue.test.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/util/mpsc_queue.h>
|
||||
#include <mx/util/queue_item.h>
|
||||
|
||||
TEST(MxTasking, MPSCQueue)
|
||||
{
|
||||
auto queue = mx::util::MPSCQueue<mx::util::QueueItem>{};
|
||||
EXPECT_EQ(queue.empty(), true);
|
||||
|
||||
auto queue_item = mx::util::QueueItem{};
|
||||
queue.push_back(&queue_item);
|
||||
EXPECT_EQ(queue.empty(), false);
|
||||
auto *pulled_item = queue.pop_front();
|
||||
EXPECT_EQ(&queue_item, pulled_item);
|
||||
EXPECT_EQ(queue.empty(), true);
|
||||
EXPECT_EQ(queue.pop_front(), nullptr);
|
||||
}
|
||||
17
test/mx/util/queue.test.cpp
Normal file
17
test/mx/util/queue.test.cpp
Normal file
@@ -0,0 +1,17 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/util/queue.h>
|
||||
#include <mx/util/queue_item.h>
|
||||
|
||||
TEST(MxTasking, Queue)
|
||||
{
|
||||
auto queue = mx::util::Queue<mx::util::QueueItem>{};
|
||||
EXPECT_EQ(queue.empty(), true);
|
||||
|
||||
auto queue_item = mx::util::QueueItem{};
|
||||
queue.push_back(&queue_item);
|
||||
EXPECT_EQ(queue.empty(), false);
|
||||
auto *pulled_item = queue.pop_front();
|
||||
EXPECT_EQ(&queue_item, pulled_item);
|
||||
EXPECT_EQ(queue.empty(), true);
|
||||
EXPECT_EQ(queue.pop_front(), nullptr);
|
||||
}
|
||||
35
test/mx/util/vector.test.cpp
Normal file
35
test/mx/util/vector.test.cpp
Normal file
@@ -0,0 +1,35 @@
|
||||
#include <gtest/gtest.h>
|
||||
#include <mx/util/vector.h>
|
||||
|
||||
TEST(MxTasking, vector)
|
||||
{
|
||||
auto items = mx::util::vector<std::uint64_t, std::uint32_t>{};
|
||||
EXPECT_TRUE(items.empty());
|
||||
EXPECT_EQ(items.size(), 0U);
|
||||
|
||||
items.emplace_back(42U);
|
||||
EXPECT_FALSE(items.empty());
|
||||
EXPECT_EQ(items.size(), 1U);
|
||||
EXPECT_EQ(items[0], 42U);
|
||||
|
||||
items.clear();
|
||||
EXPECT_TRUE(items.empty());
|
||||
|
||||
for (auto i = 0U; i < 1024U; ++i)
|
||||
{
|
||||
items.emplace_back(i + 1U);
|
||||
}
|
||||
EXPECT_EQ(items.size(), 1024U);
|
||||
EXPECT_EQ(items[0], 1U);
|
||||
EXPECT_EQ(items[1023], 1024U);
|
||||
|
||||
EXPECT_EQ(sizeof(decltype(items)), 24U);
|
||||
|
||||
for (const auto &i : items)
|
||||
{
|
||||
EXPECT_TRUE(i > 0U);
|
||||
}
|
||||
|
||||
items[0] = 1337U;
|
||||
EXPECT_EQ(items[0], 1337U);
|
||||
}
|
||||
Reference in New Issue
Block a user