master
 1#include "stack.h"
 2#include <cgreen/cgreen.h>
 3#include <string.h>
 4
 5Describe(Stack);
 6BeforeEach(Stack) {}
 7AfterEach(Stack) {}
 8
 9Ensure(Stack, when_pushing_an_item_on_to_a_stack) {
10  Stack *stack = stack_init();
11
12  stack_push(stack, (void *)10);
13
14  assert_that(stack_size(stack), is_equal_to(1));
15  assert_that(stack_peek(stack), is_equal_to(10));
16}
17
18Ensure(Stack, when_pushing_multiple_items_on_to_a_stack) {
19  Stack *stack = stack_init();
20
21  stack_push(stack, (void *)20);
22  stack_push(stack, (void *)10);
23  stack_push(stack, (void *)50);
24
25  assert_that(stack_size(stack), is_equal_to(3));
26  assert_that(stack_peek(stack), is_equal_to(50));
27}
28
29Ensure(Stack, when_pushing_a_custom_type_on_to_a_stack) {
30  typedef struct {
31  } Item;
32
33  Stack *stack = stack_init();
34  Item *item = malloc(sizeof(Item));
35
36  stack_push(stack, item);
37
38  assert_that(stack_size(stack), is_equal_to(1));
39  assert_that(stack_peek(stack), is_equal_to(item));
40  assert_that(stack_pop(stack), is_equal_to(item));
41}
42
43Ensure(Stack, when_popping_an_item_off_of_a_stack) {
44  Stack *stack = stack_init();
45
46  stack_push(stack, (void *)20);
47  stack_push(stack, (void *)10);
48  stack_push(stack, (void *)50);
49
50  void *result = stack_pop(stack);
51
52  assert_that(stack_size(stack), is_equal_to(2));
53  assert_that(stack_peek(stack), is_equal_to(10));
54  assert_that(result, is_equal_to(50));
55}
56
57TestSuite *stack_tests() {
58  TestSuite *suite = create_test_suite();
59  add_test_with_context(suite, Stack, when_pushing_an_item_on_to_a_stack);
60  add_test_with_context(suite, Stack,
61                        when_pushing_multiple_items_on_to_a_stack);
62  add_test_with_context(suite, Stack, when_pushing_a_custom_type_on_to_a_stack);
63  add_test_with_context(suite, Stack, when_popping_an_item_off_of_a_stack);
64  return suite;
65}