summaryrefslogtreecommitdiff
path: root/src/02/05/stack_test.c
blob: 2f3eae092461d1b2b47c2ce1233014904e062fa7 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
#include "stack.h"
#include <cgreen/cgreen.h>
#include <string.h>

Describe(Stack);
BeforeEach(Stack) {}
AfterEach(Stack) {}

Ensure(Stack, when_pushing_an_item_on_to_a_stack) {
  Stack *stack = stack_init();

  stack_push(stack, (void *)10);

  assert_that(stack_size(stack), is_equal_to(1));
  assert_that(stack_peek(stack), is_equal_to(10));
}

Ensure(Stack, when_pushing_multiple_items_on_to_a_stack) {
  Stack *stack = stack_init();

  stack_push(stack, (void *)20);
  stack_push(stack, (void *)10);
  stack_push(stack, (void *)50);

  assert_that(stack_size(stack), is_equal_to(3));
  assert_that(stack_peek(stack), is_equal_to(50));
}

Ensure(Stack, when_pushing_a_custom_type_on_to_a_stack) {
  typedef struct {
  } Item;

  Stack *stack = stack_init();
  Item *item = malloc(sizeof(Item));

  stack_push(stack, item);

  assert_that(stack_size(stack), is_equal_to(1));
  assert_that(stack_peek(stack), is_equal_to(item));
  assert_that(stack_pop(stack), is_equal_to(item));
}

Ensure(Stack, when_popping_an_item_off_of_a_stack) {
  Stack *stack = stack_init();

  stack_push(stack, (void *)20);
  stack_push(stack, (void *)10);
  stack_push(stack, (void *)50);

  void *result = stack_pop(stack);

  assert_that(stack_size(stack), is_equal_to(2));
  assert_that(stack_peek(stack), is_equal_to(10));
  assert_that(result, is_equal_to(50));
}

TestSuite *stack_tests() {
  TestSuite *suite = create_test_suite();
  add_test_with_context(suite, Stack, when_pushing_an_item_on_to_a_stack);
  add_test_with_context(suite, Stack,
                        when_pushing_multiple_items_on_to_a_stack);
  add_test_with_context(suite, Stack, when_pushing_a_custom_type_on_to_a_stack);
  add_test_with_context(suite, Stack, when_popping_an_item_off_of_a_stack);
  return suite;
}