1 /*
2  * Copyright (c) 2021 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include <zephyr/ztest.h>
8 
dummy_fn(struct k_timer * timer)9 void dummy_fn(struct k_timer *timer)
10 {
11 	ARG_UNUSED(timer);
12 }
13 
14 K_TIMER_DEFINE(timer_s, dummy_fn, NULL);
15 K_MEM_SLAB_DEFINE(slab_s, 8, 2, 8);
16 K_SEM_DEFINE(sem_s, 0, 1);
17 K_MUTEX_DEFINE(mutex_s);
18 K_STACK_DEFINE(stack_s, 64);
19 K_MSGQ_DEFINE(msgq_s, sizeof(int), 2, 4);
20 K_MBOX_DEFINE(mbox_s);
21 K_PIPE_DEFINE(pipe_s, 64, 4);
22 K_QUEUE_DEFINE(queue_s);
23 K_EVENT_DEFINE(event_s);
24 
25 unsigned char __aligned(4) pipe_buffer[64];
26 char __aligned(4) slab_buffer[8 * 4];
27 stack_data_t stack_array[8 * 4];
28 int msgq_buffer[64];
29 
ZTEST(obj_tracking,test_obj_tracking_sanity)30 ZTEST(obj_tracking, test_obj_tracking_sanity)
31 {
32 	struct k_timer timer;
33 	struct k_mem_slab slab;
34 	struct k_sem sem;
35 	struct k_mutex mutex;
36 	struct k_stack stack;
37 	struct k_msgq msgq;
38 	struct k_mbox mbox;
39 	struct k_pipe pipe;
40 	struct k_queue queue;
41 	struct k_event event;
42 	void *list;
43 	int count;
44 
45 	k_timer_init(&timer, dummy_fn, NULL);
46 	count = 0;
47 	list = _track_list_k_timer;
48 	while (list != NULL) {
49 		if (list == &timer || list == &timer_s) {
50 			count++;
51 		}
52 		list = SYS_PORT_TRACK_NEXT((struct k_timer *)list);
53 	}
54 	zassert_equal(count, 2, "Wrong number of timer objects");
55 
56 	k_mem_slab_init(&slab, slab_buffer, 8, 4);
57 	count = 0;
58 	list = _track_list_k_mem_slab;
59 	while (list != NULL) {
60 		if (list == &slab || list == &slab_s) {
61 			count++;
62 		}
63 		list = SYS_PORT_TRACK_NEXT((struct k_mem_slab *)list);
64 	}
65 	zassert_equal(count, 2, "Wrong number of mem_slab objects");
66 
67 	k_sem_init(&sem, 1, 2);
68 	count = 0;
69 	list = _track_list_k_sem;
70 	while (list != NULL) {
71 		if (list == &sem || list == &sem_s) {
72 			count++;
73 		}
74 		list = SYS_PORT_TRACK_NEXT((struct k_sem *)list);
75 	}
76 	zassert_equal(count, 2, "Wrong number of semaphore objects");
77 
78 	k_mutex_init(&mutex);
79 	count = 0;
80 	list = _track_list_k_mutex;
81 	while (list != NULL) {
82 		if (list == &mutex || list == &mutex_s) {
83 			count++;
84 		}
85 		list = SYS_PORT_TRACK_NEXT((struct k_mutex *)list);
86 	}
87 	zassert_equal(count, 2, "Wrong number of mutex objects");
88 
89 	k_stack_init(&stack, stack_array, 20);
90 	count = 0;
91 	list = _track_list_k_stack;
92 	while (list != NULL) {
93 		if (list == &stack || list == &stack_s) {
94 			count++;
95 		}
96 		list = SYS_PORT_TRACK_NEXT((struct k_stack *)list);
97 	}
98 	zassert_equal(count, 2, "Wrong number of stack objects");
99 
100 	k_msgq_init(&msgq, (char *)msgq_buffer, sizeof(int), 8);
101 	count = 0;
102 	list = _track_list_k_msgq;
103 	while (list != NULL) {
104 		if (list == &msgq || list == &msgq_s) {
105 			count++;
106 		}
107 		list = SYS_PORT_TRACK_NEXT((struct k_msgq *)list);
108 	}
109 	zassert_equal(count, 2, "Wrong number of message queue objects");
110 
111 	k_mbox_init(&mbox);
112 	count = 0;
113 	list = _track_list_k_mbox;
114 	while (list != NULL) {
115 		if (list == &mbox || list == &mbox_s) {
116 			count++;
117 		}
118 		list = SYS_PORT_TRACK_NEXT((struct k_mbox *)list);
119 	}
120 	zassert_equal(count, 2, "Wrong number of mbox objects");
121 
122 	k_pipe_init(&pipe, pipe_buffer, sizeof(pipe_buffer));
123 	count = 0;
124 	list = _track_list_k_pipe;
125 	while (list != NULL) {
126 		if (list == &pipe || list == &pipe_s) {
127 			count++;
128 		}
129 		list = SYS_PORT_TRACK_NEXT((struct k_pipe *)list);
130 	}
131 	zassert_equal(count, 2, "Wrong number of pipe objects");
132 
133 	k_queue_init(&queue);
134 	count = 0;
135 	list = _track_list_k_queue;
136 	while (list != NULL) {
137 		if (list == &queue || list == &queue_s) {
138 			count++;
139 		}
140 		list = SYS_PORT_TRACK_NEXT((struct k_queue *)list);
141 	}
142 	zassert_equal(count, 2, "Wrong number of queue objects");
143 
144 	k_event_init(&event);
145 	count = 0;
146 	list = _track_list_k_event;
147 	while (list != NULL) {
148 		if (list == &event || list == &event_s) {
149 			count++;
150 		}
151 		list = SYS_PORT_TRACK_NEXT((struct k_event *)list);
152 	}
153 	zassert_equal(count, 2, "Wrong number of queue objects");
154 
155 
156 }
157 
158 ZTEST_SUITE(obj_tracking, NULL, NULL, NULL, NULL, NULL);
159