public async Task MoveStreamAsync() { using (HashedStream hashOut = new HashedStream(new MemoryStream())) using (StreamWriter tw = new StreamWriter(hashOut)) { string asciiOnlyString = "Something or other"; await tw.WriteAsync(asciiOnlyString); await tw.FlushAsync(); Assert.Equal(asciiOnlyString.Length, hashOut.Length); Assert.Equal(asciiOnlyString.Length, hashOut.Position); Assert.True(hashOut.CanSeek); hashOut.Seek(0, SeekOrigin.Begin); Assert.True(hashOut.WasMoved); } using (HashedStream hashOut = new HashedStream(new MemoryStream())) using (StreamWriter tw = new StreamWriter(hashOut)) { await tw.WriteAsync("Something or other"); hashOut.SetLength(0); Assert.True(hashOut.WasMoved); } using (HashedStream hashOut = new HashedStream(new MemoryStream())) using (StreamWriter tw = new StreamWriter(hashOut)) { await tw.WriteAsync("Something or other"); Assert.True(hashOut.CanSeek); hashOut.Position = 0; Assert.True(hashOut.WasMoved); } }
public void TimeoutWrite() { using (Stream stm = new MockStream()) using (HashedStream hs = new HashedStream(stm)) { stm.WriteTimeout = 123; Assert.Equal(123, hs.WriteTimeout); hs.WriteTimeout = 456; Assert.Equal(456, stm.WriteTimeout); } }
public void LongDiffConst() { FileStream fs = GetFileStream(); Stream ms = Stream.Null; HashedStream hashIn = new HashedStream(fs, 42L, 53L, 23L, 34L); HashedStream hashOut = new HashedStream(ms, 42L, 53L, 23L, 34L); WriteOut(hashIn, hashOut); Assert.False(hashIn.WasMoved); Assert.False(hashIn.ReadHash128 == hashOut.WriteHash128); Assert.False(hashIn.ReadHash32 == hashOut.WriteHash32); Assert.False(hashIn.ReadHash64 == hashOut.WriteHash64); }
public void DefaultConst() { FileStream fs = GetFileStream(); Stream ms = Stream.Null; HashedStream hashIn = new HashedStream(fs); HashedStream hashOut = new HashedStream(ms); WriteOut(hashIn, hashOut); Assert.False(hashIn.WasMoved); Assert.True(hashIn.ReadHash128 == hashOut.WriteHash128); Assert.True(hashIn.ReadHash32 == hashOut.WriteHash32); Assert.True(hashIn.ReadHash64 == hashOut.WriteHash64); }
public async Task LongConstAsync() { FileStream fs = GetFileStream(); Stream ms = Stream.Null; HashedStream hashIn = new HashedStream(fs, 42L, 53L); HashedStream hashOut = new HashedStream(ms, 42L, 53L); await WriteOutAsync(hashIn, hashOut); Assert.False(hashIn.WasMoved); Assert.True(hashIn.ReadHash128 == hashOut.WriteHash128); Assert.True(hashIn.ReadHash32 == hashOut.WriteHash32); Assert.True(hashIn.ReadHash64 == hashOut.WriteHash64); }
public void HashExternal64() { long hash; using (HashedStream hs = new HashedStream(GetFileStream())) { hash = hs.SpookyHash64(); Assert.Equal(hash, hs.ReadHash64); } using (FileStream fs = GetFileStream()) { Assert.Equal((ulong)hash, fs.SpookyHash64(0xDEADBEEFDEADBEEF)); } using (FileStream fs = GetFileStream()) { Assert.Equal(hash, unchecked (fs.SpookyHash64((long)0xDEADBEEFDEADBEEF))); } }
public async Task HashExternal64Async() { long hash; using (HashedStream hs = new HashedStream(GetFileStream())) { hash = await SpookyHasher.SpookyHash64Async(hs); Assert.Equal(hash, hs.ReadHash64); } using (FileStream fs = GetFileStream()) { Assert.Equal((ulong)hash, await fs.SpookyHash64Async(0xDEADBEEFDEADBEEF)); } using (FileStream fs = GetFileStream()) { Assert.Equal(hash, unchecked (await fs.SpookyHash64Async((long)0xDEADBEEFDEADBEEF))); } }
public void HashExternal128() { HashCode128 hash; using (HashedStream hs = new HashedStream(GetFileStream())) { hash = hs.SpookyHash128(); Assert.Equal(hash, hs.ReadHash128); } using (FileStream fs = GetFileStream()) { Assert.Equal(hash, fs.SpookyHash128(0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF)); } using (FileStream fs = GetFileStream()) { Assert.Equal( hash, unchecked (fs.SpookyHash128((long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF))); } }
public async Task HashExternal128Async() { HashCode128 hash; using (HashedStream hs = new HashedStream(GetFileStream())) { hash = await hs.SpookyHash128Async(); Assert.Equal(hash, hs.ReadHash128); } using (FileStream fs = GetFileStream()) { Assert.Equal(hash, await fs.SpookyHash128Async(0xDEADBEEFDEADBEEF, 0xDEADBEEFDEADBEEF)); } using (FileStream fs = GetFileStream()) { Assert.Equal( hash, await unchecked (fs.SpookyHash128Async((long)0xDEADBEEFDEADBEEF, (long)0xDEADBEEFDEADBEEF))); } }
private static async Task WriteOutAsync(HashedStream inStr, HashedStream outStr) { Random rand = new Random(); using (inStr) using (outStr) { Assert.True(inStr.CanRead); Assert.True(outStr.CanWrite); if (inStr.CanTimeout) { Assert.NotEqual(0, inStr.ReadTimeout); } if (outStr.CanTimeout) { Assert.NotEqual(0, outStr.WriteTimeout); } for (;;) { byte[] buffer = new byte[rand.Next(1, 20000)]; int read = await inStr.ReadAsync(buffer, 0, buffer.Length); if (read == 0) { return; } await outStr.WriteAsync(buffer, 0, read); int by = inStr.ReadByte(); if (by == -1) { return; } outStr.WriteByte((byte)by); } } }