1// SPDX-License-Identifier: GPL-3.0-or-later
2
3#include "test_engine_impl.h"
4
5#include <mos/lib/structures/stack.h>
6#include <mos_stdlib.h>
7
8MOS_TEST_CASE(stack_init_deinit)
9{
10 downwards_stack_t stack;
11 stack_init(stack: &stack, mem_region_bottom: (void *) 12345, size: 6789);
12 MOS_TEST_CHECK(stack.top, 12345 + 6789);
13 MOS_TEST_CHECK(stack.head, 12345 + 6789);
14 MOS_TEST_CHECK(stack.capacity, 6789);
15 stack_deinit(stack: &stack);
16
17 MOS_TEST_CHECK(stack.top, (ptr_t) NULL);
18 MOS_TEST_CHECK(stack.head, (ptr_t) NULL);
19 MOS_TEST_CHECK(stack.capacity, 0);
20}
21
22MOS_TEST_CASE(stack_push_pop_stack)
23{
24 downwards_stack_t stack = { 0 };
25 void *addr = kmalloc(4096);
26 stack_init(stack: &stack, mem_region_bottom: addr, size: 4096);
27
28 ptr_t stack_bottom = (ptr_t) addr + 4096;
29 MOS_TEST_CHECK(stack.capacity, 4096);
30 MOS_TEST_CHECK(stack.head, stack_bottom);
31 MOS_TEST_CHECK(stack.top, stack_bottom);
32
33 int pushed_1[10] = { 12345, 54321, 67890, 98765, 43210, 56789, 1234, 54321, 67890, 98765 };
34
35 stack_push(stack: &stack, data: pushed_1, size: sizeof(pushed_1));
36 MOS_TEST_CHECK(stack.top, stack_bottom);
37 MOS_TEST_CHECK(stack.head, stack_bottom - sizeof(pushed_1));
38 MOS_TEST_CHECK(stack.capacity, 4096);
39
40 int pushed_2[10] = { 4444, 5555, 6666, 7777, 8888, 9999, 10101, 11011, 12012, 13013 };
41 stack_push(stack: &stack, data: pushed_2, size: sizeof(pushed_2));
42 MOS_TEST_CHECK(stack.top, stack_bottom);
43 MOS_TEST_CHECK(stack.head, stack_bottom - sizeof(pushed_1) - sizeof(pushed_2));
44 MOS_TEST_CHECK(stack.capacity, 4096);
45
46 int tmp[10] = { 0 };
47 stack_pop(stack: &stack, size: sizeof(tmp), data: tmp);
48 MOS_TEST_CHECK(stack.top, stack_bottom);
49 MOS_TEST_CHECK(stack.head, stack_bottom - sizeof(pushed_1));
50 MOS_TEST_CHECK(stack.capacity, 4096);
51 MOS_TEST_CHECK(tmp[0], pushed_2[0]);
52 MOS_TEST_CHECK(tmp[1], pushed_2[1]);
53 MOS_TEST_CHECK(tmp[2], pushed_2[2]);
54 MOS_TEST_CHECK(tmp[3], pushed_2[3]);
55 MOS_TEST_CHECK(tmp[4], pushed_2[4]);
56 MOS_TEST_CHECK(tmp[5], pushed_2[5]);
57 MOS_TEST_CHECK(tmp[6], pushed_2[6]);
58 MOS_TEST_CHECK(tmp[7], pushed_2[7]);
59 MOS_TEST_CHECK(tmp[8], pushed_2[8]);
60 MOS_TEST_CHECK(tmp[9], pushed_2[9]);
61
62 stack_pop(stack: &stack, size: sizeof(tmp), data: tmp);
63 MOS_TEST_CHECK(stack.top, stack_bottom);
64 MOS_TEST_CHECK(stack.head, stack_bottom);
65 MOS_TEST_CHECK(stack.capacity, 4096);
66 MOS_TEST_CHECK(tmp[0], pushed_1[0]);
67 MOS_TEST_CHECK(tmp[1], pushed_1[1]);
68 MOS_TEST_CHECK(tmp[2], pushed_1[2]);
69 MOS_TEST_CHECK(tmp[3], pushed_1[3]);
70 MOS_TEST_CHECK(tmp[4], pushed_1[4]);
71 MOS_TEST_CHECK(tmp[5], pushed_1[5]);
72 MOS_TEST_CHECK(tmp[6], pushed_1[6]);
73 MOS_TEST_CHECK(tmp[7], pushed_1[7]);
74 MOS_TEST_CHECK(tmp[8], pushed_1[8]);
75 MOS_TEST_CHECK(tmp[9], pushed_1[9]);
76
77 stack_deinit(stack: &stack);
78 kfree(ptr: addr);
79}
80