summaryrefslogtreecommitdiff
path: root/src/01/06/min_stack_test.c
diff options
context:
space:
mode:
authormo khan <mo.khan@gmail.com>2020-07-05 12:43:33 -0600
committermo khan <mo.khan@gmail.com>2020-07-05 12:43:33 -0600
commit313092818159142508866eb975a1d5f6089d9399 (patch)
treef31c1695715bc75909bdbb6005cc77926fab0040 /src/01/06/min_stack_test.c
parentfb9a4afc4b9cdbcd90b7d3e7ea2e264faddfa70a (diff)
Convert min stack to near constant time algorithm
Diffstat (limited to 'src/01/06/min_stack_test.c')
-rw-r--r--src/01/06/min_stack_test.c88
1 files changed, 85 insertions, 3 deletions
diff --git a/src/01/06/min_stack_test.c b/src/01/06/min_stack_test.c
index b892d5f..c796dca 100644
--- a/src/01/06/min_stack_test.c
+++ b/src/01/06/min_stack_test.c
@@ -15,6 +15,74 @@ Ensure(MinStack, when_empty) {
free(stack);
}
+Ensure(MinStack, when_empty_it_has_a_size_of_zero) {
+ Stack *stack = initialize();
+
+ assert_that(size(stack), is_equal_to(0));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_empty_it_has_a_min_of_null) {
+ Stack *stack = initialize();
+
+ assert_that(min(stack), is_equal_to(NULL));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one) {
+ Stack *stack = initialize();
+
+ push(stack, 1);
+
+ assert_that(size(stack), is_equal_to(1));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_a_single_item_is_on_the_stack_it_is_the_min) {
+ Stack *stack = initialize();
+
+ push(stack, -100);
+
+ assert_that(min(stack), is_equal_to(-100));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
+ Stack *stack = initialize();
+
+ push(stack, 200);
+
+ assert_that(pop(stack), is_equal_to(200));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
+ Stack *stack = initialize();
+
+ push(stack, 200);
+ pop(stack);
+
+ assert_that(size(stack), is_equal_to(0));
+
+ free(stack);
+}
+
+Ensure(MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
+ Stack *stack = initialize();
+
+ push(stack, 200);
+ pop(stack);
+
+ assert_that(min(stack), is_equal_to(NULL));
+
+ free(stack);
+}
+
Ensure(MinStack, when_pushing_a_single_integer) {
Stack *stack = initialize();
@@ -33,18 +101,23 @@ Ensure(MinStack, when_pushing_multiple_integers_out_of_order) {
push(stack, 2);
push(stack, 3);
+ push(stack, 4);
push(stack, 1);
- assert_that(size(stack), is_equal_to(3));
+ assert_that(size(stack), is_equal_to(4));
assert_that(min(stack), is_equal_to(1));
assert_that(pop(stack), is_equal_to(1));
+ assert_that(size(stack), is_equal_to(3));
+ assert_that(min(stack), is_equal_to(2));
+
+ assert_that(pop(stack), is_equal_to(4));
assert_that(size(stack), is_equal_to(2));
assert_that(min(stack), is_equal_to(2));
assert_that(pop(stack), is_equal_to(3));
assert_that(size(stack), is_equal_to(1));
- assert_that(min(stack), is_equal_to(3));
+ assert_that(min(stack), is_equal_to(2));
assert_that(pop(stack), is_equal_to(2));
assert_that(size(stack), is_equal_to(0));
@@ -83,7 +156,16 @@ Ensure(MinStack, when_pushing_duplicate_values_on_to_the_stack) {
TestSuite *min_stack_tests() {
TestSuite *suite = create_test_suite();
- add_test_with_context(suite, MinStack, when_empty);
+ add_test_with_context(suite, MinStack, when_empty_it_has_a_size_of_zero);
+ add_test_with_context(suite, MinStack, when_empty_it_has_a_min_of_null);
+
+ add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
+ add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_it_is_the_min);
+
+ add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
+ add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
+ add_test_with_context(suite, MinStack, when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
+
add_test_with_context(suite, MinStack, when_pushing_a_single_integer);
add_test_with_context(suite, MinStack, when_pushing_multiple_integers_out_of_order);
return suite;