23 #include <freerdp/utils/ringbuffer.h>
25 static BOOL test_overlaps(
void)
33 for (
size_t i = 0; i <
sizeof(bytes); i++)
36 ringbuffer_init(&rb, 5);
37 if (!ringbuffer_write(&rb, bytes, 4))
40 ringbuffer_commit_read_bytes(&rb, 2);
42 if (!ringbuffer_write(&rb, &bytes[counter], 2))
46 nchunks = ringbuffer_peek(&rb, chunks, 4);
47 if (nchunks != 2 || chunks[0].size != 3 || chunks[1].size != 1)
50 for (
int x = 0, j = 2; x < nchunks; x++)
52 for (
size_t k = 0; k < chunks[x].size; k++, j++)
54 if (chunks[x].data[k] != (BYTE)j)
59 ringbuffer_commit_read_bytes(&rb, 3);
60 if (ringbuffer_used(&rb) != 1)
63 if (!ringbuffer_write(&rb, &bytes[counter], 6))
66 ringbuffer_commit_read_bytes(&rb, 6);
67 nchunks = ringbuffer_peek(&rb, chunks, 10);
68 if (nchunks != 1 || chunks[0].size != 1 || (*chunks[0].data != 0xb))
71 if (ringbuffer_capacity(&rb) != 5)
74 ringbuffer_destroy(&rb);
77 ringbuffer_destroy(&rb);
81 int TestRingBuffer(
int argc,
char* argv[])
92 if (!ringbuffer_init(&ringBuffer, 10))
94 (void)fprintf(stderr,
"unable to initialize ringbuffer\n");
98 tmpBuf = (BYTE*)malloc(50);
102 for (
int i = 0; i < 50; i++)
105 (void)fprintf(stderr,
"%d: basic tests...", ++testNo);
106 if (!ringbuffer_write(&ringBuffer, tmpBuf, 5) || !ringbuffer_write(&ringBuffer, tmpBuf, 5) ||
107 !ringbuffer_write(&ringBuffer, tmpBuf, 5))
109 (void)fprintf(stderr,
"error when writing bytes\n");
113 if (ringbuffer_used(&ringBuffer) != 15)
115 (void)fprintf(stderr,
"invalid used size got %" PRIuz
" when I would expect 15\n",
116 ringbuffer_used(&ringBuffer));
120 if (ringbuffer_peek(&ringBuffer, chunks, 10) != 1 || chunks[0].size != 10)
122 (void)fprintf(stderr,
"error when reading bytes\n");
125 ringbuffer_commit_read_bytes(&ringBuffer, chunks[0].size);
128 for (
size_t i = 0; i < chunks[0].size; i++)
130 if (chunks[0].data[i] != i % 5)
132 (void)fprintf(stderr,
133 "invalid byte at %" PRIuz
", got %" PRIu8
" instead of %" PRIuz
"\n", i,
134 chunks[0].data[i], i % 5ULL);
139 if (ringbuffer_used(&ringBuffer) != 5)
141 (void)fprintf(stderr,
"invalid used size after read got %" PRIuz
" when I would expect 5\n",
142 ringbuffer_used(&ringBuffer));
147 if (!ringbuffer_write(&ringBuffer, tmpBuf, 6) ||
148 ringbuffer_peek(&ringBuffer, chunks, 11) != 2 || chunks[0].size != 10 ||
151 (void)fprintf(stderr,
"invalid read of split data\n");
155 ringbuffer_commit_read_bytes(&ringBuffer, 11);
156 (void)fprintf(stderr,
"ok\n");
158 (void)fprintf(stderr,
"%d: peek with nothing to read...", ++testNo);
159 if (ringbuffer_peek(&ringBuffer, chunks, 10))
161 (void)fprintf(stderr,
"peek returns some chunks\n");
164 (void)fprintf(stderr,
"ok\n");
166 (void)fprintf(stderr,
"%d: ensure_linear_write / read() shouldn't grow...", ++testNo);
167 for (
int i = 0; i < 1000; i++)
169 rb_ptr = ringbuffer_ensure_linear_write(&ringBuffer, 50);
172 (void)fprintf(stderr,
"ringbuffer_ensure_linear_write() error\n");
176 memcpy(rb_ptr, tmpBuf, 50);
178 if (!ringbuffer_commit_written_bytes(&ringBuffer, 50))
180 (void)fprintf(stderr,
"ringbuffer_commit_written_bytes() error, i=%d\n", i);
187 for (
int i = 0; i < 1000; i++)
188 ringbuffer_commit_read_bytes(&ringBuffer, 25);
190 for (
int i = 0; i < 1000; i++)
191 ringbuffer_commit_read_bytes(&ringBuffer, 25);
193 if (ringbuffer_capacity(&ringBuffer) != 10)
195 (void)fprintf(stderr,
"not the expected capacity, have %" PRIuz
" and expects 10\n",
196 ringbuffer_capacity(&ringBuffer));
199 (void)fprintf(stderr,
"ok\n");
201 (void)fprintf(stderr,
"%d: free size is correctly computed...", ++testNo);
202 for (
int i = 0; i < 1000; i++)
204 ringbuffer_ensure_linear_write(&ringBuffer, 50);
205 if (!ringbuffer_commit_written_bytes(&ringBuffer, 50))
207 (void)fprintf(stderr,
"ringbuffer_commit_written_bytes() error, i=%d\n", i);
211 ringbuffer_commit_read_bytes(&ringBuffer, 50ULL * 1000ULL);
212 (void)fprintf(stderr,
"ok\n");
214 ringbuffer_destroy(&ringBuffer);
216 (void)fprintf(stderr,
"%d: specific overlaps test...", ++testNo);
217 if (!test_overlaps())
219 (void)fprintf(stderr,
"ko\n");
222 (void)fprintf(stderr,
"ok\n");
224 ringbuffer_destroy(&ringBuffer);
a piece of data in the ring buffer, exactly like a glibc iovec