public void TestMixOfAllTypes() { // we need a buffer that is larger than the 32 byte one that is // used for the round trip tests: byte[] bigBuffer = new byte[1024]; var spe = new SpanBinaryEditor(bigBuffer); spe.Seek(16, SeekOrigin.Begin); Assert.Equal(16, spe.Position); int int32 = GetRandom <int>(); bool boolean = GetRandom <bool>(); byte octet = GetRandom <byte>(); short int16 = GetRandom <short>(); long int64 = GetRandom <long>(); long int64be = GetRandom <long>(); ushort uint16 = GetRandom <ushort>(); uint uint32 = GetRandom <uint>(); ulong uint64 = GetRandom <ulong>(); ulong uint64be = GetRandom <ulong>(); spe.WriteInt32(int32); spe.WriteBoolean(boolean); spe.WriteByte(octet); spe.WriteInt16(int16); spe.WriteInt64(int64); spe.WriteInt64BE(int64be); spe.WriteUInt16(uint16); spe.WriteUInt32(uint32); spe.WriteUInt64(uint64); spe.WriteUInt64BE(uint64be); spe.Seek(-20, SeekOrigin.Current); Assert.Equal(uint32, spe.ReadUInt32()); Assert.Equal(uint64, spe.ReadUInt64()); Assert.Equal(uint64be, spe.ReadUInt64BE()); spe.Seek(-spe.Position, SeekOrigin.Current); Assert.Equal(0, spe.Position); spe.Seek(16, SeekOrigin.Current); Assert.Equal(16, spe.Position); Assert.Equal(int32, spe.ReadInt32()); if (boolean) { // same special handling of boolean as in RoundTripBoolean Assert.True(spe.ReadBoolean()); } else { Assert.False(spe.ReadBoolean()); } Assert.Equal(octet, spe.ReadByte()); Assert.Equal(int16, spe.ReadInt16()); Assert.Equal(int64, spe.ReadInt64()); Assert.Equal(int64be, spe.ReadInt64BE()); Assert.Equal(uint16, spe.ReadUInt16()); }
public void RoundTripInt64() { Check(0); Check(1); Check(-1); Check(10); Check(-10); Check(sbyte.MaxValue); Check(sbyte.MinValue); Check(byte.MaxValue); Check(short.MaxValue); Check(short.MinValue); Check(ushort.MaxValue); Check(int.MaxValue); Check(int.MinValue); Check(uint.MaxValue); Check(long.MaxValue); Check(long.MinValue); for (int i = 0; i < 15; ++i) { Check(GetRandom <long>()); } void Check(Int64 toWrite) { Array.Clear(buffer, 0, buffer.Length); var sbe = new SpanBinaryEditor(buffer); sbe.WriteInt64(toWrite); sbe.Seek(0, SeekOrigin.Begin); Int64 actual = sbe.ReadInt64(); CheckEquality(toWrite, actual); stream.Seek(0, SeekOrigin.Begin); CheckEquality(toWrite, binary.ReadInt64()); } }