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
20 static int ritest_buff(ULONG test
, CONST_STRPTR in
, LONG ret
, LONG ioerr
, CONST_STRPTR out
, LONG cur
)
22 static char buff
[256];
27 cs
.CS_Buffer
= (APTR
)in
;
28 cs
.CS_Length
= strlen(in
);
31 SetIoErr(IOERR_UNCHANGED
);
32 err
= ReadItem(buff
, sizeof(buff
), &cs
);
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;
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
);
48 static int ritest_file(ULONG test
, CONST_STRPTR in
, LONG ret
, LONG ioerr
, CONST_STRPTR out
, LONG cur
)
50 static char buff
[256];
55 io
= Open("RAM:readitem.tmp", MODE_NEWFILE
);
56 Write(io
, in
, strlen(in
));
58 io
= Open("RAM:readitem.tmp", MODE_OLDFILE
);
62 SetIoErr(IOERR_UNCHANGED
);
63 err
= ReadItem(buff
, sizeof(buff
), NULL
);
67 failed
|= (err
!= ret
) ? 1 : 0;
68 failed
|= (strcmp(buff
, out
) != 0) ? 1 : 0;
69 failed
|= (reterr
!= ioerr
) ? 1 : 0;
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
);
78 DeleteFile("RAM:readitem.tmp");
82 #define RITEST(in, ret, out, cur) \
84 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
86 failed += ritest_file(__LINE__, in, ret, RETURN_OK, out, cur); \
90 #define RITERR(in, ret, out, cur, ioerr) \
92 failed += ritest_buff(__LINE__, in, ret, IOERR_UNCHANGED, out, cur); \
94 failed += ritest_file(__LINE__, in, ret, ioerr, out, cur); \
98 int main(int argc
, char **argv
)
100 int failed
= 0, tests
= 0;
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 */
154 SetIoErr(IOERR_UNCHANGED
);
155 ret
= ReadItem(NULL
, 128, NULL
);
158 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
159 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
161 Printf("Edge1: expected %ld (%ld)\n", 0, IOERR_UNCHANGED
);
162 Printf("Edge1: returned %ld (%ld)\n", ret
, ioerr
);
167 /* Edge2: Buffer size is 0 */
171 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
172 SetIoErr(IOERR_UNCHANGED
);
173 ret
= ReadItem(buff
, 0, NULL
);
176 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
177 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
178 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
186 /* Edge3: Input is NIL: */
192 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
194 io
= Open("NIL:", MODE_OLDFILE
);
197 SetIoErr(IOERR_UNCHANGED
);
198 ret
= ReadItem(buff
, sizeof(buff
), NULL
);
203 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
204 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
205 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
213 /* Edge4: Input is BNULL */
218 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
222 SetIoErr(IOERR_UNCHANGED
);
223 ret
= ReadItem(buff
, sizeof(buff
), NULL
);
227 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
228 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
229 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
237 /* Edge5: Buffer length < 0 */
242 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44 };
246 SetIoErr(IOERR_UNCHANGED
);
247 ret
= ReadItem(buff
, -1, NULL
);
251 lfailed
|= (ret
!= ITEM_NOTHING
) ? 1 : 0;
252 lfailed
|= (ioerr
!= IOERR_UNCHANGED
) ? 1 : 0;
253 lfailed
|= (buff
[0] != 0x0) ? 1 : 0;
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]);
261 /* Edge6: Buffer size is equal to input length */
265 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
266 BYTE input
[] = { '1', '2', '3', '4' };
267 struct CSource cs
= {
272 SetIoErr(IOERR_UNCHANGED
);
273 ret
= ReadItem(buff
, 4, &cs
);
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;
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]);
290 /* Edge7: Buffer size is one less than input length */
294 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
295 BYTE input
[] = { '1', '2', '3', '4' };
296 struct CSource cs
= {
301 SetIoErr(IOERR_UNCHANGED
);
302 ret
= ReadItem(buff
, 3, &cs
);
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;
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]);
319 /* Edge8: Buffer size is one more than input length */
323 BYTE buff
[] = { 0x11, 0x22, 0x33, 0x44, 0x55 };
324 BYTE input
[] = { '1', '2', '3', '4' };
325 struct CSource cs
= {
330 SetIoErr(IOERR_UNCHANGED
);
331 ret
= ReadItem(buff
, 5, &cs
);
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;
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]);
349 Printf("All %ld tests passed\n", tests
);
351 Printf("FAILED: %ld out of %ld tests.\n", failed
, tests
);
353 return (failed
) ? RETURN_WARN
: RETURN_OK
;