librom.a - Add some more 'clean' string functions
[AROS.git] / test / dos / readitem.c
blobce480cdae5be929a5950651d0f160570963e6698
1 /*
2 * Copyright (C) 2011, The AROS Development Team. All rights reserved.
3 * Author: Jason S. McMullan <jason.mcmullan@gmail.com>
5 * Licensed under the AROS PUBLIC LICENSE (APL) Version 1.1
6 */
7 #include <string.h>
9 #include <proto/exec.h>
10 #include <proto/dos.h>
12 #include <dos/stdio.h>
14 #define IOERR_UNCHANGED -1
16 static int ritest_buff(ULONG test, CONST_STRPTR in, LONG ret, LONG ioerr, CONST_STRPTR out, LONG cur)
18 static char buff[256];
19 struct CSource cs;
20 int failed = 0;
21 LONG err, reterr;
23 cs.CS_Buffer = (APTR)in;
24 cs.CS_Length = strlen(in);
25 cs.CS_CurChr = 0;
27 SetIoErr(IOERR_UNCHANGED);
28 err = ReadItem(buff, sizeof(buff), &cs);
29 reterr = IoErr();
30 failed |= (err != ret) ? 1 : 0;
31 failed |= (strcmp(buff, out) != 0) ? 1 : 0;
32 failed |= (cur != cs.CS_CurChr) ? 1 : 0;
33 failed |= (reterr != ioerr) ? 1 : 0;
35 if (failed) {
36 Printf("%ld: buff: in: (%s)\n", test, in);
37 Printf("%ld: buff: expected: ret %ld (%ld), buf (%s), cur %ld\n", test, ret, ioerr, out, cur);
38 Printf("%ld: buff: returned: ret %ld (%ld), buf (%s), cur %ld\n", test, err, reterr, buff, cs.CS_CurChr);
41 return failed;
44 static int ritest_file(ULONG test, CONST_STRPTR in, LONG ret, LONG ioerr, CONST_STRPTR out, LONG cur)
46 static char buff[256];
47 int failed = 0;
48 LONG err, reterr;
49 BPTR io, oldin;
51 io = Open("RAM:readitem.tmp", MODE_NEWFILE);
52 Write(io, in, strlen(in));
53 Close(io);
54 io = Open("RAM:readitem.tmp", MODE_OLDFILE);
56 oldin = Input();
57 SelectInput(io);
58 SetIoErr(IOERR_UNCHANGED);
59 err = ReadItem(buff, sizeof(buff), NULL);
60 reterr = IoErr();
61 SelectInput(oldin);
63 failed |= (err != ret) ? 1 : 0;
64 failed |= (strcmp(buff, out) != 0) ? 1 : 0;
65 failed |= (reterr != ioerr) ? 1 : 0;
67 if (failed) {
68 Printf("%ld: file: in: (%s)\n", test, in);
69 Printf("%ld: file: expected: ret %ld (%ld), buf (%s)\n", test, ret, ioerr, out);
70 Printf("%ld: file: returned: ret %ld (%ld), buf (%s)\n", test, err, reterr, buff);
73 Close(io);
74 DeleteFile("RAM:readitem.tmp");
75 return failed;
78 #define RITEST(in, ret, out, cur) \
79 do { \
80 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
81 tests++; \
82 failed += ritest_file(__LINE__, in, ret, RETURN_OK, out, cur); \
83 tests++; \
84 } while (0)
86 #define RITERR(in, ret, out, cur, ioerr) \
87 do { \
88 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
89 tests++; \
90 failed += ritest_file(__LINE__, in, ret, ioerr, out, cur); \
91 tests++; \
92 } while (0)
94 int main(int argc, char **argv)
96 int failed = 0, tests = 0;
97 LONG ret;
99 /* The following behaviour is versus AOS 3.1,
100 * which should be used as the reference for
101 * the AROS implementation
103 RITEST(";",ITEM_NOTHING,"",0);
104 RITEST(";word",ITEM_NOTHING,"",0);
105 RITEST(" ;word",ITEM_NOTHING,"",1);
106 RITEST("\t;word",ITEM_NOTHING,"",1);
107 RITEST("word",ITEM_UNQUOTED,"word",3);
108 RITEST("word\n",ITEM_UNQUOTED,"word",4);
109 RITEST("word=thing\n",ITEM_UNQUOTED,"word",5);
110 RITEST("word crazy\n",ITEM_UNQUOTED,"word",5);
111 RITEST("word\tcrazy\n",ITEM_UNQUOTED,"word",5);
112 RITEST("\"word\"=thing\n",ITEM_QUOTED,"word",6);
113 RITEST("\"word\" crazy\n",ITEM_QUOTED,"word",6);
114 RITEST("\"word\"\tcrazy\n",ITEM_QUOTED,"word",6);
115 RITERR("\"word",ITEM_ERROR,"word",4,0);
116 RITERR("\"word\n\"\n",ITEM_ERROR,"word",5,0);
117 RITEST("\"word=\"\n",ITEM_QUOTED,"word=",7);
118 RITEST("\"word \"\n",ITEM_QUOTED,"word ",7);
119 RITERR("\"word*",ITEM_ERROR,"word",5,0);
120 RITERR("\"word*\n",ITEM_ERROR,"word",6,0);
121 RITERR("\"word**",ITEM_ERROR,"word*",6,0);
122 RITERR("\"word*e",ITEM_ERROR,"word\e",6,0);
123 RITERR("\"word*E",ITEM_ERROR,"word\e",6,0);
124 RITERR("\"word*n",ITEM_ERROR,"word\n",6,0);
125 RITERR("\"word*N",ITEM_ERROR,"word\n",6,0);
126 RITERR("\"word**\n",ITEM_ERROR,"word*",7,0);
127 RITERR("\"word*e\n",ITEM_ERROR,"word\e",7,0);
128 RITERR("\"word*E\n",ITEM_ERROR,"word\e",7,0);
129 RITERR("\"word*n\n",ITEM_ERROR,"word\n",7,0);
130 RITERR("\"word*N\n",ITEM_ERROR,"word\n",7,0);
131 RITEST("\"word**\"",ITEM_QUOTED,"word*",8);
132 RITEST("\"word*e\"",ITEM_QUOTED,"word\e",8);
133 RITEST("\"word*E\"",ITEM_QUOTED,"word\e",8);
134 RITEST("\"word*n\"",ITEM_QUOTED,"word\n",8);
135 RITEST("\"word*N\"",ITEM_QUOTED,"word\n",8);
136 RITEST(" word\n",ITEM_UNQUOTED,"word",5);
137 RITEST("\nword\n",ITEM_NOTHING,"",0);
138 RITEST("\"word\"\n",ITEM_QUOTED,"word",6);
139 RITEST("\n",ITEM_NOTHING,"",0);
140 RITEST("",ITEM_NOTHING,"",0);
141 RITEST("word\"hello \"world",ITEM_UNQUOTED,"word\"hello",11);
142 RITEST("",ITEM_NOTHING,"",0);
144 /* Edge1: Buffer is NULL */
146 int lfailed = 0;
147 LONG ioerr;
148 SetIoErr(IOERR_UNCHANGED);
149 ret = ReadItem(NULL, 128, NULL);
150 ioerr = IoErr();
151 tests++;
152 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
153 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
154 if (lfailed) {
155 Printf("Edge1: expected %ld (%ld)\n", 0, IOERR_UNCHANGED);
156 Printf("Edge1: returned %ld (%ld)\n", ret, ioerr);
158 failed += lfailed;
161 /* Edge2: Buffer size is 0 */
163 int lfailed = 0;
164 LONG ioerr;
165 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
166 SetIoErr(IOERR_UNCHANGED);
167 ret = ReadItem(buff, 0, NULL);
168 ioerr = IoErr();
169 tests++;
170 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
171 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
172 lfailed |= (buff[0] != 0x0) ? 1 : 0;
173 if (lfailed) {
174 Printf("Edge2: expected %ld (%ld), buff[0] = 0x00\n", 0, IOERR_UNCHANGED);
175 Printf("Edge2: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
177 failed += lfailed;
180 /* Edge3: Input is NIL: */
182 int lfailed = 0;
183 LONG ioerr;
184 BPTR io, oldio;
186 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
188 io = Open("NIL:", MODE_OLDFILE);
189 oldio = Input();
190 SelectInput(io);
191 SetIoErr(IOERR_UNCHANGED);
192 ret = ReadItem(buff, sizeof(buff), NULL);
193 ioerr = IoErr();
194 SelectInput(oldio);
195 Close(io);
196 tests++;
197 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
198 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
199 lfailed |= (buff[0] != 0x0) ? 1 : 0;
200 if (lfailed) {
201 Printf("Edge3: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
202 Printf("Edge3: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
204 failed += lfailed;
207 /* Edge4: Input is BNULL */
209 int lfailed = 0;
210 LONG ioerr;
211 BPTR oldio;
212 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
214 oldio = Input();
215 SelectInput(BNULL);
216 SetIoErr(IOERR_UNCHANGED);
217 ret = ReadItem(buff, sizeof(buff), NULL);
218 ioerr = IoErr();
219 SelectInput(oldio);
220 tests++;
221 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
222 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
223 lfailed |= (buff[0] != 0x0) ? 1 : 0;
224 if (lfailed) {
225 Printf("Edge4: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
226 Printf("Edge4: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
228 failed += lfailed;
231 /* Edge5: Buffer length < 0 */
233 int lfailed = 0;
234 LONG ioerr;
235 BPTR oldio;
236 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
238 oldio = Input();
239 SelectInput(BNULL);
240 SetIoErr(IOERR_UNCHANGED);
241 ret = ReadItem(buff, -1, NULL);
242 ioerr = IoErr();
243 SelectInput(oldio);
244 tests++;
245 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
246 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
247 lfailed |= (buff[0] != 0x0) ? 1 : 0;
248 if (lfailed) {
249 Printf("Edge5: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
250 Printf("Edge5: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
252 failed += lfailed;
255 /* Edge6: Buffer size is equal to input length */
257 int lfailed = 0;
258 LONG ioerr;
259 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
260 BYTE input[] = { '1', '2', '3', '4' };
261 struct CSource cs = {
262 .CS_Buffer = input,
263 .CS_Length = 4,
264 .CS_CurChr = 0,
266 SetIoErr(IOERR_UNCHANGED);
267 ret = ReadItem(buff, 4, &cs);
268 ioerr = IoErr();
269 tests++;
270 lfailed |= (ret != ITEM_ERROR) ? 1 : 0;
271 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
272 lfailed |= (buff[0] != '1') ? 1 : 0;
273 lfailed |= (buff[1] != '2') ? 1 : 0;
274 lfailed |= (buff[2] != '3') ? 1 : 0;
275 lfailed |= (buff[3] != 0) ? 1 : 0;
276 lfailed |= (buff[4] != 0x55) ? 1 : 0;
277 if (lfailed) {
278 Printf("Edge6: expected %ld (%ld), buff[] = \"123\",0x0,0x55\n", ITEM_ERROR, IOERR_UNCHANGED);
279 Printf("Edge6: returned %ld (%ld), buff[] = \"%lc%lc%lc\",0x%lx,0x%lx\n", ret, ioerr, (LONG)buff[0], (LONG)buff[1], (LONG)buff[2], (LONG)buff[3], (LONG)buff[4]);
281 failed += lfailed;
284 /* Edge7: Buffer size is one less than input length */
286 int lfailed = 0;
287 LONG ioerr;
288 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
289 BYTE input[] = { '1', '2', '3', '4' };
290 struct CSource cs = {
291 .CS_Buffer = input,
292 .CS_Length = 4,
293 .CS_CurChr = 0
295 SetIoErr(IOERR_UNCHANGED);
296 ret = ReadItem(buff, 3, &cs);
297 ioerr = IoErr();
298 tests++;
299 lfailed |= (ret != ITEM_ERROR) ? 1 : 0;
300 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
301 lfailed |= (buff[0] != '1') ? 1 : 0;
302 lfailed |= (buff[1] != '2') ? 1 : 0;
303 lfailed |= (buff[2] != 0) ? 1 : 0;
304 lfailed |= (buff[3] != 0x44) ? 1 : 0;
305 lfailed |= (buff[4] != 0x55) ? 1 : 0;
306 if (lfailed) {
307 Printf("Edge7: expected %ld (%ld), buff[] = \"12\",0x00,0x44,0x55\n", ITEM_ERROR, IOERR_UNCHANGED);
308 Printf("Edge7: returned %ld (%ld), buff[] = \"%lc%lc\",0x%lx,0x%lx,0x%lx\n", ret, ioerr, (LONG)buff[0], (LONG)buff[1], (LONG)buff[2], (LONG)buff[3], (LONG)buff[4]);
310 failed += lfailed;
313 /* Edge8: Buffer size is one more than input length */
315 int lfailed = 0;
316 LONG ioerr;
317 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
318 BYTE input[] = { '1', '2', '3', '4' };
319 struct CSource cs = {
320 .CS_Buffer = input,
321 .CS_Length = 4,
322 .CS_CurChr = 0,
324 SetIoErr(IOERR_UNCHANGED);
325 ret = ReadItem(buff, 5, &cs);
326 ioerr = IoErr();
327 tests++;
328 lfailed |= (ret != ITEM_UNQUOTED) ? 1 : 0;
329 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
330 lfailed |= (buff[0] != '1') ? 1 : 0;
331 lfailed |= (buff[1] != '2') ? 1 : 0;
332 lfailed |= (buff[2] != '3') ? 1 : 0;
333 lfailed |= (buff[3] != '4') ? 1 : 0;
334 lfailed |= (buff[4] != 0) ? 1 : 0;
335 if (lfailed) {
336 Printf("Edge8: expected %ld (%ld), buff[] = \"1234\",0x0\n", 0, IOERR_UNCHANGED);
337 Printf("Edge8: returned %ld (%ld), buff[] = \"%lc%lc%lc%lc\",0x%lx\n", ret, ioerr, (LONG)buff[0], (LONG)buff[1], (LONG)buff[2], (LONG)buff[3], (LONG)buff[4]);
339 failed += lfailed;
342 if (failed == 0)
343 Printf("All %ld tests passed\n", tests);
344 else
345 Printf("FAILED: %ld out of %ld tests.\n", failed, tests);
347 return (failed) ? RETURN_WARN : RETURN_OK;