Bug 1845715 - Check for failure when getting RegExp match result template r=iain
[gecko.git] / nsprpub / pr / tests / dbmalloc.c
blob2a7ad4fa852a19b134d4d71a8efe7466a0d300af
1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3 * License, v. 2.0. If a copy of the MPL was not distributed with this
4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
6 /***********************************************************************
7 **
8 ** Name: dbmalloc.c
9 **
10 ** Description: Testing malloc (OBSOLETE)
12 ** Modification History:
13 ** 14-May-97 AGarcia- Converted the test to accomodate the debug_mode flag.
14 ** The debug mode will print all of the printfs associated with this test.
15 ** The regress mode will be the default mode. Since the regress tool limits
16 ** the output to a one line status:PASS or FAIL,all of the printf statements
17 ** have been handled with an if (debug_mode) statement.
18 ***********************************************************************/
19 #include <stdio.h>
20 #include <stdlib.h>
21 #include <time.h>
22 #include <string.h>
23 #include "nspr.h"
25 void
26 usage
28 void
31 fprintf(stderr, "Usage: dbmalloc ('-m'|'-s') '-f' num_fails ('-d'|'-n') filename [...]\n");
32 exit(0);
35 typedef struct node_struct
37 struct node_struct *next, *prev;
38 int line;
39 char value[4];
41 node_t,
42 *node_pt;
44 node_pt get_node(const char *line)
46 node_pt rv;
47 int l = strlen(line);
48 rv = (node_pt)PR_MALLOC(sizeof(node_t) + l + 1 - 4);
49 if( (node_pt)0 == rv ) {
50 return (node_pt)0;
52 memcpy(&rv->value[0], line, l+1);
53 rv->next = rv->prev = (node_pt)0;
54 return rv;
57 void
58 dump
60 const char *name,
61 node_pt node,
62 int mf,
63 int debug
66 if( (node_pt)0 != node->prev ) {
67 dump(name, node->prev, mf, debug);
69 if( 0 != debug ) {
70 printf("[%s]: %6d: %s", name, node->line, node->value);
72 if( node->line == mf ) {
73 fprintf(stderr, "[%s]: Line %d was allocated!\n", name, node->line);
75 if( (node_pt)0 != node->next ) {
76 dump(name, node->next, mf, debug);
78 return;
81 void
82 release
84 node_pt node
87 if( (node_pt)0 != node->prev ) {
88 release(node->prev);
90 if( (node_pt)0 != node->next ) {
91 release(node->next);
93 PR_DELETE(node);
96 int
99 const char *name,
100 int mf,
101 int debug
104 int rv;
105 FILE *fp;
106 int l = 0;
107 node_pt head = (node_pt)0;
108 char buffer[ BUFSIZ ];
110 fp = fopen(name, "r");
111 if( (FILE *)0 == fp )
113 fprintf(stderr, "[%s]: Cannot open \"%s.\"\n", name, name);
114 return -1;
117 /* fgets mallocs a buffer, first time through. */
118 if( (char *)0 == fgets(buffer, BUFSIZ, fp) )
120 fprintf(stderr, "[%s]: \"%s\" is empty.\n", name, name);
121 (void)fclose(fp);
122 return -1;
125 rewind(fp);
127 if( PR_SUCCESS != PR_ClearMallocCount() )
129 fprintf(stderr, "[%s]: Cannot clear malloc count.\n", name);
130 (void)fclose(fp);
131 return -1;
134 if( PR_SUCCESS != PR_SetMallocCountdown(mf) )
136 fprintf(stderr, "[%s]: Cannot set malloc countdown to %d\n", name, mf);
137 (void)fclose(fp);
138 return -1;
141 while( fgets(buffer, BUFSIZ, fp) )
143 node_pt n;
144 node_pt *w = &head;
146 if( (strlen(buffer) == (BUFSIZ-1)) && (buffer[BUFSIZ-2] != '\n') ) {
147 buffer[BUFSIZ-2] == '\n';
150 l++;
152 n = get_node(buffer);
153 if( (node_pt)0 == n )
155 printf("[%s]: Line %d: malloc failure!\n", name, l);
156 continue;
159 n->line = l;
161 while( 1 )
163 int comp;
165 if( (node_pt)0 == *w )
167 *w = n;
168 break;
171 comp = strcmp((*w)->value, n->value);
172 if( comp < 0 ) {
173 w = &(*w)->next;
175 else {
176 w = &(*w)->prev;
181 (void)fclose(fp);
183 dump(name, head, mf, debug);
185 rv = PR_GetMallocCount();
186 PR_ClearMallocCountdown();
188 release(head);
190 return rv;
193 int nf = 0;
194 int debug = 0;
196 void
197 test
199 const char *name
202 int n, i;
204 extern int nf, debug;
206 printf("[%s]: starting test 0\n", name);
207 n = t2(name, 0, debug);
208 if( -1 == n ) {
209 return;
211 printf("[%s]: test 0 had %ld allocations.\n", name, n);
213 if( 0 >= n ) {
214 return;
217 for( i = 0; i < nf; i++ )
219 int which = rand() % n;
220 if( 0 == which ) {
221 printf("[%s]: starting test %d -- no allocation should fail\n", name, i+1);
223 else {
224 printf("[%s]: starting test %d -- allocation %d should fail\n", name, i+1, which);
226 (void)t2(name, which, debug);
227 printf("[%s]: test %d done.\n", name, i+1);
230 return;
233 int main(int argc, char **argv)
235 int okay = 0;
236 int multithread = 0;
238 struct threadlist
240 struct threadlist *next;
241 PRThread *thread;
243 *threadhead = (struct threadlist *)0;
245 extern int nf, debug;
247 srand(time(0));
249 PR_Init(PR_USER_THREAD, PR_PRIORITY_NORMAL, 0);
250 PR_STDIO_INIT();
252 printf("[main]: We %s using the debugging malloc.\n",
253 PR_IsDebuggingMalloc() ? "ARE" : "ARE NOT");
255 while( argv++, --argc )
257 if( '-' == argv[0][0] )
259 switch( argv[0][1] )
261 case 'f':
262 nf = atoi(argv[0][2] ? &argv[0][2] :
263 --argc ? *++argv : "0");
264 break;
265 case 'd':
266 debug = 1;
267 break;
268 case 'n':
269 debug = 0;
270 break;
271 case 'm':
272 multithread = 1;
273 break;
274 case 's':
275 multithread = 0;
276 break;
277 default:
278 usage();
279 break;
282 else
284 FILE *fp = fopen(*argv, "r");
285 if( (FILE *)0 == fp )
287 fprintf(stderr, "Cannot open \"%s.\"\n", *argv);
288 continue;
291 okay++;
292 (void)fclose(fp);
293 if( multithread )
295 struct threadlist *n;
297 n = (struct threadlist *)malloc(sizeof(struct threadlist));
298 if( (struct threadlist *)0 == n )
300 fprintf(stderr, "This is getting tedious. \"%s\"\n", *argv);
301 continue;
304 n->next = threadhead;
305 n->thread = PR_CreateThread(PR_USER_THREAD, (void (*)(void *))test,
306 *argv, PR_PRIORITY_NORMAL,
307 PR_LOCAL_THREAD, PR_JOINABLE_THREAD,
309 if( (PRThread *)0 == n->thread )
311 fprintf(stderr, "Can't create thread for \"%s.\"\n", *argv);
312 continue;
314 else
316 threadhead = n;
319 else
321 test(*argv);
326 if( okay == 0 ) {
327 usage();
329 else while( (struct threadlist *)0 != threadhead )
331 struct threadlist *x = threadhead->next;
332 (void)PR_JoinThread(threadhead->thread);
333 PR_DELETE(threadhead);
334 threadhead = x;
337 return 0;