]> git.sur5r.net Git - cc65/blob - testcode/lib/heaptest.c
Removed (pretty inconsistently used) tab chars from source code base.
[cc65] / testcode / lib / heaptest.c
1 #include <stdio.h>
2 #include <stdlib.h>
3 #include <string.h>
4 #include <time.h>
5 #include <_heap.h>
6
7
8
9 static unsigned char* V[256];
10
11
12
13 static char* Alloc (void)
14 /* Allocate a random sized chunk of memory */
15 {
16     /* Determine the size */
17     unsigned char Size = (((unsigned char)rand()) & 0x7F) + 1;
18
19     /* Allocate memory */
20     unsigned char* P = malloc (Size);
21
22     /* Set the string to a defined value. We use the size, since this will
23      * also allow us to retrieve it later.
24      */
25     if (P) {
26         memset (P, Size, Size);
27     } else {
28         printf ("Could not allocate %u bytes\n", Size);
29         exit (EXIT_FAILURE);
30     }
31     return P;
32 }
33
34
35
36 static void Free (unsigned char* P)
37 /* Check a memory block and free it */
38 {
39     unsigned char I;
40
41     /* Get the size of the block */
42     unsigned char Size = P[0];
43
44     /* Scan the block */
45     for (I = 1; I < Size; ++I) {
46         if (P[I] != Size) {
47             printf ("Scan failed - expected %02X, got %02X\n",
48                     Size, P[I]);
49             exit (EXIT_FAILURE);
50         }
51     }
52
53     /* Free the block */
54     free (P);
55 }
56
57
58
59 static void FillArray (void)
60 /* Fill the array with randomly allocated memory chunks */
61 {
62     unsigned char I = 0;
63     do {
64         V[I] = Alloc ();
65         ++I;
66     } while (I != 0);
67 }
68
69
70
71 static void ShowInfo (void)
72 /* Show heap info */
73 {
74     /* Count free blocks */
75     unsigned Count = 0;
76     register struct freeblock* P = _heapfirst;
77     while (P) {
78         ++Count;
79         P = P->next;
80     }
81     printf ("%04X  %04X  %04X  %04X  %04X %u\n",
82             _heaporg, _heapptr, _heapend, _heapfirst, _heaplast, Count);
83
84     if (Count) {
85         P = _heapfirst;
86         while (P) {
87             printf ("%04X  %04X  %04X %04X(%u)\n",
88                     (unsigned) P, P[2], P[1], P[0], P[0]);
89             P = P->next;
90         }
91         getchar ();
92     }
93 }
94
95
96
97 static void Test1 (void)
98 {
99     unsigned char I;
100     FillArray ();
101     for (I = 0; I < 0x80; ++I) {
102         Free (V[0x7F-I]);
103         Free (V[0x80+I]);
104     }
105     ShowInfo ();
106 }
107
108
109
110 static void Test2 (void)
111 {
112     unsigned char I;
113     FillArray ();
114     I = 0;
115     do {
116         Free (V[I]);
117         ++I;
118     } while (I != 0);
119     ShowInfo ();
120 }
121
122
123
124 static void Test3 (void)
125 {
126     unsigned char I;
127     FillArray ();
128     I = 0;
129     do {
130         --I;
131         Free (V[I]);
132     } while (I != 0);
133     ShowInfo ();
134 }
135
136
137
138 static void Test4 (void)
139 {
140     unsigned char I;
141     FillArray ();
142     I = 0;
143     do {
144         Free (V[I]);
145         I += 2;
146     } while (I != 0);
147     I = 1;
148     do {
149         Free (V[I]);
150         I += 2;
151     } while (I != 1);
152     ShowInfo ();
153 }
154
155
156
157 static void Test5 (void)
158 {
159     unsigned char I;
160     FillArray ();
161     I = 0;
162     do {
163         Free (V[I]);
164         I += 2;
165     } while (I != 0);
166     do {
167         V[I] = Alloc ();
168         I += 2;
169     } while (I != 0);
170     I = 1;
171     do {
172         Free (V[I]);
173         I += 2;
174     } while (I != 1);
175     do {
176         V[I] = Alloc ();
177         I += 2;
178     } while (I != 1);
179     I = 0;
180     do {
181         Free (V[I]);
182         ++I;
183     } while (I != 0);
184     ShowInfo ();
185 }
186
187
188
189 static void Test6 (void)
190 {
191     unsigned char I, J;
192     FillArray ();
193     I = J = 0;
194     do {
195         do {
196             Free (V[I]);
197             V[I] = Alloc ();
198             ++I;
199         } while (I != 0);
200         ++J;
201     } while (J < 5);
202     do {
203         Free (V[I]);
204         ++I;
205     } while (I != 0);
206     ShowInfo ();
207 }
208
209
210
211 int main (void)
212 {
213     unsigned long T;
214
215     /* Show info at start */
216     ShowInfo ();
217
218     /* Remember the time */
219     T = clock ();
220
221     /* Do the tests */
222     Test1 ();
223     Test2 ();
224     Test3 ();
225     Test4 ();
226     Test5 ();
227     Test6 ();
228
229     /* Calculate the time and print it */
230     T = clock () - T;
231     printf ("Time needed: %lu ticks\n", T);
232
233     /* Done */
234     return EXIT_SUCCESS;
235 }
236
237
238