1 USING: alien alien.c-types alien.data 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 vocabs
8 FROM: specialized-arrays.private => specialized-array-vocab ;
12 [ ] [ "append-test" temp-file dup exists? [ delete-file ] [ drop ] if ] unit-test
14 [ ] [ "append-test" temp-file ascii <file-appender> dispose ] unit-test
17 "This is a line.\rThis is another line.\r"
19 "vocab:io/test/mac-os-eol.txt" latin1
20 [ 500 read ] with-file-reader
26 "vocab:io/test/binary.txt" latin1
27 [ read1 ] with-file-reader >fixnum
33 "vocab:io/test/read-until-test.txt" ascii
34 [ " " read-until ] with-file-reader
40 "vocab:io/test/read-until-test.txt" binary
41 [ " " read-until [ ascii decode ] dip ] with-file-reader
45 "It seems Jobs has lost his grasp on reality again.\n"
46 "separator-test.txt" temp-file latin1 set-file-contents
51 { "It seems " CHAR: J }
52 { "obs has lost h" CHAR: i }
53 { "s grasp on reality again.\n" f }
57 "separator-test.txt" temp-file
59 "J" read-until 2array ,
60 "i" read-until 2array ,
61 "X" read-until 2array ,
68 10 [ 65536 read drop ] times
72 ! Writing specialized arrays to binary streams should work
74 "test.txt" temp-file binary [
75 int-array{ 1 2 3 } write
79 [ int-array{ 1 2 3 } ] [
80 "test.txt" temp-file binary [
87 BV{ 0 1 2 } "test.txt" temp-file binary set-file-contents
91 "test.txt" temp-file binary file-contents
95 STRUCT: pt { x uint } { y uint } ;
99 pt-array{ S{ pt f 1 1 } S{ pt f 2 2 } S{ pt f 3 3 } }
103 "test.txt" temp-file binary set-file-contents
107 "test.txt" temp-file binary file-contents
108 pt-array-1 >c-ptr sequence=
111 ! Slices should support >c-ptr and byte-length
114 pt-array-1 rest-slice
115 "test.txt" temp-file binary set-file-contents
119 "test.txt" temp-file binary file-contents
121 pt-array-1 rest-slice sequence=
126 pt specialized-array-vocab forget-vocab
127 ] with-compilation-unit
130 ! Writing strings to binary streams should fail
132 "test.txt" temp-file binary [
145 ! Make sure that writing to a closed stream from another thread doesn't crash
146 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
148 [ ] [ "test-quux.txt" temp-file delete-file ] unit-test
150 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
152 [ ] [ "test-quux.txt" "quux-test.txt" [ temp-file ] bi@ move-file ] unit-test
154 [ t ] [ "quux-test.txt" temp-file exists? ] unit-test
156 [ ] [ "quux-test.txt" temp-file delete-file ] unit-test
161 "seek-test1" unique-file binary
165 tell-output 5 assert=
166 0 seek-absolute seek-output
167 tell-output 0 assert=
169 tell-output 1 assert=
178 "seek-test2" unique-file binary
182 tell-output 5 assert=
183 -1 seek-relative seek-output
184 tell-output 4 assert=
186 tell-output 5 assert=
193 [ B{ 1 2 3 4 5 0 3 } ]
195 "seek-test3" unique-file binary
199 tell-output 5 assert=
200 1 seek-relative seek-output
201 tell-output 6 assert=
203 tell-output 7 assert=
212 B{ 1 2 3 4 5 } "seek-test4" unique-file binary [
217 -3 seek-end seek-input
227 B{ 1 2 3 4 5 } "seek-test5" unique-file binary [
232 3 seek-absolute seek-input
234 -2 seek-relative seek-input
243 "seek-test6" unique-file binary [
244 -10 seek-absolute seek-input
249 "resource:misc/icons/Factor_48x48.png" binary [
251 tell-input 44 assert=
252 -44 seek-relative seek-input
258 "non-string-error" unique-file ascii [
261 ] [ no-method? ] must-fail-with
264 "non-byte-array-error" unique-file binary [
267 ] [ no-method? ] must-fail-with
269 ! What happens if we close a file twice?
271 "closing-twice" unique-file ascii <file-writer>
272 [ dispose ] [ dispose ] bi