-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathc-stack-test-main.c
102 lines (85 loc) · 2.22 KB
/
c-stack-test-main.c
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
#include "c_stack.h"
#include "test_infrastructure.h"
#include <assert.h>
#include <stdint.h>
#include <stdlib.h>
const char * failing_test()
{
return "What is the sound of a failing test passing?";
}
const char * null_is_empty()
{
TEST_THAT(stack_empty(NULL), "NULL stack head should evaluate empty");
return NULL;
}
const char * not_empty_after_push()
{
stack_node *stack = NULL;
stack_push(42, &stack);
TEST_THAT(!stack_empty(stack), "Stack should not be empty after push");
stack_clear(&stack);
return NULL;
}
const char * peak_returns_pushed_value()
{
stack_node *stack=NULL;
const int data[] = {2,3,5,7,11,13,19};
const size_t data_size = sizeof(data)/(sizeof(data[0]));
for (size_t i=0; i<data_size; ++i)
{
stack_push(data[i], &stack);
TEST_THAT((stack_peek(stack) == data[i]), "Peak didn't match push");
}
stack_clear(&stack);
return NULL;
}
const char * can_pop_all_entries_to_empty()
{
stack_node *stack = NULL;
const int size = 9;
for (int i=0; i<size; ++i)
{
stack_push(i+11, &stack);
}
int pop_count = 0;
while (!stack_empty(stack))
{
TEST_THAT((pop_count < size), "Too many pops, isn't that thing removing nodes?");
stack_pop(&stack);
++pop_count;
}
return NULL;
}
const char * first_in_last_out()
{
stack_node *stack=NULL;
const int data[] = {2,3,5,7,11,13,19};
const size_t data_size = sizeof(data)/(sizeof(data[0]));
for (size_t i=0; i<data_size; ++i)
{
stack_push(data[i], &stack);
}
for (size_t i=data_size; i-->0; )
{
TEST_THAT((stack_pop(&stack) == data[i]), "Pop aren't reversing pushes?");
}
return NULL;
}
const char * performance_test()
{
return "Too slow!";
}
int main()
{
initialize_tests();
register_test("NULL is empty", null_is_empty, NO_DIRECTIVE, NULL);
register_test("Not empty after push", not_empty_after_push, NO_DIRECTIVE,
NULL);
register_test("Peak returns pushed value", peak_returns_pushed_value,
NO_DIRECTIVE, NULL);
register_test("Stack emptied as expected", can_pop_all_entries_to_empty,
NO_DIRECTIVE, NULL);
register_test("First in last out", first_in_last_out, NO_DIRECTIVE, NULL);
int fail_count = run_tests(stdout);
return fail_count;
}