]> gitweb.factorcode.org Git - factor.git/blob - basis/io/streams/limited/limited-tests.factor
643776107914e9712d8d8bbb10947c8d03b1b806
[factor.git] / basis / io / streams / limited / limited-tests.factor
1 USING: destructors io io.encodings io.encodings.latin1
2 io.encodings.ascii io.encodings.binary io.encodings.string
3 io.encodings.utf8 io.files io.pipes io.streams.byte-array
4 io.streams.duplex io.streams.limited io.streams.string kernel
5 namespaces strings tools.test ;
6
7 { } [
8     "hello world\nhow are you today\nthis is a very long line indeed"
9     ascii encode binary <byte-reader> "data" set
10 ] unit-test
11
12 { } [ "data" get 24 <limited-stream> "limited" set ] unit-test
13
14 { CHAR: h } [ "limited" get stream-read1 ] unit-test
15
16 { } [ "limited" get ascii <decoder> "decoded" set ] unit-test
17
18 { "ello world" } [ "decoded" get stream-readln ] unit-test
19
20 { "how " } [ 4 "decoded" get stream-read ] unit-test
21
22 { "are you " } [ "decoded" get stream-readln ] unit-test
23
24 { f } [ "decoded" get stream-readln ] unit-test
25
26 { } [
27     "abc\ndef\nghi"
28     ascii encode binary <byte-reader> "data" set
29 ] unit-test
30
31 { } [ "data" get 4 <limited-stream> "limited" set ] unit-test
32
33 { "abc" CHAR: \n }
34 [ "\n" "limited" get stream-read-until [ >string ] dip ] unit-test
35
36 { "" f } [ "\n" "limited" get stream-read-until [ >string ] dip ] unit-test
37
38 { CHAR: a }
39 [ "a" <string-reader> 1 <limited-stream> stream-read1 ] unit-test
40
41 { "abc" }
42 [
43     "abc" <string-reader> 3 <limited-stream>
44     4 swap stream-read
45 ] unit-test
46
47 { f }
48 [
49     "abc" <string-reader> 3 <limited-stream>
50     4 over stream-read drop 10 swap stream-read
51 ] unit-test
52
53 ! pipes are duplex and not seekable
54 { "as" } [
55     latin1 <pipe> [
56         input-stream [ 2 <limited-stream> ] change
57         "asdf" write flush 2 read
58     ] with-stream
59 ] unit-test
60
61 { "as" } [
62     latin1 <pipe> [
63         input-stream [ 2 <limited-stream> ] change
64         "asdf" write flush 3 read
65     ] with-stream
66 ] unit-test
67
68 ! test seeking on limited unseekable streams
69 { "as" } [
70     latin1 <pipe> [
71         input-stream [ 2 <limited-stream> ] change
72         "asdf" write flush 2 read
73     ] with-stream
74 ] unit-test
75
76 { "as" } [
77     latin1 <pipe> [
78         input-stream [ 2 <limited-stream> ] change
79         "asdf" write flush 3 read
80     ] with-stream
81 ] unit-test
82
83 { t }
84 [
85     "abc" <string-reader> 3 limit-stream unlimit-stream
86     "abc" <string-reader> =
87 ] unit-test
88
89 { t }
90 [
91     "abc" <string-reader> 3 limit-stream unlimit-stream
92     "abc" <string-reader> =
93 ] unit-test
94
95 { t }
96 [
97     [
98         "resource:LICENSE.txt" utf8 <file-reader> &dispose
99         3 limit-stream unlimit-stream
100         "resource:LICENSE.txt" utf8 <file-reader> &dispose
101         [ decoder? ] both?
102     ] with-destructors
103 ] unit-test
104
105 { "asdf" } [
106     "asdf" <string-reader> 2 <limited-stream> [
107         unlimited-input contents
108     ] with-input-stream
109 ] unit-test
110
111 { "asdf" } [
112     "asdf" <string-reader> 2 <limited-stream> [
113         [ contents ] with-unlimited-input
114     ] with-input-stream
115 ] unit-test
116
117 { "gh" } [
118     "asdfgh" <string-reader> 4 <limited-stream> [
119         2 [
120             [ contents drop ] with-unlimited-input
121         ] with-limited-input
122         [ contents ] with-unlimited-input
123     ] with-input-stream
124 ] unit-test
125
126 { 4 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 4 <limited-stream> stream-length ] unit-test
127 { 6 } [ B{ 0 1 2 3 4 5 } binary <byte-reader> 8 <limited-stream> stream-length ] unit-test