3 * ***** BEGIN GPL LICENSE BLOCK *****
5 * This program is free software; you can redistribute it and/or
6 * modify it under the terms of the GNU General Public License
7 * as published by the Free Software Foundation; either version 2
8 * of the License, or (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU General Public License for more details.
15 * You should have received a copy of the GNU General Public License
16 * along with this program; if not, write to the Free Software Foundation,
17 * Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301, USA.
19 * The Original Code is Copyright (C) 2001-2002 by NaN Holding BV.
20 * All rights reserved.
22 * The Original Code is: all of this file.
24 * Contributor(s): none yet.
26 * ***** END GPL LICENSE BLOCK *****
32 * Copyright (C) 2001 NaN Technologies B.V.
33 * Simple test of memory.
38 /* Number of chunks to test with */
44 #include "MEM_guardedalloc.h"
50 int main (int argc, char *argv[])
60 /* ----------------------------------------------------------------- */
63 verbose = atoi(argv[1]);
64 if (verbose < 0) verbose = 0;
71 fprintf(stderr,"\n*** Simple memory test\n|\n");
74 /* ----------------------------------------------------------------- */
75 /* Round one, do a normal allocation, and free the blocks again. */
76 /* ----------------------------------------------------------------- */
77 /* flush mem lib output to stderr */
78 MEM_set_error_callback(stderr);
80 for (i = 0; i < NUM_BLOCKS; i++) {
81 int blocksize = 10000;
83 if (verbose >1) printf("|--* Allocating block %d\n", i);
84 sprintf(tagstring,"Memblock no. %d : ", i);
85 p[i]= MEM_callocN(blocksize, strdup(tagstring));
89 if (verbose > 1) MEM_printmemlist();
91 /* memory is there: test it */
92 error_status = MEM_check_memory_integrity();
96 fprintf(stderr, "|--* Memory test FAILED\n|\n");
98 fprintf(stderr, "|--* Memory tested as good (as it should be)\n|\n");
102 for (i = 0; i < NUM_BLOCKS; i++) {
106 /* ----------------------------------------------------------------- */
107 /* Round two, do a normal allocation, and corrupt some blocks. */
108 /* ----------------------------------------------------------------- */
109 /* switch off, because it will complain about some things. */
110 MEM_set_error_callback(NULL);
112 for (i = 0; i < NUM_BLOCKS; i++) {
113 int blocksize = 10000;
114 char tagstring[1000];
115 if (verbose >1) printf("|--* Allocating block %d\n", i);
116 sprintf(tagstring,"Memblock no. %d : ", i);
117 p[i]= MEM_callocN(blocksize, strdup(tagstring));
120 /* now corrupt a few blocks...*/
121 ip = (int*) p[5] - 50 ;
122 for (i = 0; i< 1000; i++,ip++) *ip = i+1;
126 retval = MEM_check_memory_integrity();
128 /* the test should have failed */
129 error_status |= !retval;
132 fprintf(stderr, "|--* Memory test failed (as it should be)\n");
134 fprintf(stderr, "|--* Memory test FAILED to find corrupted blocks \n");
138 for (i = 0; i < NUM_BLOCKS; i++) {
143 if (verbose && error_status) {
144 fprintf(stderr,"|--* Memory was corrupted\n");
146 /* ----------------------------------------------------------------- */
149 fprintf(stderr,"|\n|--* Errors were detected\n");
151 fprintf(stderr,"|\n|--* Test exited succesfully\n");
154 fprintf(stderr,"|\n*** Finished test\n\n");