1 /*
2  * Copyright (c) 2018 Intel Corporation
3  *
4  * SPDX-License-Identifier: Apache-2.0
5  */
6 
7 #include "test_msgq.h"
8 extern struct k_msgq msgq;
9 static ZTEST_BMEM char __aligned(4) tbuffer[MSG_SIZE * MSGQ_LEN];
10 static ZTEST_DMEM uint32_t send_buf[MSGQ_LEN] = { MSG0, MSG1 };
11 static ZTEST_DMEM uint32_t rec_buf[MSGQ_LEN] = { MSG0, MSG1 };
12 
attrs_get(struct k_msgq * q)13 static void attrs_get(struct k_msgq *q)
14 {
15 	int ret;
16 	struct k_msgq_attrs attrs;
17 
18 	k_msgq_get_attrs(q, &attrs);
19 	zassert_equal(attrs.used_msgs, 0);
20 
21 	/*fill the queue to full*/
22 	for (int i = 0; i < MSGQ_LEN; i++) {
23 		ret = k_msgq_put(q, (void *)&send_buf[i], K_NO_WAIT);
24 		zassert_equal(ret, 0);
25 	}
26 
27 	k_msgq_get_attrs(q, &attrs);
28 	zassert_equal(attrs.used_msgs, MSGQ_LEN);
29 
30 	for (int i = 0; i < MSGQ_LEN; i++) {
31 		ret = k_msgq_get(q, (void *)&rec_buf[i], K_NO_WAIT);
32 		zassert_equal(ret, 0);
33 	}
34 
35 	k_msgq_get_attrs(q, &attrs);
36 	zassert_equal(attrs.used_msgs, 0);
37 }
38 
39 /**
40  * @addtogroup kernel_message_queue_tests
41  * @{
42  */
43 
44 /**
45  * @brief Test basic attributes of a message queue
46  *
47  * @see  k_msgq_get_attrs()
48  */
ZTEST(msgq_api,test_msgq_attrs_get)49 ZTEST(msgq_api, test_msgq_attrs_get)
50 {
51 	k_msgq_init(&msgq, tbuffer, MSG_SIZE, MSGQ_LEN);
52 	attrs_get(&msgq);
53 }
54 
55 #ifdef CONFIG_USERSPACE
56 
57 /**
58  * @brief Test basic attributes of a message queue
59  *
60  * @see  k_msgq_get_attrs()
61  */
ZTEST_USER(msgq_api,test_msgq_user_attrs_get)62 ZTEST_USER(msgq_api, test_msgq_user_attrs_get)
63 {
64 	struct k_msgq *q;
65 
66 	q = k_object_alloc(K_OBJ_MSGQ);
67 	zassert_not_null(q, "couldn't alloc message queue");
68 	zassert_false(k_msgq_alloc_init(q, MSG_SIZE, MSGQ_LEN));
69 	attrs_get(q);
70 }
71 #endif
72 
73 /**
74  * @}
75  */
76