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
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];
23 cs
.CS_Buffer
= (APTR
)in
;
24 cs
.CS_Length
= strlen(in
);
27 SetIoErr(IOERR_UNCHANGED
);
28 err
= ReadItem(buff
, sizeof(buff
), &cs
);
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;
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
);
44 static int ritest_file(ULONG test
, CONST_STRPTR in
, LONG ret
, LONG ioerr
, CONST_STRPTR out
, LONG cur
)
46 static char buff
[256];
51 io
= Open("RAM:readitem.tmp", MODE_NEWFILE
);
52 Write(io
, in
, strlen(in
));
54 io
= Open("RAM:readitem.tmp", MODE_OLDFILE
);
58 SetIoErr(IOERR_UNCHANGED
);
59 err
= ReadItem(buff
, sizeof(buff
), NULL
);
63 failed
|= (err
!= ret
) ? 1 : 0;
64 failed
|= (strcmp(buff
, out
) != 0) ? 1 : 0;
65 failed
|= (reterr
!= ioerr
) ? 1 : 0;
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
);
74 DeleteFile("RAM:readitem.tmp");
78 #define RITEST(in, ret, out, cur) \
80 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
82 failed += ritest_file(__LINE__, in, ret, RETURN_OK, out, cur); \
86 #define RITERR(in, ret, out, cur, ioerr) \
88 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
90 failed += ritest_file(__LINE__, in, ret, ioerr, out, cur); \
94 int main(int argc
, char **argv
)
96 int failed
= 0, tests
= 0;
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 */
148 SetIoErr(IOERR_UNCHANGED
);
149 ret
= ReadItem(NULL
, 128, NULL
);
152 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
153 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
155 Printf("Edge1: expected %ld (%ld)\n", 0, IOERR_UNCHANGED
);
156 Printf("Edge1: returned %ld (%ld)\n", ret
, ioerr
);
161 /* Edge2: Buffer size is 0 */
165 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
166 SetIoErr(IOERR_UNCHANGED
);
167 ret
= ReadItem(buff
, 0, NULL
);
170 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
171 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
172 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
180 /* Edge3: Input is NIL: */
186 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
188 io
= Open("NIL:", MODE_OLDFILE
);
191 SetIoErr(IOERR_UNCHANGED
);
192 ret
= ReadItem(buff
, sizeof(buff
), NULL
);
197 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
198 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
199 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
207 /* Edge4: Input is BNULL */
212 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
216 SetIoErr(IOERR_UNCHANGED
);
217 ret
= ReadItem(buff
, sizeof(buff
), NULL
);
221 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
222 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
223 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
231 /* Edge5: Buffer length < 0 */
236 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
240 SetIoErr(IOERR_UNCHANGED
);
241 ret
= ReadItem(buff
, -1, NULL
);
245 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
246 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
247 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
255 /* Edge6: Buffer size is equal to input length */
259 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
260 BYTE input
[] = { '1', '2', '3', '4' };
261 struct CSource cs
= {
266 SetIoErr(IOERR_UNCHANGED
);
267 ret
= ReadItem(buff
, 4, &cs
);
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;
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]);
284 /* Edge7: Buffer size is one less than input length */
288 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
289 BYTE input
[] = { '1', '2', '3', '4' };
290 struct CSource cs
= {
295 SetIoErr(IOERR_UNCHANGED
);
296 ret
= ReadItem(buff
, 3, &cs
);
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;
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]);
313 /* Edge8: Buffer size is one more than input length */
317 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
318 BYTE input
[] = { '1', '2', '3', '4' };
319 struct CSource cs
= {
324 SetIoErr(IOERR_UNCHANGED
);
325 ret
= ReadItem(buff
, 5, &cs
);
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;
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]);
343 Printf("All %ld tests passed\n", tests
);
345 Printf("FAILED: %ld out of %ld tests.\n", failed
, tests
);
347 return (failed
) ? RETURN_WARN
: RETURN_OK
;