xref: /nrf52832-nimble/packages/NimBLE-latest/porting/npl/linux/test/test_npl_mempool.c (revision 042d53a763ad75cb1465103098bb88c245d95138)
1 /*
2  * Licensed to the Apache Software Foundation (ASF) under one
3  * or more contributor license agreements.  See the NOTICE file
4  * distributed with this work for additional information
5  * regarding copyright ownership.  The ASF licenses this file
6  * to you under the Apache License, Version 2.0 (the
7  * "License"); you may not use this file except in compliance
8  * with the License.  You may obtain a copy of the License at
9  *
10  *  http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 
20 #include "test_util.h"
21 #include "nimble/nimble_npl.h"
22 
23 #define    TEST_MEMPOOL_BLOCKS       4
24 #define    TEST_MEMPOOL_BLOCK_SIZE   128
25 
26 static struct ble_npl_mempool s_mempool;
27 
28 static os_membuf_t s_mempool_mem[OS_MEMPOOL_SIZE(TEST_MEMPOOL_BLOCKS,
29 						 TEST_MEMPOOL_BLOCK_SIZE)];
30 
31 static void *s_memblock[TEST_MEMPOOL_BLOCKS];
32 
33 /**
34  * Unit test for initializing a mempool.
35  *
36  * ble_npl_error_t ble_npl_mempool_init(struct ble_npl_mempool *mp, int blocks,
37  *                            int block_size, void *membuf, char *name);
38  *
39  */
test_init()40 int test_init()
41 {
42     int err;
43     err = ble_npl_mempool_init(NULL,
44 			  TEST_MEMPOOL_BLOCKS,
45 			  TEST_MEMPOOL_BLOCK_SIZE,
46 			  NULL,
47 			  "Null mempool");
48     VerifyOrQuit(err, "ble_npl_mempool_init accepted NULL parameters.");
49 
50     err = ble_npl_mempool_init(&s_mempool,
51 			  TEST_MEMPOOL_BLOCKS,
52 			  TEST_MEMPOOL_BLOCK_SIZE,
53 			  s_mempool_mem,
54 			  "s_mempool");
55     return err;
56 }
57 
58 /**
59  * Test integrity check of a mempool.
60  *
61  * bool ble_npl_mempool_is_sane(const struct ble_npl_mempool *mp);
62  */
test_is_sane()63 int test_is_sane()
64 {
65     return (ble_npl_mempool_is_sane(&s_mempool)) ? PASS : FAIL;
66 }
67 
68 /**
69  * Test getting a memory block from the pool, putting it back,
70  * and checking if it is still valid.
71  *
72  * void *ble_npl_memblock_get(struct ble_npl_mempool *mp);
73  *
74  * ble_npl_error_t ble_npl_memblock_put(struct ble_npl_mempool *mp, void *block_addr);
75  *
76  * int ble_npl_memblock_from(const struct ble_npl_mempool *mp, const void *block_addr);
77  */
test_stress()78 int test_stress()
79 {
80     int loops = 3;
81     while(loops--)
82     {
83         for (int i = 0; i < 4; i++)
84 	{
85 	    s_memblock[i] = ble_npl_memblock_get(&s_mempool);
86 	    VerifyOrQuit(ble_npl_memblock_from(&s_mempool, s_memblock[i]),
87 			 "ble_npl_memblock_get return invalid block.");
88 	}
89 
90 
91         for (int i = 0; i < 4; i++)
92 	{
93  	    SuccessOrQuit(ble_npl_memblock_put(&s_mempool, s_memblock[i]),
94 			"ble_npl_memblock_put refused to take valid block.");
95 	    //VerifyOrQuit(!ble_npl_memblock_from(&s_mempool, s_memblock[i]),
96 	    //		 "Block still valid after ble_npl_memblock_put.");
97 	}
98 
99     }
100     return PASS;
101 }
102 
main(void)103 int main(void)
104 {
105     SuccessOrQuit(test_init(),    "Failed: ble_npl_mempool_init");
106     SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane");
107     SuccessOrQuit(test_stress(),  "Failed: ble_npl_mempool stree test");
108     SuccessOrQuit(test_is_sane(), "Failed: ble_npl_mempool_is_sane");
109     printf("All tests passed\n");
110     return PASS;
111 }
112