1 #include <gtest/gtest.h> 2 3 extern "C" { 4 #include "list.h" 5 #include "osi.h" 6 } 7 8 TEST(ListTest, test_new_simple) { 9 list_t *list = list_new(NULL); 10 ASSERT_TRUE(list != NULL); 11 } 12 13 TEST(ListTest, test_free_simple) { 14 // In this test we just verify that list_free is callable with a valid list. 15 list_t *list = list_new(NULL); 16 list_free(list); 17 } 18 19 TEST(ListTest, test_free_null) { 20 // In this test we just verify that list_free is callable with NULL. 21 list_free(NULL); 22 } 23 24 TEST(ListTest, test_empty_list_is_empty) { 25 list_t *list = list_new(NULL); 26 EXPECT_TRUE(list_is_empty(list)); 27 list_free(list); 28 } 29 30 TEST(ListTest, test_empty_list_has_no_length) { 31 list_t *list = list_new(NULL); 32 EXPECT_EQ(list_length(list), 0U); 33 list_free(list); 34 } 35 36 TEST(ListTest, test_simple_list_prepend) { 37 list_t *list = list_new(NULL); 38 EXPECT_TRUE(list_prepend(list, &list)); 39 EXPECT_FALSE(list_is_empty(list)); 40 EXPECT_EQ(list_length(list), 1U); 41 list_free(list); 42 } 43 44 TEST(ListTest, test_simple_list_append) { 45 list_t *list = list_new(NULL); 46 EXPECT_TRUE(list_append(list, &list)); 47 EXPECT_FALSE(list_is_empty(list)); 48 EXPECT_EQ(list_length(list), 1U); 49 list_free(list); 50 } 51 52 TEST(ListTest, test_list_remove_found) { 53 list_t *list = list_new(NULL); 54 list_append(list, &list); 55 EXPECT_TRUE(list_remove(list, &list)); 56 EXPECT_TRUE(list_is_empty(list)); 57 EXPECT_EQ(list_length(list), 0U); 58 list_free(list); 59 } 60 61 TEST(ListTest, test_list_remove_not_found) { 62 int x; 63 list_t *list = list_new(NULL); 64 list_append(list, &list); 65 EXPECT_FALSE(list_remove(list, &x)); 66 EXPECT_FALSE(list_is_empty(list)); 67 EXPECT_EQ(list_length(list), 1U); 68 list_free(list); 69 } 70 71 TEST(ListTest, test_list_front) { 72 int x[] = { 1, 2, 3, 4, 5 }; 73 list_t *list = list_new(NULL); 74 75 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) 76 list_append(list, &x[i]); 77 78 EXPECT_EQ(list_front(list), &x[0]); 79 80 list_free(list); 81 } 82 83 TEST(ListTest, test_list_back) { 84 int x[] = { 1, 2, 3, 4, 5 }; 85 list_t *list = list_new(NULL); 86 87 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) 88 list_append(list, &x[i]); 89 90 EXPECT_EQ(list_back(list), &x[ARRAY_SIZE(x) - 1]); 91 92 list_free(list); 93 } 94 95 TEST(ListTest, test_list_clear) { 96 int x[] = { 1, 2, 3, 4, 5 }; 97 list_t *list = list_new(NULL); 98 99 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) 100 list_append(list, &x[i]); 101 102 list_clear(list); 103 EXPECT_TRUE(list_is_empty(list)); 104 EXPECT_EQ(list_length(list), 0U); 105 106 list_free(list); 107 } 108 109 TEST(ListTest, test_list_append_multiple) { 110 int x[] = { 1, 2, 3, 4, 5 }; 111 list_t *list = list_new(NULL); 112 113 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) 114 list_append(list, &x[i]); 115 116 int i = 0; 117 for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node), ++i) 118 EXPECT_EQ(list_node(node), &x[i]); 119 120 list_free(list); 121 } 122 123 TEST(ListTest, test_list_prepend_multiple) { 124 int x[] = { 1, 2, 3, 4, 5 }; 125 list_t *list = list_new(NULL); 126 127 for (size_t i = 0; i < ARRAY_SIZE(x); ++i) 128 list_prepend(list, &x[i]); 129 130 int i = ARRAY_SIZE(x) - 1; 131 for (const list_node_t *node = list_begin(list); node != list_end(list); node = list_next(node), --i) 132 EXPECT_EQ(list_node(node), &x[i]); 133 134 list_free(list); 135 } 136 137 TEST(ListTest, test_list_begin_empty_list) { 138 list_t *list = list_new(NULL); 139 EXPECT_EQ(list_begin(list), list_end(list)); 140 list_free(list); 141 } 142 143 TEST(ListTest, test_list_next) { 144 list_t *list = list_new(NULL); 145 list_append(list, &list); 146 EXPECT_NE(list_begin(list), list_end(list)); 147 EXPECT_EQ(list_next(list_begin(list)), list_end(list)); 148 list_free(list); 149 } 150