1 #include <gtest/gtest.h>
2
3 #include "AllocationTestHarness.h"
4
5 #include "osi/include/array.h"
6
7 class ArrayTest : public AllocationTestHarness {};
8
TEST_F(ArrayTest,test_new_free_simple)9 TEST_F(ArrayTest, test_new_free_simple) {
10 array_t* array = array_new(4);
11 ASSERT_TRUE(array != NULL);
12 array_free(array);
13 }
14
TEST_F(ArrayTest,test_free_null)15 TEST_F(ArrayTest, test_free_null) { array_free(NULL); }
16
TEST_F(ArrayTest,test_invalid_ptr)17 TEST_F(ArrayTest, test_invalid_ptr) {
18 array_t* array = array_new(4);
19 EXPECT_DEATH(array_ptr(array), "");
20 array_free(array);
21 }
22
TEST_F(ArrayTest,test_invalid_at)23 TEST_F(ArrayTest, test_invalid_at) {
24 array_t* array = array_new(4);
25 EXPECT_DEATH(array_at(array, 1), "");
26 array_free(array);
27 }
28
TEST_F(ArrayTest,test_append_value)29 TEST_F(ArrayTest, test_append_value) {
30 array_t* array = array_new(sizeof(int));
31 for (int i = 0; i < 100; ++i) {
32 array_append_value(array, i * i);
33 }
34 for (int i = 0; i < 100; ++i) {
35 EXPECT_EQ(*(int*)array_at(array, i), i * i);
36 }
37 array_free(array);
38 }
39
TEST_F(ArrayTest,test_append_ptr)40 TEST_F(ArrayTest, test_append_ptr) {
41 int items[100];
42 array_t* array = array_new(sizeof(int));
43 for (int i = 0; i < 100; ++i) {
44 items[i] = i * i;
45 array_append_ptr(array, &items[i]);
46 }
47 for (int i = 0; i < 100; ++i) {
48 EXPECT_EQ(*(int*)array_at(array, i), i * i);
49 }
50 array_free(array);
51 }
52
TEST_F(ArrayTest,test_large_element)53 TEST_F(ArrayTest, test_large_element) {
54 char strings[][128] = {
55 "string 1", "string 2", "string 3", "string 4",
56 "string 5", "string 6", "string 7", "string 8",
57 };
58
59 array_t* array = array_new(128);
60 for (int i = 0; i < 100; ++i) {
61 array_append_ptr(array, strings[i % 8]);
62 }
63 for (int i = 0; i < 100; ++i) {
64 EXPECT_TRUE(!memcmp(array_at(array, i), strings[i % 8], 128));
65 }
66 array_free(array);
67 }
68