public void Align() { using (TempFile temp = new TempFile()) { Guid guid = Guid.NewGuid(); byte[] bytes = guid.ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(0, reader.Position); reader.Align(2); Assert.Equal(0, reader.Position); reader.Align(4); Assert.Equal(0, reader.Position); reader.Align(8); Assert.Equal(0, reader.Position); reader.Position = 1; reader.Align(2); Assert.Equal(2, reader.Position); reader.Align(4); Assert.Equal(4, reader.Position); reader.Align(8); Assert.Equal(8, reader.Position); } } }
public void ReadCStringWide() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { (byte)'T', 0, (byte)'e', 0, (byte)'s', 0, (byte)'t', 0, 0, 0 }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); Assert.Equal("Test", reader.ReadCStringWide().ToString()); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadAllBytes() { using (TempFile temp = new TempFile()) { byte[] bytes = Guid.NewGuid().ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(bytes, reader.ReadAllBytes()); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadBString() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { 4, 0, (byte)'T', (byte)'e', (byte)'s', (byte)'t' }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal("Test", reader.ReadBString().String); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadUintArray() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 }; uint[] validation = new uint[] { 127, uint.MaxValue, 256 * 256 * 256U }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(validation, reader.ReadUintArray(validation.Length)); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadDouble() { using (TempFile temp = new TempFile()) { double value = 42.24; byte[] bytes = BitConverter.GetBytes(value); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(value, reader.ReadDouble()); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadUint() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); Assert.Equal(127U, reader.ReadUint()); Assert.Equal(uint.MaxValue, reader.ReadUint()); Assert.Equal(256 * 256 * 256U, reader.ReadUint()); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadInt() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { 127, 0, 0, 0, 255, 255, 255, 255, 0, 0, 0, 1 }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(127, reader.ReadInt()); Assert.Equal(-1, reader.ReadInt()); Assert.Equal(256 * 256 * 256, reader.ReadInt()); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadByte() { using (TempFile temp = new TempFile()) { byte[] bytes = new byte[] { 127, 128, 129, 200 }; File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); for (int i = 0; i < bytes.Length; i++) { Assert.Equal(bytes[i], reader.ReadByte()); } Assert.Equal(0, reader.BytesRemaining); } } }
public unsafe void Move() { using (TempFile temp = new TempFile()) { byte[] bytes = Guid.NewGuid().ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); Assert.Equal(bytes.Length, reader.Length); reader.Move((uint)bytes.Length / 2); Assert.Equal(bytes.Length / 2, reader.BytesRemaining); reader.Position += bytes.Length / 2; Assert.Equal(0, reader.BytesRemaining); } } }
public unsafe void Duplicate() { using (TempFile temp = new TempFile()) { byte[] bytes = Guid.NewGuid().ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); reader.Position = 5; IBinaryReader reader2 = reader.Duplicate(); Assert.Equal(reader.Position, reader2.Position); reader.Position++; Assert.NotEqual(reader.Position, reader2.Position); reader2.Position++; Assert.Equal(reader.Position, reader2.Position); reader2.Position++; Assert.NotEqual(reader.Position, reader2.Position); } } }
public unsafe void ReadBytes() { using (TempFile temp = new TempFile()) { byte[] bytes = Guid.NewGuid().ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { IBinaryReader reader = new MemoryLoadedFileReader(file).ReadSubstream(); byte[] readBytes = new byte[reader.BytesRemaining]; fixed(byte *b = readBytes) { reader.ReadBytes(b, (uint)readBytes.Length); } Assert.Equal(bytes, readBytes); Assert.Equal(0, reader.BytesRemaining); } } }
public void ReadSubstream() { using (TempFile temp = new TempFile()) { Guid guid = Guid.NewGuid(); byte[] bytes = guid.ToByteArray(); File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); IBinaryReader reader2 = reader.ReadSubstream(); Assert.Equal(0, reader2.Position); Assert.Equal(reader.Length, reader2.Length); Assert.Equal(0, reader.BytesRemaining); reader2.Position = 1; Assert.Equal(1, reader2.Position); Assert.Equal(0, reader.BytesRemaining); Assert.NotEqual(reader.Position, reader2.Position); } } }
public unsafe void ReadDecimal() { using (TempFile temp = new TempFile()) { decimal[] values = new decimal[] { new decimal(42.24) }; byte[] bytes = new byte[16]; fixed(decimal *d = values) fixed(byte *b = bytes) { MemoryBuffer.MemCpy(b, d, 16); } File.WriteAllBytes(temp.Path, bytes); using (MemoryLoadedFile file = new MemoryLoadedFile(temp.Path)) { MemoryLoadedFileReader reader = new MemoryLoadedFileReader(file); Assert.Equal(values[0], reader.ReadDecimal()); Assert.Equal(0, reader.BytesRemaining); } } }