88using System . Diagnostics . CodeAnalysis ;
99using System . IO ;
1010using System . Text ;
11+ using System . Threading . Tasks ;
1112
1213namespace Lucene . Net . Support . IO
1314{
@@ -33,6 +34,8 @@ public class TestStreamExtensions : LuceneTestCase
3334 {
3435 private Stream stream ;
3536
37+ private static readonly string unihw = "\u0048 \u0065 \u006C \u006C \u006F \u0020 \u0057 \u006F \u0072 \u006C \u0064 " ;
38+
3639 private const string fileString = "Test_All_Tests\n Test_java_io_BufferedInputStream\n Test_java_io_BufferedOutputStream\n Test_java_io_ByteArrayInputStream\n Test_java_io_ByteArrayOutputStream\n Test_DataInputStream\n " ;
3740
3841 [ Test ]
@@ -115,6 +118,98 @@ public void TestReadInt64()
115118 Assert . AreEqual ( 9875645283333L , stream . ReadInt64 ( ) , "Incorrect long read" ) ;
116119 }
117120
121+ // Additional async tests
122+
123+ [ Test ]
124+ // LUCENENET note: adapted from test_writeInt()
125+ public async Task TestWriteInt32BigEndianAsync ( )
126+ {
127+ await stream . WriteInt32BigEndianAsync ( 9087589 ) ;
128+ // Reset the stream so we can read back
129+ ResetStreamForReading ( ) ;
130+ int c = await stream . ReadInt32BigEndianAsync ( ) ;
131+ Assert . AreEqual ( 9087589 , c , "Incorrect int written (async)" ) ;
132+ }
133+
134+ [ Test ]
135+ // LUCENENET note: adapted from test_writeLong()
136+ public async Task TestWriteInt64BigEndianAsync ( )
137+ {
138+ await stream . WriteInt64BigEndianAsync ( 908755555456L ) ;
139+ // Reset the stream so we can read back
140+ ResetStreamForReading ( ) ;
141+ long c = await stream . ReadInt64BigEndianAsync ( ) ;
142+ Assert . AreEqual ( 908755555456L , c , "Incorrect long written (async)" ) ;
143+ }
144+
145+ [ Test ]
146+ // LUCENENET note: adapted from test_writeUTF()
147+ public async Task TestWriteUTFAsync ( )
148+ {
149+ await stream . WriteUTFAsync ( unihw ) ;
150+ // Reset the stream so we can read back
151+ ResetStreamForReading ( ) ;
152+ string result = await stream . ReadUTFAsync ( ) ;
153+ Assert . AreEqual ( unihw , result , "Incorrect string written (async)" ) ;
154+ }
155+
156+ [ Test ]
157+ // LUCENENET note: adapted from test_readInt()
158+ public async Task TestReadInt32BigEndianAsync ( )
159+ {
160+ await stream . WriteInt32BigEndianAsync ( 768347202 ) ;
161+ // Reset the stream so we can read back
162+ ResetStreamForReading ( ) ;
163+ int result = await stream . ReadInt32BigEndianAsync ( ) ;
164+ Assert . AreEqual ( 768347202 , result , "Incorrect int read (async)" ) ;
165+ }
166+
167+ [ Test ]
168+ // LUCENENET note: adapted from test_readLong()
169+ public async Task TestReadInt64BigEndianAsync ( )
170+ {
171+ await stream . WriteInt64BigEndianAsync ( 9875645283333L ) ;
172+ // Reset the stream so we can read back
173+ ResetStreamForReading ( ) ;
174+ long result = await stream . ReadInt64BigEndianAsync ( ) ;
175+ Assert . AreEqual ( 9875645283333L , result , "Incorrect long read (async)" ) ;
176+ }
177+
178+ [ Test ]
179+ // LUCENENET note: adapted from test_readUTF()
180+ public async Task TestReadUTFAsync ( )
181+ {
182+ await stream . WriteUTFAsync ( unihw ) ;
183+
184+ // Check that the length was written correctly (UTF length + 2 bytes for length header)
185+ long expectedStreamLength = CalculateExpectedUTFStreamLength ( unihw ) ;
186+ Assert . AreEqual ( expectedStreamLength , stream . Length , "Failed to write string in UTF format" ) ;
187+
188+ // Reset and read the string
189+ ResetStreamForReading ( ) ;
190+ string result = await stream . ReadUTFAsync ( ) ;
191+ Assert . AreEqual ( unihw , result , "Incorrect string read (async)" ) ;
192+ }
193+
194+ /// <summary>
195+ /// Helper method to calculate expected UTF stream length for validation
196+ /// Matches DataOutput.writeUTF() spec (Java)
197+ /// </summary>
198+ private long CalculateExpectedUTFStreamLength ( string value )
199+ {
200+ long utfCount = 0 ;
201+ foreach ( char ch in value )
202+ {
203+ if ( ch > 0 && ch <= 127 )
204+ utfCount ++ ;
205+ else if ( ch <= 2047 )
206+ utfCount += 2 ;
207+ else
208+ utfCount += 3 ;
209+ }
210+ return utfCount + 2 ; // +2 for the 2-byte length header
211+ }
212+
118213 private void ResetStreamForReading ( ) // LUCENENET - was "OpenDataInputStream" in Harmony tests
119214 {
120215 // LUCENENET specific - in the Harmony tests, there were separate streams
0 commit comments