1+ using System ;
2+ using System . IO ;
3+ using System . Text ;
4+ using System . Threading . Tasks ;
5+ using CSUtilities . IO ;
6+ using Xunit ;
7+
8+ namespace CSUtilities . Tests . IO
9+ {
10+ public class StreamIOTests : IDisposable
11+ {
12+ private readonly MemoryStream _memoryStream ;
13+ private readonly StreamIO _streamIO ;
14+
15+ public StreamIOTests ( )
16+ {
17+ _memoryStream = new MemoryStream ( ) ;
18+ _streamIO = new StreamIO ( _memoryStream ) ;
19+ }
20+
21+ public void Dispose ( )
22+ {
23+ _streamIO . Dispose ( ) ;
24+ _memoryStream . Dispose ( ) ;
25+ }
26+
27+ [ Fact ]
28+ public void WriteAndReadByte_WorksCorrectly ( )
29+ {
30+ _streamIO . Write < byte > ( 0x42 ) ;
31+ _streamIO . Position = 0 ;
32+ Assert . Equal ( 0x42 , _streamIO . ReadByte ( ) ) ;
33+ }
34+
35+ [ Fact ]
36+ public void WriteAndReadBytes_WorksCorrectly ( )
37+ {
38+ byte [ ] data = { 1 , 2 , 3 , 4 , 5 } ;
39+ _streamIO . WriteBytes ( data ) ;
40+ _streamIO . Position = 0 ;
41+ Assert . Equal ( data , _streamIO . ReadBytes ( data . Length ) ) ;
42+ }
43+
44+ [ Fact ]
45+ public void WriteAndReadString_WorksCorrectly ( )
46+ {
47+ string value = "Hello, world!" ;
48+ _streamIO . Write ( value , Encoding . UTF8 ) ;
49+ _streamIO . Position = 0 ;
50+ string result = _streamIO . ReadString ( value . Length , Encoding . UTF8 ) ;
51+ Assert . Equal ( value , result ) ;
52+ }
53+
54+ [ Fact ]
55+ public void GetBytes_ReturnsCorrectBytes ( )
56+ {
57+ byte [ ] data = { 10 , 20 , 30 , 40 , 50 } ;
58+ _streamIO . WriteBytes ( data ) ;
59+ byte [ ] result = _streamIO . GetBytes ( 1 , 3 ) ;
60+ Assert . Equal ( new byte [ ] { 20 , 30 , 40 } , result ) ;
61+ }
62+
63+ [ Fact ]
64+ public async Task GetBytesAsync_ReturnsCorrectBytes ( )
65+ {
66+ byte [ ] data = { 100 , 101 , 102 , 103 , 104 } ;
67+ _streamIO . WriteBytes ( data ) ;
68+ byte [ ] result = await _streamIO . GetBytesAsync ( 2 , 2 ) ;
69+ Assert . Equal ( new byte [ ] { 102 , 103 } , result ) ;
70+ }
71+
72+ [ Fact ]
73+ public void LookByte_DoesNotAdvancePosition ( )
74+ {
75+ _streamIO . Write < byte > ( 0x7F ) ;
76+ _streamIO . Position = 0 ;
77+ byte b = _streamIO . LookByte ( ) ;
78+ Assert . Equal ( 0x7F , b ) ;
79+ Assert . Equal ( 0 , _streamIO . Position ) ;
80+ }
81+
82+ [ Fact ]
83+ public void LookBytes_DoesNotAdvancePosition ( )
84+ {
85+ byte [ ] data = { 1 , 2 , 3 , 4 } ;
86+ _streamIO . WriteBytes ( data ) ;
87+ _streamIO . Position = 0 ;
88+ byte [ ] looked = _streamIO . LookBytes ( 2 ) ;
89+ Assert . Equal ( new byte [ ] { 1 , 2 } , looked ) ;
90+ Assert . Equal ( 0 , _streamIO . Position ) ;
91+ }
92+
93+ [ Fact ]
94+ public void ReadChar_ReadsCorrectChar ( )
95+ {
96+ _streamIO . Write < byte > ( ( byte ) 'A' ) ;
97+ _streamIO . Position = 0 ;
98+ Assert . Equal ( 'A' , _streamIO . ReadChar ( ) ) ;
99+ }
100+
101+ [ Fact ]
102+ public void ReadUntil_ReadsUntilMatch ( )
103+ {
104+ string value = "abc;def" ;
105+ _streamIO . Write ( value , Encoding . ASCII ) ;
106+ _streamIO . Position = 0 ;
107+ string result = _streamIO . ReadUntil ( ';' ) ;
108+ Assert . Equal ( "abc;" , result ) ;
109+ }
110+
111+ [ Fact ]
112+ public void ReadShort_ReadsCorrectValue ( )
113+ {
114+ short value = 0x1234 ;
115+ _streamIO . Write ( value ) ;
116+ _streamIO . Position = 0 ;
117+ Assert . Equal ( value , _streamIO . ReadShort ( ) ) ;
118+ }
119+
120+ [ Fact ]
121+ public void ReadInt_ReadsCorrectValue ( )
122+ {
123+ int value = 0x12345678 ;
124+ _streamIO . Write ( value ) ;
125+ _streamIO . Position = 0 ;
126+ Assert . Equal ( value , _streamIO . ReadInt ( ) ) ;
127+ }
128+
129+ [ Fact ]
130+ public void ReadLong_ReadsCorrectValue ( )
131+ {
132+ long value = 0x123456789ABCDEF0 ;
133+ _streamIO . Write ( value ) ;
134+ _streamIO . Position = 0 ;
135+ Assert . Equal ( value , _streamIO . ReadLong ( ) ) ;
136+ }
137+
138+ [ Fact ]
139+ public void ReadSingle_ReadsCorrectValue ( )
140+ {
141+ float value = 123.456f ;
142+ _streamIO . Write ( value ) ;
143+ _streamIO . Position = 0 ;
144+ Assert . Equal ( value , _streamIO . ReadSingle ( ) , 3 ) ;
145+ }
146+
147+ [ Fact ]
148+ public void ReadDouble_ReadsCorrectValue ( )
149+ {
150+ double value = 123456.789 ;
151+ _streamIO . Write ( value ) ;
152+ _streamIO . Position = 0 ;
153+ Assert . Equal ( value , _streamIO . ReadDouble ( ) , 6 ) ;
154+ }
155+
156+ [ Fact ]
157+ public void ReadUShort_ReadsCorrectValue ( )
158+ {
159+ ushort value = 0xFEDC ;
160+ _streamIO . Write ( value ) ;
161+ _streamIO . Position = 0 ;
162+ Assert . Equal ( value , _streamIO . ReadUShort ( ) ) ;
163+ }
164+
165+ [ Fact ]
166+ public void ReadUInt_ReadsCorrectValue ( )
167+ {
168+ uint value = 0xDEADBEEF ;
169+ _streamIO . Write ( value ) ;
170+ _streamIO . Position = 0 ;
171+ Assert . Equal ( value , _streamIO . ReadUInt ( ) ) ;
172+ }
173+
174+ [ Fact ]
175+ public void ReadULong_ReadsCorrectValue ( )
176+ {
177+ ulong value = 0x123456789ABCDEF0UL ;
178+ _streamIO . Write ( value ) ;
179+ _streamIO . Position = 0 ;
180+ Assert . Equal ( value , _streamIO . ReadULong ( ) ) ;
181+ }
182+
183+ [ Fact ]
184+ public void WriteBytes_WithOffsetAndCount_WritesCorrectly ( )
185+ {
186+ byte [ ] data = { 1 , 2 , 3 , 4 , 5 } ;
187+ _streamIO . WriteBytes ( data , 1 , 3 ) ;
188+ _streamIO . Position = 0 ;
189+ Assert . Equal ( new byte [ ] { 2 , 3 , 4 } , _streamIO . ReadBytes ( 3 ) ) ;
190+ }
191+
192+ [ Fact ]
193+ public void ReadBytes_ThrowsOnNegativeLength ( )
194+ {
195+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => _streamIO . ReadBytes ( - 1 ) ) ;
196+ }
197+
198+ [ Fact ]
199+ public void GetBytes_ThrowsOnNegativeLength ( )
200+ {
201+ Assert . Throws < ArgumentOutOfRangeException > ( ( ) => _streamIO . GetBytes ( 0 , - 1 ) ) ;
202+ }
203+
204+ [ Fact ]
205+ public void ReadBytes_ThrowsOnEndOfStream ( )
206+ {
207+ Assert . Throws < EndOfStreamException > ( ( ) => _streamIO . ReadBytes ( 1 ) ) ;
208+ }
209+ }
210+ }
0 commit comments