|
1 /* |
|
2 * |
|
3 */ |
|
4 |
|
5 #include "memstream_tests.h" |
|
6 |
|
7 UCX_TEST_IMPLEMENT(test_ucx_memseektell) { |
|
8 char *buffer = malloc(16); |
|
9 memset(buffer, 32, 7); |
|
10 buffer[7] = 0; |
|
11 |
|
12 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
13 int r; |
|
14 |
|
15 UCX_TEST_BEGIN |
|
16 |
|
17 r = ucx_memseek(m, 5, SEEK_SET); |
|
18 UCX_TEST_ASSERT(r == 0, "seek SET+5 failed"); |
|
19 UCX_TEST_ASSERT(ucx_memtell(m) == 5, "seek SET+5 set wrong position"); |
|
20 |
|
21 r = ucx_memseek(m, 20, SEEK_SET); |
|
22 UCX_TEST_ASSERT(r != 0, "seek beyond bounds shall fail"); |
|
23 UCX_TEST_ASSERT(ucx_memtell(m) == 5, |
|
24 "failed seek shall leave pos unchanged"); |
|
25 |
|
26 r = ucx_memseek(m, 5, SEEK_CUR); |
|
27 UCX_TEST_ASSERT(r == 0, "seek CUR+5 failed"); |
|
28 UCX_TEST_ASSERT(ucx_memtell(m) == 10, "seek CUR+5 set wrong position"); |
|
29 |
|
30 r = ucx_memseek(m, 10, SEEK_CUR); |
|
31 UCX_TEST_ASSERT(r != 0, "seek CUR beyond bounds shall fail"); |
|
32 UCX_TEST_ASSERT(ucx_memtell(m) == 10, |
|
33 "failed seek shall leave pos unchanged"); |
|
34 |
|
35 r = ucx_memseek(m, -5, SEEK_END); |
|
36 UCX_TEST_ASSERT(r == 0, "seek END-5 failed"); |
|
37 UCX_TEST_ASSERT(ucx_memtell(m) == 2, "seek END-5 set wrong position"); |
|
38 |
|
39 r = ucx_memseek(m, -10, SEEK_END); |
|
40 UCX_TEST_ASSERT(r != 0, "seek END beyond bounds shall fail"); |
|
41 UCX_TEST_ASSERT(ucx_memtell(m) == 2, |
|
42 "failed seek shall leave pos unchanged"); |
|
43 |
|
44 UCX_TEST_END |
|
45 |
|
46 ucx_memclose(m); |
|
47 free(buffer); |
|
48 } |
|
49 |
|
50 UCX_TEST_IMPLEMENT(test_ucx_memputc) { |
|
51 char *buffer = malloc(16); |
|
52 memset(buffer, 32, 16); |
|
53 |
|
54 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
55 int r; |
|
56 |
|
57 UCX_TEST_BEGIN |
|
58 |
|
59 ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); |
|
60 UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts"); |
|
61 ucx_memseek(m, 10, SEEK_CUR); |
|
62 ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); |
|
63 UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after last 3 puts"); |
|
64 UCX_TEST_ASSERT(ucx_memeof(m), "eof not set"); |
|
65 UCX_TEST_ASSERT(!ucx_memoverflow(m), "overflow shall not be set"); |
|
66 UCX_TEST_ASSERT(ucx_memputc(m, 48) == EOF, "put shall return EOF on memof"); |
|
67 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, |
|
68 "buffer contains incorrect content"); |
|
69 |
|
70 UCX_TEST_END |
|
71 |
|
72 ucx_memclose(m); |
|
73 free(buffer); |
|
74 } |
|
75 |
|
76 UCX_TEST_IMPLEMENT(test_ucx_memgetc) { |
|
77 char *buffer = malloc(16); |
|
78 memset(buffer, 32, 8); |
|
79 for (int i = 8; i < 16 ; i++) { |
|
80 buffer[i] = 40+i; |
|
81 } |
|
82 |
|
83 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
84 int r; |
|
85 |
|
86 UCX_TEST_BEGIN |
|
87 |
|
88 char rb[16]; |
|
89 for (int i = 0 ; i < 16 ; i++) { |
|
90 UCX_TEST_ASSERT(ucx_memtell(m) == i, "pos wrong during read loop"); |
|
91 UCX_TEST_ASSERT(!ucx_memeof(m), |
|
92 "EOF shall not be set during read loop"); |
|
93 rb[i] = ucx_memgetc(m); |
|
94 } |
|
95 UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after read loop"); |
|
96 UCX_TEST_ASSERT(ucx_memeof(m), "EOF not set"); |
|
97 UCX_TEST_ASSERT(memcmp(rb, " 01234567", 16) == 0, |
|
98 "read data incorrect"); |
|
99 |
|
100 UCX_TEST_END |
|
101 |
|
102 ucx_memclose(m); |
|
103 free(buffer); |
|
104 } |
|
105 |
|
106 UCX_TEST_IMPLEMENT(test_ucx_memwrite) { |
|
107 char *buffer = malloc(16); |
|
108 memset(buffer, 32, 8); |
|
109 for (int i = 8; i < 16 ; i++) { |
|
110 buffer[i] = 40+i; |
|
111 } |
|
112 |
|
113 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
114 int r; |
|
115 |
|
116 UCX_TEST_BEGIN |
|
117 |
|
118 char* teststring = "this is way too much"; |
|
119 r = ucx_memwrite(teststring, 1, 20, m); |
|
120 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); |
|
121 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, |
|
122 "buffer data incorrect"); |
|
123 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); |
|
124 UCX_TEST_ASSERT(!ucx_memoverflow(m), "no overflow shall be caused"); |
|
125 |
|
126 ucx_memseek(m, 8, SEEK_SET); |
|
127 r = ucx_memwrite("not", 1, 3, m); |
|
128 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); |
|
129 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, |
|
130 "modified buffer is incorrect"); |
|
131 |
|
132 char* threebytestring = " t h r e e "; |
|
133 memset(buffer, 49, 16); |
|
134 ucx_memseek(m, 0, SEEK_SET); |
|
135 r = ucx_memwrite(threebytestring, 3, 6, m); |
|
136 UCX_TEST_ASSERT(r == 15, "three byte string not correctly trimed"); |
|
137 UCX_TEST_ASSERT(ucx_memtell(m) == 15, |
|
138 "position after write of three byte string incorrect"); |
|
139 UCX_TEST_ASSERT(!ucx_memeof(m), "eof shall not be set"); |
|
140 UCX_TEST_ASSERT(memcmp(buffer, " t h r e e1", 16) == 0, |
|
141 "bufer is incorrect after three byte string has been written"); |
|
142 |
|
143 UCX_TEST_END |
|
144 |
|
145 ucx_memclose(m); |
|
146 free(buffer); |
|
147 } |
|
148 |
|
149 UCX_TEST_IMPLEMENT(test_ucx_memread) { |
|
150 char *buffer = malloc(16); |
|
151 memset(buffer, 56, 8); |
|
152 for (int i = 8; i < 16 ; i++) { |
|
153 buffer[i] = 40+i; |
|
154 } |
|
155 |
|
156 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
157 int r; |
|
158 |
|
159 UCX_TEST_BEGIN |
|
160 |
|
161 char rb[16]; |
|
162 memset(rb, 32, 16); |
|
163 |
|
164 ucx_memseek(m, 8, SEEK_SET); |
|
165 r = ucx_memread(rb, 1, 16, m); |
|
166 UCX_TEST_ASSERT(r == 8, "read did not stop at buffer end"); |
|
167 UCX_TEST_ASSERT(memcmp(rb, "01234567 ", 16) == 0, |
|
168 "buffer incorrect after first read"); |
|
169 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); |
|
170 |
|
171 ucx_memseek(m, 0, SEEK_SET); |
|
172 r = ucx_memread(rb+8, 1, 8, m); |
|
173 UCX_TEST_ASSERT(r == 8, "read did not read the specified amount of bytes"); |
|
174 UCX_TEST_ASSERT(memcmp(rb, "0123456788888888", 16) == 0, |
|
175 "buffer incorrect after second read"); |
|
176 |
|
177 ucx_memseek(m, 0, SEEK_SET); |
|
178 r = ucx_memread(rb, 3, 6, m); |
|
179 UCX_TEST_ASSERT(r == 15, |
|
180 "three byte read did not read the desired amount of bytes"); |
|
181 UCX_TEST_ASSERT(memcmp(rb, "8888888801234568", 16) == 0, |
|
182 "buffer incorrect after three byte read"); |
|
183 |
|
184 UCX_TEST_END |
|
185 |
|
186 ucx_memclose(m); |
|
187 free(buffer); |
|
188 } |
|
189 |
|
190 UCX_TEST_IMPLEMENT(test_ucx_memprintf) { |
|
191 char *buffer = malloc(32); |
|
192 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
193 |
|
194 UCX_TEST_BEGIN |
|
195 int r = ucx_memprintf(m, "number: %d char: %c", 15, '6'); |
|
196 UCX_TEST_ASSERT(r == 18, "incorrect number of bytes written"); |
|
197 UCX_TEST_ASSERT(ucx_memoverflow(m), "overflow shall be detected"); |
|
198 UCX_TEST_ASSERT(memcmp(buffer, "number: 15 char:", 16) == 0, |
|
199 "incorrect buffer content"); |
|
200 |
|
201 ucx_memseek(m, 0, SEEK_SET); |
|
202 ucx_memprintf(m, "a: %d - b: %d", 1, 2); |
|
203 UCX_TEST_ASSERT(!ucx_memoverflow(m), "no overflow shall be deteceted"); |
|
204 UCX_TEST_ASSERT(memcmp(buffer, "a: 1 - b: 2char:", 16), |
|
205 "incorrect modified buffer content"); |
|
206 UCX_TEST_ASSERT(ucx_memtell(m) == 11, "incorrect position"); |
|
207 |
|
208 UCX_TEST_END |
|
209 |
|
210 ucx_memclose(m); |
|
211 free(buffer); |
|
212 } |
|
213 |
|
214 UCX_TEST_IMPLEMENT(test_ucx_memscanf) { |
|
215 char *buffer = "string 3.5 1 stuff"; |
|
216 UcxMemstream *m = ucx_memopen(buffer, 16); |
|
217 |
|
218 char s[6]; |
|
219 float f; |
|
220 int d; |
|
221 UCX_TEST_BEGIN |
|
222 int r = ucx_memscanf(m, "%s %f %d", s, &f, &d); |
|
223 UCX_TEST_ASSERT(r == 3, "3 arguments shall be read"); |
|
224 UCX_TEST_ASSERT(strncmp(s, "string", 6) == 0, "incorrect string"); |
|
225 UCX_TEST_ASSERT(f == 3.5, "incorrect float"); |
|
226 UCX_TEST_ASSERT(d == 1, "incorrect integer"); |
|
227 UCX_TEST_ASSERT(ucx_memtell(m) == 12, "incorrect position"); |
|
228 UCX_TEST_END |
|
229 |
|
230 ucx_memclose(m); |
|
231 } |