public void ReadWritePrimitives() { var s = new ReusableStream(100); var rng = new Random(); var b = (byte)rng.Next(255); var sb = (sbyte)rng.Next(255); var sh = (short)rng.Next(short.MinValue, short.MaxValue); var ush = (ushort)rng.Next(ushort.MaxValue); var i = rng.Next(int.MinValue, int.MaxValue); var ui = (uint)rng.Next(int.MinValue, int.MaxValue); var l = (long)RandomULong(rng); var ul = RandomULong(rng); var f = (float)rng.NextDouble(); var d = rng.NextDouble(); var c = (char)rng.Next(char.MinValue, char.MaxValue); var t = DateTime.UtcNow; var g = Guid.NewGuid(); var expectedLength = 0; s.Write(b); expectedLength += 1; Assert.AreEqual(expectedLength, s.Length); s.Write(sb); expectedLength += 1; Assert.AreEqual(expectedLength, s.Length); s.Write(sh); expectedLength += 2; Assert.AreEqual(expectedLength, s.Length); s.Write(ush); expectedLength += 2; Assert.AreEqual(expectedLength, s.Length); s.Write(i); expectedLength += 4; Assert.AreEqual(expectedLength, s.Length); s.Write(ui); expectedLength += 4; Assert.AreEqual(expectedLength, s.Length); s.Write(l); expectedLength += 8; Assert.AreEqual(expectedLength, s.Length); s.Write(ul); expectedLength += 8; Assert.AreEqual(expectedLength, s.Length); s.Write(f); expectedLength += 4; Assert.AreEqual(expectedLength, s.Length); s.Write(d); expectedLength += 8; Assert.AreEqual(expectedLength, s.Length); s.Write(c); expectedLength += 2; Assert.AreEqual(expectedLength, s.Length); s.Write(false); expectedLength += 1; Assert.AreEqual(expectedLength, s.Length); s.Write(true); expectedLength += 1; Assert.AreEqual(expectedLength, s.Length); s.Write(t); expectedLength += 8; Assert.AreEqual(expectedLength, s.Length); s.Write(g); expectedLength += 16; Assert.AreEqual(expectedLength, s.Length); s.ResetForReading(); Assert.AreEqual(b, s.ReadUInt8()); Assert.AreEqual(sb, s.ReadInt8()); Assert.AreEqual(sh, s.ReadInt16()); Assert.AreEqual(ush, s.ReadUInt16()); Assert.AreEqual(i, s.ReadInt32()); Assert.AreEqual(ui, s.ReadUInt32()); Assert.AreEqual(l, s.ReadInt64()); Assert.AreEqual(ul, s.ReadUInt64()); Assert.AreEqual(f, s.ReadSingle()); Assert.AreEqual(d, s.ReadDouble()); Assert.AreEqual(c, s.ReadChar()); Assert.AreEqual(false, s.ReadBoolean()); Assert.AreEqual(true, s.ReadBoolean()); Assert.AreEqual(t, s.ReadDateTime()); Assert.AreEqual(g, s.ReadGuid()); // verify that we read to the end Assert.AreEqual(s.Length, s.Position); s.ResetForReading(); Assert.AreEqual((int)b, s.ReadByte()); }