forked from beanstalkd/beanstalkd
-
Notifications
You must be signed in to change notification settings - Fork 0
/
testms.c
139 lines (105 loc) · 2.33 KB
/
testms.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
135
136
137
138
#include "dat.h"
#include <stdint.h>
#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include <sys/time.h>
#include "ct/ct.h"
void
cttest_ms_append()
{
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
int i = 10;
int ok = ms_append(a, &i);
assertf(a->len == 1, "a should contain one item");
assertf(ok, "should be added");
ok = ms_append(a, &i);
assertf(a->len == 2, "a should contain two items");
assertf(ok, "should be added");
ms_clear(a);
assertf(a->len == 0, "a should be empty");
free(a->items);
free(a);
}
void
cttest_ms_remove()
{
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
int i = 1;
ms_append(a, &i);
int j = 2;
int ok = ms_remove(a, &j);
assertf(!ok, "j should not be removed");
ok = ms_remove(a, &i);
assertf(ok, "i should be removed");
ok = ms_remove(a, &i);
assertf(!ok, "i was already removed");
assertf(a->len == 0, "a should be empty");
free(a->items);
free(a);
}
void
cttest_ms_contains()
{
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
int i = 1;
ms_append(a, &i);
int ok = ms_contains(a, &i);
assertf(ok, "i should be in a");
int j = 2;
ok = ms_contains(a, &j);
assertf(!ok, "j should not be in a");
ms_clear(a);
free(a->items);
free(a);
}
void
cttest_ms_clear_empty()
{
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
ms_clear(a);
assertf(a->len == 0, "a should be empty");
free(a->items);
free(a);
}
void
cttest_ms_take()
{
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
int i = 10;
int j = 20;
ms_append(a, &i);
ms_append(a, &j);
int *n;
n = (int *)ms_take(a);
assertf(n == &i, "n should point to i");
n = (int *)ms_take(a);
assertf(n == &j, "n should point to j");
n = (int *)ms_take(a);
assertf(n == NULL, "n should be NULL; ms is empty");
free(a->items);
free(a);
}
void
cttest_ms_take_sequence()
{
size_t i;
int s[] = {1, 2, 3, 4, 5, 6};
int e[] = {1, 2, 3, 6, 5, 4};
Ms *a = new(struct Ms);
ms_init(a, NULL, NULL);
size_t n = sizeof(s)/sizeof(s[0]);
for (i = 0; i < n; i++)
ms_append(a, &s[i]);
for (i = 0; i < n; i++) {
int *got = (int *)ms_take(a);
assert(*got == e[i]);
}
free(a->items);
free(a);
}