tlsf_test.cpp
Go to the documentation of this file.
1 /***************************************************************************
2  tag: Intermodalics BVBA Mon Sep 8 2014 tlsf_test.cpp
3 
4  tlsf_test.cpp - description
5  -------------------
6  begin : Mon September 08 2014
7  copyright : (C) 2014 Intermodalics BVBA
8  email : ruben@intermodalics.eu
9 
10  ***************************************************************************
11  * *
12  * This program is free software; you can redistribute it and/or modify *
13  * it under the terms of the GNU General Public License as published by *
14  * the Free Software Foundation; either version 2 of the License, or *
15  * (at your option) any later version. *
16  * *
17  ***************************************************************************/
18 
19 #define ORO_MEMORY_POOL
20 
21 #include "unit.hpp"
22 #include <rtt/os/tlsf/tlsf.h>
23 #include <signal.h>
24 
25 void signal_handler(int sig_num){
26  if(sig_num == SIGABRT){
27  BOOST_TEST_MESSAGE("Catched SIGABRT. That is what is expected.");
28  exit(0);
29  }
30 }
31 
32 class TLSFTest
33 {
34 public:
35  void* rtMem;
36  void setUp(){
37  rtMem = malloc(10000);
38  BOOST_CHECK(rtMem);
39  BOOST_CHECK_LE(0,(int)(init_memory_pool(10000,rtMem)));
40  }
41  void tearDown(){
42  destroy_memory_pool(rtMem);
43  free(rtMem);
44  }
45  TLSFTest(){ setUp(); };
47 };
48 
49 BOOST_FIXTURE_TEST_SUITE(TLSFTestSuite, TLSFTest)
50 
51 BOOST_AUTO_TEST_CASE(testCreateAndDestroy)
52 {
53  //Do nothing, see if memory pool is created and destroyed without error
54 }
55 
56 BOOST_AUTO_TEST_CASE(testInitAllocFree){
57  void* a = 0;
58  a = oro_rt_malloc(100);//Allocate
59  BOOST_CHECK(a);
60  oro_rt_free(a);//Free
61 }
62 
63 BOOST_AUTO_TEST_CASE(testAllocToBig){
64 #if USE_MMAP || USE_SBRK
65  BOOST_CHECK(oro_rt_malloc(20000)); //Allocation bigger than memorypool should be successful
66 #else
67  BOOST_CHECK(!oro_rt_malloc(20000)); //Allocation bigger than memorypool should fail
68 #endif
69 }
70 
71 BOOST_AUTO_TEST_CASE(testInitTwiceSameMemPool){
72  BOOST_CHECK_LE(0,(int)(init_memory_pool(10000,rtMem)));//Allocation of the same memory pool should succeed
73  BOOST_CHECK_LE(0,(int)(init_memory_pool(9000,rtMem)));//Allocation of the same memory pool but smaller should succeed
74  BOOST_CHECK_LE(0,(int)(init_memory_pool(15000,rtMem)));//Allocation of the same memory pool but bigger should fail
75 }
76 
77 BOOST_AUTO_TEST_CASE(testUseSecondaryMemPool){
78  void* a = 0;
79  a = oro_rt_malloc(500);
80  BOOST_CHECK(a);
81  void* rtMem_p = malloc(15000);
82  BOOST_CHECK_LE(0,(int)init_memory_pool(15000,rtMem_p));
83  void* b = 0;
84  b = malloc_ex(500,rtMem_p);
85  BOOST_CHECK(b);
86  free_ex(b,rtMem_p);
87  destroy_memory_pool(rtMem_p);
88  oro_rt_free(a);
89  a = 0;
90  a = oro_rt_malloc(500);
91  BOOST_CHECK(a);
92  oro_rt_free(a);
93 }
94 
95 BOOST_AUTO_TEST_CASE(testDoubleFree)
96 {
97  signal(SIGABRT,&signal_handler);
98  void* a = oro_rt_malloc(500);
99  oro_rt_free(a);
100  oro_rt_free(a);
101  BOOST_FAIL("Double free undetected");
102 }
103 
#define BOOST_FIXTURE_TEST_SUITE(suite_name, F)
void tearDown()
Definition: tlsf_test.cpp:41
void * malloc_ex(size_t size, void *mem_pool)
Definition: tlsf.c:812
void signal_handler(int sig_num)
Definition: tlsf_test.cpp:25
#define BOOST_AUTO_TEST_SUITE_END()
size_t init_memory_pool(size_t mem_pool_size, void *mem_pool)
Definition: tlsf.c:498
void * rtMem
Definition: tlsf_test.cpp:35
void destroy_memory_pool(void *mem_pool)
Definition: tlsf.c:718
#define oro_rt_malloc
Definition: oro_malloc.h:54
void setUp()
Definition: tlsf_test.cpp:36
void free_ex(void *ptr, void *mem_pool)
Definition: tlsf.c:880
BOOST_AUTO_TEST_CASE(testCreateAndDestroy)
Definition: tlsf_test.cpp:51
#define oro_rt_free
Definition: oro_malloc.h:55


rtt
Author(s): RTT Developers
autogenerated on Fri Oct 25 2019 03:59:44