1 // SPDX-License-Identifier: BSD-3-Clause
2 //
3 // Copyright(c) 2018 Intel Corporation. All rights reserved.
4 //
5 // Author: Janusz Jankowski <janusz.jankowski@linux.intel.com>
6 
7 #include <sof/list.h>
8 
9 #include <stdlib.h>
10 #include <stdarg.h>
11 #include <stddef.h>
12 #include <setjmp.h>
13 #include <stdint.h>
14 #include <cmocka.h>
15 
16 struct test_data {
17 	struct list_item *head;
18 	struct list_item *tail_minus_1;
19 	struct list_item *tail;
20 };
21 
setup(void ** state)22 static int setup(void **state)
23 {
24 	struct test_data *data = malloc(sizeof(struct test_data));
25 
26 	if (!data)
27 		return -1;
28 
29 	data->head = malloc(sizeof(struct list_item));
30 	data->tail_minus_1 = malloc(sizeof(struct list_item));
31 	data->tail = malloc(sizeof(struct list_item));
32 
33 	if (!data->head || !data->tail_minus_1
34 			|| !data->tail) {
35 		free(data->head);
36 		free(data->tail_minus_1);
37 		free(data->tail);
38 
39 		free(data);
40 
41 		return -1;
42 	}
43 
44 	list_init(data->head);
45 	list_init(data->tail_minus_1);
46 	list_init(data->tail);
47 
48 	list_item_append(data->tail_minus_1, data->head);
49 	list_item_append(data->tail, data->head);
50 
51 	*state = data;
52 	return 0;
53 }
54 
teardown(void ** state)55 static int teardown(void **state)
56 {
57 	struct test_data *data = *state;
58 
59 	free(data->head);
60 	free(data->tail_minus_1);
61 	free(data->tail);
62 
63 	free(data);
64 	return 0;
65 }
66 
test_list_list_item_append_head_prev_is_tail(void ** state)67 static void test_list_list_item_append_head_prev_is_tail(void **state)
68 {
69 	struct test_data *data = *state;
70 
71 	assert_ptr_equal(data->head->prev, data->tail);
72 }
73 
test_list_list_item_append_head_next_is_tail_minus_1(void ** state)74 static void test_list_list_item_append_head_next_is_tail_minus_1(void **state)
75 {
76 	struct test_data *data = *state;
77 
78 	assert_ptr_equal(data->head->next, data->tail_minus_1);
79 }
80 
test_list_list_item_append_tail_minus_1_prev_is_head(void ** state)81 static void test_list_list_item_append_tail_minus_1_prev_is_head(void **state)
82 {
83 	struct test_data *data = *state;
84 
85 	assert_ptr_equal(data->tail_minus_1->prev, data->head);
86 }
87 
test_list_list_item_append_tail_minus_1_next_is_tail(void ** state)88 static void test_list_list_item_append_tail_minus_1_next_is_tail(void **state)
89 {
90 	struct test_data *data = *state;
91 
92 	assert_ptr_equal(data->tail_minus_1->next, data->tail);
93 }
94 
test_list_list_item_append_tail_prev_is_tail_minus_1(void ** state)95 static void test_list_list_item_append_tail_prev_is_tail_minus_1(void **state)
96 {
97 	struct test_data *data = *state;
98 
99 	assert_ptr_equal(data->tail->prev, data->tail_minus_1);
100 }
101 
test_list_list_item_append_tail_next_is_head(void ** state)102 static void test_list_list_item_append_tail_next_is_head(void **state)
103 {
104 	struct test_data *data = *state;
105 
106 	assert_ptr_equal(data->tail->next, data->head);
107 }
108 
main(void)109 int main(void)
110 {
111 	const struct CMUnitTest tests[] = {
112 		cmocka_unit_test(test_list_list_item_append_head_prev_is_tail),
113 		cmocka_unit_test(test_list_list_item_append_head_next_is_tail_minus_1),
114 		cmocka_unit_test(test_list_list_item_append_tail_minus_1_prev_is_head),
115 		cmocka_unit_test(test_list_list_item_append_tail_minus_1_next_is_tail),
116 		cmocka_unit_test(test_list_list_item_append_tail_prev_is_tail_minus_1),
117 		cmocka_unit_test(test_list_list_item_append_tail_next_is_head),
118 	};
119 
120 	cmocka_set_message_output(CM_OUTPUT_TAP);
121 
122 	return cmocka_run_group_tests(tests, setup, teardown);
123 }
124