]>
Commit | Line | Data |
---|---|---|
acddc0ed | 1 | // SPDX-License-Identifier: GPL-2.0-or-later |
5318d896 QY |
2 | /* |
3 | * Circular buffer tests. | |
4 | * Copyright (C) 2017 Cumulus Networks | |
5 | * Quentin Young | |
5318d896 QY |
6 | */ |
7 | #include <zebra.h> | |
8 | #include <memory.h> | |
9 | #include "ringbuf.h" | |
10 | ||
11 | static void validate_state(struct ringbuf *buf, size_t size, size_t contains) | |
12 | { | |
13 | assert(buf->size == size); | |
14 | assert(ringbuf_remain(buf) == contains); | |
15 | assert(ringbuf_space(buf) == buf->size - contains); | |
16 | assert(buf->empty != (bool)contains); | |
17 | } | |
18 | ||
19 | int main(int argc, char **argv) | |
20 | { | |
21 | struct ringbuf *soil = ringbuf_new(BUFSIZ); | |
22 | ||
23 | validate_state(soil, BUFSIZ, 0); | |
24 | ||
25 | /* verify reset functionality on clean buffer */ | |
26 | printf("Validating reset on empty buffer...\n"); | |
27 | ringbuf_reset(soil); | |
28 | ||
29 | validate_state(soil, BUFSIZ, 0); | |
30 | ||
31 | /* put one byte */ | |
32 | printf("Validating write...\n"); | |
33 | uint8_t walnut = 47; | |
34 | assert(ringbuf_put(soil, &walnut, sizeof(walnut)) == 1); | |
35 | ||
36 | validate_state(soil, BUFSIZ, 1); | |
37 | ||
38 | /* validate read limitations */ | |
39 | printf("Validating read limits...\n"); | |
40 | uint8_t nuts[2]; | |
41 | assert(ringbuf_get(soil, &nuts, sizeof(nuts)) == 1); | |
42 | ||
43 | /* reset */ | |
44 | printf("Validating reset on full buffer...\n"); | |
45 | ringbuf_reset(soil); | |
46 | validate_state(soil, BUFSIZ, 0); | |
47 | ||
48 | /* copy stack garbage to buffer */ | |
49 | printf("Validating big write...\n"); | |
50 | uint8_t compost[BUFSIZ]; | |
51 | assert(ringbuf_put(soil, &compost, sizeof(compost)) == BUFSIZ); | |
52 | ||
53 | validate_state(soil, BUFSIZ, BUFSIZ); | |
54 | assert(soil->start == 0); | |
74e4a329 | 55 | assert(soil->end == 0); |
5318d896 QY |
56 | |
57 | /* read 15 bytes of garbage */ | |
58 | printf("Validating read...\n"); | |
59 | assert(ringbuf_get(soil, &compost, 15) == 15); | |
60 | ||
61 | validate_state(soil, BUFSIZ, BUFSIZ - 15); | |
62 | assert(soil->start == 15); | |
74e4a329 | 63 | assert(soil->end == 0); |
5318d896 QY |
64 | |
65 | /* put another 10 bytes and validate wraparound */ | |
66 | printf("Validating wraparound...\n"); | |
a5080622 | 67 | assert(ringbuf_put(soil, &compost[BUFSIZ/2], 10) == 10); |
5318d896 QY |
68 | |
69 | validate_state(soil, BUFSIZ, BUFSIZ - 15 + 10); | |
70 | assert(soil->start == 15); | |
71 | assert(soil->end == 10); | |
72 | ||
73 | /* put another 15 bytes and validate state */ | |
74 | printf("Validating size limits...\n"); | |
75 | assert(ringbuf_put(soil, &compost, 15) == 5); | |
76 | validate_state(soil, BUFSIZ, BUFSIZ); | |
77 | ||
78 | /* read entire buffer */ | |
79 | printf("Validating big read...\n"); | |
80 | assert(ringbuf_get(soil, &compost, BUFSIZ) == BUFSIZ); | |
81 | ||
82 | validate_state(soil, BUFSIZ, 0); | |
8dff30f8 | 83 | assert(soil->empty == true); |
5318d896 QY |
84 | assert(soil->start == soil->end); |
85 | assert(soil->start == 15); | |
86 | ||
87 | /* read empty buffer */ | |
88 | printf("Validating empty read...\n"); | |
89 | assert(ringbuf_get(soil, &compost, 1) == 0); | |
90 | validate_state(soil, BUFSIZ, 0); | |
91 | ||
92 | /* reset, validate state */ | |
93 | printf("Validating reset...\n"); | |
94 | ringbuf_reset(soil); | |
95 | validate_state(soil, BUFSIZ, 0); | |
96 | assert(soil->start == 0); | |
97 | assert(soil->end == 0); | |
98 | ||
99 | /* wipe, validate state */ | |
100 | printf("Validating wipe...\n"); | |
101 | memset(&compost, 0x00, sizeof(compost)); | |
102 | ringbuf_wipe(soil); | |
103 | assert(memcmp(&compost, soil->data, sizeof(compost)) == 0); | |
104 | ||
105 | /* validate maximum write */ | |
106 | printf("Validating very big write...\n"); | |
107 | const char flower[BUFSIZ * 2]; | |
108 | assert(ringbuf_put(soil, &flower, sizeof(flower)) == BUFSIZ); | |
109 | ||
110 | validate_state(soil, BUFSIZ, BUFSIZ); | |
111 | ||
112 | /* wipe, validate state */ | |
113 | printf("Validating wipe...\n"); | |
114 | memset(&compost, 0x00, sizeof(compost)); | |
115 | ringbuf_wipe(soil); | |
116 | assert(memcmp(&compost, soil->data, sizeof(compost)) == 0); | |
117 | ||
118 | /* validate simple data encode / decode */ | |
119 | const char *organ = "seed"; | |
120 | printf("Encoding: '%s'\n", organ); | |
a5080622 | 121 | assert(ringbuf_put(soil, organ, strlen(organ)) == 4); |
5318d896 | 122 | char water[strlen(organ) + 1]; |
a5080622 | 123 | assert(ringbuf_get(soil, &water, strlen(organ)) == 4); |
5318d896 QY |
124 | water[strlen(organ)] = '\0'; |
125 | printf("Retrieved: '%s'\n", water); | |
126 | ||
127 | validate_state(soil, BUFSIZ, 0); | |
128 | ||
129 | /* validate simple data encode / decode across ring boundary */ | |
130 | soil->start = soil->size - 2; | |
131 | soil->end = soil->start; | |
132 | const char *phloem = "root"; | |
133 | printf("Encoding: '%s'\n", phloem); | |
a5080622 | 134 | assert(ringbuf_put(soil, phloem, strlen(phloem)) == 4); |
5318d896 | 135 | char xylem[strlen(phloem) + 1]; |
a5080622 | 136 | assert(ringbuf_get(soil, &xylem, 100) == 4); |
5318d896 QY |
137 | xylem[strlen(phloem)] = '\0'; |
138 | printf("Retrieved: '%s'\n", xylem); | |
139 | ||
a5080622 QY |
140 | ringbuf_wipe(soil); |
141 | ||
142 | /* validate simple data peek across ring boundary */ | |
143 | soil->start = soil->size - 2; | |
144 | soil->end = soil->start; | |
145 | const char *cytoplasm = "tree"; | |
146 | printf("Encoding: '%s'\n", cytoplasm); | |
147 | assert(ringbuf_put(soil, cytoplasm, strlen(cytoplasm)) == 4); | |
148 | char chloroplast[strlen(cytoplasm) + 1]; | |
149 | assert(ringbuf_peek(soil, 2, &chloroplast[0], 100) == 2); | |
150 | assert(ringbuf_peek(soil, 0, &chloroplast[2], 2) == 2); | |
151 | chloroplast[strlen(cytoplasm)] = '\0'; | |
152 | assert(!strcmp(chloroplast, "eetr")); | |
153 | printf("Retrieved: '%s'\n", chloroplast); | |
154 | ||
5318d896 QY |
155 | printf("Deleting...\n"); |
156 | ringbuf_del(soil); | |
157 | ||
158 | printf("Creating new buffer...\n"); | |
159 | soil = ringbuf_new(15); | |
160 | soil->start = soil->end = 7; | |
161 | ||
162 | /* validate data encode of excessive data */ | |
163 | const char *twenty = "vascular plants----"; | |
164 | char sixteen[16]; | |
165 | printf("Encoding: %s\n", twenty); | |
166 | assert(ringbuf_put(soil, twenty, strlen(twenty)) == 15); | |
167 | assert(ringbuf_get(soil, sixteen, 20)); | |
168 | sixteen[15] = '\0'; | |
169 | printf("Retrieved: %s\n", sixteen); | |
170 | assert(!strcmp(sixteen, "vascular plants")); | |
171 | ||
172 | printf("Deleting...\n"); | |
173 | ringbuf_del(soil); | |
174 | ||
175 | printf("Done.\n"); | |
176 | return 0; | |
177 | } |