-
Notifications
You must be signed in to change notification settings - Fork 15
/
Copy pathtests.c
134 lines (103 loc) · 2.66 KB
/
tests.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
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
#include <stdio.h>
#include <assert.h>
#include "tests.h"
/**
* These are tests for arraylist.c and hastable.c
*
* The tests are currently a work in progress. Their aim is to cover
* all of arraylist and hashtable functionality.
*/
void arraylist_debug(arraylist* l) {
int i;
void* v;
arraylist_iterate(l, i, v) {
printf("[%i] = %p\n", i, v);
}
}
int main()
{
/*
* Make a few dummy pointers as values for testing.
* These are never dereferenced.
*/
void* a = (void*)0x500;
void* b = (void*)0x501;
void* c = (void*)0x502;
void* d = (void*)0x503;
void* e = (void*)0x504;
void* f = (void*)0x505;
/*
* Arraylist tests
*/
printf("Running arraylist.c tests.\n");
arraylist* l = arraylist_create();
assert(l->size == 0);
arraylist_add(l, a);
arraylist_add(l, b);
assert(l->size == 2);
assert(arraylist_get(l, 0) == a);
assert(arraylist_get(l, 1) == b);
arraylist_add(l, c);
arraylist_add(l, d);
assert(l->size == 4);
assert(arraylist_get(l, 3) == d);
assert(arraylist_get(l, 2) == c);
assert(arraylist_pop(l) == d);
assert(arraylist_pop(l) == c);
assert(l->size == 2);
arraylist_add(l, e);
arraylist_add(l, f);
arraylist_insert(l, 2, c);
assert(l->size == 5);
assert(arraylist_get(l, 0) == a);
assert(arraylist_get(l, 1) == b);
assert(arraylist_get(l, 2) == c);
assert(arraylist_get(l, 3) == e);
assert(arraylist_get(l, 4) == f);
arraylist_remove(l, 1);
assert(l->size == 4);
assert(arraylist_get(l, 0) == a);
assert(arraylist_get(l, 1) == c);
assert(arraylist_get(l, 2) == e);
arraylist* slice = arraylist_slice(l, 1, 2);
assert(slice->size == 2);
assert(arraylist_get(slice, 0) == c);
assert(arraylist_get(slice, 1) == e);
arraylist* copy = arraylist_copy(l);
assert(copy->size == l->size);
int i;
void* v;
arraylist_iterate(copy, i, v) {
assert(v == arraylist_get(l, i));
}
arraylist_debug(copy);
arraylist_clear(l);
assert(l->size == 0);
arraylist_iterate(l, i, v) {
assert(0); // Iterating over an empty list should never run.
}
arraylist_destroy(l);
arraylist_destroy(slice);
arraylist_destroy(copy);
/*
* Hashtable tests
*/
printf("Running hashtable.c tests.\n");
hashtable* t = hashtable_create();
assert(t->size == 0);
hashtable_set(t, "alpha", a);
assert(t->size == 1);
assert(hashtable_get(t, "alpha") == a);
assert(hashtable_get(t, "beta") == NULL);
hashtable_remove(t, "alpha");
assert(t->size == 0);
assert(hashtable_get(t, "alpha") == NULL);
hashtable_set(t, "beta", b);
assert(t->size == 1);
assert(hashtable_get(t, "beta") == b);
hashtable_set(t, "beta", c);
assert(t->size == 1);
assert(hashtable_get(t, "beta") == c);
hashtable_destroy(t);
printf("All tests completed.\n");
}