test/memstream_tests.c

changeset 58
733f22fca61a
parent 56
76caac0da4a0
equal deleted inserted replaced
57:e18157c52985 58:733f22fca61a
60 UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts"); 60 UCX_TEST_ASSERT(ucx_memtell(m) == 3, "pos wrong after first 3 puts");
61 ucx_memseek(m, 10, SEEK_CUR); 61 ucx_memseek(m, 10, SEEK_CUR);
62 ucx_memputc(m, 48); ucx_memputc(m, 48); ucx_memputc(m, 48); 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"); 63 UCX_TEST_ASSERT(ucx_memtell(m) == 16, "pos wrong after last 3 puts");
64 UCX_TEST_ASSERT(ucx_memeof(m), "eof not set"); 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"); 65 UCX_TEST_ASSERT(ucx_memputc(m, 48) == EOF, "put shall return EOF on memof");
67 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0, 66 UCX_TEST_ASSERT(memcmp(buffer, "000 000", 16) == 0,
68 "buffer contains incorrect content"); 67 "buffer contains incorrect content");
69 68
70 UCX_TEST_END 69 UCX_TEST_END
119 r = ucx_memwrite(teststring, 1, 20, m); 118 r = ucx_memwrite(teststring, 1, 20, m);
120 UCX_TEST_ASSERT(r == 16, "string not correctly trimed"); 119 UCX_TEST_ASSERT(r == 16, "string not correctly trimed");
121 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0, 120 UCX_TEST_ASSERT(memcmp(buffer, teststring, 16) == 0,
122 "buffer data incorrect"); 121 "buffer data incorrect");
123 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set"); 122 UCX_TEST_ASSERT(ucx_memeof(m), "eof shall be set");
124 UCX_TEST_ASSERT(!ucx_memoverflow(m), "no overflow shall be caused");
125 123
126 ucx_memseek(m, 8, SEEK_SET); 124 ucx_memseek(m, 8, SEEK_SET);
127 r = ucx_memwrite("not", 1, 3, m); 125 r = ucx_memwrite("not", 1, 3, m);
128 UCX_TEST_ASSERT(r == 3, "three bytes should be replace"); 126 UCX_TEST_ASSERT(r == 3, "three bytes should be replace");
129 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0, 127 UCX_TEST_ASSERT(memcmp(buffer, "this is not too much", 16) == 0,
184 UCX_TEST_END 182 UCX_TEST_END
185 183
186 ucx_memclose(m); 184 ucx_memclose(m);
187 free(buffer); 185 free(buffer);
188 } 186 }
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 }

mercurial