1 | // |
2 | // immer: immutable data structures for C++ |
3 | // Copyright (C) 2016, 2017, 2018 Juan Pedro Bolivar Puente |
4 | // |
5 | // This software is distributed under the Boost Software License, Version 1.0. |
6 | // See accompanying file LICENSE or copy at http://boost.org/LICENSE_1_0.txt |
7 | // |
8 | |
9 | #include <immer/heap/malloc_heap.hpp> |
10 | #include <immer/heap/free_list_heap.hpp> |
11 | #include <immer/heap/thread_local_free_list_heap.hpp> |
12 | #include <immer/heap/gc_heap.hpp> |
13 | #include <immer/heap/cpp_heap.hpp> |
14 | |
15 | #include <catch.hpp> |
16 | #include <numeric> |
17 | |
18 | void do_stuff_to(void* buf, std::size_t size) |
19 | { |
20 | auto ptr = static_cast<unsigned char*>(buf); |
21 | CHECK(buf != 0); |
22 | std::iota(ptr, ptr + size, 42); |
23 | } |
24 | |
25 | TEST_CASE("malloc" ) |
26 | { |
27 | using heap = immer::malloc_heap; |
28 | |
29 | SECTION("basic" ) |
30 | { |
31 | auto p = heap::allocate(42u); |
32 | do_stuff_to(p, 42u); |
33 | heap::deallocate(42, p); |
34 | } |
35 | } |
36 | |
37 | TEST_CASE("gc" ) |
38 | { |
39 | using heap = immer::gc_heap; |
40 | |
41 | SECTION("basic" ) |
42 | { |
43 | auto p = heap::allocate(42u); |
44 | do_stuff_to(p, 42u); |
45 | heap::deallocate(42, p); |
46 | } |
47 | } |
48 | |
49 | TEST_CASE("cpp" ) |
50 | { |
51 | using heap = immer::cpp_heap; |
52 | |
53 | SECTION("basic" ) |
54 | { |
55 | auto p = heap::allocate(42u); |
56 | do_stuff_to(p, 42u); |
57 | heap::deallocate(42, p); |
58 | } |
59 | } |
60 | |
61 | template <typename Heap> |
62 | void test_free_list_heap() |
63 | { |
64 | using heap = Heap; |
65 | |
66 | SECTION("basic" ) |
67 | { |
68 | auto p = heap::allocate(42u); |
69 | do_stuff_to(p, 42u); |
70 | heap::deallocate(42, p); |
71 | } |
72 | |
73 | SECTION("reuse" ) |
74 | { |
75 | auto p = heap::allocate(42u); |
76 | do_stuff_to(p, 42u); |
77 | heap::deallocate(42, p); |
78 | |
79 | auto u = heap::allocate(12u); |
80 | do_stuff_to(u, 12u); |
81 | heap::deallocate(12, u); |
82 | CHECK(u == p); |
83 | } |
84 | } |
85 | |
86 | TEST_CASE("free list" ) |
87 | { |
88 | test_free_list_heap<immer::free_list_heap<42u, 2, immer::malloc_heap>>(); |
89 | } |
90 | |
91 | TEST_CASE("thread local free list" ) |
92 | { |
93 | test_free_list_heap<immer::thread_local_free_list_heap<42u, 2, immer::malloc_heap>>(); |
94 | } |
95 | |
96 | TEST_CASE("unsafe free_list" ) |
97 | { |
98 | test_free_list_heap<immer::unsafe_free_list_heap<42u, 2, immer::malloc_heap>>(); |
99 | } |
100 | |