| 1 |
#include <pbcopper/container/CapacityPointer.h> |
| 2 |
|
| 3 |
#include <gtest/gtest.h> |
| 4 |
|
| 5 |
#include <algorithm> |
| 6 |
#include <cstddef> |
| 7 |
#include <cstdint> |
| 8 |
#include <cstdlib> |
| 9 |
#include <sstream> |
| 10 |
#include <string_view> |
| 11 |
|
| 12 |
using namespace PacBio; |
| 13 |
|
| 14 |
TEST(Container_CapacityPointer, Basic) |
| 15 |
{ |
| 16 |
static int32_t deallocations = 0; |
| 17 |
|
| 18 |
{ |
| 19 |
struct DummyAllocate |
| 20 |
{ |
| 21 |
std::byte* operator()(const ptrdiff_t size) const noexcept |
| 22 |
{ |
| 23 |
return static_cast<std::byte*>(std::malloc(size)); |
| 24 |
} |
| 25 |
}; |
| 26 |
|
| 27 |
struct DummyDeallocate |
| 28 |
{ |
| 29 |
void operator()(std::byte* ptr) const noexcept |
| 30 |
{ |
| 31 |
std::free(ptr); |
| 32 |
++deallocations; |
| 33 |
} |
| 34 |
}; |
| 35 |
|
| 36 |
Container::CapacityPointer<std::byte, DummyAllocate, DummyDeallocate> ptr{12}; |
| 37 |
EXPECT_EQ(ptr.Capacity(), 12); |
| 38 |
|
| 39 |
ptr.Reserve(16); |
| 40 |
EXPECT_EQ(ptr.Capacity(), 18); // 12 * 150% == 18 |
| 41 |
std::fill_n(ptr.get(), ptr.Capacity(), std::byte{}); |
| 42 |
|
| 43 |
const std::string_view str{"CapacityPointer"}; |
| 44 |
std::copy(std::cbegin(str), std::cend(str), reinterpret_cast<char*>(ptr.get())); |
| 45 |
|
| 46 |
std::ostringstream os; |
| 47 |
os << reinterpret_cast<const char*>(ptr.get()); |
| 48 |
|
| 49 |
EXPECT_EQ(os.str(), str); |
| 50 |
} |
| 51 |
|
| 52 |
EXPECT_EQ(deallocations, 2); |
| 53 |
} |