@@ -10,16 +10,6 @@ namespace TechnitiumLibrary.UnitTests.TechnitiumLibrary.IO
1010 [ TestClass ]
1111 public sealed class BinaryWriterExtensionsTests
1212 {
13- private static ( BinaryWriter writer , MemoryStream stream ) CreateWriter ( )
14- {
15- MemoryStream ms = new MemoryStream ( ) ;
16- BinaryWriter bw = new BinaryWriter ( ms ) ;
17- return ( bw , ms ) ;
18- }
19-
20- private static byte [ ] WrittenBytes ( MemoryStream ms ) =>
21- ms . ToArray ( ) ;
22-
2313 // ---------------------------------------
2414 // WriteLength() tests
2515 // ---------------------------------------
@@ -28,20 +18,23 @@ private static byte[] WrittenBytes(MemoryStream ms) =>
2818 public void WriteLength_ShouldEncodeSingleByte_WhenLessThan128 ( )
2919 {
3020 // GIVEN
31- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
21+ using MemoryStream ms = new MemoryStream ( ) ;
22+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
3223
3324 // WHEN
3425 bw . WriteLength ( 42 ) ;
3526
3627 // THEN
37- CollectionAssert . AreEqual ( new byte [ ] { 42 } , WrittenBytes ( ms ) ) ;
28+
29+ CollectionAssert . AreEqual ( new byte [ ] { 42 } , ms . ToArray ( ) ) ;
3830 }
3931
4032 [ TestMethod ]
4133 public void WriteLength_ShouldEncodeMultiByte_BigEndianForm ( )
4234 {
4335 // GIVEN
44- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
36+ using MemoryStream ms = new MemoryStream ( ) ;
37+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
4538
4639 // WHEN
4740 // length = 0x0000012C (300 decimal)
@@ -50,10 +43,17 @@ public void WriteLength_ShouldEncodeMultiByte_BigEndianForm()
5043 // THEN
5144 // Prefix = 0x82 (2 bytes follow)
5245 // Then big-endian 01 2C
53- CollectionAssert . AreEqual (
54- new byte [ ] { 0x82 , 0x01 , 0x2C } ,
55- WrittenBytes ( ms )
56- ) ;
46+ try
47+ {
48+ CollectionAssert . AreEqual (
49+ new byte [ ] { 0x82 , 0x01 , 0x2C } ,
50+ ms . ToArray ( )
51+ ) ;
52+ }
53+ finally
54+ {
55+ ms . Dispose ( ) ;
56+ }
5757 }
5858
5959 // ---------------------------------------
@@ -64,34 +64,50 @@ public void WriteLength_ShouldEncodeMultiByte_BigEndianForm()
6464 public void WriteBuffer_ShouldPrefixLength_AndWriteBytes ( )
6565 {
6666 // GIVEN
67- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
67+ using MemoryStream ms = new MemoryStream ( ) ;
68+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
6869 byte [ ] data = new byte [ ] { 0xAA , 0xBB , 0xCC } ;
6970
7071 // WHEN
7172 bw . WriteBuffer ( data ) ;
7273
7374 // THEN
74- CollectionAssert . AreEqual (
75- new byte [ ] { 0x03 , 0xAA , 0xBB , 0xCC } ,
76- WrittenBytes ( ms )
77- ) ;
75+ try
76+ {
77+ CollectionAssert . AreEqual (
78+ new byte [ ] { 0x03 , 0xAA , 0xBB , 0xCC } ,
79+ ms . ToArray ( )
80+ ) ;
81+ }
82+ finally
83+ {
84+ ms . Dispose ( ) ;
85+ }
7886 }
7987
8088 [ TestMethod ]
8189 public void WriteBuffer_WithOffset_ShouldWriteExpectedSegment ( )
8290 {
8391 // GIVEN
84- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
92+ using MemoryStream ms = new MemoryStream ( ) ;
93+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
8594 byte [ ] data = new byte [ ] { 1 , 2 , 3 , 4 , 5 } ;
8695
8796 // WHEN
8897 bw . WriteBuffer ( data , offset : 1 , count : 3 ) ;
8998
9099 // THEN
91- CollectionAssert . AreEqual (
92- new byte [ ] { 0x03 , 2 , 3 , 4 } ,
93- WrittenBytes ( ms )
94- ) ;
100+ try
101+ {
102+ CollectionAssert . AreEqual (
103+ new byte [ ] { 0x03 , 2 , 3 , 4 } ,
104+ ms . ToArray ( )
105+ ) ;
106+ }
107+ finally
108+ {
109+ ms . Dispose ( ) ;
110+ }
95111 }
96112
97113 // ---------------------------------------
@@ -102,7 +118,8 @@ public void WriteBuffer_WithOffset_ShouldWriteExpectedSegment()
102118 public void WriteShortString_ShouldWriteUtf8EncodedWithLength ( )
103119 {
104120 // GIVEN
105- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
121+ using MemoryStream ms = new MemoryStream ( ) ;
122+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
106123 string text = "Hello" ;
107124 byte [ ] utf8 = Encoding . UTF8 . GetBytes ( text ) ;
108125
@@ -114,14 +131,22 @@ public void WriteShortString_ShouldWriteUtf8EncodedWithLength()
114131 . Concat ( utf8 )
115132 . ToArray ( ) ;
116133
117- CollectionAssert . AreEqual ( expected , WrittenBytes ( ms ) ) ;
134+ try
135+ {
136+ CollectionAssert . AreEqual ( expected , ms . ToArray ( ) ) ;
137+ }
138+ finally
139+ {
140+ ms . Dispose ( ) ;
141+ }
118142 }
119143
120144 [ TestMethod ]
121145 public void WriteShortString_ShouldUseSpecifiedEncoding ( )
122146 {
123147 // GIVEN
124- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
148+ using MemoryStream ms = new MemoryStream ( ) ;
149+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
125150 string text = "Å" ;
126151 Encoding enc = Encoding . UTF32 ;
127152 byte [ ] bytes = enc . GetBytes ( text ) ;
@@ -134,14 +159,22 @@ public void WriteShortString_ShouldUseSpecifiedEncoding()
134159 . Concat ( bytes )
135160 . ToArray ( ) ;
136161
137- CollectionAssert . AreEqual ( expected , WrittenBytes ( ms ) ) ;
162+ try
163+ {
164+ CollectionAssert . AreEqual ( expected , ms . ToArray ( ) ) ;
165+ }
166+ finally
167+ {
168+ ms . Dispose ( ) ;
169+ }
138170 }
139171
140172 [ TestMethod ]
141173 public void WriteShortString_ShouldThrow_WhenStringTooLong ( )
142174 {
143175 // GIVEN
144- ( BinaryWriter bw , MemoryStream _ ) = CreateWriter ( ) ;
176+ using MemoryStream ms = new MemoryStream ( ) ;
177+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
145178 string input = new string ( 'x' , 256 ) ; // UTF-8 => 256 bytes
146179
147180 // WHEN–THEN
@@ -162,13 +195,14 @@ public void WriteDate_ShouldEncodeMillisecondsFromUnixEpoch()
162195 long millis = ( long ) ( expected - DateTime . UnixEpoch ) . TotalMilliseconds ;
163196
164197 byte [ ] bytes = BitConverter . GetBytes ( millis ) ;
165- ( BinaryWriter bw , MemoryStream ms ) = CreateWriter ( ) ;
198+ using MemoryStream ms = new MemoryStream ( ) ;
199+ using BinaryWriter bw = new BinaryWriter ( ms ) ;
166200
167201 // WHEN
168202 bw . Write ( expected ) ;
169203
170204 // THEN
171- CollectionAssert . AreEqual ( bytes , WrittenBytes ( ms ) ) ;
205+ CollectionAssert . AreEqual ( bytes , ms . ToArray ( ) ) ;
172206 }
173207 }
174208}
0 commit comments