private void WriteImperativeBits(PackedBitWriter writer, object value, IEnumerable <PropertyInfo> properties, Func <PropertyInfo, BitPair> getBitPair) { foreach (PropertyInfo prop in properties) { if (prop.PropertyType == typeof(bool)) { writer.Write((bool)prop.GetValue(value, null)); } else { BitPair write = getBitPair(prop); int bitRange = write.Count; if (write.Signed) { if (prop.PropertyType == typeof(sbyte)) { writer.WriteSigned(bitRange, (sbyte)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(short)) { writer.WriteSigned(bitRange, (short)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(int)) { writer.WriteSigned(bitRange, (int)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(long)) { writer.WriteSigned(bitRange, (long)prop.GetValue(value, null)); } else { throw CodePath.Unreachable; } } else { if (prop.PropertyType == typeof(sbyte)) { writer.Write(bitRange, (sbyte)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(short)) { writer.Write(bitRange, (short)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(int)) { writer.Write(bitRange, (int)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(long)) { writer.Write(bitRange, (long)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(byte)) { writer.Write(bitRange, (byte)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(ushort)) { writer.Write(bitRange, (ushort)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(uint)) { writer.Write(bitRange, (uint)prop.GetValue(value, null)); } else if (prop.PropertyType == typeof(ulong)) { writer.Write(bitRange, (ulong)prop.GetValue(value, null)); } else { throw CodePath.Unreachable; } } } } }
public void ArgumentValidation() { PackedBitWriter writer = new PackedBitWriter(); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, byte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, ushort.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, uint.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, ulong.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, byte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, ushort.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, uint.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, ulong.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, byte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, ushort.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, uint.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, ulong.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, sbyte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, short.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, int.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, long.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, sbyte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, short.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, int.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, long.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, sbyte.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, short.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, int.MaxValue)); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, long.MaxValue)); Assert.ThrowsExact <ArgumentNullException>(() => writer.Write(null as string)); Assert.ThrowsExact <ArgumentNullException>(() => writer.Write(null as string, Encoding.UTF8)); Assert.ThrowsExact <ArgumentNullException>(() => writer.Write("string", null)); Assert.ThrowsExact <BitCountException>(() => writer.Write(-1, new byte[] { 0 })); Assert.ThrowsExact <BitCountException>(() => writer.Write(0, new byte[] { 0 })); Assert.ThrowsExact <BitCountException>(() => writer.Write(65, new byte[] { 0 })); Assert.ThrowsExact <ArgumentNullException>(() => writer.Write(1, null as byte[])); Assert.ThrowsExact <ArgumentException>(() => writer.Write(1, new byte[0])); Assert.ThrowsExact <ArgumentException>(() => writer.Write(1, new byte[0])); }