Coverage Report

Created: 2023-12-13 14:58

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