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,
"invalid byte at %d, got %" PRIu8
" instead of %d\n", i,
133 chunks[0].data[i], i % 5);
138 if (ringbuffer_used(&ringBuffer) != 5)
140 (void)fprintf(stderr,
"invalid used size after read got %" PRIuz
" when I would expect 5\n",
141 ringbuffer_used(&ringBuffer));
146 if (!ringbuffer_write(&ringBuffer, tmpBuf, 6) ||
147 ringbuffer_peek(&ringBuffer, chunks, 11) != 2 || chunks[0].size != 10 ||
150 (void)fprintf(stderr,
"invalid read of splitted data\n");
154 ringbuffer_commit_read_bytes(&ringBuffer, 11);
155 (void)fprintf(stderr,
"ok\n");
157 (void)fprintf(stderr,
"%d: peek with nothing to read...", ++testNo);
158 if (ringbuffer_peek(&ringBuffer, chunks, 10))
160 (void)fprintf(stderr,
"peek returns some chunks\n");
163 (void)fprintf(stderr,
"ok\n");
165 (void)fprintf(stderr,
"%d: ensure_linear_write / read() shouldn't grow...", ++testNo);
166 for (
int i = 0; i < 1000; i++)
168 rb_ptr = ringbuffer_ensure_linear_write(&ringBuffer, 50);
171 (void)fprintf(stderr,
"ringbuffer_ensure_linear_write() error\n");
175 memcpy(rb_ptr, tmpBuf, 50);
177 if (!ringbuffer_commit_written_bytes(&ringBuffer, 50))
179 (void)fprintf(stderr,
"ringbuffer_commit_written_bytes() error, i=%d\n", i);
186 for (
int i = 0; i < 1000; i++)
187 ringbuffer_commit_read_bytes(&ringBuffer, 25);
189 for (
int i = 0; i < 1000; i++)
190 ringbuffer_commit_read_bytes(&ringBuffer, 25);
192 if (ringbuffer_capacity(&ringBuffer) != 10)
194 (void)fprintf(stderr,
"not the expected capacity, have %" PRIuz
" and expects 10\n",
195 ringbuffer_capacity(&ringBuffer));
198 (void)fprintf(stderr,
"ok\n");
200 (void)fprintf(stderr,
"%d: free size is correctly computed...", ++testNo);
201 for (
int i = 0; i < 1000; i++)
203 ringbuffer_ensure_linear_write(&ringBuffer, 50);
204 if (!ringbuffer_commit_written_bytes(&ringBuffer, 50))
206 (void)fprintf(stderr,
"ringbuffer_commit_written_bytes() error, i=%d\n", i);
210 ringbuffer_commit_read_bytes(&ringBuffer, 50ULL * 1000ULL);
211 (void)fprintf(stderr,
"ok\n");
213 ringbuffer_destroy(&ringBuffer);
215 (void)fprintf(stderr,
"%d: specific overlaps test...", ++testNo);
216 if (!test_overlaps())
218 (void)fprintf(stderr,
"ko\n");
221 (void)fprintf(stderr,
"ok\n");
223 ringbuffer_destroy(&ringBuffer);
a piece of data in the ring buffer, exactly like a glibc iovec