1 | #include "mycpp/bump_leak_heap.h"
|
2 |
|
3 | #include <unistd.h> // STDERR_FILENO
|
4 |
|
5 | #include "mycpp/gc_alloc.h" // gHeap
|
6 | #include "vendor/greatest.h"
|
7 |
|
8 | TEST Reallocate_test() {
|
9 | log("sizeof(size_t) = %zu", sizeof(size_t));
|
10 |
|
11 | BumpLeakHeap h;
|
12 |
|
13 | char* p1 = static_cast<char*>(h.Allocate(10));
|
14 | strcpy(p1, "abcdef");
|
15 | log("p1 = %p %s", p1, p1);
|
16 |
|
17 | char* p2 = static_cast<char*>(h.Reallocate(p1, 20));
|
18 | log("p2 = %p %s", p2, p2);
|
19 | ASSERT_EQ_FMT(0, strcmp(p1, p2), "%d");
|
20 |
|
21 | char* p3 = static_cast<char*>(h.Reallocate(p2, 30));
|
22 | log("p3 = %p %s", p3, p3);
|
23 | ASSERT_EQ_FMT(0, strcmp(p1, p3), "%d");
|
24 |
|
25 | PASS();
|
26 | }
|
27 |
|
28 | // Test empty and trivial methods
|
29 | TEST for_code_coverage() {
|
30 | BumpLeakHeap h;
|
31 |
|
32 | h.Init();
|
33 | h.Init(10);
|
34 | h.PushRoot(nullptr);
|
35 | h.PopRoot();
|
36 | h.RootGlobalVar(nullptr);
|
37 | (void)h.Allocate(10);
|
38 | ASSERT_EQ(-1, h.MaybeCollect());
|
39 |
|
40 | h.PrintStats(STDERR_FILENO);
|
41 |
|
42 | h.ProcessExit();
|
43 | h.CleanProcessExit();
|
44 |
|
45 | PASS();
|
46 | }
|
47 |
|
48 | GREATEST_MAIN_DEFS();
|
49 |
|
50 | int main(int argc, char** argv) {
|
51 | gHeap.Init();
|
52 |
|
53 | GREATEST_MAIN_BEGIN();
|
54 |
|
55 | RUN_TEST(Reallocate_test);
|
56 | RUN_TEST(for_code_coverage);
|
57 |
|
58 | gHeap.CleanProcessExit();
|
59 |
|
60 | GREATEST_MAIN_END();
|
61 | return 0;
|
62 | }
|