]> gitweb.factorcode.org Git - factor.git/blob - core/io/files/files-tests.factor
Add vocab: for vocab-relative paths
[factor.git] / core / io / files / files-tests.factor
1 USING: arrays debugger.threads destructors io io.directories
2 io.encodings.8-bit io.encodings.ascii io.encodings.binary
3 io.files io.files.private io.files.temp io.files.unique kernel
4 make math sequences system threads tools.test ;
5 IN: io.files.tests
6
7 \ exists? must-infer
8 \ (exists?) must-infer
9
10 [ ] [ "append-test" temp-file dup exists? [ delete-file ] [ drop ] if ] unit-test
11
12 [ ] [ "append-test" temp-file ascii <file-appender> dispose ] unit-test
13
14 [
15     "This is a line.\rThis is another line.\r"
16 ] [
17     "vocab:io/test/mac-os-eol.txt" latin1 <file-reader>
18     [ 500 read ] with-input-stream
19 ] unit-test
20
21 [
22     255
23 ] [
24     "vocab:io/test/binary.txt" latin1 <file-reader>
25     [ read1 ] with-input-stream >fixnum
26 ] unit-test
27
28 [ ] [
29     "It seems Jobs has lost his grasp on reality again.\n"
30     "separator-test.txt" temp-file latin1 set-file-contents
31 ] unit-test
32
33 [
34     {
35         { "It seems " CHAR: J }
36         { "obs has lost h" CHAR: i }
37         { "s grasp on reality again.\n" f }
38     }
39 ] [
40     [
41         "separator-test.txt" temp-file
42         latin1 <file-reader> [
43             "J" read-until 2array ,
44             "i" read-until 2array ,
45             "X" read-until 2array ,
46         ] with-input-stream
47     ] { } make
48 ] unit-test
49
50 [ ] [
51     image binary [
52         10 [ 65536 read drop ] times
53     ] with-file-reader
54 ] unit-test
55
56 ! Test EOF behavior
57 [ 10 ] [
58     image binary [
59         0 read drop
60         10 read length
61     ] with-file-reader
62 ] unit-test
63
64 USE: debugger.threads
65
66 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
67
68 [ ] [ "test-quux.txt" temp-file delete-file ] unit-test
69
70 [ ] [ "test-quux.txt" temp-file ascii [ [ yield "Hi" write ] "Test" spawn drop ] with-file-writer ] unit-test
71
72 [ ] [ "test-quux.txt" "quux-test.txt" [ temp-file ] bi@ move-file ] unit-test
73
74 [ t ] [ "quux-test.txt" temp-file exists? ] unit-test
75
76 [ ] [ "quux-test.txt" temp-file delete-file ] unit-test
77
78 ! File seeking tests
79 [ B{ 3 2 3 4 5 } ]
80 [
81     "seek-test1" unique-file binary
82     [
83         [
84             B{ 1 2 3 4 5 } write 0 seek-absolute seek-output
85             B{ 3 } write
86         ] with-file-writer
87     ] [
88         file-contents
89     ] 2bi
90 ] unit-test
91
92 [ B{ 1 2 3 4 3 } ]
93 [
94     "seek-test2" unique-file binary
95     [
96         [
97             B{ 1 2 3 4 5 } write -1 seek-relative seek-output
98             B{ 3 } write
99         ] with-file-writer
100     ] [
101         file-contents
102     ] 2bi
103 ] unit-test
104
105 [ B{ 1 2 3 4 5 0 3 } ]
106 [
107     "seek-test3" unique-file binary
108     [
109         [
110             B{ 1 2 3 4 5 } write 1 seek-relative seek-output
111             B{ 3 } write
112         ] with-file-writer
113     ] [
114         file-contents
115     ] 2bi
116 ] unit-test
117
118 [ B{ 3 } ]
119 [
120     B{ 1 2 3 4 5 } "seek-test4" unique-file binary [
121         set-file-contents
122     ] [
123         [
124             -3 seek-end seek-input 1 read
125         ] with-file-reader
126     ] 2bi
127 ] unit-test
128
129 [ B{ 2 } ]
130 [
131     B{ 1 2 3 4 5 } "seek-test5" unique-file binary [
132         set-file-contents
133     ] [
134         [
135             3 seek-absolute seek-input
136             -2 seek-relative seek-input
137             1 read
138         ] with-file-reader
139     ] 2bi
140 ] unit-test
141
142 [
143     "seek-test6" unique-file binary [
144         -10 seek-absolute seek-input
145     ] with-file-reader
146 ] must-fail