2023-06-17 16:10:56 +02:00
|
|
|
// ArduinoJson - https://arduinojson.org
|
|
|
|
// Copyright © 2014-2023, Benoit BLANCHON
|
|
|
|
// MIT License
|
|
|
|
|
2023-07-03 09:23:18 +02:00
|
|
|
#include <ArduinoJson/Memory/Alignment.hpp>
|
2023-06-17 16:10:56 +02:00
|
|
|
#include <ArduinoJson/Memory/ResourceManager.hpp>
|
2023-06-17 16:24:12 +02:00
|
|
|
#include <ArduinoJson/Memory/VariantPoolImpl.hpp>
|
2023-06-17 16:10:56 +02:00
|
|
|
#include <catch.hpp>
|
|
|
|
|
|
|
|
#include "Allocators.hpp"
|
|
|
|
|
|
|
|
using namespace ArduinoJson::detail;
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
TEST_CASE("ResourceManager::allocSlot()") {
|
2023-06-17 16:10:56 +02:00
|
|
|
SECTION("Returns different pointer") {
|
|
|
|
ResourceManager resources(4096);
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
VariantSlot* s1 = resources.allocSlot();
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(s1 != 0);
|
2023-06-14 11:57:31 +02:00
|
|
|
VariantSlot* s2 = resources.allocSlot();
|
2023-06-17 16:10:56 +02:00
|
|
|
REQUIRE(s2 != 0);
|
|
|
|
|
|
|
|
REQUIRE(s1 != s2);
|
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Returns aligned pointers") {
|
|
|
|
ResourceManager resources(4096);
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
REQUIRE(isAligned(resources.allocSlot().operator VariantSlot*()));
|
|
|
|
REQUIRE(isAligned(resources.allocSlot().operator VariantSlot*()));
|
2023-06-17 16:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Returns zero if capacity is 0") {
|
|
|
|
ResourceManager resources(0);
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
auto variant = resources.allocSlot();
|
|
|
|
REQUIRE(variant.id() == NULL_SLOT);
|
|
|
|
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
|
2023-06-17 16:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Returns zero if buffer is null") {
|
|
|
|
ResourceManager resources(4096, FailingAllocator::instance());
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
auto variant = resources.allocSlot();
|
|
|
|
REQUIRE(variant.id() == NULL_SLOT);
|
|
|
|
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
|
2023-06-17 16:10:56 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
SECTION("Returns zero if capacity is insufficient") {
|
|
|
|
ResourceManager resources(sizeof(VariantSlot));
|
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
resources.allocSlot();
|
2023-06-17 16:10:56 +02:00
|
|
|
|
2023-06-14 11:57:31 +02:00
|
|
|
auto variant = resources.allocSlot();
|
|
|
|
REQUIRE(variant.id() == NULL_SLOT);
|
|
|
|
REQUIRE(static_cast<VariantSlot*>(variant) == nullptr);
|
2023-06-17 16:10:56 +02:00
|
|
|
}
|
|
|
|
}
|