xref: /btstack/test/btstack_memory/btstack_memory_pool_test.cpp (revision 1d3bd1e51ca491d6783233c8d7431c44f06daa5a)
1 /*
2  * Copyright (C) 2014 BlueKitchen GmbH
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * 1. Redistributions of source code must retain the above copyright
9  *    notice, this list of conditions and the following disclaimer.
10  * 2. Redistributions in binary form must reproduce the above copyright
11  *    notice, this list of conditions and the following disclaimer in the
12  *    documentation and/or other materials provided with the distribution.
13  * 3. Neither the name of the copyright holders nor the names of
14  *    contributors may be used to endorse or promote products derived
15  *    from this software without specific prior written permission.
16  * 4. Any redistribution, use, or modification is done solely for
17  *    personal benefit and not for any commercial purpose or for
18  *    monetary gain.
19  *
20  * THIS SOFTWARE IS PROVIDED BY BLUEKITCHEN GMBH AND CONTRIBUTORS
21  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
22  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
23  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MATTHIAS
24  * RINGWALD OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
25  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
26  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
27  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
28  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
29  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF
30  * THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
31  * SUCH DAMAGE.
32  *
33  * Please inquire about commercial licensing options at
34  * [email protected]
35  *
36  */
37 
38 // *****************************************************************************
39 //
40 // test rfcomm query tests
41 //
42 // *****************************************************************************
43 
44 
45 #include <stdint.h>
46 #include <stdio.h>
47 #include <stdlib.h>
48 #include <string.h>
49 
50 #include "CppUTest/TestHarness.h"
51 #include "CppUTest/CommandLineTestRunner.h"
52 
53 #include "bluetooth_data_types.h"
54 #include "btstack_util.h"
55 #include "btstack_memory_pool.h"
56 
57 #define MAX_NUM_PDUS 20
58 
59 typedef struct {
60     btstack_linked_item_t  item;
61     uint8_t value;
62 } test_pdu_t;
63 
64 TEST_GROUP(MemoryPool){
65     test_pdu_t pdu_storage[MAX_NUM_PDUS];
66     btstack_memory_pool_t pdu_pool;
67 
68     void setup(void){
69         int i;
70         for (i = 0; i < MAX_NUM_PDUS; i++){
71             pdu_storage[i].value = i;
72         }
73     }
74 };
75 
76 TEST(MemoryPool, CreateAndGetZero){
77     btstack_memory_pool_create(&pdu_pool, pdu_storage, 0, sizeof(test_pdu_t));
78     test_pdu_t * node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
79     CHECK(node == NULL);
80 }
81 
82 
83 TEST(MemoryPool, CreateThreeAndGetFour){
84     btstack_memory_pool_create(&pdu_pool, pdu_storage, 3, sizeof(test_pdu_t));
85     test_pdu_t * node;
86 
87     node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
88     CHECK(node != NULL);
89     CHECK(node->value == 2);
90 
91     node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
92     CHECK(node != NULL);
93     CHECK(node->value == 1);
94 
95     node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
96     CHECK(node != NULL);
97     CHECK(node->value == 0);
98 
99     node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
100     CHECK(node == NULL);
101 }
102 
103 TEST(MemoryPool, CreateAndFree){
104     btstack_memory_pool_create(&pdu_pool, pdu_storage, 3, sizeof(test_pdu_t));
105 
106     test_pdu_t * next_node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
107     CHECK_EQUAL(2, next_node->value);
108     btstack_memory_pool_free(&pdu_pool, next_node);
109 
110     next_node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
111     CHECK(next_node != NULL);
112     CHECK_EQUAL(2, next_node->value);
113 
114     next_node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
115     CHECK(next_node != NULL);
116     CHECK_EQUAL(1, next_node->value);
117 
118     next_node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
119     CHECK(next_node != NULL);
120     CHECK_EQUAL(0, next_node->value);
121 
122     next_node = (test_pdu_t *) btstack_memory_pool_get(&pdu_pool);
123     CHECK(next_node == NULL);
124 }
125 
126 int main (int argc, const char * argv[]){
127     return CommandLineTestRunner::RunAllTests(argc, argv);
128 }
129