]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files-tests.factor
cf58dbfe05585a5823cf367c90af71840974c7a3
[factor.git] / core / io / files / files-tests.factor
1 USING: arrays debugger.threads destructors io io.directories
2 io.encodings.ascii io.encodings.binary io.encodings.string
3 io.encodings.8-bit.latin1 io.files io.files.private
4 io.files.temp io.files.unique kernel make math sequences system
5 threads tools.test generic.single specialized-arrays alien.c-types ;
6 SPECIALIZED-ARRAY: int
7 IN: io.files.tests
8
9 [ ] [ "append-test" temp-file dup exists? [ delete-file ] [ drop ] if ] unit-test
10
11 [ ] [ "append-test" temp-file ascii <file-appender> dispose ] unit-test
12
13 [
14     "This is a line.\rThis is another line.\r"
15 ] [
16     "vocab:io/test/mac-os-eol.txt" latin1
17     [ 500 read ] with-file-reader
18 ] unit-test
19
20 [
21     255
22 ] [
23     "vocab:io/test/binary.txt" latin1
24     [ read1 ] with-file-reader >fixnum
25 ] unit-test
26
27 [
28     "This" CHAR: \s
29 ] [
30     "vocab:io/test/read-until-test.txt" ascii
31     [ " " read-until ] with-file-reader
32 ] unit-test
33
34 [
35     "This" CHAR: \s
36 ] [
37     "vocab:io/test/read-until-test.txt" binary
38     [ " " read-until [ ascii decode ] dip ] with-file-reader
39 ] unit-test
40
41 [ ] [
42     "It seems Jobs has lost his grasp on reality again.\n"
43     "separator-test.txt" temp-file latin1 set-file-contents
44 ] unit-test
45
46 [
47     {
48         { "It seems " CHAR: J }
49         { "obs has lost h" CHAR: i }
50         { "s grasp on reality again.\n" f }
51     }
52 ] [
53     [
54         "separator-test.txt" temp-file
55         latin1 [
56             "J" read-until 2array ,
57             "i" read-until 2array ,
58             "X" read-until 2array ,
59         ] with-file-reader
60     ] { } make
61 ] unit-test
62
63 [ ] [
64     image binary [
65         10 [ 65536 read drop ] times
66     ] with-file-reader
67 ] unit-test
68
69 ! Writing specialized arrays to binary streams should work
70 [ ] [
71     "test.txt" temp-file binary [
72         int-array{ 1 2 3 } write
73     ] with-file-writer
74 ] unit-test
75
76 [ int-array{ 1 2 3 } ] [
77     "test.txt" temp-file binary [
78         3 4 * read
79     ] with-file-reader
80     byte-array>int-array
81 ] unit-test
82
83 ! Writing strings to binary streams should fail
84 [
85     "test.txt" temp-file binary [
86         "OMGFAIL" write
87     ] with-file-writer
88 ] must-fail
89
90 ! Test EOF behavior
91 [ 10 ] [
92     image binary [
93         0 read drop
94         10 read length
95     ] with-file-reader
96 ] unit-test
97
98 ! Make sure that writing to a closed stream from another thread doesn't crash
99 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
100
101 [ ] [ "test-quux.txt" temp-file delete-file ] unit-test
102
103 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
104
105 [ ] [ "test-quux.txt" "quux-test.txt" [ temp-file ] bi@ move-file ] unit-test
106
107 [ t ] [ "quux-test.txt" temp-file exists? ] unit-test
108
109 [ ] [ "quux-test.txt" temp-file delete-file ] unit-test
110
111 ! File seeking tests
112 [ B{ 3 2 3 4 5 } ]
113 [
114     "seek-test1" unique-file binary
115     [
116         [
117             B{ 1 2 3 4 5 } write 0 seek-absolute seek-output
118             B{ 3 } write
119         ] with-file-writer
120     ] [
121         file-contents
122     ] 2bi
123 ] unit-test
124
125 [ B{ 1 2 3 4 3 } ]
126 [
127     "seek-test2" unique-file binary
128     [
129         [
130             B{ 1 2 3 4 5 } write -1 seek-relative seek-output
131             B{ 3 } write
132         ] with-file-writer
133     ] [
134         file-contents
135     ] 2bi
136 ] unit-test
137
138 [ B{ 1 2 3 4 5 0 3 } ]
139 [
140     "seek-test3" unique-file binary
141     [
142         [
143             B{ 1 2 3 4 5 } write 1 seek-relative seek-output
144             B{ 3 } write
145         ] with-file-writer
146     ] [
147         file-contents
148     ] 2bi
149 ] unit-test
150
151 [ B{ 3 } ]
152 [
153     B{ 1 2 3 4 5 } "seek-test4" unique-file binary [
154         set-file-contents
155     ] [
156         [
157             -3 seek-end seek-input 1 read
158         ] with-file-reader
159     ] 2bi
160 ] unit-test
161
162 [ B{ 2 } ]
163 [
164     B{ 1 2 3 4 5 } "seek-test5" unique-file binary [
165         set-file-contents
166     ] [
167         [
168             3 seek-absolute seek-input
169             -2 seek-relative seek-input
170             1 read
171         ] with-file-reader
172     ] 2bi
173 ] unit-test
174
175 [
176     "seek-test6" unique-file binary [
177         -10 seek-absolute seek-input
178     ] with-file-reader
179 ] must-fail
180
181 [
182     "non-string-error" unique-file ascii [
183         { } write
184     ] with-file-writer
185 ] [ no-method? ] must-fail-with
186
187 [
188     "non-byte-array-error" unique-file binary [
189         "" write
190     ] with-file-writer
191 ] [ no-method? ] must-fail-with
192
193 ! What happens if we close a file twice?
194 [ ] [
195     "closing-twice" unique-file ascii <file-writer>
196     [ dispose ] [ dispose ] bi
197 ] unit-test