X-Git-Url: https://pintos-os.org/cgi-bin/gitweb.cgi?a=blobdiff_plain;f=tests%2Flibpspp%2Ftower-test.c;h=78198cb554fb9974b69a2780723151959cee58bc;hb=b70e534e07eec7a2cbe249e8e609ff7cbc87eaff;hp=504e19f409aa05bb8a4df7b6969d6197d91f739b;hpb=43b1296aafe7582e7dbe6c2b6a8b478d7d9b0fcf;p=pspp diff --git a/tests/libpspp/tower-test.c b/tests/libpspp/tower-test.c index 504e19f409..78198cb554 100644 --- a/tests/libpspp/tower-test.c +++ b/tests/libpspp/tower-test.c @@ -1,5 +1,5 @@ /* PSPP - a program for statistical analysis. - Copyright (C) 2007 Free Software Foundation, Inc. + Copyright (C) 2007, 2010, 2013 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 @@ -39,9 +39,6 @@ #include "xalloc.h" -/* Currently running test. */ -static const char *test_name; - /* Exit with a failure code. (Place a breakpoint on this function while debugging.) */ static void @@ -57,8 +54,7 @@ check_func (bool ok, int line) { if (!ok) { - printf ("Check failed in %s test at %s, line %d\n", - test_name, __FILE__, line); + fprintf (stderr, "%s:%d: check failed\n", __FILE__, line); check_die (); } } @@ -245,7 +241,7 @@ next_composition (int n, int *k, int parts[]) /* A block expected to be found in a tower. */ struct expected_block { - int height; /* Expected height of bottom of block. */ + int size; /* Expected thickness of block. */ int x; /* Expected value for `x' member. */ }; @@ -259,6 +255,7 @@ check_tower (struct tower *t, struct tower_node *node; size_t i; + check (tower_count (t) == block_cnt); check (tower_is_empty (t) == (block_cnt == 0)); total_height = 0; @@ -266,7 +263,7 @@ check_tower (struct tower *t, { unsigned long int level; for (level = total_height; - level < total_height + blocks[i].height; + level < total_height + blocks[i].size; level++) { struct tower_node *found; @@ -275,8 +272,11 @@ check_tower (struct tower *t, check (found != NULL); check (tower_node_to_block (found)->x == blocks[i].x); check (block_start == total_height); + check (tower_node_get_level (found) == total_height); + check (tower_node_get_index (found) == i); + check (tower_get (t, i) == found); } - total_height += blocks[i].height; + total_height += blocks[i].size; } check (tower_height (t) == total_height); @@ -284,7 +284,7 @@ check_tower (struct tower *t, node != NULL; node = tower_next (t, node), i++) { - check (tower_node_get_height (node) == blocks[i].height); + check (tower_node_get_size (node) == blocks[i].size); check (tower_node_to_block (node)->x == blocks[i].x); } check (i == block_cnt); @@ -293,7 +293,7 @@ check_tower (struct tower *t, node != NULL; node = tower_prev (t, node), i--) { - check (tower_node_get_height (node) == blocks[i].height); + check (tower_node_get_size (node) == blocks[i].size); check (tower_node_to_block (node)->x == blocks[i].x); } check (i == SIZE_MAX); @@ -312,19 +312,19 @@ test_insert (void) { unsigned int composition_cnt; struct expected_block *expected; - int *heights; + int *sizes; int block_cnt; int *order; struct block *blocks; expected = xnmalloc (cnt, sizeof *expected); - heights = xnmalloc (cnt, sizeof *heights); + sizes = xnmalloc (cnt, sizeof *sizes); order = xnmalloc (cnt, sizeof *order); blocks = xnmalloc (cnt, sizeof *blocks); block_cnt = 0; composition_cnt = 0; - while (next_composition (cnt, &block_cnt, heights)) + while (next_composition (cnt, &block_cnt, sizes)) { int i, j; unsigned int permutation_cnt; @@ -338,7 +338,7 @@ test_insert (void) struct tower t; /* Inserts the block_cnt blocks with the given - heights[] into T in the order given by order[]. */ + sizes[] into T in the order given by order[]. */ tower_init (&t); for (i = 0; i < block_cnt; i++) { @@ -354,14 +354,14 @@ test_insert (void) && (under == NULL || under->x > order[j])) under = &blocks[order[j]]; - tower_insert (&t, heights[idx], &blocks[idx].node, + tower_insert (&t, sizes[idx], &blocks[idx].node, under != NULL ? &under->node : NULL); } /* Check that the result is what we expect. */ for (i = 0; i < block_cnt; i++) { - expected[i].height = heights[i]; + expected[i].size = sizes[i]; expected[i].x = i; } check_tower (&t, expected, block_cnt); @@ -375,14 +375,14 @@ test_insert (void) check (composition_cnt == 1 << (cnt - 1)); free (expected); - free (heights); + free (sizes); free (order); free (blocks); } } /* Tests deleting blocks from towers that initially contain all - possible sets of block heights into a tower in all possible + possible sets of block sizes into a tower in all possible orders, up to a specified maximum tower height. */ static void test_delete (void) @@ -394,19 +394,19 @@ test_delete (void) { unsigned int composition_cnt; struct expected_block *expected; - int *heights; + int *sizes; int block_cnt; int *order; struct block *blocks; expected = xnmalloc (cnt, sizeof *expected); - heights = xnmalloc (cnt, sizeof *heights); + sizes = xnmalloc (cnt, sizeof *sizes); order = xnmalloc (cnt, sizeof *order); blocks = xnmalloc (cnt, sizeof *blocks); block_cnt = 0; composition_cnt = 0; - while (next_composition (cnt, &block_cnt, heights)) + while (next_composition (cnt, &block_cnt, sizes)) { int i; unsigned int permutation_cnt; @@ -424,9 +424,9 @@ test_delete (void) for (i = 0; i < block_cnt; i++) { blocks[i].x = i; - tower_insert (&t, heights[i], &blocks[i].node, NULL); + tower_insert (&t, sizes[i], &blocks[i].node, NULL); expected[i].x = i; - expected[i].height = heights[i]; + expected[i].size = sizes[i]; } check_tower (&t, expected, block_cnt); @@ -442,8 +442,8 @@ test_delete (void) assert (j < block_cnt - i); if (expected[j].x == idx) { - memcpy (&expected[j], &expected[j + 1], - sizeof *expected * (block_cnt - i - j - 1)); + memmove (&expected[j], &expected[j + 1], + sizeof *expected * (block_cnt - i - j - 1)); break; } } @@ -459,14 +459,14 @@ test_delete (void) check (composition_cnt == 1 << (cnt - 1)); free (expected); - free (heights); + free (sizes); free (order); free (blocks); } } -/* Tests towers containing all possible block heights, resizing - the blocks to all possible heights that conserve the total +/* Tests towers containing all possible block sizes, resizing + the blocks to all possible sizes that conserve the total tower height, up to a maximum total tower height. */ static void test_resize (void) @@ -478,27 +478,27 @@ test_resize (void) { unsigned int composition_cnt; struct expected_block *expected; - int *heights, *new_heights; + int *sizes, *new_sizes; int block_cnt; int *order; struct block *blocks; expected = xnmalloc (cnt, sizeof *expected); - heights = xnmalloc (cnt, sizeof *heights); - new_heights = xnmalloc (cnt, sizeof *new_heights); + sizes = xnmalloc (cnt, sizeof *sizes); + new_sizes = xnmalloc (cnt, sizeof *new_sizes); order = xnmalloc (cnt, sizeof *order); blocks = xnmalloc (cnt, sizeof *blocks); block_cnt = 0; composition_cnt = 0; - while (next_composition (cnt, &block_cnt, heights)) + while (next_composition (cnt, &block_cnt, sizes)) { int i; unsigned int resizes = 0; - for (resizes = 0, first_k_composition (cnt, block_cnt, new_heights); + for (resizes = 0, first_k_composition (cnt, block_cnt, new_sizes); (resizes == 0 - || next_k_composition (cnt, block_cnt, new_heights)); + || next_k_composition (cnt, block_cnt, new_sizes)); resizes++) { struct tower t; @@ -508,18 +508,18 @@ test_resize (void) for (i = 0; i < block_cnt; i++) { blocks[i].x = i; - tower_insert (&t, heights[i], &blocks[i].node, NULL); + tower_insert (&t, sizes[i], &blocks[i].node, NULL); expected[i].x = i; - expected[i].height = heights[i]; + expected[i].size = sizes[i]; } check_tower (&t, expected, block_cnt); /* Resize all the blocks. */ for (i = 0; i < block_cnt; i++) { - if (expected[i].height != new_heights[i] || rand () % 2) - tower_resize (&t, &blocks[i].node, new_heights[i]); - expected[i].height = new_heights[i]; + if (expected[i].size != new_sizes[i] || rand () % 2) + tower_resize (&t, &blocks[i].node, new_sizes[i]); + expected[i].size = new_sizes[i]; } check_tower (&t, expected, block_cnt); } @@ -530,8 +530,8 @@ test_resize (void) check (composition_cnt == 1 << (cnt - 1)); free (expected); - free (new_heights); - free (heights); + free (new_sizes); + free (sizes); free (order); free (blocks); } @@ -549,20 +549,20 @@ test_splice_out (void) { unsigned int composition_cnt; struct expected_block *expected; - int *heights, *new_heights; + int *sizes, *new_sizes; int block_cnt; int *order; struct block *blocks; expected = xnmalloc (cnt, sizeof *expected); - heights = xnmalloc (cnt, sizeof *heights); - new_heights = xnmalloc (cnt, sizeof *new_heights); + sizes = xnmalloc (cnt, sizeof *sizes); + new_sizes = xnmalloc (cnt, sizeof *new_sizes); order = xnmalloc (cnt, sizeof *order); blocks = xnmalloc (cnt, sizeof *blocks); block_cnt = 0; composition_cnt = 0; - while (next_composition (cnt, &block_cnt, heights)) + while (next_composition (cnt, &block_cnt, sizes)) { int i, j; @@ -579,9 +579,9 @@ test_splice_out (void) for (k = 0; k < block_cnt; k++) { blocks[k].x = k; - tower_insert (&src, heights[k], &blocks[k].node, NULL); + tower_insert (&src, sizes[k], &blocks[k].node, NULL); expected[k].x = k; - expected[k].height = heights[k]; + expected[k].size = sizes[k]; } check_tower (&src, expected, block_cnt); @@ -598,8 +598,8 @@ test_splice_out (void) check (composition_cnt == 1 << (cnt - 1)); free (expected); - free (new_heights); - free (heights); + free (new_sizes); + free (sizes); free (order); free (blocks); } @@ -617,20 +617,20 @@ test_splice_in (void) { unsigned int composition_cnt; struct expected_block *expected; - int *heights, *new_heights; + int *sizes, *new_sizes; int block_cnt; int *order; struct block *blocks; expected = xnmalloc (cnt, sizeof *expected); - heights = xnmalloc (cnt, sizeof *heights); - new_heights = xnmalloc (cnt, sizeof *new_heights); + sizes = xnmalloc (cnt, sizeof *sizes); + new_sizes = xnmalloc (cnt, sizeof *new_sizes); order = xnmalloc (cnt, sizeof *order); blocks = xnmalloc (cnt, sizeof *blocks); block_cnt = 0; composition_cnt = 0; - while (next_composition (cnt, &block_cnt, heights)) + while (next_composition (cnt, &block_cnt, sizes)) { int i, j; @@ -648,9 +648,9 @@ test_splice_in (void) { blocks[k].x = k; tower_insert (k >= i && k < j ? &src : &dst, - heights[k], &blocks[k].node, NULL); + sizes[k], &blocks[k].node, NULL); expected[k].x = k; - expected[k].height = heights[k]; + expected[k].size = sizes[k]; } /* Splice SRC into DST. */ @@ -663,8 +663,8 @@ test_splice_in (void) check (composition_cnt == 1 << (cnt - 1)); free (expected); - free (new_heights); - free (heights); + free (new_sizes); + free (sizes); free (order); free (blocks); } @@ -673,25 +673,74 @@ test_splice_in (void) /* Main program. */ -/* Runs TEST_FUNCTION and prints a message about NAME. */ -static void -run_test (void (*test_function) (void), const char *name) -{ - test_name = name; - putchar ('.'); - fflush (stdout); - test_function (); -} +struct test + { + const char *name; + const char *description; + void (*function) (void); + }; + +static const struct test tests[] = + { + { + "insert", + "insert", + test_insert + }, + { + "delete", + "delete", + test_delete + }, + { + "resize", + "resize", + test_resize + }, + { + "splice-out", + "splice out", + test_splice_out + }, + { + "splice-in", + "splice in", + test_splice_in + }, + }; + +enum { N_TESTS = sizeof tests / sizeof *tests }; int -main (void) +main (int argc, char *argv[]) { - run_test (test_insert, "insert"); - run_test (test_delete, "delete"); - run_test (test_resize, "resize"); - run_test (test_splice_out, "splice out"); - run_test (test_splice_in, "splice in"); - putchar ('\n'); - - return 0; + int i; + + if (argc != 2) + { + fprintf (stderr, "exactly one argument required; use --help for help\n"); + return EXIT_FAILURE; + } + else if (!strcmp (argv[1], "--help")) + { + printf ("%s: test tower library\n" + "usage: %s TEST-NAME\n" + "where TEST-NAME is one of the following:\n", + argv[0], argv[0]); + for (i = 0; i < N_TESTS; i++) + printf (" %s\n %s\n", tests[i].name, tests[i].description); + return 0; + } + else + { + for (i = 0; i < N_TESTS; i++) + if (!strcmp (argv[1], tests[i].name)) + { + tests[i].function (); + return 0; + } + + fprintf (stderr, "unknown test %s; use --help for help\n", argv[1]); + return EXIT_FAILURE; + } }