#include "slots.h"
#include "gtest/gtest.h"
namespace {
TEST(SlotsInit, nominal) {
size_t const number_of_slots = 128;
std::shared_ptr<slots_t> const slots(slots_init(number_of_slots, "TEST"), slots_shutdown);
ASSERT_NE(slots, nullptr);
}
TEST(SlotsInit, invalidnumberOfSlots) {
size_t const number_of_slots = 129;
std::shared_ptr<slots_t> const slots(slots_init(number_of_slots, "TEST"), slots_shutdown);
ASSERT_EQ(slots, nullptr);
}
TEST(SlotsShutdown, null) {
EXPECT_NO_THROW(slots_shutdown(nullptr)); }
TEST(SlotsAllocate, nominal) {
size_t const number_of_slots = 128;
std::shared_ptr<slots_t> const slots(slots_init(number_of_slots, "TEST"), slots_shutdown);
ASSERT_NE(slots, nullptr);
size_t const slot = slots_allocate(slots.get());
EXPECT_NE(slot, SLOT_INVALID);
slots_free(slots.get(), slot);
}
TEST(SlotsAllocate, failonmaxplusone) {
size_t const number_of_slots = 128;
std::shared_ptr<slots_t> const slots(slots_init(number_of_slots, "TEST"), slots_shutdown);
ASSERT_NE(slots, nullptr);
std::vector<slot_t> allocated;
for (size_t i = 0; i < number_of_slots; ++i) {
size_t const slot = slots_allocate(slots.get());
EXPECT_NE(slot, SLOT_INVALID);
allocated.push_back(slot);
}
size_t const slot = slots_allocate(slots.get());
EXPECT_EQ(slot, SLOT_INVALID);
slots_free(slots.get(), slot);
for (unsigned int const i : allocated) {
slots_free(slots.get(), i);
}
}
TEST(SlotsFree, doublemax) {
size_t const number_of_slots = 128;
std::shared_ptr<slots_t> const slots(slots_init(number_of_slots, "TEST"), slots_shutdown);
ASSERT_NE(slots, nullptr);
std::vector<slot_t> allocated;
for (size_t i = 0; i < number_of_slots; ++i) {
size_t const slot = slots_allocate(slots.get());
EXPECT_NE(slot, SLOT_INVALID);
allocated.push_back(slot);
}
for (unsigned int const i : allocated) {
slots_free(slots.get(), i);
}
std::vector<slot_t> allocated2;
for (size_t i = 0; i < number_of_slots; ++i) {
size_t const slot = slots_allocate(slots.get());
EXPECT_NE(slot, SLOT_INVALID);
allocated2.push_back(slot);
}
for (unsigned int const i : allocated2) {
slots_free(slots.get(), i);
}
}
}