b888c89638365323417d385042e3c6d47185c365
[pspp-builds.git] / src / language / tests / pool-test.c
1 /* PSPP - computes sample statistics.
2    Copyright (C) 2000 Free Software Foundation, Inc.
3    Written by Ben Pfaff <blp@gnu.org>.
4
5    This program is free software; you can redistribute it and/or
6    modify it under the terms of the GNU General Public License as
7    published by the Free Software Foundation; either version 2 of the
8    License, or (at your option) any later version.
9
10    This program is distributed in the hope that it will be useful, but
11    WITHOUT ANY WARRANTY; without even the implied warranty of
12    MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
13    General Public License for more details.
14
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
17    Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
18    02110-1301, USA. */
19
20 #include <config.h>
21 #include <libpspp/pool.h>
22 #include <language/command.h>
23 #include <errno.h>
24 #include <stdio.h>
25 #include <stdlib.h>
26 #include <string.h>
27 #include <time.h>
28
29 #define N_ITERATIONS 8192
30 #define N_FILES 16
31
32 /* Self-test routine.
33    This is not exhaustive, but it can be useful. */
34 int
35 cmd_debug_pool (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
36 {
37   int seed = time (0) * 257 % 32768;
38
39   for (;;)
40     {
41       struct pool *pool;
42       struct pool_mark m1, m2;
43       FILE *files[N_FILES];
44       int cur_file;
45       long i;
46
47       printf ("Random number seed: %d\n", seed);
48       srand (seed++);
49
50       printf ("Creating pool...\n");
51       pool = pool_create ();
52
53       printf ("Marking pool state...\n");
54       pool_mark (pool, &m1);
55
56       printf ("    Populating pool with random-sized small objects...\n");
57       for (i = 0; i < N_ITERATIONS; i++)
58         {
59           size_t size = rand () % MAX_SUBALLOC;
60           void *p = pool_alloc (pool, size);
61           memset (p, 0, size);
62         }
63
64       printf ("    Marking pool state...\n");
65       pool_mark (pool, &m2);
66       
67       printf ("       Populating pool with random-sized small "
68               "and large objects...\n");
69       for (i = 0; i < N_ITERATIONS; i++)
70         {
71           size_t size = rand () % (2 * MAX_SUBALLOC);
72           void *p = pool_alloc (pool, size);
73           memset (p, 0, size);
74         }
75
76       printf ("    Releasing pool state...\n");
77       pool_release (pool, &m2);
78
79       printf ("    Populating pool with random objects and gizmos...\n");
80       for (i = 0; i < N_FILES; i++)
81         files[i] = NULL;
82       cur_file = 0;
83       for (i = 0; i < N_ITERATIONS; i++)
84         {
85           int type = rand () % 32;
86
87           if (type == 0)
88             {
89               if (files[cur_file] != NULL
90                   && EOF == pool_fclose (pool, files[cur_file]))
91                 printf ("error on fclose: %s\n", strerror (errno));
92
93               files[cur_file] = pool_fopen (pool, "/dev/null", "r");
94
95               if (++cur_file >= N_FILES)
96                 cur_file = 0;
97             }
98           else if (type == 1)
99             pool_create_subpool (pool);
100           else 
101             {
102               size_t size = rand () % (2 * MAX_SUBALLOC);
103               void *p = pool_alloc (pool, size);
104               memset (p, 0, size);
105             }
106         }
107       
108       printf ("Releasing pool state...\n");
109       pool_release (pool, &m1);
110
111       printf ("Destroying pool...\n");
112       pool_destroy (pool);
113
114       putchar ('\n');
115     }
116
117   return CMD_SUCCESS;
118 }
119