Reactor/tests/test_utilities.cpp
2025-06-27 19:06:26 -05:00

176 lines
4.0 KiB
C++

#include "../lib/Utilities.hpp"
#include <cassert>
#include <iostream>
#include <thread>
#include <chrono>
#include <vector>
#include <future>
#include <latch> // For std::latch
#include <atomic>
void test_object_pool()
{
std::cout << "Testing object pool...\n";
struct TestObject
{
int value = 42;
};
auto pool = std::make_shared<reactor::ObjectPool<TestObject>>();
{
auto obj1 = pool->getObject();
auto obj2 = pool->getObject();
assert(obj1->value == 42);
assert(obj2->value == 42);
assert(obj1.get() != obj2.get());
}
{
auto obj3 = pool->getObject();
assert(obj3->value == 42);
}
std::cout << "✓ Object pool passed\n";
}
void test_logger()
{
std::cout << "Testing logger...\n";
reactor::Logger::setLevel(reactor::LogLevel::DEBUG);
reactor::Logger(reactor::LogLevel::DEBUG) << "Debug message";
reactor::Logger(reactor::LogLevel::INFO) << "Info message with number: " << 42;
reactor::Logger(reactor::LogLevel::WARN) << "Warning message";
reactor::Logger(reactor::LogLevel::ERROR) << "Error message";
reactor::Logger::setLevel(reactor::LogLevel::ERROR);
reactor::Logger(reactor::LogLevel::DEBUG) << "This should not appear";
reactor::Logger(reactor::LogLevel::INFO) << "This should not appear";
reactor::Logger(reactor::LogLevel::ERROR) << "This should appear";
reactor::Logger::setLevel(reactor::LogLevel::DEBUG);
std::cout << "✓ Logger passed\n";
}
void test_concurrent_task_queue()
{
std::cout << "Testing concurrent task queue...\n";
reactor::ConcurrentTaskQueue queue(4, "TestQueue");
assert(queue.getName() == "TestQueue");
constexpr int num_tasks = 50;
std::atomic<int> counter{0};
std::latch all_tasks_done(num_tasks); // C++20 latch for synchronization
for (int i = 0; i < num_tasks; ++i)
{
queue.runTaskInQueue([&counter, &all_tasks_done]()
{
counter++;
// Signal that one task is complete
all_tasks_done.count_down();
});
}
// Wait for all tasks to complete
all_tasks_done.wait();
assert(counter.load() == num_tasks);
std::cout << "✓ Concurrent task queue passed\n";
}
void test_sync_task()
{
std::cout << "Testing sync task execution...\n";
reactor::ConcurrentTaskQueue queue(1, "SyncTest");
bool task_executed = false;
queue.syncTaskInQueue([&task_executed]()
{
std::this_thread::sleep_for(std::chrono::milliseconds(50));
task_executed = true;
});
assert(task_executed);
std::cout << "✓ Sync task execution passed\n";
}
void test_utility_functions()
{
std::cout << "Testing utility functions...\n";
std::size_t seed = 0;
reactor::hashCombine(seed, 42);
reactor::hashCombine(seed, std::string("test"));
assert(seed != 0);
std::string text = "hello,world,test,data";
auto parts = reactor::splitString(text, ",");
assert(parts.size() == 4);
assert(parts[0] == "hello");
assert(parts[1] == "world");
assert(parts[2] == "test");
assert(parts[3] == "data");
auto single = reactor::splitString("single", ",");
assert(single.size() == 1);
assert(single[0] == "single");
std::cout << "✓ Utility functions passed\n";
}
void test_network_utilities()
{
std::cout << "Testing network utilities...\n";
uint64_t original = 0x123456789ABCDEF0ULL;
uint64_t network = reactor::hton64(original);
uint64_t back = reactor::ntoh64(network);
assert(back == original);
std::cout << "✓ Network utilities passed\n";
}
void test_non_copyable()
{
std::cout << "Testing NonCopyable...\n";
class TestClass : public reactor::NonCopyable
{
public:
int value = 42;
TestClass() = default;
TestClass(TestClass&& other) noexcept : value(other.value) { other.value = 0; }
};
TestClass obj1;
TestClass obj2 = std::move(obj1);
assert(obj2.value == 42);
assert(obj1.value == 0);
std::cout << "✓ NonCopyable passed\n";
}
int main()
{
std::cout << "=== Utilities Tests (C++20 Version) ===\n";
test_object_pool();
test_logger();
test_concurrent_task_queue();
test_sync_task();
test_utility_functions();
test_network_utilities();
test_non_copyable();
std::cout << "All utilities tests passed! ✓\n";
return 0;
}