diff options
author | Thomas White <taw@physics.org> | 2011-02-08 11:44:03 +0100 |
---|---|---|
committer | Thomas White <taw@physics.org> | 2012-02-22 15:27:13 +0100 |
commit | f20d9a294624b55b9e2b696ac9cc3b061d3043f6 (patch) | |
tree | 1f2c20a1eee3d320ea16df0d24e5673ec48f229e /tests/list_check.c | |
parent | 1ffb2b7ddb1e8ce4f4fbc5c7f1a13e8533859bb6 (diff) |
Test more thoroughly
Diffstat (limited to 'tests/list_check.c')
-rw-r--r-- | tests/list_check.c | 110 |
1 files changed, 96 insertions, 14 deletions
diff --git a/tests/list_check.c b/tests/list_check.c index 91d7367d..00e27e00 100644 --- a/tests/list_check.c +++ b/tests/list_check.c @@ -37,34 +37,45 @@ static int test_lists(int num_items) struct refltemp *check; RefList *list; int i; - - fprintf(stderr, "Testing with %i items.\n", num_items); + signed int h, k, l; check = malloc(num_items * sizeof(struct refltemp)); list = reflist_new(); + printf("Testing with %i\n", num_items); + + h = RANDOM_INDEX; + k = RANDOM_INDEX; + l = RANDOM_INDEX; + for ( i=0; i<num_items; i++ ) { - signed int h, k, l; int j; int duplicate = 0; - do { - duplicate = 0; - + if ( random() > RAND_MAX/2 ) { h = RANDOM_INDEX; k = RANDOM_INDEX; l = RANDOM_INDEX; - - for ( j=0; j<i; j++ ) { - if ( (check[j].h == h) - && (check[j].k == k) - && (check[j].l == l) ) { - duplicate++; - } + } /* else use the same as last time */ + + /* Count the number of times this reflection appeared before */ + for ( j=0; j<i; j++ ) { + if ( (check[j].h == h) + && (check[j].k == k) + && (check[j].l == l) ) { + duplicate++; } + } - } while ( duplicate ); + /* Update all copies with this number */ + for ( j=0; j<i; j++ ) { + if ( (check[j].h == h) + && (check[j].k == k) + && (check[j].l == l) ) { + check[j].dup = duplicate; + } + } add_refl(list, h, k, l); check[i].h = h; @@ -75,6 +86,8 @@ static int test_lists(int num_items) } + /* Check that all the reflections can be found, + * and delete the first few. */ for ( i=0; i<num_items; i++ ) { signed int h, k, l; @@ -90,9 +103,73 @@ static int test_lists(int num_items) return 1; } + /* Delete some reflections */ if ( i<num_items/2 ) { + + int j; + delete_refl(refl); check[i].del = 1; + + /* Update all counts */ + for ( j=0; j<i; j++ ) { + if ( (check[j].h == h) + && (check[j].k == k) + && (check[j].l == l) ) { + check[j].dup--; + } + } + + } + + } + + /* Check that the deleted reflections can no longer be found, unless + * duplicated. If duplicated, remove all the remaining copies. */ + for ( i=0; i<num_items/2; i++ ) { + + signed int h, k, l; + Reflection *refl; + + h = check[i].h; + k = check[i].k; + l = check[i].l; + + refl = find_refl(list, h, k, l); + if ( refl != NULL ) { + + /* Whoops, found it. Was it a duplicate? */ + if ( check[i].dup == 0 ) { + fprintf(stderr, "Found %3i %i %3i after" + " deletion.\n", h, k, l); + return 1; + } else { + + int j; + Reflection *c; + + for ( j=0; j<check[i].dup; j++ ) { + Reflection *r2; + r2 = find_refl(list, h, k, l); + if ( r2 == NULL ) { + fprintf(stderr, "Found too few" + " duplicates for" + " %3i %3i %3i\n", + h, k, l); + return 1; + } + delete_refl(r2); + } + + c = find_refl(list, h, k, l); + if ( c != NULL ) { + fprintf(stderr, "Found too many " + "duplicates for %3i %3i %3i\n", + h, k, l); + return 1; + } + + } } } @@ -107,9 +184,14 @@ int main(int argc, char *argv[]) { int i; + printf("Running list test..."); + fflush(stdout); + for ( i=0; i<100; i++ ) { if ( test_lists(4096*random()/RAND_MAX) ) return 1; } + printf("\r"); + return 0; } |