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