aboutsummaryrefslogtreecommitdiff
path: root/test/test_list.c
blob: 14752588f239edf86a02ac97762a132f94e05fc1 (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
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
103
104
105
106
107
#include <criterion/criterion.h>
#include <criterion/internal/assert.h>
#include "../src/lib/list.h"

Node* head = NULL;

// Run on every test
void setup() {
  head = node_init(7);
}

// Run after every test
void teardown() {
  list_free(&head);
}

// Configure test suite
TestSuite(list, .init=setup, .fini=teardown);

// Node
Test(list, init) {
  int val = 17;
  head = node_init(val);
  cr_expect(head != NULL);
  cr_expect(head->value == val);
}

// Length
Test(list, length) {
  cr_expect(list_length(head) == 1);
}

// Push
Test(list, push) {
  int val = 12;
  list_push(&head,val);
  cr_expect(head->value == val);
  cr_expect(head->next != NULL);
}

// Pop
Test(list, pop) {
  int val = list_pop(&head);
  cr_expect(val == 7);
  cr_expect(list_length(head) == 0);
}

// Get
Test(list, get) {
  int value = 7;
  int result = list_get(head,0);
  cr_expect(result == value,
            "Error: Result '%i' did not match expect value '%i'.",
            result, value);
}

// Append
Test(list, append) {
  int value=10;
  list_append(&head, value);
  int result = list_get(head,1);
  cr_expect(result == value,
            "Error: Result '%i' did not match expect value '%i'.",
            result, value);
}

// Insert
Test(list, insert) {
  list_push(&head, 22);
  list_push(&head, 44);
  list_push(&head, 55);
  int value = 33, pos = 2;
  list_insert(&head, value, pos);
  int result = list_get(head, pos);
  cr_expect(result == value,
            "Error: Result '%i' did not match expect value '%i'.",
            result, value);
}

// Remove
Test(list, remove) {
  list_push(&head, 22);
  list_push(&head, 44);
  list_push(&head, 55);
  list_rm(&head, 2);
  int result = list_get(head, 2);
  cr_expect(result != 22,
            "Error: Result '%i' did not match expected value.",
            result);
}

Test(list, set) {
  int value = 8, index = 1;
  list_push(&head, 5);
  list_push(&head, 9);
  list_set(head, index, value);
  int result = list_get(head, index);
  cr_expect(result == value,
            "Error: Result '%i' did not match expected value '%i'.",
            result, value);
}

// Free
Test(list, free) {
  list_free(&head);
  cr_expect(head == NULL);
}