-
Notifications
You must be signed in to change notification settings - Fork 187
/
simple.c
87 lines (70 loc) · 2.11 KB
/
simple.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
#include <assert.h>
#include <stdio.h>
#include "../ringbuffer.h"
int main(void) {
int i, cnt;
char tmp;
char tmp_arr[50];
/* Create and initialize ring buffer */
ring_buffer_t ring_buffer;
char buf_arr[128];
ring_buffer_init(&ring_buffer, buf_arr, sizeof(buf_arr));
/* Add elements to buffer; one at a time */
for(i = 0; i < 100; i++) {
ring_buffer_queue(&ring_buffer, i);
}
/* Verify size */
assert(ring_buffer_num_items(&ring_buffer) == 100);
/* Peek third element */
cnt = ring_buffer_peek(&ring_buffer, &tmp, 3);
/* Assert byte returned */
assert(cnt == 1);
/* Assert contents */
assert(tmp == 3);
/* Dequeue all elements */
for(cnt = 0; ring_buffer_dequeue(&ring_buffer, &tmp) > 0; cnt++) {
/* Do something with buf... */
assert(tmp == cnt);
printf("Read: %d\n", tmp);
}
printf("\n===============\n");
/* Add array */
ring_buffer_queue_arr(&ring_buffer, "Hello, Ring Buffer!", 20);
/* Is buffer empty? */
assert(!ring_buffer_is_empty(&ring_buffer));
/* Dequeue all elements */
while(ring_buffer_dequeue(&ring_buffer, &tmp) > 0) {
/* Print contents */
printf("Read: %c\n", tmp);
}
/* Add new array */
ring_buffer_queue_arr(&ring_buffer, "Hello again, Ring Buffer!", 26);
/* Dequeue array in two parts */
printf("Read:\n");
cnt = ring_buffer_dequeue_arr(&ring_buffer, tmp_arr, 13);
printf("%d\n", cnt);
assert(cnt == 13);
/* Add \0 termination before printing */
tmp_arr[13] = '\0';
printf("%s\n", tmp_arr);
/* Dequeue remaining */
cnt = ring_buffer_dequeue_arr(&ring_buffer, tmp_arr, 13);
assert(cnt == 13);
printf("%s", tmp_arr);
printf("\n===============\n");
/* Overfill buffer */
for(i = 0; i < 1000; i++) {
ring_buffer_queue(&ring_buffer, (i % 127));
}
/* Is buffer full? */
if(ring_buffer_is_full(&ring_buffer)) {
cnt = ring_buffer_num_items(&ring_buffer);
printf("Buffer is full and contains %d bytes\n", cnt);
}
/* Dequeue all elements */
while(ring_buffer_dequeue(&ring_buffer, &tmp) > 0) {
/* Print contents */
printf("Read: 0x%02x\n", tmp);
}
return 0;
}