X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Flibpspp%2Frange-set-test.c;h=b0eb7abbf81f5249fb2b29131a4a1351e9c2b726;hb=186db0c54bcb0caf4a6df1fcc60b8158bc11ab92;hp=be09d884db809beb5fb0d0e5e58dfa550c78de0d;hpb=f5c108becd49d78f4898cab11352291f5689d24e;p=pspp-builds.git diff --git a/tests/libpspp/range-set-test.c b/tests/libpspp/range-set-test.c index be09d884..b0eb7abb 100644 --- a/tests/libpspp/range-set-test.c +++ b/tests/libpspp/range-set-test.c @@ -1,20 +1,18 @@ -/* PSPP - computes sample statistics. - Copyright (C) 2007 Free Software Foundation, Inc. +/* PSPP - a program for statistical analysis. + Copyright (C) 2007, 2009 Free Software Foundation, Inc. - This program is free software; you can redistribute it and/or - modify it under the terms of the GNU General Public License as - published by the Free Software Foundation; either version 2 of the - License, or (at your option) any later version. + This program is free software: you can redistribute it and/or modify + it under the terms of the GNU General Public License as published by + the Free Software Foundation, either version 3 of the License, or + (at your option) any later version. - This program is distributed in the hope that it will be useful, but - WITHOUT ANY WARRANTY; without even the implied warranty of - MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU - General Public License for more details. + This program is distributed in the hope that it will be useful, + but WITHOUT ANY WARRANTY; without even the implied warranty of + MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the + GNU General Public License for more details. You should have received a copy of the GNU General Public License - along with this program; if not, write to the Free Software - Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA - 02110-1301, USA. */ + along with this program. If not, see . */ /* This is a test program for the routines defined in range-set.c. This test program aims to be as comprehensive as @@ -290,6 +288,60 @@ test_allocate (void) } } +/* Tests all possible full allocations in all possible range sets + (up to a small maximum number of bits). */ +static void +test_allocate_fully (void) +{ + const int positions = 9; + unsigned int init_pat; + int request; + + for (init_pat = 0; init_pat < (1u << positions); init_pat++) + for (request = 1; request <= positions + 1; request++) + { + struct range_set *rs; + unsigned long int start, expect_start; + bool success, expect_success; + unsigned int final_pat; + int i; + + /* Figure out expected results. */ + expect_success = false; + expect_start = 0; + final_pat = init_pat; + for (i = 0; i < positions - request + 1; i++) + { + int j; + + final_pat = init_pat; + for (j = i; j < i + request; j++) + { + if (!(init_pat & (1u << j))) + goto next; + final_pat &= ~(1u << j); + } + + expect_success = true; + expect_start = i; + break; + next: + final_pat = init_pat; + } + + /* Test. */ + rs = make_pattern (init_pat); + success = range_set_allocate_fully (rs, request, &start); + check_pattern (rs, final_pat); + range_set_destroy (rs); + + /* Check results. */ + check (success == expect_success); + if (expect_success) + check (start == expect_start); + } +} + /* Tests freeing a range set through a pool. */ static void test_pool (void) @@ -331,6 +383,7 @@ main (void) run_test (test_insert, "insert"); run_test (test_delete, "delete"); run_test (test_allocate, "allocate"); + run_test (test_allocate_fully, "allocate_fully"); run_test (test_pool, "pool"); putchar ('\n');