tools/adflib: build only host variant which is used by Sam440 target
[AROS.git] / test / dos / readitem.c
blob77bb7e734c0394fb2623af7008c71ef1ea98cc17
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 #ifndef BNULL
17 #define BNULL 0
18 #endif
20 static int ritest_buff(ULONG test, CONST_STRPTR in, LONG ret, LONG ioerr, CONST_STRPTR out, LONG cur)
22 static char buff[256];
23 struct CSource cs;
24 int failed = 0;
25 LONG err, reterr;
27 cs.CS_Buffer = (APTR)in;
28 cs.CS_Length = strlen(in);
29 cs.CS_CurChr = 0;
31 SetIoErr(IOERR_UNCHANGED);
32 err = ReadItem(buff, sizeof(buff), &cs);
33 reterr = IoErr();
34 failed |= (err != ret) ? 1 : 0;
35 failed |= (strcmp(buff, out) != 0) ? 1 : 0;
36 failed |= (cur != cs.CS_CurChr) ? 1 : 0;
37 failed |= (reterr != ioerr) ? 1 : 0;
39 if (failed) {
40 Printf("%ld: buff: in: (%s)\n", test, in);
41 Printf("%ld: buff: expected: ret %ld (%ld), buf (%s), cur %ld\n", test, ret, ioerr, out, cur);
42 Printf("%ld: buff: returned: ret %ld (%ld), buf (%s), cur %ld\n", test, err, reterr, buff, cs.CS_CurChr);
45 return failed;
48 static int ritest_file(ULONG test, CONST_STRPTR in, LONG ret, LONG ioerr, CONST_STRPTR out, LONG cur)
50 static char buff[256];
51 int failed = 0;
52 LONG err, reterr;
53 BPTR io, oldin;
55 io = Open("RAM:readitem.tmp", MODE_NEWFILE);
56 Write(io, in, strlen(in));
57 Close(io);
58 io = Open("RAM:readitem.tmp", MODE_OLDFILE);
60 oldin = Input();
61 SelectInput(io);
62 SetIoErr(IOERR_UNCHANGED);
63 err = ReadItem(buff, sizeof(buff), NULL);
64 reterr = IoErr();
65 SelectInput(oldin);
67 failed |= (err != ret) ? 1 : 0;
68 failed |= (strcmp(buff, out) != 0) ? 1 : 0;
69 failed |= (reterr != ioerr) ? 1 : 0;
71 if (failed) {
72 Printf("%ld: file: in: (%s)\n", test, in);
73 Printf("%ld: file: expected: ret %ld (%ld), buf (%s)\n", test, ret, ioerr, out);
74 Printf("%ld: file: returned: ret %ld (%ld), buf (%s)\n", test, err, reterr, buff);
77 Close(io);
78 DeleteFile("RAM:readitem.tmp");
79 return failed;
82 #define RITEST(in, ret, out, cur) \
83 do { \
84 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
85 tests++; \
86 failed += ritest_file(__LINE__, in, ret, RETURN_OK, out, cur); \
87 tests++; \
88 } while (0)
90 #define RITERR(in, ret, out, cur, ioerr) \
91 do { \
92 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
93 tests++; \
94 failed += ritest_file(__LINE__, in, ret, ioerr, out, cur); \
95 tests++; \
96 } while (0)
98 int main(int argc, char **argv)
100 int failed = 0, tests = 0;
101 LONG ret;
103 /* The following behaviour is versus AOS 3.1,
104 * which should be used as the reference for
105 * the AROS implementation
107 RITEST(";",ITEM_NOTHING,"",0);
108 RITEST(";word",ITEM_NOTHING,"",0);
109 RITEST(" ;word",ITEM_NOTHING,"",1);
110 RITEST("\t;word",ITEM_NOTHING,"",1);
111 RITEST("word",ITEM_UNQUOTED,"word",3);
112 RITEST("word\n",ITEM_UNQUOTED,"word",4);
113 RITEST("word=thing\n",ITEM_UNQUOTED,"word",5);
114 RITEST("word crazy\n",ITEM_UNQUOTED,"word",5);
115 RITEST("word\tcrazy\n",ITEM_UNQUOTED,"word",5);
116 RITEST("\"word\"",ITEM_QUOTED,"word",6);
117 RITEST("\"word word2\"",ITEM_QUOTED,"word word2",12);
118 RITEST("\"word\"=thing\n",ITEM_QUOTED,"word",6);
119 RITEST("\"word\" crazy\n",ITEM_QUOTED,"word",6);
120 RITEST("\"word\"\tcrazy\n",ITEM_QUOTED,"word",6);
121 RITERR("\"word",ITEM_ERROR,"word",4,0);
122 RITERR("\"word\n\"\n",ITEM_ERROR,"word",5,0);
123 RITEST("\"word=\"\n",ITEM_QUOTED,"word=",7);
124 RITEST("\"word \"\n",ITEM_QUOTED,"word ",7);
125 RITERR("\"word*",ITEM_ERROR,"word",5,0);
126 RITERR("\"word*\n",ITEM_ERROR,"word",6,0);
127 RITERR("\"word**",ITEM_ERROR,"word*",6,0);
128 RITERR("\"word*e",ITEM_ERROR,"word\e",6,0);
129 RITERR("\"word*E",ITEM_ERROR,"word\e",6,0);
130 RITERR("\"word*n",ITEM_ERROR,"word\n",6,0);
131 RITERR("\"word*N",ITEM_ERROR,"word\n",6,0);
132 RITERR("\"word**\n",ITEM_ERROR,"word*",7,0);
133 RITERR("\"word*e\n",ITEM_ERROR,"word\e",7,0);
134 RITERR("\"word*E\n",ITEM_ERROR,"word\e",7,0);
135 RITERR("\"word*n\n",ITEM_ERROR,"word\n",7,0);
136 RITERR("\"word*N\n",ITEM_ERROR,"word\n",7,0);
137 RITEST("\"word**\"",ITEM_QUOTED,"word*",8);
138 RITEST("\"word*e\"",ITEM_QUOTED,"word\e",8);
139 RITEST("\"word*E\"",ITEM_QUOTED,"word\e",8);
140 RITEST("\"word*n\"",ITEM_QUOTED,"word\n",8);
141 RITEST("\"word*N\"",ITEM_QUOTED,"word\n",8);
142 RITEST(" word\n",ITEM_UNQUOTED,"word",5);
143 RITEST("\nword\n",ITEM_NOTHING,"",0);
144 RITEST("\"word\"\n",ITEM_QUOTED,"word",6);
145 RITEST("\n",ITEM_NOTHING,"",0);
146 RITEST("",ITEM_NOTHING,"",0);
147 RITEST("word\"hello \"world",ITEM_UNQUOTED,"word\"hello",11);
148 RITEST("",ITEM_NOTHING,"",0);
150 /* Edge1: Buffer is NULL */
152 int lfailed = 0;
153 LONG ioerr;
154 SetIoErr(IOERR_UNCHANGED);
155 ret = ReadItem(NULL, 128, NULL);
156 ioerr = IoErr();
157 tests++;
158 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
159 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
160 if (lfailed) {
161 Printf("Edge1: expected %ld (%ld)\n", 0, IOERR_UNCHANGED);
162 Printf("Edge1: returned %ld (%ld)\n", ret, ioerr);
164 failed += lfailed;
167 /* Edge2: Buffer size is 0 */
169 int lfailed = 0;
170 LONG ioerr;
171 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
172 SetIoErr(IOERR_UNCHANGED);
173 ret = ReadItem(buff, 0, NULL);
174 ioerr = IoErr();
175 tests++;
176 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
177 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
178 lfailed |= (buff[0] != 0x0) ? 1 : 0;
179 if (lfailed) {
180 Printf("Edge2: expected %ld (%ld), buff[0] = 0x00\n", 0, IOERR_UNCHANGED);
181 Printf("Edge2: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
183 failed += lfailed;
186 /* Edge3: Input is NIL: */
188 int lfailed = 0;
189 LONG ioerr;
190 BPTR io, oldio;
192 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
194 io = Open("NIL:", MODE_OLDFILE);
195 oldio = Input();
196 SelectInput(io);
197 SetIoErr(IOERR_UNCHANGED);
198 ret = ReadItem(buff, sizeof(buff), NULL);
199 ioerr = IoErr();
200 SelectInput(oldio);
201 Close(io);
202 tests++;
203 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
204 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
205 lfailed |= (buff[0] != 0x0) ? 1 : 0;
206 if (lfailed) {
207 Printf("Edge3: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
208 Printf("Edge3: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
210 failed += lfailed;
213 /* Edge4: Input is BNULL */
215 int lfailed = 0;
216 LONG ioerr;
217 BPTR oldio;
218 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
220 oldio = Input();
221 SelectInput(BNULL);
222 SetIoErr(IOERR_UNCHANGED);
223 ret = ReadItem(buff, sizeof(buff), NULL);
224 ioerr = IoErr();
225 SelectInput(oldio);
226 tests++;
227 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
228 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
229 lfailed |= (buff[0] != 0x0) ? 1 : 0;
230 if (lfailed) {
231 Printf("Edge4: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
232 Printf("Edge4: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
234 failed += lfailed;
237 /* Edge5: Buffer length < 0 */
239 int lfailed = 0;
240 LONG ioerr;
241 BPTR oldio;
242 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44 };
244 oldio = Input();
245 SelectInput(BNULL);
246 SetIoErr(IOERR_UNCHANGED);
247 ret = ReadItem(buff, -1, NULL);
248 ioerr = IoErr();
249 SelectInput(oldio);
250 tests++;
251 lfailed |= (ret != ITEM_NOTHING) ? 1 : 0;
252 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
253 lfailed |= (buff[0] != 0x0) ? 1 : 0;
254 if (lfailed) {
255 Printf("Edge5: expected %ld (%ld), buff[0] = 0x00\n", ITEM_NOTHING, IOERR_UNCHANGED);
256 Printf("Edge5: returned %ld (%ld), buff[0] = 0x%02lx\n", ret, ioerr, (LONG)buff[0]);
258 failed += lfailed;
261 /* Edge6: Buffer size is equal to input length */
263 int lfailed = 0;
264 LONG ioerr;
265 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
266 BYTE input[] = { '1', '2', '3', '4' };
267 struct CSource cs = {
268 .CS_Buffer = input,
269 .CS_Length = 4,
270 .CS_CurChr = 0,
272 SetIoErr(IOERR_UNCHANGED);
273 ret = ReadItem(buff, 4, &cs);
274 ioerr = IoErr();
275 tests++;
276 lfailed |= (ret != ITEM_ERROR) ? 1 : 0;
277 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
278 lfailed |= (buff[0] != '1') ? 1 : 0;
279 lfailed |= (buff[1] != '2') ? 1 : 0;
280 lfailed |= (buff[2] != '3') ? 1 : 0;
281 lfailed |= (buff[3] != 0) ? 1 : 0;
282 lfailed |= (buff[4] != 0x55) ? 1 : 0;
283 if (lfailed) {
284 Printf("Edge6: expected %ld (%ld), buff[] = \"123\",0x0,0x55\n", ITEM_ERROR, IOERR_UNCHANGED);
285 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]);
287 failed += lfailed;
290 /* Edge7: Buffer size is one less than input length */
292 int lfailed = 0;
293 LONG ioerr;
294 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
295 BYTE input[] = { '1', '2', '3', '4' };
296 struct CSource cs = {
297 .CS_Buffer = input,
298 .CS_Length = 4,
299 .CS_CurChr = 0
301 SetIoErr(IOERR_UNCHANGED);
302 ret = ReadItem(buff, 3, &cs);
303 ioerr = IoErr();
304 tests++;
305 lfailed |= (ret != ITEM_ERROR) ? 1 : 0;
306 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
307 lfailed |= (buff[0] != '1') ? 1 : 0;
308 lfailed |= (buff[1] != '2') ? 1 : 0;
309 lfailed |= (buff[2] != 0) ? 1 : 0;
310 lfailed |= (buff[3] != 0x44) ? 1 : 0;
311 lfailed |= (buff[4] != 0x55) ? 1 : 0;
312 if (lfailed) {
313 Printf("Edge7: expected %ld (%ld), buff[] = \"12\",0x00,0x44,0x55\n", ITEM_ERROR, IOERR_UNCHANGED);
314 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]);
316 failed += lfailed;
319 /* Edge8: Buffer size is one more than input length */
321 int lfailed = 0;
322 LONG ioerr;
323 BYTE buff[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
324 BYTE input[] = { '1', '2', '3', '4' };
325 struct CSource cs = {
326 .CS_Buffer = input,
327 .CS_Length = 4,
328 .CS_CurChr = 0,
330 SetIoErr(IOERR_UNCHANGED);
331 ret = ReadItem(buff, 5, &cs);
332 ioerr = IoErr();
333 tests++;
334 lfailed |= (ret != ITEM_UNQUOTED) ? 1 : 0;
335 lfailed |= (ioerr != IOERR_UNCHANGED) ? 1 : 0;
336 lfailed |= (buff[0] != '1') ? 1 : 0;
337 lfailed |= (buff[1] != '2') ? 1 : 0;
338 lfailed |= (buff[2] != '3') ? 1 : 0;
339 lfailed |= (buff[3] != '4') ? 1 : 0;
340 lfailed |= (buff[4] != 0) ? 1 : 0;
341 if (lfailed) {
342 Printf("Edge8: expected %ld (%ld), buff[] = \"1234\",0x0\n", 0, IOERR_UNCHANGED);
343 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]);
345 failed += lfailed;
348 if (failed == 0)
349 Printf("All %ld tests passed\n", tests);
350 else
351 Printf("FAILED: %ld out of %ld tests.\n", failed, tests);
353 return (failed) ? RETURN_WARN : RETURN_OK;