]> git.sesse.net Git - fjl/blob - bytesource_test.c
Add a new input source converting JPEG-format bytes into unstuffed bytes.
[fjl] / bytesource_test.c
1 #include <stdio.h>
2 #include <string.h>
3 #include <assert.h>
4
5 #include "bytesource.h"
6 #include "choice.h"
7
8 struct custom_read_userdata {
9         uint8_t* bytes;
10         unsigned bytes_left;
11 };
12
13 ssize_t custom_read(void* userdata, uint8_t* buf, size_t count)
14 {
15         struct custom_read_userdata* ud = (struct custom_read_userdata*)userdata;
16         size_t num_to_read = (ud->bytes_left > count ? count : ud->bytes_left);
17         memcpy(buf, ud->bytes, num_to_read);
18         ud->bytes += num_to_read;
19         ud->bytes_left -= num_to_read;
20         return num_to_read;     
21 }
22
23 ssize_t custom_read_slow(void* userdata, uint8_t* buf, size_t count)
24 {
25         struct custom_read_userdata* ud = (struct custom_read_userdata*)userdata;
26         size_t num_to_read = ((count > 0 && ud->bytes_left > 0) ? 1 : 0);
27         memcpy(buf, ud->bytes, num_to_read);
28         ud->bytes += num_to_read;
29         ud->bytes_left -= num_to_read;
30         return num_to_read;
31 }
32
33 // Two streams, separated by a marker.
34 uint8_t bytes[] = {
35         0x01, 0x02, 0x03, 0xff, 0x00, 0x04,     // some bytes
36         0xff, 0xf7,                             // a marker
37         0x05, 0x06, 0x07, 0x08, 0xff, 0x00      // more bytes
38 };
39
40 // Expected data.
41 uint8_t first_stream[] = {
42         0x01, 0x02, 0x03, 0xff, 0x04
43 };
44 uint8_t second_stream[] = {
45         0x05, 0x06, 0x07, 0x08, 0xff
46 };
47
48 // Reading with a regular source.
49 void test_basic_reading()
50 {
51         struct custom_read_userdata ud;
52         ud.bytes = bytes;
53         ud.bytes_left = sizeof(bytes);
54
55         struct byte_source source;
56         init_byte_source(&source, custom_read, &ud);
57
58         // Read the first stream.
59         uint8_t buf[4096];
60         ssize_t ret;
61         ret = byte_source_input_func(&source, buf, 4096);
62         assert(ret == sizeof(first_stream));
63         assert(memcmp(buf, first_stream, sizeof(first_stream)) == 0);
64
65         // Now we should get EOF.
66         ret = byte_source_input_func(&source, buf, 4096);
67         assert(ret == 0);
68
69         // Read the marker.
70         uint8_t marker = byte_source_read_marker(&source);
71         assert(marker == 0xf7);
72
73         // Read the second stream.
74         ret = byte_source_input_func(&source, buf, 4096);
75         assert(ret == sizeof(second_stream));
76         assert(memcmp(buf, second_stream, sizeof(second_stream)) == 0);
77
78         // ...and EOF again.
79         ret = byte_source_input_func(&source, buf, 4096);
80         assert(ret == 0);
81 }
82
83 // Reading with a slow source.
84 void test_slow_source()
85 {
86         struct custom_read_userdata ud;
87         ud.bytes = bytes;
88         ud.bytes_left = sizeof(bytes);
89
90         struct byte_source source;
91         init_byte_source(&source, custom_read_slow, &ud);
92                 
93         uint8_t buf[4096];
94         ssize_t ret;
95         
96         // Read the first stream. Since the source is slow, we'll get one by
97         // one byte, even though we asked for 4096.
98         for (unsigned i = 0; i < sizeof(first_stream); ++i) {
99                 ret = byte_source_input_func(&source, buf, 4096);
100                 assert(ret == 1);
101                 assert(buf[0] == first_stream[i]);
102         }
103
104         // Now we should get EOF.
105         ret = byte_source_input_func(&source, buf, 4096);
106         assert(ret == 0);
107
108         // Read the marker.
109         uint8_t marker = byte_source_read_marker(&source);
110         assert(marker == 0xf7);
111
112         // Read the second stream.
113         for (unsigned i = 0; i < sizeof(second_stream); ++i) {
114                 uint8_t buf[4096];
115                 ssize_t ret;
116                 ret = byte_source_input_func(&source, buf, 4096);
117                 assert(ret == 1);
118                 assert(buf[0] == second_stream[i]);
119         }
120
121         // ...and EOF again.
122         ret = byte_source_input_func(&source, buf, 4096);
123         assert(ret == 0);
124 }
125
126 int main(void)
127 {
128         init_choices();
129
130         printf("test_basic_reading()\n");
131         test_basic_reading();
132
133         printf("test_slow_source()\n");
134         test_slow_source();
135         
136         printf("All tests pass.\n");
137         return 0;
138 }