source: trunk/kStuff/kLdr/tstkLdrHeap.c

Last change on this file was 3601, checked in by bird, 18 years ago

license update.

  • Property svn:keywords set to Id
File size: 5.8 KB
RevLine 
[2826]1/* $Id: tstkLdrHeap.c 3601 2007-10-29 00:21:13Z bird $ */
[2825]2/** @file
3 * kLdr - Heap testcase.
[3601]4 */
5
6/*
7 * Copyright (c) 2006-2007 knut st. osmundsen <bird-kStuff-spam@anduin.net>
[2825]8 *
[3601]9 * This file is part of kStuff.
[2825]10 *
[3601]11 * kStuff is free software; you can redistribute it and/or
12 * modify it under the terms of the GNU Lesser General Public
13 * License as published by the Free Software Foundation; either
14 * version 2.1 of the License, or (at your option) any later version.
[2825]15 *
[3601]16 * In addition to the permissions in the GNU Lesser General Public
17 * License, you are granted unlimited permission to link the compiled
18 * version of this file into combinations with other programs, and to
19 * distribute those combinations without any restriction coming from
20 * the use of this file.
[2825]21 *
[3601]22 * kStuff is distributed in the hope that it will be useful,
[2825]23 * but WITHOUT ANY WARRANTY; without even the implied warranty of
[3601]24 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
25 * Lesser General Public License for more details.
[2825]26 *
[3601]27 * You should have received a copy of the GNU Lesser General Public
28 * License along with kStuff; if not, write to the Free Software
29 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
30 * 02110-1301, USA
[2825]31 */
32
33
34/*******************************************************************************
35* Header Files *
36*******************************************************************************/
[3570]37#include <k/kLdr.h>
[3573]38#include <k/kHlp.h>
[2825]39
40#include <stdio.h>
41#include <stdlib.h>
42
43
44/*******************************************************************************
45* Defined Constants And Macros *
46*******************************************************************************/
47#define CHECK_FATAL(expr) \
48 do { if (!(expr)) { printf("tstkLdrHeap(%d): FATAL FAILURE - %s\n", __LINE__, #expr); return 1; } \
49 } while (0)
50
51#define CHECK(expr) \
[3573]52 do { if (!(expr)) { printf("tstkLdrHeap(%d): ERROR - %s\n", __LINE__, #expr); cErrors++; kHlpAssertBreakpoint();} \
[2825]53 } while (0)
54
55
56/**
57 * Get a random size.
58 * @returns random size.
59 */
60static unsigned RandSize(void)
61{
62 unsigned i = (unsigned)rand() % (256*1024 - 1);
63 return i ? i : 1;
64}
65
66/**
67 * Get a random index.
68 * @returns random index.
69 * @param cEntries The number of entries in the table.
70 */
71static unsigned RandIdx(unsigned cEntries)
72{
73 unsigned i = (unsigned)rand();
74 while (i >= cEntries)
75 i >>= 1;
76 return i;
77}
78
79#if 0
[3573]80# define kHlpAlloc(a) malloc(a)
81# define kHlpFree(a) free(a)
[2825]82#endif
83
84int main()
85{
86 int cErrors = 0;
87 int rc;
88#define MAX_ALLOCS 256
89 static struct
90 {
91 void *pv;
92 unsigned cb;
93 } s_aAllocs[MAX_ALLOCS];
94 unsigned cAllocs;
95 unsigned i;
96 unsigned j;
97
98 /*
99 * Some simple init / term.
100 */
[3573]101 rc = kHlpHeapInit();
[2825]102 CHECK_FATAL(!rc);
[3573]103 kHlpHeapTerm();
[2825]104
[3573]105 rc = kHlpHeapInit();
[2825]106 CHECK_FATAL(!rc);
[3573]107 kHlpHeapTerm();
[2825]108
109
110 /*
111 * Simple alloc all, free all in FIFO order.
112 */
[3573]113 rc = kHlpHeapInit();
[2825]114 CHECK_FATAL(!rc);
115
116 /* 1. allocate all slots. */
117 for (i = 0; i < MAX_ALLOCS; i++)
118 {
119 s_aAllocs[i].cb = RandSize();
[3573]120 s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
[2825]121 CHECK(s_aAllocs[i].pv);
122 }
123
124 /* 2. free all slots. */
125 for (i = 0; i < MAX_ALLOCS; i++)
[3573]126 kHlpFree(s_aAllocs[i].pv);
[2825]127
128 /* terminate */
[3573]129 kHlpHeapTerm();
[2825]130
131
132 /*
133 * Simple alloc all, free all in LIFO order.
134 */
[3573]135 rc = kHlpHeapInit();
[2825]136 CHECK_FATAL(!rc);
137
138 /* 1. allocate all slots. */
139 for (i = 0; i < MAX_ALLOCS; i++)
140 {
141 s_aAllocs[i].cb = RandSize();
[3573]142 s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
[2825]143 CHECK(s_aAllocs[i].pv);
144 }
145
146 /* 2. free all slots. */
147 i = MAX_ALLOCS;
148 while (i-- > 0)
[3573]149 kHlpFree(s_aAllocs[i].pv);
[2825]150
151 /* terminate */
[3573]152 kHlpHeapTerm();
[2825]153
154
155 /*
156 * Bunch of allocations, free half, allocate and free in pairs, free all.
157 */
[3573]158 rc = kHlpHeapInit();
[2825]159 CHECK_FATAL(!rc);
160
161 /* 1. allocate all slots. */
162 for (i = 0; i < MAX_ALLOCS; i++)
163 {
164 s_aAllocs[i].cb = RandSize();
[3573]165 s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
[2825]166 CHECK(s_aAllocs[i].pv);
167 }
168 cAllocs = MAX_ALLOCS;
169
170 /* 2. free half (random order). */
171 while (cAllocs > MAX_ALLOCS / 2)
172 {
173 i = RandIdx(cAllocs);
[3573]174 kHlpFree(s_aAllocs[i].pv);
[2825]175 cAllocs--;
176 if (i != cAllocs)
177 s_aAllocs[i] = s_aAllocs[cAllocs];
178 }
179
180 /* 3. lots of alloc and free activity. */
181 for (j = 0; j < MAX_ALLOCS * 32; j++)
182 {
183 /* allocate */
184 unsigned cMax = RandIdx(MAX_ALLOCS / 4) + 1;
185 while (cAllocs < MAX_ALLOCS && cMax-- > 0)
186 {
187 i = cAllocs;
188 s_aAllocs[i].cb = RandSize();
[3573]189 s_aAllocs[i].pv = kHlpAlloc(s_aAllocs[i].cb);
[2825]190 CHECK(s_aAllocs[i].pv);
191 cAllocs++;
192 }
193
194 /* free */
195 cMax = RandIdx(MAX_ALLOCS / 4) + 1;
196 while (cAllocs > MAX_ALLOCS / 2 && cMax-- > 0)
197 {
198 i = RandIdx(cAllocs);
[3573]199 kHlpFree(s_aAllocs[i].pv);
[2825]200 cAllocs--;
201 if (i != cAllocs)
202 s_aAllocs[i] = s_aAllocs[cAllocs];
203 }
204 }
205
206 /* 4. free all */
207 while (cAllocs > 0)
208 {
209 i = RandIdx(cAllocs);
[3573]210 kHlpFree(s_aAllocs[i].pv);
[2825]211 cAllocs--;
212 if (i != cAllocs)
213 s_aAllocs[i] = s_aAllocs[cAllocs];
214 }
215
216 /* terminate */
[3573]217 kHlpHeapTerm();
[2825]218
219
220 /* summary */
221 if (!cErrors)
222 printf("tstkLdrHeap: SUCCESS\n");
223 else
224 printf("tstkLdrHeap: FAILURE - %d errors\n", cErrors);
225 return !!cErrors;
226}
Note: See TracBrowser for help on using the repository browser.