protocol: wl_surface.frame needs wl_surface.commit
[profile/ivi/wayland.git] / tests / list-test.c
1 /*
2  * Copyright © 2012 Intel Corporation
3  *
4  * Permission to use, copy, modify, distribute, and sell this software and its
5  * documentation for any purpose is hereby granted without fee, provided that
6  * the above copyright notice appear in all copies and that both that copyright
7  * notice and this permission notice appear in supporting documentation, and
8  * that the name of the copyright holders not be used in advertising or
9  * publicity pertaining to distribution of the software without specific,
10  * written prior permission.  The copyright holders make no representations
11  * about the suitability of this software for any purpose.  It is provided "as
12  * is" without express or implied warranty.
13  *
14  * THE COPYRIGHT HOLDERS DISCLAIM ALL WARRANTIES WITH REGARD TO THIS SOFTWARE,
15  * INCLUDING ALL IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS, IN NO
16  * EVENT SHALL THE COPYRIGHT HOLDERS BE LIABLE FOR ANY SPECIAL, INDIRECT OR
17  * CONSEQUENTIAL DAMAGES OR ANY DAMAGES WHATSOEVER RESULTING FROM LOSS OF USE,
18  * DATA OR PROFITS, WHETHER IN AN ACTION OF CONTRACT, NEGLIGENCE OR OTHER
19  * TORTIOUS ACTION, ARISING OUT OF OR IN CONNECTION WITH THE USE OR PERFORMANCE
20  * OF THIS SOFTWARE.
21  */
22
23 #include <stdio.h>
24 #include <stdlib.h>
25 #include <assert.h>
26 #include "wayland-private.h"
27 #include "test-runner.h"
28
29 TEST(list_init)
30 {
31         struct wl_list list;
32
33         wl_list_init(&list);
34         assert(list.next == &list);
35         assert(list.prev == &list);
36         assert(wl_list_empty(&list));
37 }
38
39 struct element {
40         int i;
41         struct wl_list link;
42 };
43
44 TEST(list_insert)
45 {
46         struct wl_list list;
47         struct element e;
48
49         wl_list_init(&list);
50         wl_list_insert(&list, &e.link);
51         assert(list.next == &e.link);
52         assert(list.prev == &e.link);
53         assert(e.link.next == &list);
54         assert(e.link.prev == &list);
55 }
56
57 TEST(list_iterator)
58 {
59         struct wl_list list;
60         struct element e1, e2, e3, e4, *e;
61         int reference[] = { 708090, 102030, 5588, 12 };
62         unsigned int i;
63
64         e1.i = 708090;
65         e2.i = 102030;
66         e3.i = 5588;
67         e4.i = 12;
68
69         wl_list_init(&list);
70         wl_list_insert(list.prev, &e1.link);
71         wl_list_insert(list.prev, &e2.link);
72         wl_list_insert(list.prev, &e3.link);
73         wl_list_insert(list.prev, &e4.link);
74
75         i = 0;
76         wl_list_for_each(e, &list, link) {
77                 assert(i < ARRAY_LENGTH(reference));
78                 assert(e->i == reference[i]);
79                 i++;
80         }
81         assert(i == ARRAY_LENGTH(reference));
82
83         i = 0;
84         wl_list_for_each_reverse(e, &list, link) {
85                 assert(i < ARRAY_LENGTH(reference));
86                 assert(e->i == reference[ARRAY_LENGTH(reference) - i - 1]);
87                 i++;
88         }
89         assert(i == ARRAY_LENGTH(reference));
90 }
91
92 static int
93 validate_list(struct wl_list *list, int *reference, int length)
94 {
95         struct element *e;
96         int i;
97
98         i = 0;
99         wl_list_for_each(e, list, link) {
100                 if (i >= length)
101                         return 0;
102                 if (e->i != reference[i])
103                         return 0;
104                 i++;
105         }
106                 
107         if (i != length)
108                 return 0;
109
110         return 1;
111 }
112
113 TEST(list_remove)
114 {
115         struct wl_list list;
116         struct element e1, e2, e3;
117         int reference1[] = { 17, 8888, 1000 }, reference2[] = { 17, 1000 };
118
119         e1.i = 17;
120         e2.i = 8888;
121         e3.i = 1000;
122
123         wl_list_init(&list);
124         wl_list_insert(&list, &e1.link);
125         wl_list_insert(list.prev, &e2.link);
126         wl_list_insert(list.prev, &e3.link);
127         assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
128
129         wl_list_remove(&e2.link);
130         assert(validate_list(&list, reference2, ARRAY_LENGTH(reference2)));
131 }
132
133 TEST(list_insert_list)
134 {
135         struct wl_list list, other;
136         struct element e1, e2, e3, e4, e5, e6;
137         int reference1[] = { 17, 8888, 1000 };
138         int reference2[] = { 76543, 1, -500 };
139         int reference3[] = { 17, 76543, 1, -500, 8888, 1000 };
140
141         e1.i = 17;
142         e2.i = 8888;
143         e3.i = 1000;
144
145         wl_list_init(&list);
146         wl_list_insert(&list, &e1.link);
147         wl_list_insert(list.prev, &e2.link);
148         wl_list_insert(list.prev, &e3.link);
149         assert(validate_list(&list, reference1, ARRAY_LENGTH(reference1)));
150
151         e4.i = 76543;
152         e5.i = 1;
153         e6.i = -500;
154
155         wl_list_init(&other);
156         wl_list_insert(&other, &e4.link);
157         wl_list_insert(other.prev, &e5.link);
158         wl_list_insert(other.prev, &e6.link);
159         assert(validate_list(&other, reference2, ARRAY_LENGTH(reference2)));
160
161         wl_list_insert_list(list.next, &other);
162         assert(validate_list(&list, reference3, ARRAY_LENGTH(reference3)));
163 }