]> gitweb.factorcode.org Git - factor.git/blob - core/io/io-tests.factor
be79ff6f47e3f4751f58f61a518befbebd326fe9
[factor.git] / core / io / io-tests.factor
1 USING: accessors io io.streams.string kernel math parser sbufs
2 sequences tools.test words namespaces strings ;
3 IN: io.tests
4
5 { f } [
6     "vocab:io/test/no-trailing-eol.factor" run-file
7     "foo" "io.tests" lookup-word
8 ] unit-test
9
10 ! Make sure we use correct to_c_string form when writing
11 { } [ "\0" write ] unit-test
12
13 ! Test default input stream protocol methods
14
15 TUPLE: up-to-13-reader { i fixnum initial: 0 } ;
16 INSTANCE: up-to-13-reader input-stream
17
18 M: up-to-13-reader stream-element-type drop +byte+ ; inline
19 M: up-to-13-reader stream-read1
20     [ dup 1 + ] change-i drop
21     dup 13 >= [ drop f ] when ; inline
22
23 { B{ 0 1 2 } } [ 3 up-to-13-reader new stream-read ] unit-test
24 { B{ 0 1 2 } } [ 3 up-to-13-reader new stream-read-partial ] unit-test
25
26 { B{ 0 1 2 3 4 5 6 7 8 9 10 11 12 } f }
27 [ up-to-13-reader new [ 20 swap stream-read ] [ 20 swap stream-read ] bi ] unit-test
28
29 {
30     T{ slice f 0 8 B{ 0 1 2 3 4 5 6 7 } } t
31     T{ slice f 0 5 B{ 8 9 10 11 12 205 206 207 } } t
32     T{ slice f 0 0 B{ 8 9 10 11 12 205 206 207 } } f
33 } [
34     up-to-13-reader new
35     [ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ]
36     [ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ]
37     [ B{ 200 201 202 203 204 205 206 207 } swap stream-read-into ] tri
38 ] unit-test
39
40 {
41     B{ 0 1 2 3 4 5 6 7 8 } 9
42     B{ 10 11 12 } f
43     f f
44 } [
45     up-to-13-reader new
46     [ "\t" swap stream-read-until ]
47     [ "\t" swap stream-read-until ]
48     [ "\t" swap stream-read-until ] tri
49 ] unit-test
50
51 { B{ 0 1 2 3 4 5 6 7 8 9 } B{ 11 12 } f } [
52     up-to-13-reader new
53     [ stream-readln ] [ stream-readln ] [ stream-readln ] tri
54 ] unit-test
55
56 ! Test default output stream protocol methods
57
58 TUPLE: dumb-writer vector ;
59 INSTANCE: dumb-writer output-stream
60
61 : <dumb-writer> ( -- x ) BV{ } clone dumb-writer boa ; inline
62
63 M: dumb-writer stream-element-type drop +byte+ ; inline
64 M: dumb-writer stream-write1 vector>> push ; inline
65
66 { BV{ 11 22 33 } } [
67     <dumb-writer> 
68     [ B{ 11 22 33 } swap stream-write ]
69     [ vector>> ] bi
70 ] unit-test
71
72 { BV{ 11 22 33 10 } } [
73     <dumb-writer> 
74     [ B{ 11 22 33 } swap stream-write ]
75     [ stream-nl ]
76     [ vector>> ] tri
77 ] unit-test
78
79 { SBUF" asdf" }
80 [ "asdf" <string-reader> 4 <sbuf> [ stream-copy ] keep ] unit-test
81
82 [ "asdf" ]
83 [
84     [
85         [ "asdf" error-stream get stream-write ] with-error>output
86     ] with-string-writer
87 ] unit-test
88
89 [ "asdf" ]
90 [
91     <string-writer> [
92         [
93             [ "asdf" output-stream get stream-write ] with-output>error
94         ] with-error-stream
95     ] keep >string
96 ] unit-test