File: test_allocator.cpp

package info (click to toggle)
c4core 0.2.7-1
  • links: PTS
  • area: main
  • in suites: forky, sid
  • size: 5,184 kB
  • sloc: cpp: 35,521; python: 2,786; javascript: 414; makefile: 6
file content (261 lines) | stat: -rw-r--r-- 6,234 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
#ifndef C4CORE_SINGLE_HEADER
#include "c4/allocator.hpp"
#endif

#include "c4/test.hpp"
#include "c4/libtest/supprwarn_push.hpp"

#include <vector>
#include <string>
#include <map>

namespace c4 {

template< class T > using small_adapter = c4::small_allocator< T >;
template< class T > using small_adapter_mr = c4::small_allocator_mr< T >;

#define _c4definealloctypes(Alloc) \
using AllocInt = typename Alloc::template rebind<int>::other;\
using AllocChar = typename Alloc::template rebind<char>::other;\
using _string = std::basic_string< char, std::char_traits<char>, AllocChar >;\
using AllocString = typename Alloc::template rebind<_string>::other;\
using AllocPair = typename Alloc::template rebind<std::pair<const _string,int>>::other;\
using _vector_int = std::vector<int, AllocInt >;\
using _vector_string = std::vector<_string, AllocString >;\
using _map_string_int = std::map<_string, int, std::less<_string>, AllocPair >;

//-----------------------------------------------------------------------------
template< class Alloc >
void test_traits_compat_construct(typename Alloc::value_type const& val, Alloc &a)
{
    using atraits = std::allocator_traits< Alloc >;
    using value_type = typename Alloc::value_type;

    value_type *mem = a.allocate(1);
    REQUIRE_NE(mem, nullptr);
    atraits::construct(a, mem, val);
    CHECK_EQ(*mem, val);

    atraits::destroy(a, mem);
    a.deallocate(mem, 1);
}

TEST_CASE("allocator.traits_compat_construct")
{
    allocator<int> a;
    test_traits_compat_construct(1, a);
}

TEST_CASE("small_allocator.traits_compat_construct")
{
    small_allocator<int> a;
    test_traits_compat_construct(1, a);
}

TEST_CASE("allocator_mr_global.traits_compat_construct")
{
    allocator_mr<int> a;
    test_traits_compat_construct(1, a);
}

TEST_CASE("allocator_mr_linear.traits_compat_construct")
{
    MemoryResourceLinear mr(1024);
    allocator_mr<int> a(&mr);
    test_traits_compat_construct(1, a);
}

TEST_CASE("allocator_mr_linear_arr.traits_compat_construct")
{
    MemoryResourceLinearArr<1024> mr;
    allocator_mr<int> a(&mr);
    test_traits_compat_construct(1, a);
}

TEST_CASE("small_allocator_mr_global.traits_compat_construct")
{
    small_allocator_mr<int> a;
    test_traits_compat_construct(1, a);
}

TEST_CASE("small_allocator_mr_linear.traits_compat_construct")
{
    MemoryResourceLinear mr(1024);
    small_allocator_mr<int> a(&mr);
    test_traits_compat_construct(1, a);
}

TEST_CASE("small_allocator_mr_linear_arr.traits_compat_construct")
{
    MemoryResourceLinearArr<1024> mr;
    small_allocator_mr<int> a(&mr);
    test_traits_compat_construct(1, a);
}

//-----------------------------------------------------------------------------

template<bool IsSingleChunk>
struct clear_mr
{
    template<class Alloc>
    static void clear(Alloc)
    {
        //std::cout << C4_PRETTY_FUNC << "\n"; std::cout.flush();
    }
};

template<>
struct clear_mr<true>
{
    template<class Alloc>
    static void clear(Alloc a)
    {
        //std::cout << C4_PRETTY_FUNC << "\n"; std::cout.flush();
        MemoryResourceLinear* mrl = static_cast<MemoryResourceLinear*>(a.resource());
        mrl->clear();
    }
};


template<bool Clear, class Alloc>
void do_std_containers_test(Alloc alloc)
{
    _c4definealloctypes(Alloc);

    {
        _string v(alloc);
        v.reserve(256);
        v = "adskjhsdfkjdflkjsdfkjhsdfkjh";
        CHECK_EQ(v, "adskjhsdfkjdflkjsdfkjhsdfkjh");
    }

    clear_mr<Clear>::clear(alloc);

    {
        int arr[128];
        for(int &i : arr)
        {
            i = 42;
        }
        _vector_int vi(arr, arr+C4_COUNTOF(arr), alloc);
        for(int i : vi)
        {
            CHECK_EQ(i, 42);
        }
    }

    clear_mr<Clear>::clear(alloc);

    {
        AllocChar a = alloc;
        _vector_string v({"foo", "bar", "baz", "bat", "bax"}, a);
        CHECK_EQ(v.size(), 5);
        CHECK_EQ(v[0], "foo");
        CHECK_EQ(v[1], "bar");
        CHECK_EQ(v[2], "baz");
        CHECK_EQ(v[3], "bat");
        CHECK_EQ(v[4], "bax");
    }

    clear_mr<Clear>::clear(alloc);

    {
        AllocString a = alloc;
        _vector_string v(a);
        v.resize(4);
        int count = 0;
        for(auto &s : v)
        {
            _string ss(size_t(64), (char)('0' + count++));
            s = ss;
        }
    }

    clear_mr<Clear>::clear(alloc);

    {
#if !defined(__GNUC__) || (__GNUC__ >= 5)
        /* constructor does not exist on gcc < 5) */
        AllocPair a = alloc;
        _map_string_int v(a);
#else
        _map_string_int v;
#endif
        CHECK_EQ(v.size(), 0);
        v["foo"] = 0;
        v["bar"] = 1;
        v["baz"] = 2;
        v["bat"] = 3;
        CHECK_EQ(v.size(), 4);
        CHECK_EQ(v["foo"], 0);
        CHECK_EQ(v["bar"], 1);
        CHECK_EQ(v["baz"], 2);
        CHECK_EQ(v["bat"], 3);
    }

    clear_mr<Clear>::clear(alloc);
}

TEST_CASE("allocator_global.std_containers")
{
    allocator<int> a;
    do_std_containers_test<false>(a);
}

TEST_CASE("small_allocator_global.std_containers")
{
    /* this is failing, investigate
    small_allocator<int> a;
    do_std_containers_test(a);
    */
}

TEST_CASE("allocator_mr_global.std_containers")
{
    allocator_mr<int> a;
    do_std_containers_test<false>(a);
}

TEST_CASE("allocator_mr_linear.std_containers")
{
    MemoryResourceLinear mr(1024);
    allocator_mr<int> a(&mr);
    do_std_containers_test<true>(a);
}

TEST_CASE("allocator_mr_linear_arr.std_containers")
{
    MemoryResourceLinearArr<1024> mr;
    allocator_mr<int> a(&mr);
    do_std_containers_test<true>(a);
}

TEST_CASE("small_allocator_mr_global.std_containers")
{
    /* this is failing, investigate
    small_allocator_mr<int> a;
    do_std_containers_test<false>(a);
    */
}

TEST_CASE("small_allocator_mr_linear.std_containers")
{
    /* this is failing, investigate
    MemoryResourceLinear mr(1024);
    small_allocator_mr<int> a(&mr);
    do_std_containers_test<true>(a);
    */
}

TEST_CASE("small_allocator_mr_linear_arr.std_containers")
{
    /* this is failing, investigate
    MemoryResourceLinearArr<1024> mr;
    small_allocator_mr<int> a(&mr);
    do_std_containers_test<true>(a);
    */
}

} // namespace c4

#include "c4/libtest/supprwarn_pop.hpp"