1 USING: alien alien.c-types alien.data arrays classes.struct
2 compiler.units continuations destructors generic.single io
3 io.backend io.directories io.encodings io.encodings.ascii
4 io.encodings.binary io.encodings.latin1 io.encodings.string
5 io.encodings.utf16 io.encodings.utf8 io.files io.files.private
6 io.pathnames kernel locals make math sequences
7 specialized-arrays system threads tools.test vocabs ;
8 FROM: specialized-arrays.private => specialized-array-vocab ;
11 SPECIALIZED-ARRAY: int
14 [ ascii <file-appender> dispose ] with-test-file
18 "This is a line.\rThis is another line.\r"
20 "vocab:io/test/mac-os-eol.txt" latin1
21 [ 500 read ] with-file-reader
27 "vocab:io/test/binary.txt" latin1
28 [ read1 ] with-file-reader >fixnum
34 "vocab:io/test/read-until-test.txt" ascii
35 [ " " read-until ] with-file-reader
41 "vocab:io/test/read-until-test.txt" binary
42 [ " " read-until [ ascii decode ] dip ] with-file-reader
47 "It seems Jobs has lost his grasp on reality again.\n"
48 path latin1 set-file-contents
53 { "It seems " CHAR: J }
54 { "obs has lost h" CHAR: i }
55 { "s grasp on reality again.\n" f }
60 "J" read-until 2array ,
61 "i" read-until 2array ,
62 "X" read-until 2array ,
70 10 [ 65536 read drop ] times
74 ! Writing specialized arrays to binary streams should work
78 int-array{ 1 2 3 } write
82 { int-array{ 1 2 3 } } [
92 BV{ 0 1 2 } path binary set-file-contents
96 path binary file-contents
101 STRUCT: pt { x uint } { y uint } ;
102 SPECIALIZED-ARRAY: pt
105 pt-array{ S{ pt f 1 1 } S{ pt f 2 2 } S{ pt f 3 3 } }
109 pt-array-1 path binary set-file-contents
113 path binary file-contents
114 pt-array-1 >c-ptr sequence=
118 ! Slices should support >c-ptr and byte-length
121 pt-array-1 rest-slice
122 path binary set-file-contents
126 path binary file-contents
128 pt-array-1 rest-slice sequence=
134 pt specialized-array-vocab forget-vocab
135 ] with-compilation-unit
138 ! Writing strings to binary streams should fail
141 path binary [ "OMGFAIL" write ] with-file-writer
153 ! Make sure that writing to a closed stream from another thread doesn't crash
155 { } [ "test.txt" ascii [ [ yield "Hi" write ] "Test-write-file" spawn drop ] with-file-writer ] unit-test
157 { } [ "test.txt" delete-file ] unit-test
159 { } [ "test.txt" ascii [ [ yield "Hi" write ] "Test-write-file" spawn drop ] with-file-writer ] unit-test
161 { } [ "test.txt" "test2.txt" move-file ] unit-test
163 { t } [ "test2.txt" file-exists? ] unit-test
165 { } [ "test2.txt" delete-file ] unit-test
166 ] with-test-directory
173 tell-output 5 assert=
174 0 seek-absolute seek-output
175 tell-output 0 assert=
177 tell-output 1 assert=
178 ] with-file-writer path binary file-contents
186 tell-output 5 assert=
187 -1 seek-relative seek-output
188 tell-output 4 assert=
190 tell-output 5 assert=
191 ] with-file-writer path binary file-contents
196 { B{ 1 2 3 4 5 0 3 } } [
199 tell-output 5 assert=
200 1 seek-relative seek-output
201 tell-output 6 assert=
203 tell-output 7 assert=
204 ] with-file-writer path binary file-contents
210 B{ 1 2 3 4 5 } path binary set-file-contents
213 -3 seek-end seek-input
224 B{ 1 2 3 4 5 } path binary set-file-contents
227 3 seek-absolute seek-input
229 -2 seek-relative seek-input
238 "does-not-exist" binary [
239 -10 seek-absolute seek-input
244 "resource:LICENSE.txt" binary [
246 tell-input 44 assert=
247 -44 seek-relative seek-input
253 [ path ascii [ { 129 } write ] with-file-writer ]
254 [ encode-error? ] must-fail-with
259 [ path ascii [ { } write ] with-file-writer ] unit-test
263 [ path binary [ "" write ] with-file-writer ]
264 [ no-method? ] must-fail-with
267 ! What happens if we close a file twice?
269 "closing-twice" ascii <file-writer>
270 [ dispose ] [ dispose ] bi
271 ] with-test-directory
275 "resource:core" normalize-path
276 [ cwd = ] [ cd ] [ cwd = ] tri
277 ] cwd '[ _ dup cd cwd = ] finally
282 [ 0 1 "å" <slice> swap utf8 set-file-contents ]
283 [ utf8 file-contents ] bi "å" =
289 [ 0 1 "å" <slice> swap utf16 set-file-contents ]
290 [ utf16 file-contents ] bi "å" =
296 [ 0 1 "a" <slice> swap ascii set-file-contents ]
297 [ ascii file-contents ] bi "a" =