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