Update all #include directives to the currently preferred style.
[pspp-builds.git] / src / language / tests / pool-test.c
1 /* PSPP - a program for statistical analysis.
2    Copyright (C) 2000, 2011 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
19 #include <errno.h>
20 #include <stdio.h>
21 #include <stdlib.h>
22 #include <string.h>
23 #include <time.h>
24
25 #include "libpspp/pool.h"
26 #include "language/command.h"
27
28 #define N_ITERATIONS 8192
29 #define N_FILES 16
30
31 /* Self-test routine.
32    This is not exhaustive, but it can be useful. */
33 int
34 cmd_debug_pool (struct lexer *lexer UNUSED, struct dataset *ds UNUSED)
35 {
36   int seed = time (0) * 257 % 32768;
37
38   for (;;)
39     {
40       struct pool *pool;
41       struct pool_mark m1, m2;
42       FILE *files[N_FILES];
43       int cur_file;
44       long i;
45
46       printf ("Random number seed: %d\n", seed);
47       srand (seed++);
48
49       printf ("Creating pool...\n");
50       pool = pool_create ();
51
52       printf ("Marking pool state...\n");
53       pool_mark (pool, &m1);
54
55       printf ("    Populating pool with random-sized small objects...\n");
56       for (i = 0; i < N_ITERATIONS; i++)
57         {
58           size_t size = rand () % MAX_SUBALLOC;
59           void *p = pool_alloc (pool, size);
60           memset (p, 0, size);
61         }
62
63       printf ("    Marking pool state...\n");
64       pool_mark (pool, &m2);
65
66       printf ("       Populating pool with random-sized small "
67               "and large objects...\n");
68       for (i = 0; i < N_ITERATIONS; i++)
69         {
70           size_t size = rand () % (2 * MAX_SUBALLOC);
71           void *p = pool_alloc (pool, size);
72           memset (p, 0, size);
73         }
74
75       printf ("    Releasing pool state...\n");
76       pool_release (pool, &m2);
77
78       printf ("    Populating pool with random objects and gizmos...\n");
79       for (i = 0; i < N_FILES; i++)
80         files[i] = NULL;
81       cur_file = 0;
82       for (i = 0; i < N_ITERATIONS; i++)
83         {
84           int type = rand () % 32;
85
86           if (type == 0)
87             {
88               if (files[cur_file] != NULL
89                   && EOF == pool_fclose (pool, files[cur_file]))
90                 printf ("error on fclose: %s\n", strerror (errno));
91
92               files[cur_file] = pool_fopen (pool, "/dev/null", "r");
93
94               if (++cur_file >= N_FILES)
95                 cur_file = 0;
96             }
97           else if (type == 1)
98             pool_create_subpool (pool);
99           else
100             {
101               size_t size = rand () % (2 * MAX_SUBALLOC);
102               void *p = pool_alloc (pool, size);
103               memset (p, 0, size);
104             }
105         }
106
107       printf ("Releasing pool state...\n");
108       pool_release (pool, &m1);
109
110       printf ("Destroying pool...\n");
111       pool_destroy (pool);
112
113       putchar ('\n');
114     }
115
116   return CMD_SUCCESS;
117 }
118