-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathrb_test.c
More file actions
187 lines (148 loc) · 3.94 KB
/
rb_test.c
File metadata and controls
187 lines (148 loc) · 3.94 KB
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
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
#include <stdint.h>
#include <stdio.h>
#include <assert.h>
#include <time.h>
#include <stdlib.h>
#include "ring_buffer.h"
#define RB_SIZE 101
void test_full_empty()
{
unsigned int i = 0, data;
struct ring_buffer* rb = ring_buffer_create(RB_SIZE, sizeof(i));
while (!ring_buffer_full(rb)) {
ring_buffer_put(rb, &i);
printf("Added %lu, Size now: %lu\n",
(unsigned long)i,
(unsigned long)ring_buffer_size(rb));
i++;
}
while (!ring_buffer_empty(rb)) {
ring_buffer_get(rb, &data);
printf("Data read %lu\n", (unsigned long)data);
}
ring_buffer_destroy(rb);
}
#if defined (HAVE_INT128)
typedef __uint128_t rb_uint_t;
#else
typedef uint64_t rb_uint_t;
#endif
void test_continuous()
{
/* see above typedef */
rb_uint_t i = 0;
struct ring_buffer* rb = ring_buffer_create(RB_SIZE, sizeof(i));
puts("Test Continuous");
for (i = 0; i < 1000000; ++i) {
rb_uint_t j=i, k=i+1, l=i+2, m=i+3, n=i+4, o=i+5, p=i+6, tmp;
ring_buffer_put(rb, &j);
ring_buffer_put(rb, &k);
ring_buffer_put(rb, &l);
ring_buffer_put(rb, &m);
ring_buffer_put(rb, &n);
ring_buffer_put(rb, &o);
ring_buffer_put(rb, &p);
ring_buffer_get(rb, &tmp);
assert(tmp == j);
ring_buffer_get(rb, &tmp);
assert(tmp == k);
ring_buffer_get(rb, &tmp);
assert(tmp == l);
ring_buffer_get(rb, &tmp);
assert(tmp == m);
ring_buffer_get(rb, &tmp);
assert(tmp == n);
ring_buffer_get(rb, &tmp);
assert(tmp == o);
ring_buffer_get(rb, &tmp);
assert(tmp == p);
}
ring_buffer_destroy(rb);
}
/*
* A more real-life test
*
* 1. open an existing file for read
* 2. create a new file for write
* 3. read a random number of bytes from (1)
* 4. put bytes in a ring buffer
* 5. get another random number of bytes from ring buffer
* 6. write those bytes to new file
* 7. repeat 3-6 until end of input.
* 8. close files and ring buffer
* 9. compare two files. Must be identical.
*
*/
void test_file_copy()
{
FILE* fin;
FILE* fout;
char c;
int num_to_put, num_to_get, index, rc;
const char* fname_in;
const char* fname_out;
struct ring_buffer* rb;
char diff_cmd[FILENAME_MAX];
srand(time(NULL));
rb = ring_buffer_create(RB_SIZE, sizeof(char));
if (!rb) {
perror("Failed to create ring buffer");
exit(1);
}
#if defined (_WIN32)
fname_in = __FILE__;
fname_out = "src_copy.c.txt";
#else
fname_in = "/usr/share/dict/words";
fname_out = "/tmp/words_copy";
#endif
fin = fopen(fname_in, "r");
if (!fin) {
perror(fname_in);
exit(1);
}
fout = fopen(fname_out, "w");
if (!fout) {
perror(fname_out);
exit(1);
}
for (;;) {
num_to_put = 1 + (rand() % RB_SIZE);
num_to_get = 1 + (rand() % RB_SIZE);
for (index = 0; index < num_to_put; ++index) {
if (ring_buffer_full(rb))
break;
if ((c = fgetc(fin)) == EOF)
break;
ring_buffer_put(rb, &c);
}
for (index = 0; index < num_to_get; ++index) {
if (ring_buffer_empty(rb))
break;
ring_buffer_get(rb, &c);
fputc(c, fout);
}
if (feof(fin) && ring_buffer_empty(rb))
break;
}
fclose(fin);
fclose(fout);
ring_buffer_destroy(rb);
#if defined (_WIN32)
snprintf(diff_cmd, sizeof diff_cmd, "fc \"%s\" \"%s\"", fname_in, fname_out);
#else
snprintf(diff_cmd, sizeof diff_cmd, "diff -s \"%s\" \"%s\"", fname_in, fname_out);
#endif
puts(diff_cmd);
rc = system(diff_cmd);
if (rc != 0)
exit(1);
}
int main()
{
printf("Sizeof rb = %lu\n", (unsigned long) sizeof(struct ring_buffer));
test_full_empty();
test_continuous();
test_file_copy();
return 0;
}