-
Notifications
You must be signed in to change notification settings - Fork 4
/
Copy pathtest.cpp
49 lines (40 loc) · 1 KB
/
test.cpp
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
#include <algorithm>
#include <stdio.h>
#include "ringbuffer.h"
#define DATALEN 100000
#define SIZE 10
#define ASSERT(cond) do { if (!(cond)) {printf("assertion \"%s\" failed\n", #cond); exit(1); }} while (0)
int main() {
ringbuffer<int> buf(SIZE);
int databuf[SIZE];
int readbuf[SIZE];
int data[DATALEN];
for (int i = 0; i < DATALEN; i++) {
data[i] = rand();
}
srand(123);
int readindex = 0;
int i = 0;
while (readindex < DATALEN) {
//printf("free: %d\n", buf.getFree());
size_t wcount = rand() % SIZE;
for (int j = 0; j < wcount; j++) {
databuf[j] = data[i];
i++;
}
i -= wcount;
wcount = std::min(wcount, buf.getFree());
wcount = std::min(wcount, (size_t)(DATALEN - i));
i += wcount;
size_t x = buf.write(databuf, wcount);
ASSERT(x == wcount);
//printf("free: %d\n", buf.getFree());
int rcount = rand() % SIZE;
x = buf.read(readbuf, rcount);
for (int j = 0; j < x; j++) {
ASSERT(readbuf[j] == data[readindex + j]);
}
readindex += x;
}
return 0;
}