1 USING: alien alien.c-types arrays classes.struct
2 debugger.threads destructors generic.single io io.directories
3 io.encodings.8-bit.latin1 io.encodings.ascii
4 io.encodings.binary io.encodings.string io.files
5 io.files.private io.files.temp io.files.unique kernel make math
6 sequences specialized-arrays system threads tools.test ;
10 [ ] [ "append-test" temp-file dup exists? [ delete-file ] [ drop ] if ] unit-test
12 [ ] [ "append-test" temp-file ascii <file-appender> dispose ] unit-test
15 "This is a line.\rThis is another line.\r"
17 "vocab:io/test/mac-os-eol.txt" latin1
18 [ 500 read ] with-file-reader
24 "vocab:io/test/binary.txt" latin1
25 [ read1 ] with-file-reader >fixnum
31 "vocab:io/test/read-until-test.txt" ascii
32 [ " " read-until ] with-file-reader
38 "vocab:io/test/read-until-test.txt" binary
39 [ " " read-until [ ascii decode ] dip ] with-file-reader
43 "It seems Jobs has lost his grasp on reality again.\n"
44 "separator-test.txt" temp-file latin1 set-file-contents
49 { "It seems " CHAR: J }
50 { "obs has lost h" CHAR: i }
51 { "s grasp on reality again.\n" f }
55 "separator-test.txt" temp-file
57 "J" read-until 2array ,
58 "i" read-until 2array ,
59 "X" read-until 2array ,
66 10 [ 65536 read drop ] times
70 ! Writing specialized arrays to binary streams should work
72 "test.txt" temp-file binary [
73 int-array{ 1 2 3 } write
77 [ int-array{ 1 2 3 } ] [
78 "test.txt" temp-file binary [
85 BV{ 0 1 2 } "test.txt" temp-file binary set-file-contents
89 "test.txt" temp-file binary file-contents
93 STRUCT: pt { x uint } { y uint } ;
97 pt-array{ S{ pt f 1 1 } S{ pt f 2 2 } S{ pt f 3 3 } }
101 "test.txt" temp-file binary set-file-contents
105 "test.txt" temp-file binary file-contents
106 pt-array-1 >c-ptr sequence=
109 ! Slices should support >c-ptr and byte-length
112 pt-array-1 rest-slice
113 "test.txt" temp-file binary set-file-contents
117 "test.txt" temp-file binary file-contents
119 pt-array-1 rest-slice sequence=
122 ! Writing strings to binary streams should fail
124 "test.txt" temp-file binary [
137 ! Make sure that writing to a closed stream from another thread doesn't crash
138 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
140 [ ] [ "test-quux.txt" temp-file delete-file ] unit-test
142 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
144 [ ] [ "test-quux.txt" "quux-test.txt" [ temp-file ] bi@ move-file ] unit-test
146 [ t ] [ "quux-test.txt" temp-file exists? ] unit-test
148 [ ] [ "quux-test.txt" temp-file delete-file ] unit-test
153 "seek-test1" unique-file binary
156 B{ 1 2 3 4 5 } write 0 seek-absolute seek-output
166 "seek-test2" unique-file binary
169 B{ 1 2 3 4 5 } write -1 seek-relative seek-output
177 [ B{ 1 2 3 4 5 0 3 } ]
179 "seek-test3" unique-file binary
182 B{ 1 2 3 4 5 } write 1 seek-relative seek-output
192 B{ 1 2 3 4 5 } "seek-test4" unique-file binary [
196 -3 seek-end seek-input 1 read
203 B{ 1 2 3 4 5 } "seek-test5" unique-file binary [
207 3 seek-absolute seek-input
208 -2 seek-relative seek-input
215 "seek-test6" unique-file binary [
216 -10 seek-absolute seek-input
221 "non-string-error" unique-file ascii [
224 ] [ no-method? ] must-fail-with
227 "non-byte-array-error" unique-file binary [
230 ] [ no-method? ] must-fail-with
232 ! What happens if we close a file twice?
234 "closing-twice" unique-file ascii <file-writer>
235 [ dispose ] [ dispose ] bi