Coverage Report

Created: 2024-04-30 09:35

test/zserio/AllocatorHolderTest.cpp
Line
Count
Source
1
#include <memory>
2
3
#include "gtest/gtest.h"
4
#include "zserio/AllocatorHolder.h"
5
6
namespace zserio
7
{
8
9
namespace
10
{
11
12
template <typename ALLOC>
13
class AllocatorHolderTester : public AllocatorHolder<ALLOC>
14
{
15
public:
16
    using allocator_type = typename AllocatorHolder<ALLOC>::allocator_type;
17
18
    explicit AllocatorHolderTester(allocator_type allocator) :
19
            AllocatorHolder<allocator_type>(allocator)
20
4
    {}
21
22
    void set_allocator(const allocator_type& allocator)
23
1
    {
24
1
        AllocatorHolder<ALLOC>::set_allocator(allocator);
25
1
    }
26
27
    void set_allocator(allocator_type&& allocator)
28
1
    {
29
1
        AllocatorHolder<ALLOC>::set_allocator(allocator);
30
1
    }
31
32
    allocator_type& get_allocator_ref()
33
1
    {
34
1
        return AllocatorHolder<ALLOC>::get_allocator_ref();
35
1
    }
36
37
    const allocator_type& get_allocator_ref() const
38
1
    {
39
1
        return AllocatorHolder<ALLOC>::get_allocator_ref();
40
1
    }
41
};
42
43
} // namespace
44
45
TEST(AllocatorHolderTest, emptyConstructor)
46
1
{
47
1
    const AllocatorHolder<std::allocator<uint8_t>> holder;
48
1
    std::allocator<uint8_t> allocator;
49
1
    ASSERT_EQ(allocator, holder.get_allocator());
50
1
}
51
52
TEST(AllocatorHolderTest, allocatorConstructor)
53
1
{
54
1
    std::allocator<uint8_t> allocator;
55
1
    const AllocatorHolder<std::allocator<uint8_t>> holder(allocator);
56
1
    ASSERT_EQ(allocator, holder.get_allocator());
57
1
}
58
59
TEST(AllocatorHolderTest, allocatorConstructorMove)
60
1
{
61
1
    std::allocator<uint8_t> allocator;
62
1
    const AllocatorHolder<std::allocator<uint8_t>> holder(std::move(allocator));
63
1
    ASSERT_EQ(std::allocator<uint8_t>(), holder.get_allocator());
64
1
}
65
66
TEST(AllocatorHolderTest, setAllocator)
67
1
{
68
1
    std::allocator<uint8_t> allocator1;
69
1
    AllocatorHolderTester<std::allocator<uint8_t>> holder(allocator1);
70
1
    std::allocator<uint8_t> allocator2;
71
1
    holder.set_allocator(allocator2);
72
1
    ASSERT_EQ(allocator2, holder.get_allocator());
73
1
}
74
75
TEST(AllocatorHolderTest, setAllocatorMove)
76
1
{
77
1
    std::allocator<uint8_t> allocator1;
78
1
    AllocatorHolderTester<std::allocator<uint8_t>> holder(allocator1);
79
1
    std::allocator<uint8_t> allocator2;
80
1
    holder.set_allocator(std::move(allocator2));
81
1
    ASSERT_EQ(std::allocator<uint8_t>(), holder.get_allocator());
82
1
}
83
84
TEST(AllocatorHolderTest, getAllocatorRef)
85
1
{
86
1
    std::allocator<uint8_t> allocator;
87
1
    AllocatorHolderTester<std::allocator<uint8_t>> holder(allocator);
88
1
    ASSERT_EQ(allocator, holder.get_allocator_ref());
89
1
}
90
91
TEST(AllocatorHolderTest, getAllocatorRefConst)
92
1
{
93
1
    std::allocator<uint8_t> allocator;
94
1
    const AllocatorHolderTester<std::allocator<uint8_t>> holder(allocator);
95
1
    ASSERT_EQ(allocator, holder.get_allocator_ref());
96
1
}
97
98
} // namespace zserio