source: rtems/testsuites/libtests/heapwalk/init.c @ 93f4ac26

4.104.11
Last change on this file since 93f4ac26 was 93f4ac26, checked in by Joel Sherrill <joel.sherrill@…>, on Jun 8, 2009 at 10:49:58 PM

2009-06-08 Joel Sherrill <joel.sherrill@…>

  • heapwalk/init.c: Add more cases.
  • Property mode set to 100644
File size: 3.5 KB
Line 
1/*
2 *  Test of Heap Walker
3 *
4 *  COPYRIGHT (c) 1989-2008.
5 *  On-Line Applications Research Corporation (OAR).
6 *
7 *  The license and distribution terms for this file may be
8 *  found in the file LICENSE in this distribution or at
9 *  http://www.rtems.com/license/LICENSE.
10 *
11 *  $Id$
12 */
13
14#define __RTEMS_VIOLATE_KERNEL_VISIBILITY__
15#define CONFIGURE_INIT
16#include "system.h"
17
18#include <stdlib.h>
19#include <inttypes.h>
20#include <errno.h>
21#include <string.h>
22#include <rtems/score/heap.h>
23#include <rtems/dumpbuf.h>
24
25#define TEST_HEAP_SIZE 1024
26uint32_t TestHeapMemory[TEST_HEAP_SIZE];
27Heap_Control TestHeap;
28
29void test_heap_init(void)
30{
31  memset( TestHeapMemory, '\0', sizeof(TestHeapMemory) );
32  _Heap_Initialize( &TestHeap, TestHeapMemory, sizeof(TestHeapMemory), 0 );
33}
34
35void test_heap_walk( int source, bool do_dump )
36{
37  int i, j, original;
38
39  _Heap_Walk( &TestHeap, source, do_dump );
40 
41  /*
42   *  Now corrupt all non-zero values
43   */
44  for (i=0 ; i<TEST_HEAP_SIZE ; i++) {
45    original = TestHeapMemory[i];
46    if ( original == 0 )
47      continue;
48
49    /* mark it free -- may or may not have already been */
50printf( "%p -- free\n", i );
51    TestHeapMemory[i] &= ~0x01;   
52    _Heap_Walk( &TestHeap, source, do_dump );
53
54    /* mark it used -- may or may not have already been */
55printf( "%p -- used\n", i );
56    TestHeapMemory[i] |= 0x01;   
57    _Heap_Walk( &TestHeap, source, do_dump );
58
59    /* try 0 and see what that does */
60printf( "%p -- 0x00\n", i );
61    TestHeapMemory[i] = 0x00;   
62    _Heap_Walk( &TestHeap, source, do_dump );
63
64    /* try 0xffffffff and see what that does */
65printf( "%p -- 0xff\n", i );
66    TestHeapMemory[i] = 0xffffffff;   
67    _Heap_Walk( &TestHeap, source, do_dump );
68
69    TestHeapMemory[i] = original;
70  }
71}
72
73void test_walk_freshly_initialized(void)
74{
75  puts( "Walk freshly initialized heap" );
76  test_heap_init();
77  test_heap_walk(1, true);
78}
79
80void test_negative_source_value(void)
81{
82  /*
83   * Passing a negative value for source so that
84   * the control enters the if block on line 67
85   */
86  puts( "Passing negative value for source" );
87  test_heap_init();
88  test_heap_walk( -1, true );
89}
90
91void test_prev_block_flag_check(void)
92{
93  /* Calling heapwalk without initialising the heap.
94   * Covers line 80 and 85 on heapwalk.
95   * Actually covers more than that.
96   */
97  puts( "Calling Heap Walk without initialising" );
98  test_heap_walk( 1, true );
99}
100
101void test_not_aligned(void)
102{
103  /*
104   * Hack to get to the error case where the blocks are
105   * not on the page size.  We initialize a heap with
106   * default settings and change the page size to something
107   * large.
108   */
109  puts( "Testing case of blocks not on page size" );
110  test_heap_init();
111  TestHeap.page_size = 128;
112  test_heap_walk( -1, true );
113}
114
115void test_not_in_free_list(void)
116{
117  void *p1, *p2, *p3;
118
119  /*
120   * Hack to get to the error case where the blocks are
121   * not on the page size.  We initialize a heap with
122   * default settings and change the page size to something
123   * large.
124   */
125  puts( "Testing case of blocks not on page size" );
126  test_heap_init();
127  p1 =_Heap_Allocate( &TestHeap, 32 );
128  p2 =_Heap_Allocate( &TestHeap, 32 );
129  p3 =_Heap_Allocate( &TestHeap, 32 );
130  _Heap_Free( &TestHeap, p2 );
131  test_heap_walk( -1, true );
132}
133
134rtems_task Init(
135  rtems_task_argument argument
136)
137{
138  puts( "\n\n*** HEAP WALK TEST ***" );
139
140  test_prev_block_flag_check();
141  test_walk_freshly_initialized();
142  test_negative_source_value();
143  test_not_aligned();
144  test_not_in_free_list();
145
146  puts( "*** END OF HEAP WALK TEST ***" );
147  rtems_test_exit(0);
148}
Note: See TracBrowser for help on using the repository browser.