1 /* PSPP - a program for statistical analysis.
2 Copyright (C) 2007, 2009 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 /* Searches the bits in PATTERN from right to left starting from
123 bit OFFSET. Returns the bit index of the first 1-bit found,
124 or ULONG_MAX if none is found. */
125 static unsigned long int
126 next_1bit (unsigned int pattern, unsigned int offset)
128 for (; offset < UINT_BIT; offset++)
129 if (pattern & (1u << offset))
134 /* Prints the regions in RS to stdout. */
136 print_regions (const struct range_set *rs)
138 const struct range_set_node *node;
141 for (node = range_set_first (rs); node != NULL;
142 node = range_set_next (rs, node))
143 printf (" (%lu,%lu)",
144 range_set_node_get_start (node), range_set_node_get_end (node));
148 /* Checks that the regions in RS match the bits in PATTERN. */
150 check_pattern (const struct range_set *rs, unsigned int pattern)
152 const struct range_set_node *node;
153 unsigned long int start, width;
154 unsigned long int s1, s2;
157 for (node = rand () % 2 ? range_set_first (rs) : range_set_next (rs, NULL),
159 next_region (pattern, start + width, &start, &width);
160 node = range_set_next (rs, node))
162 check (node != NULL);
163 check (range_set_node_get_start (node) == start);
164 check (range_set_node_get_end (node) == start + width);
165 check (range_set_node_get_width (node) == width);
167 check (node == NULL);
169 /* Scan from all possible positions, resetting the cache each
170 time, to ensure that we get the correct answers without
172 for (start = 0; start <= 32; start++)
174 struct range_set *nonconst_rs = (struct range_set *) rs;
175 nonconst_rs->cache_end = 0;
176 s1 = range_set_scan (rs, start);
177 s2 = next_1bit (pattern, start);
181 /* Scan in forward order to exercise expected cache behavior. */
182 for (s1 = range_set_scan (rs, 0), s2 = next_1bit (pattern, 0); ;
183 s1 = range_set_scan (rs, s1 + 1), s2 = next_1bit (pattern, s2 + 1))
190 /* Scan in random order to frustrate cache. */
191 for (i = 0; i < 32; i++)
193 start = rand () % 32;
194 s1 = range_set_scan (rs, start);
195 s2 = next_1bit (pattern, start);
199 /* Test range_set_scan() with negative cache. */
200 check (!range_set_contains (rs, 999));
201 check (range_set_scan (rs, 1111) == ULONG_MAX);
203 for (i = 0; i < UINT_BIT; i++)
204 check (range_set_contains (rs, i) == ((pattern & (1u << i)) != 0));
205 check (!range_set_contains (rs,
206 UINT_BIT + rand () % (ULONG_MAX - UINT_BIT)));
208 check (range_set_is_empty (rs) == (pattern == 0));
211 /* Creates and returns a range set that contains regions for the
212 bits set in PATTERN. */
213 static struct range_set *
214 make_pattern (unsigned int pattern)
216 unsigned long int start = 0;
217 unsigned long int width = 0;
218 struct range_set *rs = range_set_create_pool (NULL);
219 while (next_region (pattern, start + width, &start, &width))
220 range_set_insert (rs, start, width);
221 check_pattern (rs, pattern);
225 /* Returns an unsigned int with bits OFS...OFS+CNT (exclusive)
226 set to 1, other bits set to 0. */
228 bit_range (unsigned int ofs, unsigned int cnt)
230 assert (ofs < UINT_BIT);
231 assert (cnt <= UINT_BIT);
232 assert (ofs + cnt <= UINT_BIT);
234 return cnt < UINT_BIT ? ((1u << cnt) - 1) << ofs : UINT_MAX;
237 /* Tests inserting all possible patterns into all possible range
238 sets (up to a small maximum number of bits). */
242 const int positions = 9;
243 unsigned int init_pat;
246 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
247 for (i = 0; i < positions + 1; i++)
248 for (j = i; j <= positions + 1; j++)
250 struct range_set *rs, *rs2;
251 unsigned int final_pat;
253 rs = make_pattern (init_pat);
254 range_set_insert (rs, i, j - i);
255 final_pat = init_pat | bit_range (i, j - i);
256 check_pattern (rs, final_pat);
257 rs2 = range_set_clone (rs, NULL);
258 check_pattern (rs2, final_pat);
259 range_set_destroy (rs);
260 range_set_destroy (rs2);
264 /* Tests deleting all possible patterns from all possible range
265 sets (up to a small maximum number of bits). */
269 const int positions = 9;
270 unsigned int init_pat;
273 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
274 for (i = 0; i < positions + 1; i++)
275 for (j = i; j <= positions + 1; j++)
277 struct range_set *rs;
278 unsigned int final_pat;
280 rs = make_pattern (init_pat);
281 range_set_delete (rs, i, j - i);
282 final_pat = init_pat & ~bit_range (i, j - i);
283 check_pattern (rs, final_pat);
284 range_set_destroy (rs);
288 /* Tests all possible allocation in all possible range sets (up
289 to a small maximum number of bits). */
293 const int positions = 9;
294 unsigned int init_pat;
297 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
298 for (request = 1; request <= positions + 1; request++)
300 struct range_set *rs;
301 unsigned long int start, width, expect_start, expect_width;
302 bool success, expect_success;
303 unsigned int final_pat;
306 /* Figure out expected results. */
307 expect_success = false;
308 expect_start = expect_width = 0;
309 final_pat = init_pat;
310 for (i = 0; i < positions; i++)
311 if (init_pat & (1u << i))
313 expect_success = true;
315 expect_width = count_one_bits (init_pat >> i);
316 if (expect_width > request)
317 expect_width = request;
318 final_pat &= ~bit_range (expect_start, expect_width);
323 rs = make_pattern (init_pat);
324 success = range_set_allocate (rs, request, &start, &width);
325 check_pattern (rs, final_pat);
326 range_set_destroy (rs);
329 check (success == expect_success);
332 check (start == expect_start);
333 check (width == expect_width);
338 /* Tests all possible full allocations in all possible range sets
339 (up to a small maximum number of bits). */
341 test_allocate_fully (void)
343 const int positions = 9;
344 unsigned int init_pat;
347 for (init_pat = 0; init_pat < (1u << positions); init_pat++)
348 for (request = 1; request <= positions + 1; request++)
350 struct range_set *rs;
351 unsigned long int start, expect_start;
352 bool success, expect_success;
353 unsigned int final_pat;
356 /* Figure out expected results. */
357 expect_success = false;
359 final_pat = init_pat;
360 for (i = 0; i < positions - request + 1; i++)
364 final_pat = init_pat;
365 for (j = i; j < i + request; j++)
367 if (!(init_pat & (1u << j)))
369 final_pat &= ~(1u << j);
372 expect_success = true;
376 final_pat = init_pat;
380 rs = make_pattern (init_pat);
381 success = range_set_allocate_fully (rs, request, &start);
382 check_pattern (rs, final_pat);
383 range_set_destroy (rs);
386 check (success == expect_success);
388 check (start == expect_start);
392 /* Tests freeing a range set through a pool. */
397 struct range_set *rs;
399 /* Destroy the range set, then the pool.
400 Makes sure that this doesn't cause a double-free. */
401 pool = pool_create ();
402 rs = range_set_create_pool (pool);
403 range_set_insert (rs, 1, 10);
404 range_set_destroy (rs);
407 /* Just destroy the pool.
408 Makes sure that this doesn't cause a leak. */
409 pool = pool_create ();
410 rs = range_set_create_pool (pool);
411 range_set_insert (rs, 1, 10);
415 /* Tests range_set_destroy(NULL). */
417 test_destroy_null (void)
419 range_set_destroy (NULL);
424 /* Runs TEST_FUNCTION and prints a message about NAME. */
426 run_test (void (*test_function) (void), const char *name)
437 run_test (test_insert, "insert");
438 run_test (test_delete, "delete");
439 run_test (test_allocate, "allocate");
440 run_test (test_allocate_fully, "allocate_fully");
441 run_test (test_pool, "pool");
442 run_test (test_destroy_null, "destroy null");