X-Git-Url: https://git.auder.net/?p=cgds.git;a=blobdiff_plain;f=test%2Ft.Stack.c;h=409cafbc73e88715db56abec42d05ff28beea804;hp=3cbaef6604b497b8e311afca17df5a8e964af0da;hb=e45132acdb58c076d5e06849fa51c26de9a7486d;hpb=1ff641f9960fa6c6081817a5641afb22fad91dcd diff --git a/test/t.Stack.c b/test/t.Stack.c index 3cbaef6..409cafb 100644 --- a/test/t.Stack.c +++ b/test/t.Stack.c @@ -5,128 +5,128 @@ void t_stack_clear() { - Stack* s = stack_new(int); + Stack* s = stack_new(int); - stack_push(s, 0); - stack_push(s, 0); - stack_push(s, 0); + stack_push(s, 0); + stack_push(s, 0); + stack_push(s, 0); - stack_clear(s); - lu_assert(stack_empty(s)); + stack_clear(s); + lu_assert(stack_empty(s)); - stack_destroy(s); + stack_destroy(s); } void t_stack_size() { - Stack* s = stack_new(int); + Stack* s = stack_new(int); - stack_push(s, 0); - stack_push(s, 0); - stack_push(s, 0); - lu_assert_int_eq(stack_size(s), 3); + stack_push(s, 0); + stack_push(s, 0); + stack_push(s, 0); + lu_assert_int_eq(stack_size(s), 3); - stack_push(s, 0); - stack_push(s, 0); - lu_assert_int_eq(stack_size(s), 5); + stack_push(s, 0); + stack_push(s, 0); + lu_assert_int_eq(stack_size(s), 5); - stack_push(s, 0); - stack_push(s, 0); - stack_push(s, 0); - lu_assert_int_eq(stack_size(s), 8); + stack_push(s, 0); + stack_push(s, 0); + stack_push(s, 0); + lu_assert_int_eq(stack_size(s), 8); - stack_destroy(s); + stack_destroy(s); } void t_stack_push_pop_basic() { - int n = 10; - - Stack* s = stack_new(double); - for (int i = 0; i < n; i++) - stack_push(s, (double) i); - // iterate and check values - double ckValue = n - 1; - while (!stack_empty(s)) - { - double d; - stack_top(s, d); - lu_assert_dbl_eq(d, ckValue); - ckValue -= 1.0; - stack_pop(s); - } - - lu_assert(stack_empty(s)); - stack_destroy(s); + int n = 10; + + Stack* s = stack_new(double); + for (int i = 0; i < n; i++) + stack_push(s, (double) i); + // iterate and check values + double ckValue = n - 1; + while (!stack_empty(s)) + { + double d; + stack_top(s, d); + lu_assert_dbl_eq(d, ckValue); + ckValue -= 1.0; + stack_pop(s); + } + + lu_assert(stack_empty(s)); + stack_destroy(s); } void t_stack_push_pop_evolved() { - Stack* s = stack_new(StructTest1); - - int n = 10; - StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); - for (int i = n - 1; i >= 0; i--) - { - st1[i].a = rand() % 42; - st1[i].b = (double) rand() / RAND_MAX; - stack_push(s, *(st1 + i)); - } - for (int i = 0; i < n; i++) - { - StructTest1 st1Cell; - stack_top(s, st1Cell); - lu_assert_int_eq(st1Cell.a, st1[i].a); - lu_assert_dbl_eq(st1Cell.b, st1[i].b); - stack_pop(s); - } - safe_free(st1); - stack_destroy(s); - - s = stack_new(StructTest2*); - StructTest2* st2 = (StructTest2*) malloc(n * sizeof (StructTest2)); - for (int i = n - 1; i >= 0; i--) - { - st2[i].a = (float) rand() / RAND_MAX; - st2[i].b = (StructTest1*) malloc(sizeof (StructTest1)); - st2[i].b->a = rand() % 42; - st2[i].b->b = (double) rand() / RAND_MAX; - stack_push(s, st2 + i); - } - for (int i = 0; i < n; i++) - { - StructTest2* st2Cell; - stack_top(s, st2Cell); - lu_assert_dbl_eq(st2Cell->a, st2[i].a); - lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); - lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); - stack_pop(s); - safe_free(st2Cell->b); - } - safe_free(st2); - stack_destroy(s); + Stack* s = stack_new(StructTest1); + + int n = 10; + StructTest1* st1 = (StructTest1*) malloc(n * sizeof (StructTest1)); + for (int i = n - 1; i >= 0; i--) + { + st1[i].a = rand() % 42; + st1[i].b = (double) rand() / RAND_MAX; + stack_push(s, *(st1 + i)); + } + for (int i = 0; i < n; i++) + { + StructTest1 st1Cell; + stack_top(s, st1Cell); + lu_assert_int_eq(st1Cell.a, st1[i].a); + lu_assert_dbl_eq(st1Cell.b, st1[i].b); + stack_pop(s); + } + safe_free(st1); + stack_destroy(s); + + s = stack_new(StructTest2*); + StructTest2* st2 = (StructTest2*) malloc(n * sizeof (StructTest2)); + for (int i = n - 1; i >= 0; i--) + { + st2[i].a = (float) rand() / RAND_MAX; + st2[i].b = (StructTest1*) malloc(sizeof (StructTest1)); + st2[i].b->a = rand() % 42; + st2[i].b->b = (double) rand() / RAND_MAX; + stack_push(s, st2 + i); + } + for (int i = 0; i < n; i++) + { + StructTest2* st2Cell; + stack_top(s, st2Cell); + lu_assert_dbl_eq(st2Cell->a, st2[i].a); + lu_assert_int_eq(st2Cell->b->a, st2[i].b->a); + lu_assert_dbl_eq(st2Cell->b->b, st2[i].b->b); + stack_pop(s); + safe_free(st2Cell->b); + } + safe_free(st2); + stack_destroy(s); } void t_stack_copy() { - int n = 10; - - Stack* s = stack_new(int); - for (int i = 0; i < n; i++) - stack_push(s, rand() % 42); - Stack* sc = stack_copy(s); - - lu_assert_int_eq(stack_size(s), stack_size(sc)); - int a, b; - for (int i = 0; i < n; i++) - { - stack_top(s, a); - stack_top(sc, b); - lu_assert_int_eq(a, b); - stack_pop(s); - stack_pop(sc); - } - stack_destroy(s); - stack_destroy(sc); + int n = 10; + + Stack* s = stack_new(int); + for (int i = 0; i < n; i++) + stack_push(s, rand() % 42); + Stack* sc = stack_copy(s); + + lu_assert_int_eq(stack_size(s), stack_size(sc)); + int a, b; + for (int i = 0; i < n; i++) + { + stack_top(s, a); + stack_top(sc, b); + lu_assert_int_eq(a, b); + stack_pop(s); + stack_pop(sc); + } + stack_destroy(s); + stack_destroy(sc); }