aboutsummaryrefslogtreecommitdiff
path: root/test/validation/api/event/event.c
blob: f6ad863657b4870e70d787455a7fa601eaf64818 (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
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
/* Copyright (c) 2017-2018, Linaro Limited
 * Copyright (c) 2023, Nokia
 * All rights reserved.
 *
 * SPDX-License-Identifier:     BSD-3-Clause
 */

#include <odp_api.h>
#include <odp_cunit_common.h>

#define NUM_EVENTS  100
#define EVENT_SIZE  100
#define EVENT_BURST 10

static void event_test_free(void)
{
	odp_pool_t pool;
	odp_pool_param_t pool_param;
	int i;
	odp_buffer_t buf;
	odp_packet_t pkt;
	odp_timeout_t tmo;
	odp_event_subtype_t subtype;
	odp_event_t event[EVENT_BURST];

	/* Buffer events */
	odp_pool_param_init(&pool_param);
	pool_param.buf.num  = NUM_EVENTS;
	pool_param.buf.size = EVENT_SIZE;
	pool_param.type     = ODP_POOL_BUFFER;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (i = 0; i < EVENT_BURST; i++) {
		buf = odp_buffer_alloc(pool);
		CU_ASSERT(odp_event_is_valid(odp_buffer_to_event(buf)) == 1);
		CU_ASSERT_FATAL(buf != ODP_BUFFER_INVALID);
		event[i] = odp_buffer_to_event(buf);
		CU_ASSERT(odp_event_type(event[i]) == ODP_EVENT_BUFFER);
		CU_ASSERT(odp_event_subtype(event[i]) == ODP_EVENT_NO_SUBTYPE);
		CU_ASSERT(odp_event_types(event[i], &subtype) ==
			  ODP_EVENT_BUFFER);
		CU_ASSERT(subtype == ODP_EVENT_NO_SUBTYPE);
	}

	for (i = 0; i < EVENT_BURST; i++)
		odp_event_free(event[i]);

	CU_ASSERT(odp_pool_destroy(pool) == 0);

	/* Packet events */
	odp_pool_param_init(&pool_param);
	pool_param.pkt.num = NUM_EVENTS;
	pool_param.pkt.len = EVENT_SIZE;
	pool_param.type    = ODP_POOL_PACKET;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (i = 0; i < EVENT_BURST; i++) {
		pkt = odp_packet_alloc(pool, EVENT_SIZE);
		CU_ASSERT(odp_event_is_valid(odp_packet_to_event(pkt)) == 1);
		CU_ASSERT_FATAL(pkt != ODP_PACKET_INVALID);
		event[i] = odp_packet_to_event(pkt);
		CU_ASSERT(odp_event_type(event[i]) == ODP_EVENT_PACKET);
		CU_ASSERT(odp_event_subtype(event[i]) ==
			  ODP_EVENT_PACKET_BASIC);
		CU_ASSERT(odp_event_types(event[i], &subtype) ==
			  ODP_EVENT_PACKET);
		CU_ASSERT(subtype == ODP_EVENT_PACKET_BASIC);
	}

	for (i = 0; i < EVENT_BURST; i++)
		odp_event_free(event[i]);

	CU_ASSERT(odp_pool_destroy(pool) == 0);

	/* Timeout events */
	odp_pool_param_init(&pool_param);
	pool_param.tmo.num = NUM_EVENTS;
	pool_param.type    = ODP_POOL_TIMEOUT;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (i = 0; i < EVENT_BURST; i++) {
		tmo = odp_timeout_alloc(pool);
		CU_ASSERT(odp_event_is_valid(odp_timeout_to_event(tmo)) == 1);
		CU_ASSERT_FATAL(tmo != ODP_TIMEOUT_INVALID);
		event[i] = odp_timeout_to_event(tmo);
		CU_ASSERT(odp_event_type(event[i]) == ODP_EVENT_TIMEOUT);
		CU_ASSERT(odp_event_subtype(event[i]) == ODP_EVENT_NO_SUBTYPE);
		CU_ASSERT(odp_event_types(event[i], &subtype) ==
			  ODP_EVENT_TIMEOUT);
		CU_ASSERT(subtype == ODP_EVENT_NO_SUBTYPE);
	}

	for (i = 0; i < EVENT_BURST; i++)
		odp_event_free(event[i]);

	CU_ASSERT(odp_pool_destroy(pool) == 0);
}

static void event_test_free_multi(void)
{
	odp_pool_t pool;
	odp_pool_param_t pool_param;
	int i, j;
	odp_buffer_t buf;
	odp_packet_t pkt;
	odp_timeout_t tmo;
	odp_event_t event[EVENT_BURST];

	/* Buffer events */
	odp_pool_param_init(&pool_param);
	pool_param.buf.num  = NUM_EVENTS;
	pool_param.buf.size = EVENT_SIZE;
	pool_param.type     = ODP_POOL_BUFFER;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (j = 0; j < 2; j++) {
		for (i = 0; i < EVENT_BURST; i++) {
			buf = odp_buffer_alloc(pool);
			CU_ASSERT_FATAL(buf != ODP_BUFFER_INVALID);
			event[i] = odp_buffer_to_event(buf);
		}

		if (j == 0)
			odp_event_free_multi(event, EVENT_BURST);
		else
			odp_event_free_sp(event, EVENT_BURST);
	}

	CU_ASSERT(odp_pool_destroy(pool) == 0);

	/* Packet events */
	odp_pool_param_init(&pool_param);
	pool_param.pkt.num = NUM_EVENTS;
	pool_param.pkt.len = EVENT_SIZE;
	pool_param.type    = ODP_POOL_PACKET;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (j = 0; j < 2; j++) {
		for (i = 0; i < EVENT_BURST; i++) {
			pkt = odp_packet_alloc(pool, EVENT_SIZE);
			CU_ASSERT_FATAL(pkt != ODP_PACKET_INVALID);
			event[i] = odp_packet_to_event(pkt);
		}

		if (j == 0)
			odp_event_free_multi(event, EVENT_BURST);
		else
			odp_event_free_sp(event, EVENT_BURST);
	}

	CU_ASSERT(odp_pool_destroy(pool) == 0);

	/* Timeout events */
	odp_pool_param_init(&pool_param);
	pool_param.tmo.num = NUM_EVENTS;
	pool_param.type    = ODP_POOL_TIMEOUT;

	pool = odp_pool_create("event_free", &pool_param);
	CU_ASSERT_FATAL(pool != ODP_POOL_INVALID);

	for (j = 0; j < 2; j++) {
		for (i = 0; i < EVENT_BURST; i++) {
			tmo = odp_timeout_alloc(pool);
			CU_ASSERT_FATAL(tmo != ODP_TIMEOUT_INVALID);
			event[i] = odp_timeout_to_event(tmo);
		}

		if (j == 0)
			odp_event_free_multi(event, EVENT_BURST);
		else
			odp_event_free_sp(event, EVENT_BURST);
	}

	CU_ASSERT(odp_pool_destroy(pool) == 0);
}

static void event_test_free_multi_mixed(void)
{
	odp_pool_t pool1, pool2, pool3;
	odp_pool_param_t pool_param;
	int i, j;
	odp_buffer_t buf;
	odp_packet_t pkt;
	odp_timeout_t tmo;
	odp_event_t event[3 * EVENT_BURST];

	/* Buffer events */
	odp_pool_param_init(&pool_param);
	pool_param.buf.num  = NUM_EVENTS;
	pool_param.buf.size = EVENT_SIZE;
	pool_param.type     = ODP_POOL_BUFFER;

	pool1 = odp_pool_create("event_free1", &pool_param);
	CU_ASSERT_FATAL(pool1 != ODP_POOL_INVALID);

	/* Packet events */
	odp_pool_param_init(&pool_param);
	pool_param.pkt.num = NUM_EVENTS;
	pool_param.pkt.len = EVENT_SIZE;
	pool_param.type    = ODP_POOL_PACKET;

	pool2 = odp_pool_create("event_free2", &pool_param);
	CU_ASSERT_FATAL(pool2 != ODP_POOL_INVALID);

	/* Timeout events */
	odp_pool_param_init(&pool_param);
	pool_param.tmo.num = NUM_EVENTS;
	pool_param.type    = ODP_POOL_TIMEOUT;

	pool3 = odp_pool_create("event_free3", &pool_param);
	CU_ASSERT_FATAL(pool3 != ODP_POOL_INVALID);

	for (j = 0; j < 2; j++) {
		for (i = 0; i < 3 * EVENT_BURST;) {
			buf = odp_buffer_alloc(pool1);
			CU_ASSERT_FATAL(buf != ODP_BUFFER_INVALID);
			event[i] = odp_buffer_to_event(buf);
			i++;
			pkt = odp_packet_alloc(pool2, EVENT_SIZE);
			CU_ASSERT_FATAL(pkt != ODP_PACKET_INVALID);
			event[i] = odp_packet_to_event(pkt);
			i++;
			tmo = odp_timeout_alloc(pool3);
			CU_ASSERT_FATAL(tmo != ODP_TIMEOUT_INVALID);
			event[i] = odp_timeout_to_event(tmo);
			i++;
		}

		if (j == 0)
			odp_event_free_multi(event, 3 * EVENT_BURST);
		else
			odp_event_free_sp(event, 3 * EVENT_BURST);
	}

	CU_ASSERT(odp_pool_destroy(pool1) == 0);
	CU_ASSERT(odp_pool_destroy(pool2) == 0);
	CU_ASSERT(odp_pool_destroy(pool3) == 0);
}

#define NUM_TYPE_TEST 6

static void type_test_init(odp_pool_t *buf_pool, odp_pool_t *pkt_pool,
			   odp_event_t buf_event[],
			   odp_event_t pkt_event[],
			   odp_event_t event[])
{
	odp_pool_t pool1, pool2;
	odp_pool_param_t pool_param;
	int i;
	odp_buffer_t buf;
	odp_packet_t pkt;

	/* Buffer events */
	odp_pool_param_init(&pool_param);
	pool_param.buf.num  = NUM_EVENTS;
	pool_param.buf.size = EVENT_SIZE;
	pool_param.type     = ODP_POOL_BUFFER;

	pool1 = odp_pool_create("event_type_buf", &pool_param);
	CU_ASSERT_FATAL(pool1 != ODP_POOL_INVALID);

	for (i = 0; i < NUM_TYPE_TEST; i++) {
		buf = odp_buffer_alloc(pool1);
		CU_ASSERT_FATAL(buf != ODP_BUFFER_INVALID);
		buf_event[i] = odp_buffer_to_event(buf);
	}

	/* Packet events */
	odp_pool_param_init(&pool_param);
	pool_param.pkt.num = NUM_EVENTS;
	pool_param.pkt.len = EVENT_SIZE;
	pool_param.type    = ODP_POOL_PACKET;

	pool2 = odp_pool_create("event_type_pkt", &pool_param);
	CU_ASSERT_FATAL(pool2 != ODP_POOL_INVALID);

	for (i = 0; i < NUM_TYPE_TEST; i++) {
		pkt = odp_packet_alloc(pool2, EVENT_SIZE);
		CU_ASSERT_FATAL(pkt != ODP_PACKET_INVALID);
		pkt_event[i] = odp_packet_to_event(pkt);
	}

	/* 1 buf, 1 pkt, 2 buf, 2 pkt, 3 buf, 3 pkt */
	event[0]  = buf_event[0];
	event[1]  = pkt_event[0];
	event[2]  = buf_event[1];
	event[3]  = buf_event[2];
	event[4]  = pkt_event[1];
	event[5]  = pkt_event[2];
	event[6]  = buf_event[3];
	event[7]  = buf_event[4];
	event[8]  = buf_event[5];
	event[9]  = pkt_event[3];
	event[10] = pkt_event[4];
	event[11] = pkt_event[5];

	*buf_pool = pool1;
	*pkt_pool = pool2;
}

static void event_test_type_multi(void)
{
	odp_pool_t buf_pool, pkt_pool;
	odp_event_type_t type;
	int num;
	odp_event_t buf_event[NUM_TYPE_TEST];
	odp_event_t pkt_event[NUM_TYPE_TEST];
	odp_event_t event[2 * NUM_TYPE_TEST];

	type_test_init(&buf_pool, &pkt_pool, buf_event, pkt_event, event);

	num = odp_event_type_multi(&event[0], 12, &type);
	CU_ASSERT(num == 1);
	CU_ASSERT(type == ODP_EVENT_BUFFER);

	num = odp_event_type_multi(&event[1], 11, &type);
	CU_ASSERT(num == 1);
	CU_ASSERT(type == ODP_EVENT_PACKET);

	num = odp_event_type_multi(&event[2], 10, &type);
	CU_ASSERT(num == 2);
	CU_ASSERT(type == ODP_EVENT_BUFFER);

	num = odp_event_type_multi(&event[4], 8, &type);
	CU_ASSERT(num == 2);
	CU_ASSERT(type == ODP_EVENT_PACKET);

	num = odp_event_type_multi(&event[6], 6, &type);
	CU_ASSERT(num == 3);
	CU_ASSERT(type == ODP_EVENT_BUFFER);

	num = odp_event_type_multi(&event[9], 3, &type);
	CU_ASSERT(num == 3);
	CU_ASSERT(type == ODP_EVENT_PACKET);

	odp_event_free_multi(buf_event, NUM_TYPE_TEST);
	odp_event_free_multi(pkt_event, NUM_TYPE_TEST);

	CU_ASSERT(odp_pool_destroy(buf_pool) == 0);
	CU_ASSERT(odp_pool_destroy(pkt_pool) == 0);
}

static void event_test_types_multi(void)
{
	odp_pool_t buf_pool, pkt_pool;
	odp_event_t buf_event[NUM_TYPE_TEST];
	odp_event_t pkt_event[NUM_TYPE_TEST];
	odp_event_t event[2 * NUM_TYPE_TEST];
	odp_event_type_t event_types[2 * NUM_TYPE_TEST];
	odp_event_subtype_t event_subtypes[2 * NUM_TYPE_TEST];
	int i;

	type_test_init(&buf_pool, &pkt_pool, buf_event, pkt_event, event);

	/* Only buffers */
	odp_event_types_multi(buf_event, event_types, event_subtypes, NUM_TYPE_TEST);
	for (i = 0; i < NUM_TYPE_TEST; i++) {
		CU_ASSERT(event_types[i] == ODP_EVENT_BUFFER);
		CU_ASSERT(event_subtypes[i] == ODP_EVENT_NO_SUBTYPE);
	}

	/* Only packets */
	odp_event_types_multi(pkt_event, event_types, event_subtypes, NUM_TYPE_TEST);
	for (i = 0; i < NUM_TYPE_TEST; i++) {
		CU_ASSERT(event_types[i] == ODP_EVENT_PACKET);
		CU_ASSERT(event_subtypes[i] == ODP_EVENT_PACKET_BASIC);
	}

	/* Mixed events: B P BB PP BBB PPP */
	odp_event_types_multi(event, event_types, NULL, 2 * NUM_TYPE_TEST);
	for (i = 0; i < 2 * NUM_TYPE_TEST; i++) {
		if (i == 0 || i == 2 || i == 3 || i == 6 || i == 7 || i == 8) {
			/* CU_ASSERT requires extra brackets */
			CU_ASSERT(event_types[i] == ODP_EVENT_BUFFER);
		} else {
			CU_ASSERT(event_types[i] == ODP_EVENT_PACKET);
		}
	}

	odp_event_free_multi(buf_event, NUM_TYPE_TEST);
	odp_event_free_multi(pkt_event, NUM_TYPE_TEST);

	CU_ASSERT(odp_pool_destroy(buf_pool) == 0);
	CU_ASSERT(odp_pool_destroy(pkt_pool) == 0);
}

static void event_test_filter_packet(void)
{
	odp_pool_t buf_pool, pkt_pool;
	int i, num_pkt, num_rem;
	int num = 2 * NUM_TYPE_TEST;
	odp_event_t buf_event[NUM_TYPE_TEST];
	odp_event_t pkt_event[NUM_TYPE_TEST];
	odp_event_t event[num];
	odp_packet_t packet[num];
	odp_event_t remain[num];

	type_test_init(&buf_pool, &pkt_pool, buf_event, pkt_event, event);

	for (i = 0; i < num; i++) {
		packet[i] = ODP_PACKET_INVALID;
		remain[i] = ODP_EVENT_INVALID;
	}

	num_pkt = odp_event_filter_packet(event, packet, remain, num);
	CU_ASSERT(num_pkt == NUM_TYPE_TEST);

	for (i = 0; i < num_pkt; i++)
		CU_ASSERT(packet[i] != ODP_PACKET_INVALID);

	num_rem = num - num_pkt;
	CU_ASSERT(num_rem == NUM_TYPE_TEST);

	for (i = 0; i < num_rem; i++) {
		CU_ASSERT(remain[i] != ODP_EVENT_INVALID);
		CU_ASSERT(odp_event_type(remain[i]) == ODP_EVENT_BUFFER);
	}

	odp_event_free_multi(event, num);

	CU_ASSERT(odp_pool_destroy(buf_pool) == 0);
	CU_ASSERT(odp_pool_destroy(pkt_pool) == 0);
}

static void event_test_is_valid(void)
{
	CU_ASSERT(odp_event_is_valid(ODP_EVENT_INVALID) == 0);
	CU_ASSERT(odp_buffer_is_valid(ODP_BUFFER_INVALID) == 0);
	CU_ASSERT(odp_packet_is_valid(ODP_PACKET_INVALID) == 0);
	CU_ASSERT(odp_packet_vector_valid(ODP_PACKET_VECTOR_INVALID) == 0);
}

odp_testinfo_t event_suite[] = {
	ODP_TEST_INFO(event_test_free),
	ODP_TEST_INFO(event_test_free_multi),
	ODP_TEST_INFO(event_test_free_multi_mixed),
	ODP_TEST_INFO(event_test_type_multi),
	ODP_TEST_INFO(event_test_types_multi),
	ODP_TEST_INFO(event_test_filter_packet),
	ODP_TEST_INFO(event_test_is_valid),
	ODP_TEST_INFO_NULL,
};

odp_suiteinfo_t event_suites[] = {
	{"Event", NULL, NULL, event_suite},
	ODP_SUITE_INFO_NULL,
};

int main(int argc, char *argv[])
{
	int ret;

	/* parse common options: */
	if (odp_cunit_parse_options(argc, argv))
		return -1;

	ret = odp_cunit_register(event_suites);

	if (ret == 0)
		ret = odp_cunit_run();

	return ret;
}