]>
git.proxmox.com Git - mirror_frr.git/blob - tests/lib/test_ringbuf.c
1 // SPDX-License-Identifier: GPL-2.0-or-later
3 * Circular buffer tests.
4 * Copyright (C) 2017 Cumulus Networks
11 static void validate_state(struct ringbuf
*buf
, size_t size
, size_t contains
)
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
);
19 int main(int argc
, char **argv
)
21 struct ringbuf
*soil
= ringbuf_new(BUFSIZ
);
23 validate_state(soil
, BUFSIZ
, 0);
25 /* verify reset functionality on clean buffer */
26 printf("Validating reset on empty buffer...\n");
29 validate_state(soil
, BUFSIZ
, 0);
32 printf("Validating write...\n");
34 assert(ringbuf_put(soil
, &walnut
, sizeof(walnut
)) == 1);
36 validate_state(soil
, BUFSIZ
, 1);
38 /* validate read limitations */
39 printf("Validating read limits...\n");
41 assert(ringbuf_get(soil
, &nuts
, sizeof(nuts
)) == 1);
44 printf("Validating reset on full buffer...\n");
46 validate_state(soil
, BUFSIZ
, 0);
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
);
53 validate_state(soil
, BUFSIZ
, BUFSIZ
);
54 assert(soil
->start
== 0);
55 assert(soil
->end
== 0);
57 /* read 15 bytes of garbage */
58 printf("Validating read...\n");
59 assert(ringbuf_get(soil
, &compost
, 15) == 15);
61 validate_state(soil
, BUFSIZ
, BUFSIZ
- 15);
62 assert(soil
->start
== 15);
63 assert(soil
->end
== 0);
65 /* put another 10 bytes and validate wraparound */
66 printf("Validating wraparound...\n");
67 assert(ringbuf_put(soil
, &compost
[BUFSIZ
/2], 10) == 10);
69 validate_state(soil
, BUFSIZ
, BUFSIZ
- 15 + 10);
70 assert(soil
->start
== 15);
71 assert(soil
->end
== 10);
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
);
78 /* read entire buffer */
79 printf("Validating big read...\n");
80 assert(ringbuf_get(soil
, &compost
, BUFSIZ
) == BUFSIZ
);
82 validate_state(soil
, BUFSIZ
, 0);
83 assert(soil
->empty
== true);
84 assert(soil
->start
== soil
->end
);
85 assert(soil
->start
== 15);
87 /* read empty buffer */
88 printf("Validating empty read...\n");
89 assert(ringbuf_get(soil
, &compost
, 1) == 0);
90 validate_state(soil
, BUFSIZ
, 0);
92 /* reset, validate state */
93 printf("Validating reset...\n");
95 validate_state(soil
, BUFSIZ
, 0);
96 assert(soil
->start
== 0);
97 assert(soil
->end
== 0);
99 /* wipe, validate state */
100 printf("Validating wipe...\n");
101 memset(&compost
, 0x00, sizeof(compost
));
103 assert(memcmp(&compost
, soil
->data
, sizeof(compost
)) == 0);
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
);
110 validate_state(soil
, BUFSIZ
, BUFSIZ
);
112 /* wipe, validate state */
113 printf("Validating wipe...\n");
114 memset(&compost
, 0x00, sizeof(compost
));
116 assert(memcmp(&compost
, soil
->data
, sizeof(compost
)) == 0);
118 /* validate simple data encode / decode */
119 const char *organ
= "seed";
120 printf("Encoding: '%s'\n", organ
);
121 assert(ringbuf_put(soil
, organ
, strlen(organ
)) == 4);
122 char water
[strlen(organ
) + 1];
123 assert(ringbuf_get(soil
, &water
, strlen(organ
)) == 4);
124 water
[strlen(organ
)] = '\0';
125 printf("Retrieved: '%s'\n", water
);
127 validate_state(soil
, BUFSIZ
, 0);
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
);
134 assert(ringbuf_put(soil
, phloem
, strlen(phloem
)) == 4);
135 char xylem
[strlen(phloem
) + 1];
136 assert(ringbuf_get(soil
, &xylem
, 100) == 4);
137 xylem
[strlen(phloem
)] = '\0';
138 printf("Retrieved: '%s'\n", xylem
);
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
);
155 printf("Deleting...\n");
158 printf("Creating new buffer...\n");
159 soil
= ringbuf_new(15);
160 soil
->start
= soil
->end
= 7;
162 /* validate data encode of excessive data */
163 const char *twenty
= "vascular plants----";
165 printf("Encoding: %s\n", twenty
);
166 assert(ringbuf_put(soil
, twenty
, strlen(twenty
)) == 15);
167 assert(ringbuf_get(soil
, sixteen
, 20));
169 printf("Retrieved: %s\n", sixteen
);
170 assert(!strcmp(sixteen
, "vascular plants"));
172 printf("Deleting...\n");