1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission is hereby granted, free of charge, to any person obtaining
5  * a copy of this software and associated documentation files (the
6  * "Software"), to deal in the Software without restriction, including
7  * without limitation the rights to use, copy, modify, merge, publish,
8  * distribute, sublicense, and/or sell copies of the Software, and to
9  * permit persons to whom the Software is furnished to do so, subject to
10  * the following conditions:
11  *
12  * The above copyright notice and this permission notice (including the
13  * next paragraph) shall be included in all copies or substantial
14  * portions of the Software.
15  *
16  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23  * SOFTWARE.
24  */
25 
26 #include <stdio.h>
27 #include <stdlib.h>
28 #include <assert.h>
29 #include "wayland-private.h"
30 #include "test-runner.h"
31 
TEST(list_init)32 TEST(list_init)
33 {
34 	struct wl_list list;
35 
36 	wl_list_init(&list);
37 	assert(list.next == &list);
38 	assert(list.prev == &list);
39 	assert(wl_list_empty(&list));
40 }
41 
42 struct element {
43 	int i;
44 	struct wl_list link;
45 };
46 
TEST(list_insert)47 TEST(list_insert)
48 {
49 	struct wl_list list;
50 	struct element e;
51 
52 	wl_list_init(&list);
53 	wl_list_insert(&list, &e.link);
54 	assert(list.next == &e.link);
55 	assert(list.prev == &e.link);
56 	assert(e.link.next == &list);
57 	assert(e.link.prev == &list);
58 }
59 
TEST(list_length)60 TEST(list_length)
61 {
62 	struct wl_list list;
63 	struct element e;
64 
65 	wl_list_init(&list);
66 	assert(wl_list_length(&list) == 0);
67 	wl_list_insert(&list, &e.link);
68 	assert(wl_list_length(&list) == 1);
69 	wl_list_remove(&e.link);
70 	assert(wl_list_length(&list) == 0);
71 }
72 
TEST(list_iterator)73 TEST(list_iterator)
74 {
75 	struct wl_list list;
76 	struct element e1, e2, e3, e4, *e;
77 	int reference[] = { 708090, 102030, 5588, 12 };
78 	unsigned int i;
79 
80 	e1.i = 708090;
81 	e2.i = 102030;
82 	e3.i = 5588;
83 	e4.i = 12;
84 
85 	wl_list_init(&list);
86 	wl_list_insert(list.prev, &e1.link);
87 	wl_list_insert(list.prev, &e2.link);
88 	wl_list_insert(list.prev, &e3.link);
89 	wl_list_insert(list.prev, &e4.link);
90 
91 	i = 0;
92 	wl_list_for_each(e, &list, link) {
93 		assert(i < ARRAY_LENGTH(reference));
94 		assert(e->i == reference[i]);
95 		i++;
96 	}
97 	assert(i == ARRAY_LENGTH(reference));
98 
99 	i = 0;
100 	wl_list_for_each_reverse(e, &list, link) {
101 		assert(i < ARRAY_LENGTH(reference));
102 		assert(e->i == reference[ARRAY_LENGTH(reference) - i - 1]);
103 		i++;
104 	}
105 	assert(i == ARRAY_LENGTH(reference));
106 }
107 
108 static int
validate_list(struct wl_list * list,int * reference,int length)109 validate_list(struct wl_list *list, int *reference, int length)
110 {
111 	struct element *e;
112 	int i;
113 
114 	i = 0;
115 	wl_list_for_each(e, list, link) {
116 		if (i >= length)
117 			return 0;
118 		if (e->i != reference[i])
119 			return 0;
120 		i++;
121 	}
122 
123 	if (i != length)
124 		return 0;
125 
126 	return 1;
127 }
128 
TEST(list_remove)129 TEST(list_remove)
130 {
131 	struct wl_list list;
132 	struct element e1, e2, e3;
133 	int reference1[] = { 17, 8888, 1000 }, reference2[] = { 17, 1000 };
134 
135 	e1.i = 17;
136 	e2.i = 8888;
137 	e3.i = 1000;
138 
139 	wl_list_init(&list);
140 	wl_list_insert(&list, &e1.link);
141 	wl_list_insert(list.prev, &e2.link);
142 	wl_list_insert(list.prev, &e3.link);
143 	assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
144 
145 	wl_list_remove(&e2.link);
146 	assert(validate_list(&list, reference2, ARRAY_LENGTH(reference2)));
147 }
148 
TEST(list_insert_list)149 TEST(list_insert_list)
150 {
151 	struct wl_list list, other;
152 	struct element e1, e2, e3, e4, e5, e6;
153 	int reference1[] = { 17, 8888, 1000 };
154 	int reference2[] = { 76543, 1, -500 };
155 	int reference3[] = { 17, 76543, 1, -500, 8888, 1000 };
156 
157 	e1.i = 17;
158 	e2.i = 8888;
159 	e3.i = 1000;
160 
161 	wl_list_init(&list);
162 	wl_list_insert(&list, &e1.link);
163 	wl_list_insert(list.prev, &e2.link);
164 	wl_list_insert(list.prev, &e3.link);
165 	assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
166 
167 	e4.i = 76543;
168 	e5.i = 1;
169 	e6.i = -500;
170 
171 	wl_list_init(&other);
172 	wl_list_insert(&other, &e4.link);
173 	wl_list_insert(other.prev, &e5.link);
174 	wl_list_insert(other.prev, &e6.link);
175 	assert(validate_list(&other, reference2, ARRAY_LENGTH(reference2)));
176 
177 	wl_list_insert_list(list.next, &other);
178 	assert(validate_list(&list, reference3, ARRAY_LENGTH(reference3)));
179 }
180