master
  1#include "min_stack.h"
  2#include <cgreen/cgreen.h>
  3
  4Describe(MinStack);
  5BeforeEach(MinStack) {}
  6AfterEach(MinStack) {}
  7
  8Ensure(MinStack, when_empty) {
  9  Stack *stack = initialize();
 10
 11  assert_that(size(stack), is_equal_to(0));
 12  assert_that(min(stack), is_equal_to(NULL));
 13  assert_that(pop(stack), is_equal_to(NULL));
 14
 15  free(stack);
 16}
 17
 18Ensure(MinStack, when_empty_it_has_a_size_of_zero) {
 19  Stack *stack = initialize();
 20
 21  assert_that(size(stack), is_equal_to(0));
 22
 23  free(stack);
 24}
 25
 26Ensure(MinStack, when_empty_it_has_a_min_of_null) {
 27  Stack *stack = initialize();
 28
 29  assert_that(min(stack), is_equal_to(NULL));
 30
 31  free(stack);
 32}
 33
 34Ensure(MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one) {
 35  Stack *stack = initialize();
 36
 37  push(stack, 1);
 38
 39  assert_that(size(stack), is_equal_to(1));
 40
 41  free(stack);
 42}
 43
 44Ensure(MinStack, when_a_single_item_is_on_the_stack_it_is_the_min) {
 45  Stack *stack = initialize();
 46
 47  push(stack, -100);
 48
 49  assert_that(min(stack), is_equal_to(-100));
 50
 51  free(stack);
 52}
 53
 54Ensure(
 55    MinStack,
 56    when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item) {
 57  Stack *stack = initialize();
 58
 59  push(stack, 200);
 60
 61  assert_that(pop(stack), is_equal_to(200));
 62
 63  free(stack);
 64}
 65
 66Ensure(
 67    MinStack,
 68    when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero) {
 69  Stack *stack = initialize();
 70
 71  push(stack, 200);
 72  pop(stack);
 73
 74  assert_that(size(stack), is_equal_to(0));
 75
 76  free(stack);
 77}
 78
 79Ensure(
 80    MinStack,
 81    when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null) {
 82  Stack *stack = initialize();
 83
 84  push(stack, 200);
 85  pop(stack);
 86
 87  assert_that(min(stack), is_equal_to(NULL));
 88
 89  free(stack);
 90}
 91
 92Ensure(MinStack, when_pushing_a_single_integer) {
 93  Stack *stack = initialize();
 94
 95  push(stack, 1);
 96
 97  assert_that(size(stack), is_equal_to(1));
 98  assert_that(min(stack), is_equal_to(1));
 99  assert_that(pop(stack), is_equal_to(1));
100  assert_that(size(stack), is_equal_to(0));
101
102  free(stack);
103}
104
105Ensure(MinStack, when_pushing_multiple_integers_out_of_order) {
106  Stack *stack = initialize();
107
108  push(stack, 2);
109  push(stack, 3);
110  push(stack, 4);
111  push(stack, 1);
112
113  assert_that(size(stack), is_equal_to(4));
114  assert_that(min(stack), is_equal_to(1));
115
116  assert_that(pop(stack), is_equal_to(1));
117  assert_that(size(stack), is_equal_to(3));
118  assert_that(min(stack), is_equal_to(2));
119
120  assert_that(pop(stack), is_equal_to(4));
121  assert_that(size(stack), is_equal_to(2));
122  assert_that(min(stack), is_equal_to(2));
123
124  assert_that(pop(stack), is_equal_to(3));
125  assert_that(size(stack), is_equal_to(1));
126  assert_that(min(stack), is_equal_to(2));
127
128  assert_that(pop(stack), is_equal_to(2));
129  assert_that(size(stack), is_equal_to(0));
130  assert_that(min(stack), is_equal_to(NULL));
131
132  assert_that(pop(stack), is_equal_to(NULL));
133  assert_that(size(stack), is_equal_to(0));
134
135  free(stack);
136}
137
138Ensure(MinStack, when_pushing_duplicate_values_on_to_the_stack) {
139  Stack *stack = initialize();
140
141  push(stack, 2);
142  push(stack, 1);
143  push(stack, 2);
144
145  assert_that(size(stack), is_equal_to(3));
146  assert_that(min(stack), is_equal_to(1));
147
148  assert_that(pop(stack), is_equal_to(2));
149  assert_that(min(stack), is_equal_to(1));
150
151  assert_that(pop(stack), is_equal_to(1));
152  assert_that(min(stack), is_equal_to(2));
153
154  assert_that(pop(stack), is_equal_to(2));
155  assert_that(min(stack), is_equal_to(NULL));
156
157  assert_that(pop(stack), is_equal_to(NULL));
158
159  free(stack);
160}
161
162TestSuite *min_stack_tests() {
163  TestSuite *suite = create_test_suite();
164
165  add_test_with_context(suite, MinStack, when_empty_it_has_a_size_of_zero);
166  add_test_with_context(suite, MinStack, when_empty_it_has_a_min_of_null);
167
168  add_test_with_context(
169      suite, MinStack, when_a_single_item_is_on_the_stack_it_has_a_size_of_one);
170  add_test_with_context(suite, MinStack,
171                        when_a_single_item_is_on_the_stack_it_is_the_min);
172
173  add_test_with_context(
174      suite, MinStack,
175      when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_the_item);
176  add_test_with_context(
177      suite, MinStack,
178      when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_size_of_zero);
179  add_test_with_context(
180      suite, MinStack,
181      when_a_single_item_is_on_the_stack_when_it_is_popped_off_it_returns_a_min_of_null);
182
183  add_test_with_context(suite, MinStack, when_pushing_a_single_integer);
184  add_test_with_context(suite, MinStack,
185                        when_pushing_multiple_integers_out_of_order);
186  return suite;
187}
188
189int main(int argc, char **argv) {
190  TestSuite *suite = create_test_suite();
191  add_suite(suite, min_stack_tests());
192  return run_test_suite(suite, create_text_reporter());
193}