@@ -6,6 +6,143 @@ ioslength(io::IOBuffer) = (io.seekable ? io.size : bytesavailable(io))
6
6
7
7
bufcontents (io:: Base.GenericIOBuffer ) = unsafe_string (pointer (io. data), io. size)
8
8
9
+ @testset " Basic tests" begin
10
+ @test_throws ArgumentError IOBuffer (;maxsize= - 1 )
11
+ @test_throws ArgumentError IOBuffer ([0x01 ]; maxsize= - 1 )
12
+
13
+ # Test that sizehint actually will sizehint the vector,
14
+ v = UInt8[]
15
+ buf = IOBuffer (v; sizehint= 64 , write= true )
16
+ @test length (v. ref. mem) >= 64
17
+
18
+
19
+ end
20
+
21
+ @testset " Basic reading" begin
22
+ # Readavailable is equal to read
23
+ buf = IOBuffer (" abcdef" )
24
+ @test read (buf, UInt8) == UInt8 (' a' )
25
+ @test bytesavailable (buf) == 5
26
+ @test readavailable (buf) == b " bcdef"
27
+
28
+ # Reading less than all the bytes
29
+ buf = IOBuffer (b " ABCDEFGHIJ" )
30
+ @test read (buf, 1 ) == b " A"
31
+ @test read (buf, 3 ) == b " BCD"
32
+
33
+ # Reading more bytes than available will not error
34
+ @test read (buf, 100 ) == b " EFGHIJ"
35
+ end
36
+
37
+ @testset " Byte occursin GenericIOBuffer" begin
38
+ buf = IOBuffer (@view (collect (0x1f : 0x3d )[1 : end ]))
39
+ @test occursin (0x1f , buf)
40
+ @test occursin (0x3d , buf)
41
+ @test occursin (0x2a , buf)
42
+
43
+ @test ! occursin (0xff , buf)
44
+ @test ! occursin (0x00 , buf)
45
+
46
+ v = Vector {UInt8} (" bcdefg" )
47
+ pushfirst! (v, UInt8 (' a' ))
48
+ buf = IOBuffer (v)
49
+ @test occursin (UInt8 (' a' ), buf)
50
+ read (buf, UInt8)
51
+ @test ! occursin (UInt8 (' a' ), buf)
52
+ @test ! occursin (0x00 , buf)
53
+
54
+ buf = IOBuffer (" abcdefg" )
55
+ @test occursin (UInt8 (' a' ), buf)
56
+ end
57
+
58
+ @testset " Non-Memory backed IOBuffer" begin
59
+ buf = IOBuffer (Test. GenericArray (collect (0x02 : 0x0d )), read= true )
60
+ @test read (buf) == 0x02 : 0x0d
61
+
62
+ buf = IOBuffer (Test. GenericArray (collect (0x02 : 0x0d )), read= true )
63
+ @test read (buf, UInt8) == 0x02
64
+ @test read (buf) == 0x03 : 0x0d
65
+
66
+ v = view (collect (UInt8 (' a' ): UInt8 (' z' )), 4 : 10 )
67
+ buf = IOBuffer (v, read= true , write= true )
68
+ @test read (buf, UInt8) == UInt8 (' d' )
69
+ @test read (buf) == UInt8 (' e' ): UInt8 (' j' )
70
+ seekstart (buf)
71
+ @test read (buf, UInt8) == UInt8 (' d' )
72
+ write (buf, UInt8 (' x' ))
73
+ write (buf, " ABC" )
74
+ seekstart (buf)
75
+ @test read (buf) == b " dxABCij"
76
+ end
77
+
78
+ @testset " Copying" begin
79
+ # Test offset is preserved when copying
80
+ v = UInt8[]
81
+ pushfirst! (v, UInt8 (' a' ), UInt8 (' b' ), UInt8 (' c' ))
82
+ buf = IOBuffer (v; write= true , read= true , append= true )
83
+ write (buf, " def" )
84
+ read (buf, UInt16)
85
+ buf2 = copy (buf)
86
+ @test String (read (buf)) == " cdef"
87
+ @test String (read (buf2)) == " cdef"
88
+
89
+ # Test copying with non-Memory backed GenericIOBuffer
90
+ buf = IOBuffer (Test. GenericArray (collect (0x02 : 0x0d )), read= true )
91
+ @test_broken read (buf, UInt16)
92
+ buf2 = copy (buf)
93
+ @test isreadable (buf2)
94
+ @test ! iswritable (buf2)
95
+ @test_broken read (buf2) == 0x04 : 0x0d
96
+ end
97
+
98
+ @testset " copyuntil" begin
99
+ a = IOBuffer (b " abcdeajdgabdfg" )
100
+ b = IOBuffer (collect (b " xx" ); write= true , read= true , append= true )
101
+ copyuntil (b, a, UInt8 (' a' ))
102
+ @test read (b) == b " xx"
103
+ seekstart (b)
104
+ copyuntil (b, a, UInt8 (' a' ); keep= true )
105
+ @test read (b) == b " xxbcdea"
106
+ seekstart (b)
107
+ copyuntil (b, a, UInt (' w' ))
108
+ @test read (b) == b " xxbcdeajdgabdfg"
109
+ end
110
+
111
+ @testset " copyline" begin
112
+ a = IOBuffer (b " abcde\n abc\r\n abc\n\r\n ac" )
113
+ b = IOBuffer ()
114
+ copyline (b, a)
115
+ @test take! (copy (b)) == b " abcde"
116
+ copyline (b, a)
117
+ @test take! (copy (b)) == b " abcdeabc"
118
+ copyline (b, a; keep= true )
119
+ @test take! (copy (b)) == b " abcdeabcabc\n "
120
+ copyline (b, a; keep= false )
121
+ @test take! (copy (b)) == b " abcdeabcabc\n "
122
+ copyline (b, a; keep= false )
123
+ @test take! (copy (b)) == b " abcdeabcabc\n ac"
124
+ end
125
+
126
+ @testset " take!" begin
127
+ a = IOBuffer (" abc" )
128
+ @test take! (a) == b " abc"
129
+
130
+ v = UInt8[]
131
+ pushfirst! (v, 0x0a )
132
+ buf = IOBuffer (v; write= true , append= true )
133
+ write (buf, " def" )
134
+ @test take! (buf) == b "\n def"
135
+
136
+ v = view (collect (b " abcdefghij" ), 3 : 9 )
137
+ buf = IOBuffer (v; write= true , read= true )
138
+ read (buf, UInt8)
139
+ write (buf, " xxy" )
140
+ @test take! (buf) == b " cxxyghi"
141
+
142
+ v = view (collect (b " abcdefghij" ), 3 : 9 )
143
+ buf = IOBuffer (v; write= true , read= true )
144
+ end
145
+
9
146
@testset " Read/write empty IOBuffer" begin
10
147
io = IOBuffer ()
11
148
@test eof (io)
0 commit comments