Add a very simple ResourcePool implementation.
This commit is contained in:
parent
fb774671ad
commit
891303a578
3 changed files with 201 additions and 0 deletions
|
@ -7,6 +7,7 @@ add_executable(${PROJECT_NAME}
|
|||
int_conv_test.cpp
|
||||
reversed_sized_array_test.cpp
|
||||
bitfield_pack_test.cpp
|
||||
resource_pool_test.cpp
|
||||
)
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD 17)
|
||||
set_property(TARGET ${PROJECT_NAME} PROPERTY CXX_STANDARD_REQUIRED ON)
|
||||
|
|
83
test/unit/resource_pool_test.cpp
Normal file
83
test/unit/resource_pool_test.cpp
Normal file
|
@ -0,0 +1,83 @@
|
|||
/* Copyright 2016-2018 Michele Santullo
|
||||
* This file is part of "duckhandy".
|
||||
*
|
||||
* "duckhandy" 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.
|
||||
*
|
||||
* "duckhandy" 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 "duckhandy". If not, see <http://www.gnu.org/licenses/>.
|
||||
*/
|
||||
|
||||
#include "catch2/catch.hpp"
|
||||
#include "duckhandy/resource_pool.hpp"
|
||||
#include <cstdint>
|
||||
|
||||
TEST_CASE ("Make operations on the ResourcePool", "[ResourcePool]") {
|
||||
using dhandy::ResourcePool;
|
||||
int callback_count = 0;
|
||||
|
||||
ResourcePool<int, std::vector<int>> vec_pool([&callback_count](int count) {
|
||||
++callback_count;
|
||||
std::vector<int> nums(count);
|
||||
for (int z = 0; z < count; ++z) {
|
||||
nums[z] = z;
|
||||
}
|
||||
return nums;
|
||||
});
|
||||
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(1 == callback_count);
|
||||
CHECK(vec_pool.size() == 1);
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(1 == callback_count);
|
||||
CHECK(vec_pool.size() == 1);
|
||||
|
||||
{
|
||||
auto vec_9 = vec_pool.make(9);
|
||||
REQUIRE(vec_9->size() == 9);
|
||||
for (int z = 0; z < 9; ++z) {
|
||||
CHECK((*vec_9)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(2 == callback_count);
|
||||
CHECK(vec_pool.size() == 2);
|
||||
|
||||
{
|
||||
auto vec_10 = vec_pool.make(10);
|
||||
REQUIRE(vec_10->size() == 10);
|
||||
for (int z = 0; z < 10; ++z) {
|
||||
CHECK((*vec_10)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(2 == callback_count);
|
||||
CHECK(vec_pool.size() == 2);
|
||||
|
||||
{
|
||||
auto vec_5 = vec_pool.make(5);
|
||||
REQUIRE(vec_5->size() == 5);
|
||||
for (int z = 0; z < 5; ++z) {
|
||||
CHECK((*vec_5)[z] == z);
|
||||
}
|
||||
}
|
||||
CHECK(3 == callback_count);
|
||||
CHECK(vec_pool.size() == 3);
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue