-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathcircular_buffer_test.c
64 lines (47 loc) · 1.11 KB
/
circular_buffer_test.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
#include "unit_test.h"
#include "circular_buffer.h"
#define BUF_CAPACITY 1024
UNIT_START
cb_t cb;
int i;
int *data_buf, *res_buf;
TEST_START("cb_init()");
cb_init(&cb, BUF_CAPACITY, NULL);
CHECK_IF(cb.buffer);
TEST_END;
TEST_START("cb_push()");
for (i = 0; i < BUF_CAPACITY; ++i) {
data_buf = malloc(sizeof(int));
*data_buf = i;
res_buf = cb_push(&cb, data_buf);
CHECK_EQUAL(res_buf, data_buf);
}
data_buf = malloc(sizeof(int));
*data_buf = BUF_CAPACITY;
res_buf = cb_push(&cb, data_buf);
CHECK_EQUAL(res_buf, NULL);
free(data_buf);
TEST_END;
TEST_START("cb_pop()");
for (i = 0; i < BUF_CAPACITY; ++i) {
data_buf = cb_pop(&cb);
CHECK_EQUAL(*data_buf, i);
free(data_buf);
}
CHECK_EQUAL(cb_pop(&cb), NULL);
CHECK_EQUAL(cb.size, 0);
TEST_END;
cb_destroy(&cb);
TEST_START("cb_destroy()");
cb_init(&cb, BUF_CAPACITY, &free);
for (i = 0; i < BUF_CAPACITY; ++i) {
data_buf = malloc(sizeof(int));
*data_buf = i;
res_buf = cb_push(&cb, data_buf);
}
cb_destroy(&cb);
CHECK_EQUAL(cb.size, 0);
CHECK_EQUAL(cb.buffer, NULL);
CHECK_EQUAL(cb.capacity, 0);
TEST_END;
UNIT_END