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