summaryrefslogtreecommitdiffstats
path: root/test/test-map.c
blob: b6d7681ce5e0e6abb7e102114e0e3126bfbb8ac9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
/* PipeWire
 *
 * Copyright © 2021 Red Hat, Inc.
 *
 * Permission is hereby granted, free of charge, to any person obtaining a
 * copy of this software and associated documentation files (the "Software"),
 * to deal in the Software without restriction, including without limitation
 * the rights to use, copy, modify, merge, publish, distribute, sublicense,
 * and/or sell copies of the Software, and to permit persons to whom the
 * Software is furnished to do so, subject to the following conditions:
 *
 * The above copyright notice and this permission notice (including the next
 * paragraph) shall be included in all copies or substantial portions of the
 * Software.
 *
 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
 * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
 * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.  IN NO EVENT SHALL
 * THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
 * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING
 * FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER
 * DEALINGS IN THE SOFTWARE.
 */

#include "pwtest.h"

#include <pipewire/map.h>


PWTEST(map_add_remove)
{
	struct pw_map map = PW_MAP_INIT(2);
	int a, b, c;
	void *p1 = &a, *p2 = &b, *p3 = &c;
	uint32_t idx1, idx2, idx3;

	idx1 = pw_map_insert_new(&map, p1);
	idx2 = pw_map_insert_new(&map, p2);
	idx3 = pw_map_insert_new(&map, p3);

	/* This is implementation-defined behavior and
	 * may change in the future */
	pwtest_int_eq(idx1, 0U);
	pwtest_int_eq(idx2, 1U);
	pwtest_int_eq(idx3, 2U);

	/* public API */
	pwtest_ptr_eq(p1, pw_map_lookup(&map, idx1));
	pwtest_ptr_eq(p2, pw_map_lookup(&map, idx2));
	pwtest_ptr_eq(p3, pw_map_lookup(&map, idx3));

	pw_map_remove(&map, idx1);
	pwtest_ptr_null(pw_map_lookup(&map, idx1));
	pwtest_ptr_eq(p2, pw_map_lookup(&map, idx2));
	pwtest_ptr_eq(p3, pw_map_lookup(&map, idx3));

	pw_map_remove(&map, idx2);
	pwtest_ptr_null(pw_map_lookup(&map, idx1));
	pwtest_ptr_null(pw_map_lookup(&map, idx2));
	pwtest_ptr_eq(p3, pw_map_lookup(&map, idx3));

	pw_map_remove(&map, idx3);
	pwtest_ptr_null(pw_map_lookup(&map, idx1));
	pwtest_ptr_null(pw_map_lookup(&map, idx2));
	pwtest_ptr_null(pw_map_lookup(&map, idx3));

	idx1 = pw_map_insert_new(&map, p1);
	idx2 = pw_map_insert_new(&map, p2);
	idx3 = pw_map_insert_new(&map, p3);

	/* This is implementation-defined behavior and
	 * may change in the future */
	pwtest_int_eq(idx3, 0U);
	pwtest_int_eq(idx2, 1U);
	pwtest_int_eq(idx1, 2U);

	pw_map_clear(&map);

	return PWTEST_PASS;
}

PWTEST(map_insert)
{
	struct pw_map map = PW_MAP_INIT(2);
	int a, b, c, d;
	void *p1 = &a, *p2 = &b, *p3 = &c, *p4 = &d;
	uint32_t idx1, idx2, idx3;
	int rc;
	size_t sz;

	idx1 = pw_map_insert_new(&map, p1);
	idx2 = pw_map_insert_new(&map, p2);
	idx3 = pw_map_insert_new(&map, p3);

	pwtest_ptr_eq(p1, pw_map_lookup(&map, idx1));
	pwtest_ptr_eq(p2, pw_map_lookup(&map, idx2));
	pwtest_ptr_eq(p3, pw_map_lookup(&map, idx3));
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 3U);

	/* overwrite */
	rc = pw_map_insert_at(&map, idx1, p4);
	pwtest_neg_errno_ok(rc);
	pwtest_ptr_eq(p4, pw_map_lookup(&map, idx1));
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 3U);

	/* overwrite */
	rc = pw_map_insert_at(&map, idx2, p4);
	pwtest_neg_errno_ok(rc);
	pwtest_ptr_eq(p4, pw_map_lookup(&map, idx2));
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 3U);

	/* out of bounds  */
	rc = pw_map_insert_at(&map, 10000, p4);
	pwtest_neg_errno(rc, -ENOSPC);

	/* if id is the map size, the item is appended */
	rc = pw_map_insert_at(&map, idx3 + 1, &p4);
	pwtest_neg_errno_ok(rc);
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 4U);

	pw_map_clear(&map);

	return PWTEST_PASS;
}

PWTEST(map_size)
{
	struct pw_map map = PW_MAP_INIT(2);
	int a, b, c;
	void *p1 = &a, *p2 = &b, *p3 = &c;
	uint32_t idx1;
	size_t sz;

	idx1 = pw_map_insert_new(&map, p1);
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 1U);
	pw_map_insert_new(&map, p2);
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 2U);
	pw_map_insert_new(&map, p3);
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 3U);

	/* Removing does not alter the size */
	pw_map_remove(&map, idx1);
	sz = pw_map_get_size(&map);
	pwtest_int_eq(sz, 3U);

	pw_map_clear(&map);

	return PWTEST_PASS;
}

PWTEST(map_double_remove)
{
	struct pw_map map = PW_MAP_INIT(2);
	int a, b, c;
	void *p1 = &a, *p2 = &b, *p3 = &c;

	uint32_t idx1, idx2, idx3;

	idx1 = pw_map_insert_new(&map, p1);
	idx2 = pw_map_insert_new(&map, p2);
	idx3 = pw_map_insert_new(&map, p3);

	pw_map_remove(&map, idx1); /* idx1 in the free list */
	pw_map_remove(&map, idx2); /* idx1 and 2 in the free list */
	pw_map_remove(&map, idx2); /* should be a noop */
	idx1 = pw_map_insert_new(&map, p1);
	idx2 = pw_map_insert_new(&map, p2);

	pwtest_ptr_eq(p1, pw_map_lookup(&map, idx1));
	pwtest_ptr_eq(p2, pw_map_lookup(&map, idx2));
	pwtest_ptr_eq(p3, pw_map_lookup(&map, idx3));

	pw_map_clear(&map);

	return PWTEST_PASS;
}

PWTEST(map_insert_at_free)
{
	struct pw_map map = PW_MAP_INIT(2);
	int data[3] = {1, 2, 3};
	int new_data = 4;
	int *ptr[3] = {&data[0], &data[1], &data[3]};
	int idx[3];
	int rc;

	/* Test cases, for an item at idx:
	 * 1. remove at idx, then reinsert
	 * 2. remove at idx, remove another item, reinsert
	 * 3. remove another item, remove at idx, reinsert
	 * 4. remove another item, remove at index, remove another item, reinsert
	 *
	 * The indices are the respective 2 bits from the iteration counter,
	 * we use index 3 to indicate skipping that step to handle cases 1-3.
	 */
	int iteration = pwtest_get_iteration(current_test);
	int item_idx = iteration & 0x3;
	int before_idx = (iteration >> 2) & 0x3;
	int after_idx = (iteration >> 4) & 0x3;
	const int SKIP = 3;

	if (item_idx == SKIP)
		return PWTEST_PASS;

	idx[0] = pw_map_insert_new(&map, ptr[0]);
	idx[1] = pw_map_insert_new(&map, ptr[1]);
	idx[2] = pw_map_insert_new(&map, ptr[2]);

	if (before_idx != SKIP) {
		before_idx = idx[before_idx];
		pw_map_remove(&map, before_idx);
	}
	pw_map_remove(&map, item_idx);
	if (after_idx != SKIP) {
		after_idx = idx[after_idx];
		pw_map_remove(&map, after_idx);
	}

	rc = pw_map_insert_at(&map, item_idx, &new_data);
	pwtest_neg_errno(rc, -EINVAL);
	pw_map_clear(&map);

	return PWTEST_PASS;
}

PWTEST_SUITE(pw_map)
{
	pwtest_add(map_add_remove, PWTEST_NOARG);
	pwtest_add(map_insert, PWTEST_NOARG);
	pwtest_add(map_size, PWTEST_NOARG);
	pwtest_add(map_double_remove, PWTEST_NOARG);
	pwtest_add(map_insert_at_free, PWTEST_ARG_RANGE, 0, 63);

	return PWTEST_PASS;
}