1 USING: alien alien.c-types alien.data arrays classes.struct
2 compiler.units continuations destructors generic.single io
3 io.directories io.encodings.8-bit.latin1 io.encodings.ascii
4 io.encodings.binary io.encodings.string io.files io.pathnames
5 io.files.private io.files.temp io.files.unique kernel make math
6 sequences specialized-arrays system threads tools.test vocabs ;
7 FROM: specialized-arrays.private => specialized-array-vocab ;
11 { } [ "append-test" temp-file dup exists? [ delete-file ] [ drop ] if ] unit-test
13 { } [ "append-test" temp-file ascii <file-appender> dispose ] unit-test
16 "This is a line.\rThis is another line.\r"
18 "vocab:io/test/mac-os-eol.txt" latin1
19 [ 500 read ] with-file-reader
25 "vocab:io/test/binary.txt" latin1
26 [ read1 ] with-file-reader >fixnum
32 "vocab:io/test/read-until-test.txt" ascii
33 [ " " read-until ] with-file-reader
39 "vocab:io/test/read-until-test.txt" binary
40 [ " " read-until [ ascii decode ] dip ] with-file-reader
44 "It seems Jobs has lost his grasp on reality again.\n"
45 "separator-test.txt" temp-file latin1 set-file-contents
50 { "It seems " CHAR: J }
51 { "obs has lost h" CHAR: i }
52 { "s grasp on reality again.\n" f }
56 "separator-test.txt" temp-file
58 "J" read-until 2array ,
59 "i" read-until 2array ,
60 "X" read-until 2array ,
67 10 [ 65536 read drop ] times
71 ! Writing specialized arrays to binary streams should work
73 "test.txt" temp-file binary [
74 int-array{ 1 2 3 } write
78 { int-array{ 1 2 3 } } [
79 "test.txt" temp-file binary [
86 BV{ 0 1 2 } "test.txt" temp-file binary set-file-contents
90 "test.txt" temp-file binary file-contents
94 STRUCT: pt { x uint } { y uint } ;
98 pt-array{ S{ pt f 1 1 } S{ pt f 2 2 } S{ pt f 3 3 } }
102 "test.txt" temp-file binary set-file-contents
106 "test.txt" temp-file binary file-contents
107 pt-array-1 >c-ptr sequence=
110 ! Slices should support >c-ptr and byte-length
113 pt-array-1 rest-slice
114 "test.txt" temp-file binary set-file-contents
118 "test.txt" temp-file binary file-contents
120 pt-array-1 rest-slice sequence=
125 pt specialized-array-vocab forget-vocab
126 ] with-compilation-unit
129 ! Writing strings to binary streams should fail
131 "test.txt" temp-file binary [
144 ! Make sure that writing to a closed stream from another thread doesn't crash
145 { } [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
147 { } [ "test-quux.txt" temp-file delete-file ] unit-test
149 { } [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
151 { } [ "test-quux.txt" "quux-test.txt" [ temp-file ] bi@ move-file ] unit-test
153 { t } [ "quux-test.txt" temp-file exists? ] unit-test
155 { } [ "quux-test.txt" temp-file delete-file ] unit-test
160 "seek-test1" unique-file binary
164 tell-output 5 assert=
165 0 seek-absolute seek-output
166 tell-output 0 assert=
168 tell-output 1 assert=
177 "seek-test2" unique-file binary
181 tell-output 5 assert=
182 -1 seek-relative seek-output
183 tell-output 4 assert=
185 tell-output 5 assert=
192 { B{ 1 2 3 4 5 0 3 } }
194 "seek-test3" unique-file binary
198 tell-output 5 assert=
199 1 seek-relative seek-output
200 tell-output 6 assert=
202 tell-output 7 assert=
211 B{ 1 2 3 4 5 } "seek-test4" unique-file binary [
216 -3 seek-end seek-input
226 B{ 1 2 3 4 5 } "seek-test5" unique-file binary [
231 3 seek-absolute seek-input
233 -2 seek-relative seek-input
242 "seek-test6" unique-file binary [
243 -10 seek-absolute seek-input
248 "resource:license.txt" binary [
250 tell-input 44 assert=
251 -44 seek-relative seek-input
257 "non-string-error" unique-file ascii [
260 ] [ no-method? ] must-fail-with
263 "non-byte-array-error" unique-file binary [
266 ] [ no-method? ] must-fail-with
268 ! What happens if we close a file twice?
270 "closing-twice" unique-file ascii <file-writer>
271 [ dispose ] [ dispose ] bi
274 ! Test cwd, cd. You do not want to use with-cd, you want with-directory.
276 : with-cd ( path quot -- )
277 [ [ absolute-path cd ] curry ] dip compose
283 "resource:core/" [ "hi" print ] with-cd
289 [ "resource:core/" [ "nick cage" throw ] with-cd ] [ drop ] recover
294 "resource:core/" [ "nick cage" throw ] with-cd
295 ] [ "nick cage" = ] must-fail-with