public void NotImplementedMethods() { using (var ms = new MemoryStream()) { // ReSharper disable AccessToDisposedClosure using (var writer = new BlockStream(Zstd, ms, CompressionMode.Compress, true)) { Assert.Throws <NotSupportedException>(delegate { // ReSharper disable once UnusedVariable long len = writer.Length; }); Assert.Throws <NotSupportedException>(delegate { writer.SetLength(10); }); Assert.Throws <NotSupportedException>(delegate { writer.Seek(0, SeekOrigin.Begin); }); Assert.Throws <NotSupportedException>(delegate { writer.Position = 0; }); } // ReSharper restore AccessToDisposedClosure } }
public void ReadWriteBlocks() { var bs = new BlockStream(); var maxSize = 16 * 1024; for (int i = 0; i < maxSize; i++) { var data = new byte[i]; for (int j = 0; j < i; j++) { data[j] = (byte)j; } bs.Write(data, 0, data.Length); } bs.Seek(0, SeekOrigin.Begin); var buffer = new byte[maxSize]; for (int i = 0; i < maxSize; i++) { for (var j = 0; j < i; j++) { buffer[i] = (byte)(j + 1); } bs.Read(buffer, 0, i); for (int j = 0; j < i; j++) { Assert.Equal((byte)j, buffer[j]); } } }
public void Exceptions() { Assert.Throws <ArgumentException>(() => new BlockStream(-10)); Assert.Throws <ArgumentException>(() => new BlockStream(0, -10)); Assert.Throws <ArgumentException>(() => new BlockStream(0, 0)); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(-1); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength((long)int.MaxValue + 1); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Position = -1; }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Position = (long)int.MaxValue + 1; }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek(-1, SeekOrigin.Begin); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Position = -1; }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek(5000, SeekOrigin.Begin); s.Seek(-5001, SeekOrigin.Current); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek(-5001, SeekOrigin.End); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek((long)int.MaxValue + 1, SeekOrigin.Begin); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek(5000, SeekOrigin.Begin); s.Seek((long)int.MaxValue - 5000 + 1, SeekOrigin.Current); }); Assert.Throws <IOException>( () => { var s = new BlockStream(); s.SetLength(5000); s.Seek((long)int.MaxValue - 5000 + 1, SeekOrigin.End); }); }
public void Seek() { BlockStream s = new BlockStream(); byte[] r = new byte[10]; byte[] w = new byte[] { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9 }; byte[] c = new byte[] { 0, 0, 0, 0, 4, 5, 6, 7, 8, 9 }; for (int i = 0; i < 100000; i++) { w[0] = (byte)(i << 24); w[1] = (byte)(i << 16); w[2] = (byte)(i << 8); w[3] = (byte)(i); s.Write(w, 0, 10); } for (int i = 0; i < 100000; i++) { int pos = i; c[0] = (byte)(pos << 24); c[1] = (byte)(pos << 16); c[2] = (byte)(pos << 8); c[3] = (byte)(pos); s.Seek(pos * 10, SeekOrigin.Begin); Assert.Equal((long)(pos * 10), s.Position); Assert.Equal(10, s.Read(r, 0, 10)); Assert.Equal(c, r); } for (int i = 0; i < 100000; i++) { int pos = i; c[0] = (byte)(pos << 24); c[1] = (byte)(pos << 16); c[2] = (byte)(pos << 8); c[3] = (byte)(pos); s.Position = 50000 * 10; s.Seek(pos * 10 - 50000 * 10, SeekOrigin.Current); Assert.Equal((long)(pos * 10), s.Position); Assert.Equal(10, s.Read(r, 0, 10)); Assert.Equal(c, r); } for (int i = 0; i < 100000; i++) { int pos = i; c[0] = (byte)(pos << 24); c[1] = (byte)(pos << 16); c[2] = (byte)(pos << 8); c[3] = (byte)(pos); s.Seek(-(100000 * 10 - pos * 10), SeekOrigin.End); Assert.Equal((long)(pos * 10), s.Position); Assert.Equal(10, s.Read(r, 0, 10)); Assert.Equal(c, r); } }
public void BlockStream_Exceptions() { BlockStream s; try { s = new BlockStream(-10); Assert.Fail(); } catch { } try { s = new BlockStream(0, -10); Assert.Fail(); } catch { } try { s = new BlockStream(0, 0); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(-1); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength((long)int.MaxValue + 1); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Position = -1; Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Position = (long)int.MaxValue + 1; Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek(-1, SeekOrigin.Begin); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Position = -1; Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek(5000, SeekOrigin.Begin); s.Seek(-5001, SeekOrigin.Current); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek(-5001, SeekOrigin.End); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek((long)int.MaxValue + 1, SeekOrigin.Begin); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek(5000, SeekOrigin.Begin); s.Seek((long)int.MaxValue - 5000 + 1, SeekOrigin.Current); Assert.Fail(); } catch { } try { s = new BlockStream(); s.SetLength(5000); s.Seek((long)int.MaxValue - 5000 + 1, SeekOrigin.End); Assert.Fail(); } catch { } }