xref: /aosp_15_r20/external/wayland/tests/map-test.c (revision 84e872a0dc482bffdb63672969dd03a827d67c73)
1*84e872a0SLloyd Pique /*
2*84e872a0SLloyd Pique  * Copyright © 2012 Intel Corporation
3*84e872a0SLloyd Pique  *
4*84e872a0SLloyd Pique  * Permission is hereby granted, free of charge, to any person obtaining
5*84e872a0SLloyd Pique  * a copy of this software and associated documentation files (the
6*84e872a0SLloyd Pique  * "Software"), to deal in the Software without restriction, including
7*84e872a0SLloyd Pique  * without limitation the rights to use, copy, modify, merge, publish,
8*84e872a0SLloyd Pique  * distribute, sublicense, and/or sell copies of the Software, and to
9*84e872a0SLloyd Pique  * permit persons to whom the Software is furnished to do so, subject to
10*84e872a0SLloyd Pique  * the following conditions:
11*84e872a0SLloyd Pique  *
12*84e872a0SLloyd Pique  * The above copyright notice and this permission notice (including the
13*84e872a0SLloyd Pique  * next paragraph) shall be included in all copies or substantial
14*84e872a0SLloyd Pique  * portions of the Software.
15*84e872a0SLloyd Pique  *
16*84e872a0SLloyd Pique  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
17*84e872a0SLloyd Pique  * EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
18*84e872a0SLloyd Pique  * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
19*84e872a0SLloyd Pique  * NONINFRINGEMENT.  IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS
20*84e872a0SLloyd Pique  * BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
21*84e872a0SLloyd Pique  * ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
22*84e872a0SLloyd Pique  * CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
23*84e872a0SLloyd Pique  * SOFTWARE.
24*84e872a0SLloyd Pique  */
25*84e872a0SLloyd Pique 
26*84e872a0SLloyd Pique #include <stdio.h>
27*84e872a0SLloyd Pique #include <stdlib.h>
28*84e872a0SLloyd Pique #include <stdint.h>
29*84e872a0SLloyd Pique #include <assert.h>
30*84e872a0SLloyd Pique #include "wayland-private.h"
31*84e872a0SLloyd Pique #include "test-runner.h"
32*84e872a0SLloyd Pique 
TEST(map_insert_new)33*84e872a0SLloyd Pique TEST(map_insert_new)
34*84e872a0SLloyd Pique {
35*84e872a0SLloyd Pique 	struct wl_map map;
36*84e872a0SLloyd Pique 	uint32_t i, j, k, a, b, c;
37*84e872a0SLloyd Pique 
38*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_SERVER_SIDE);
39*84e872a0SLloyd Pique 	i = wl_map_insert_new(&map, 0, &a);
40*84e872a0SLloyd Pique 	j = wl_map_insert_new(&map, 0, &b);
41*84e872a0SLloyd Pique 	k = wl_map_insert_new(&map, 0, &c);
42*84e872a0SLloyd Pique 	assert(i == WL_SERVER_ID_START);
43*84e872a0SLloyd Pique 	assert(j == WL_SERVER_ID_START + 1);
44*84e872a0SLloyd Pique 	assert(k == WL_SERVER_ID_START + 2);
45*84e872a0SLloyd Pique 
46*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, i) == &a);
47*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, j) == &b);
48*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, k) == &c);
49*84e872a0SLloyd Pique     wl_map_release(&map);
50*84e872a0SLloyd Pique 
51*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_CLIENT_SIDE);
52*84e872a0SLloyd Pique 	i = wl_map_insert_new(&map, 0, &a);
53*84e872a0SLloyd Pique 	assert(i == 0);
54*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, i) == &a);
55*84e872a0SLloyd Pique 
56*84e872a0SLloyd Pique 	wl_map_release(&map);
57*84e872a0SLloyd Pique }
58*84e872a0SLloyd Pique 
TEST(map_insert_at)59*84e872a0SLloyd Pique TEST(map_insert_at)
60*84e872a0SLloyd Pique {
61*84e872a0SLloyd Pique 	struct wl_map map;
62*84e872a0SLloyd Pique 	uint32_t a, b, c;
63*84e872a0SLloyd Pique 
64*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_CLIENT_SIDE);
65*84e872a0SLloyd Pique 	assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START, &a) == 0);
66*84e872a0SLloyd Pique 	assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START + 3, &b) == -1);
67*84e872a0SLloyd Pique 	assert(wl_map_insert_at(&map, 0, WL_SERVER_ID_START + 1, &c) == 0);
68*84e872a0SLloyd Pique 
69*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, WL_SERVER_ID_START) == &a);
70*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, WL_SERVER_ID_START + 1) == &c);
71*84e872a0SLloyd Pique 
72*84e872a0SLloyd Pique 	wl_map_release(&map);
73*84e872a0SLloyd Pique }
74*84e872a0SLloyd Pique 
TEST(map_remove)75*84e872a0SLloyd Pique TEST(map_remove)
76*84e872a0SLloyd Pique {
77*84e872a0SLloyd Pique 	struct wl_map map;
78*84e872a0SLloyd Pique 	uint32_t i, j, k, l, a, b, c, d;
79*84e872a0SLloyd Pique 
80*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_SERVER_SIDE);
81*84e872a0SLloyd Pique 	i = wl_map_insert_new(&map, 0, &a);
82*84e872a0SLloyd Pique 	j = wl_map_insert_new(&map, 0, &b);
83*84e872a0SLloyd Pique 	k = wl_map_insert_new(&map, 0, &c);
84*84e872a0SLloyd Pique 	assert(i == WL_SERVER_ID_START);
85*84e872a0SLloyd Pique 	assert(j == WL_SERVER_ID_START + 1);
86*84e872a0SLloyd Pique 	assert(k == WL_SERVER_ID_START + 2);
87*84e872a0SLloyd Pique 
88*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, i) == &a);
89*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, j) == &b);
90*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, k) == &c);
91*84e872a0SLloyd Pique 
92*84e872a0SLloyd Pique 	wl_map_remove(&map, j);
93*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, j) == NULL);
94*84e872a0SLloyd Pique 
95*84e872a0SLloyd Pique 	/* Verify that we insert d at the hole left by removing b */
96*84e872a0SLloyd Pique 	l = wl_map_insert_new(&map, 0, &d);
97*84e872a0SLloyd Pique 	assert(l == WL_SERVER_ID_START + 1);
98*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, l) == &d);
99*84e872a0SLloyd Pique 
100*84e872a0SLloyd Pique 	wl_map_release(&map);
101*84e872a0SLloyd Pique }
102*84e872a0SLloyd Pique 
TEST(map_flags)103*84e872a0SLloyd Pique TEST(map_flags)
104*84e872a0SLloyd Pique {
105*84e872a0SLloyd Pique 	struct wl_map map;
106*84e872a0SLloyd Pique 	uint32_t i, j, a, b;
107*84e872a0SLloyd Pique 
108*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_SERVER_SIDE);
109*84e872a0SLloyd Pique 	i = wl_map_insert_new(&map, 0, &a);
110*84e872a0SLloyd Pique 	j = wl_map_insert_new(&map, 1, &b);
111*84e872a0SLloyd Pique 	assert(i == WL_SERVER_ID_START);
112*84e872a0SLloyd Pique 	assert(j == WL_SERVER_ID_START + 1);
113*84e872a0SLloyd Pique 
114*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, i) == &a);
115*84e872a0SLloyd Pique 	assert(wl_map_lookup(&map, j) == &b);
116*84e872a0SLloyd Pique 
117*84e872a0SLloyd Pique     assert(wl_map_lookup_flags(&map, i) == 0);
118*84e872a0SLloyd Pique     assert(wl_map_lookup_flags(&map, j) == 1);
119*84e872a0SLloyd Pique 
120*84e872a0SLloyd Pique 	wl_map_release(&map);
121*84e872a0SLloyd Pique }
122*84e872a0SLloyd Pique 
never_run(void * element,void * data,uint32_t flags)123*84e872a0SLloyd Pique static enum wl_iterator_result never_run(void *element, void *data, uint32_t flags)
124*84e872a0SLloyd Pique {
125*84e872a0SLloyd Pique 	assert(0);
126*84e872a0SLloyd Pique }
127*84e872a0SLloyd Pique 
TEST(map_iter_empty)128*84e872a0SLloyd Pique TEST(map_iter_empty)
129*84e872a0SLloyd Pique {
130*84e872a0SLloyd Pique 	struct wl_map map;
131*84e872a0SLloyd Pique 
132*84e872a0SLloyd Pique 	wl_map_init(&map, WL_MAP_SERVER_SIDE);
133*84e872a0SLloyd Pique 
134*84e872a0SLloyd Pique 	wl_map_for_each(&map, never_run, NULL);
135*84e872a0SLloyd Pique 
136*84e872a0SLloyd Pique 	wl_map_release(&map);
137*84e872a0SLloyd Pique }
138