1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007 Free Software Foundation, Inc.
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.
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.
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/>. */
17 /* This is a test program for the routines defined in
18 range-set.c. This test program aims to be as comprehensive as
19 possible. With -DNDEBUG, "gcov -b" should report 100%
20 coverage of lines and branches in range-set.c routines.
21 (Without -DNDEBUG, branches caused by failed assertions will
22 not be taken.) "valgrind --leak-check=yes
23 --show-reachable=yes" should give a clean report, both with
24 and without -DNDEBUG. */
30 #include <libpspp/range-set.h>
38 #include <libpspp/compiler.h>
39 #include <libpspp/pool.h>
43 /* Currently running test. */
44 static const char *test_name;
46 /* Exit with a failure code.
47 (Place a breakpoint on this function while debugging.) */
54 /* If OK is not true, prints a message about failure on the
55 current source file and the given LINE and terminates. */
57 check_func (bool ok, int line)
61 printf ("Check failed in %s test at %s, line %d\n",
62 test_name, __FILE__, line);
67 /* Verifies that EXPR evaluates to true.
68 If not, prints a message citing the calling line number and
70 #define check(EXPR) check_func ((EXPR), __LINE__)
72 /* A contiguous region. */
75 unsigned long int start; /* Start of region. */
76 unsigned long int end; /* One past the end. */
79 /* Number of bits in an unsigned int. */
80 #define UINT_BIT (CHAR_BIT * sizeof (unsigned int))
82 /* Returns the number of contiguous 1-bits in X starting from bit
84 This implementation is designed to be obviously correct, not
87 count_one_bits (unsigned long int x)
98 /* Searches the bits in PATTERN from right to left starting from
99 bit OFFSET for one or more 1-bits. If any are found, sets
100 *START to the bit index of the first and *WIDTH to the number
101 of contiguous 1-bits and returns true. Otherwise, returns
103 This implementation is designed to be obviously correct, not
106 next_region (unsigned int pattern, unsigned int offset,
107 unsigned long int *start, unsigned long int *width)
111 assert (offset <= UINT_BIT);
112 for (i = offset; i < UINT_BIT; i++)
113 if (pattern & (1u << i))
116 *width = count_one_bits (pattern >> i);
122 /* Prints the regions in RS to stdout. */
124 print_regions (const struct range_set *rs)
126 const struct range_set_node *node;
129 for (node = range_set_first (rs); node != NULL;
130 node = range_set_next (rs, node))
131 printf (" (%lu,%lu)",
132 range_set_node_get_start (node), range_set_node_get_end (node));
136 /* Checks that the regions in RS match the bits in PATTERN. */
138 check_pattern (const struct range_set *rs, unsigned int pattern)
140 const struct range_set_node *node;
141 unsigned long int start, width;
144 for (node = rand () % 2 ? range_set_first (rs) : range_set_next (rs, NULL),
146 next_region (pattern, start + width, &start, &width);
147 node = range_set_next (rs, node))
149 check (node != NULL);
150 check (range_set_node_get_start (node) == start);
151 check (range_set_node_get_end (node) == start + width);
152 check (range_set_node_get_width (node) == width);
154 check (node == NULL);
156 for (i = 0; i < UINT_BIT; i++)
157 check (range_set_contains (rs, i) == ((pattern & (1u << i)) != 0));
158 check (!range_set_contains (rs,
159 UINT_BIT + rand () % (ULONG_MAX - UINT_BIT)));
161 check (range_set_is_empty (rs) == (pattern == 0));
164 /* Creates and returns a range set that contains regions for the
165 bits set in PATTERN. */
166 static struct range_set *
167 make_pattern (unsigned int pattern)
169 unsigned long int start = 0;
170 unsigned long int width = 0;
171 struct range_set *rs = range_set_create_pool (NULL);
172 while (next_region (pattern, start + width, &start, &width))
173 range_set_insert (rs, start, width);
174 check_pattern (rs, pattern);
178 /* Returns an unsigned int with bits OFS...OFS+CNT (exclusive)
179 set to 1, other bits set to 0. */
181 bit_range (unsigned int ofs, unsigned int cnt)
183 assert (ofs < UINT_BIT);
184 assert (cnt <= UINT_BIT);
185 assert (ofs + cnt <= UINT_BIT);
187 return cnt < UINT_BIT ? ((1u << cnt) - 1) << ofs : UINT_MAX;
190 /* Tests inserting all possible patterns into all possible range
191 sets (up to a small maximum number of bits). */
195 const int positions = 9;
196 unsigned int init_pat;
199 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
200 for (i = 0; i < positions + 1; i++)
201 for (j = i; j <= positions + 1; j++)
203 struct range_set *rs, *rs2;
204 unsigned int final_pat;
206 rs = make_pattern (init_pat);
207 range_set_insert (rs, i, j - i);
208 final_pat = init_pat | bit_range (i, j - i);
209 check_pattern (rs, final_pat);
210 rs2 = range_set_clone (rs, NULL);
211 check_pattern (rs2, final_pat);
212 range_set_destroy (rs);
213 range_set_destroy (rs2);
217 /* Tests deleting all possible patterns from all possible range
218 sets (up to a small maximum number of bits). */
222 const int positions = 9;
223 unsigned int init_pat;
226 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
227 for (i = 0; i < positions + 1; i++)
228 for (j = i; j <= positions + 1; j++)
230 struct range_set *rs;
231 unsigned int final_pat;
233 rs = make_pattern (init_pat);
234 range_set_delete (rs, i, j - i);
235 final_pat = init_pat & ~bit_range (i, j - i);
236 check_pattern (rs, final_pat);
237 range_set_destroy (rs);
241 /* Tests all possible allocation in all possible range sets (up
242 to a small maximum number of bits). */
246 const int positions = 9;
247 unsigned int init_pat;
250 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
251 for (request = 1; request <= positions + 1; request++)
253 struct range_set *rs;
254 unsigned long int start, width, expect_start, expect_width;
255 bool success, expect_success;
256 unsigned int final_pat;
259 /* Figure out expected results. */
260 expect_success = false;
261 expect_start = expect_width = 0;
262 final_pat = init_pat;
263 for (i = 0; i < positions; i++)
264 if (init_pat & (1u << i))
266 expect_success = true;
268 expect_width = count_one_bits (init_pat >> i);
269 if (expect_width > request)
270 expect_width = request;
271 final_pat &= ~bit_range (expect_start, expect_width);
276 rs = make_pattern (init_pat);
277 success = range_set_allocate (rs, request, &start, &width);
278 check_pattern (rs, final_pat);
279 range_set_destroy (rs);
282 check (success == expect_success);
285 check (start == expect_start);
286 check (width == expect_width);
291 /* Tests freeing a range set through a pool. */
296 struct range_set *rs;
298 /* Destroy the range set, then the pool.
299 Makes sure that this doesn't cause a double-free. */
300 pool = pool_create ();
301 rs = range_set_create_pool (pool);
302 range_set_insert (rs, 1, 10);
303 range_set_destroy (rs);
306 /* Just destroy the pool.
307 Makes sure that this doesn't cause a leak. */
308 pool = pool_create ();
309 rs = range_set_create_pool (pool);
310 range_set_insert (rs, 1, 10);
316 /* Runs TEST_FUNCTION and prints a message about NAME. */
318 run_test (void (*test_function) (void), const char *name)
329 run_test (test_insert, "insert");
330 run_test (test_delete, "delete");
331 run_test (test_allocate, "allocate");
332 run_test (test_pool, "pool");