1 // Copyright (C) 2013-2020 Codership Oy <info@codership.com>
2
3 // $Id$
4
5 #include "../src/gu_alloc.hpp"
6
7 #include "gu_alloc_test.hpp"
8
9 class TestBaseName : public gu::Allocator::BaseName
10 {
11 std::string str_;
12
13 public:
14
TestBaseName(const char * name)15 TestBaseName(const char* name) : str_(name) {}
print(std::ostream & os) const16 void print(std::ostream& os) const { os << str_; }
17 };
18
START_TEST(basic)19 START_TEST (basic)
20 {
21 ssize_t const extra_size(1 << 12); /* extra size to force new page */
22 size_t reserved[extra_size / sizeof(size_t)]; /* size_t for alignment */
23
24 const char test0[] = "test0";
25 ssize_t const test0_size(sizeof(test0));
26
27 const char test1[] = "test1";
28 ssize_t const test1_size(sizeof(test1) + extra_size);
29
30 TestBaseName test_name("gu_alloc_test");
31 gu::Allocator a(test_name, reserved, sizeof(reserved),
32 sizeof(test1), 1 << 16);
33 mark_point();
34 void* p;
35 size_t r, s = 0;
36 bool n;
37
38 r = 0; s += r;
39 mark_point();
40 p = a.alloc(r, n);
41 ck_assert(0 == p);
42 ck_assert(!n);
43 ck_assert(a.size() == s);
44
45 r = test0_size; s += r;
46 mark_point();
47 p = a.alloc(r, n);
48 ck_assert(0 != p);
49 ck_assert(!n);
50 ck_assert(a.size() == s);
51 strcpy (reinterpret_cast<char*>(p), test0);
52
53 r = test1_size; s += r;
54 mark_point();
55 p = a.alloc(r, n);
56 ck_assert(0 != p);
57 ck_assert(n); /* new page must be allocated */
58 ck_assert(a.size() == s);
59 strcpy (reinterpret_cast<char*>(p), test1);
60
61 r = 0; s += r;
62 mark_point();
63 p = a.alloc(r, n);
64 ck_assert(0 == p);
65 ck_assert(!n);
66 ck_assert(a.size() == s);
67
68 #ifdef GU_ALLOCATOR_DEBUG
69 std::vector<gu::Buf> out;
70 out.reserve (a.count());
71 mark_point();
72
73 size_t out_size = a.gather (out);
74
75 ck_assert(out_size == test0_size + test1_size);
76 ck_assert(out.size() == 2);
77 ck_assert(out[0].size == test0_size);
78 ck_assert(!strcmp(reinterpret_cast<const char*>(out[0].ptr), test0));
79 ck_assert(out[1].size == test1_size);
80 ck_assert(!strcmp(reinterpret_cast<const char*>(out[1].ptr), test1));
81 #endif /* GU_ALLOCATOR_DEBUG */
82 }
83 END_TEST
84
gu_alloc_suite()85 Suite* gu_alloc_suite ()
86 {
87 TCase* t = tcase_create ("Allocator");
88 tcase_add_test (t, basic);
89
90 Suite* s = suite_create ("gu::Allocator");
91 suite_add_tcase (s, t);
92
93 return s;
94 }
95