27 */ |
27 */ |
28 |
28 |
29 #include "cx/allocator.h" |
29 #include "cx/allocator.h" |
30 #include <gtest/gtest.h> |
30 #include <gtest/gtest.h> |
31 |
31 |
32 #define SUITE_NAME Allocator |
32 TEST(Allocator, DefaultAllocator) { |
33 |
|
34 TEST(SUITE_NAME, DefaultAllocator) { |
|
35 cx_allocator_class *clazz = cxDefaultAllocator->cl; |
33 cx_allocator_class *clazz = cxDefaultAllocator->cl; |
36 ASSERT_NE(clazz, nullptr); |
34 ASSERT_NE(clazz, nullptr); |
37 } |
35 } |
38 |
36 |
39 TEST(SUITE_NAME, DefaultMalloc) { |
37 TEST(Allocator, DefaultMalloc) { |
40 void *test = cxMalloc(cxDefaultAllocator, 16); |
38 void *test = cxMalloc(cxDefaultAllocator, 16); |
41 ASSERT_NE(test, nullptr); |
39 ASSERT_NE(test, nullptr); |
42 free(test); |
40 free(test); |
43 } |
41 } |
44 |
42 |
45 TEST(SUITE_NAME, DefaultRealloc) { |
43 TEST(Allocator, DefaultRealloc) { |
46 void *test = calloc(8, 1); |
44 void *test = calloc(8, 1); |
47 memcpy(test, "Test", 5); |
45 memcpy(test, "Test", 5); |
48 test = cxRealloc(cxDefaultAllocator, test, 16); |
46 test = cxRealloc(cxDefaultAllocator, test, 16); |
49 ASSERT_NE(test, nullptr); |
47 ASSERT_NE(test, nullptr); |
50 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); |
48 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); |
51 free(test); |
49 free(test); |
52 } |
50 } |
53 |
51 |
54 TEST(SUITE_NAME, Reallocate) { |
52 TEST(Allocator, Reallocate) { |
55 void *test = calloc(8, 1); |
53 void *test = calloc(8, 1); |
56 memcpy(test, "Test", 5); |
54 memcpy(test, "Test", 5); |
57 int ret = cxReallocate(cxDefaultAllocator, &test, 16); |
55 int ret = cxReallocate(cxDefaultAllocator, &test, 16); |
58 EXPECT_EQ(ret, 0); |
56 EXPECT_EQ(ret, 0); |
59 ASSERT_NE(test, nullptr); |
57 ASSERT_NE(test, nullptr); |
60 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); |
58 EXPECT_STREQ(reinterpret_cast<char *>(test), "Test"); |
61 free(test); |
59 free(test); |
62 } |
60 } |
63 |
61 |
64 TEST(SUITE_NAME, DefaultCalloc) { |
62 TEST(Allocator, DefaultCalloc) { |
65 char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2)); |
63 char *test = reinterpret_cast<char *>(cxCalloc(cxDefaultAllocator, 8, 2)); |
66 ASSERT_NE(test, nullptr); |
64 ASSERT_NE(test, nullptr); |
67 for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0); |
65 for (int i = 0; i < 16; i++) ASSERT_EQ(test[i], 0); |
68 free(test); |
66 free(test); |
69 } |
67 } |
70 |
68 |
71 TEST(SUITE_NAME, DefaultFree) { |
69 TEST(Allocator, DefaultFree) { |
72 void *test = malloc(16); |
70 void *test = malloc(16); |
73 EXPECT_NO_FATAL_FAILURE( |
71 EXPECT_NO_FATAL_FAILURE( |
74 cxFree(cxDefaultAllocator, test); |
72 cxFree(cxDefaultAllocator, test); |
75 ); |
73 ); |
76 } |
74 } |