1 #include <stdlib.h>
2 #include <assert.h>
3 #include <sys/types.h>
4 #include <sys/stat.h>
5 #include <fcntl.h>
6 #include <ev.h>
7 #include "buffer.h"
8 
test1()9 static void test1() {
10     struct Buffer *buffer;
11     char input[] = "This is a test.";
12     char output[sizeof(input)];
13     int len, i;
14 
15     buffer = new_buffer(256, EV_DEFAULT);
16     assert(buffer != NULL);
17 
18     len = buffer_push(buffer, input, sizeof(input));
19     assert(len == sizeof(input));
20 
21 
22     len = buffer_peek(buffer, output, sizeof(output));
23     assert(len == sizeof(input));
24 
25     for (i = 0; i < len; i++)
26         assert(input[i] == output[i]);
27 
28     /* second peek to ensure the first didn't permute the state of the buffer */
29     len = buffer_peek(buffer, output, sizeof(output));
30     assert(len == sizeof(input));
31 
32     for (i = 0; i < len; i++)
33         assert(input[i] == output[i]);
34 
35     /* test pop */
36     len = buffer_pop(buffer, output, sizeof(output));
37     assert(len == sizeof(input));
38 
39     for (i = 0; i < len; i++)
40         assert(input[i] == output[i]);
41 
42     len = buffer_pop(buffer, output, sizeof(output));
43     assert(len == 0);
44 
45     free_buffer(buffer);
46 }
47 
test2()48 static void test2() {
49     struct Buffer *buffer;
50     char input[] = "Testing wrap around behaviour.";
51     char output[sizeof(input)];
52     int len, i = 0;
53 
54     buffer = new_buffer(256, EV_DEFAULT);
55     assert(buffer != NULL);
56 
57     while (i < 236) {
58         len = buffer_push(buffer, input, sizeof(input));
59         assert(len == sizeof(input));
60 
61         i += len;
62     }
63 
64     while (len) {
65         len = buffer_pop(buffer, output, sizeof(output));
66     }
67 
68     len = buffer_push(buffer, input, sizeof(input));
69     assert(len == sizeof(input));
70 
71 
72     len = buffer_peek(buffer, output, sizeof(output));
73     assert(len == sizeof(input));
74 
75     for (i = 0; i < len; i++)
76         assert(input[i] == output[i]);
77 
78     len = buffer_pop(buffer, output, sizeof(output));
79     assert(len == sizeof(input));
80 
81     for (i = 0; i < len; i++)
82         assert(input[i] == output[i]);
83 
84     len = buffer_push(buffer, input, sizeof(input));
85     assert(len == sizeof(input));
86 
87 
88     len = buffer_peek(buffer, output, sizeof(output));
89     assert(len == sizeof(input));
90 
91     for (i = 0; i < len; i++)
92         assert(input[i] == output[i]);
93 
94     free_buffer(buffer);
95 }
96 
test3()97 static void test3() {
98     struct Buffer *buffer;
99     char input[] = "Test buffer resizing.";
100     char output[sizeof(input)];
101     int len, i;
102 
103     buffer = new_buffer(256, EV_DEFAULT);
104     assert(buffer != NULL);
105 
106     len = buffer_push(buffer, input, sizeof(input));
107     assert(len == sizeof(input));
108 
109     /* Test resizing to too small of a buffer size */
110     len = buffer_resize(buffer, 8);
111     assert(len == -1);
112 
113 
114     buffer_resize(buffer, 32);
115     assert(buffer_room(buffer) == 32 - sizeof(input));
116 
117     len = buffer_peek(buffer, output, sizeof(output));
118     assert(len == sizeof(input));
119 
120     for (i = 0; i < len; i++)
121         assert(input[i] == output[i]);
122 
123     free_buffer(buffer);
124 }
125 
test4()126 static void test4() {
127     struct Buffer *buffer;
128     int read_fd, write_fd;
129 
130     buffer = new_buffer(4096, EV_DEFAULT);
131 
132     read_fd = open("/dev/zero", O_RDONLY);
133     if (read_fd < 0) {
134         perror("open:");
135         exit(1);
136     }
137 
138     write_fd = open("/dev/null", O_WRONLY);
139     if (write_fd < 0) {
140         perror("open:");
141         exit(1);
142     }
143 
144     while (buffer->tx_bytes < 65536) {
145         buffer_read(buffer, read_fd);
146         buffer_write(buffer, write_fd);
147     }
148 
149     free_buffer(buffer);
150 }
151 
test_buffer_coalesce()152 static void test_buffer_coalesce() {
153     struct Buffer *buffer;
154     char input[] = "Test buffer resizing.";
155     char output[sizeof(input)];
156     int len;
157 
158     buffer = new_buffer(4096, EV_DEFAULT);
159     len = buffer_push(buffer, input, sizeof(input));
160     assert(len == sizeof(input));
161 
162     len = buffer_pop(buffer, output, sizeof(output));
163     assert(len == sizeof(output));
164     assert(buffer_len(buffer) == 0);
165     assert(buffer->head != 0);
166 
167     len = buffer_coalesce(buffer, NULL);
168     assert(len == 0);
169 }
170 
main()171 int main() {
172     test1();
173 
174     test2();
175 
176     test3();
177 
178     test4();
179 
180     test_buffer_coalesce();
181 }
182