public new void GetHashCode() { var obj1 = new Testing(5, "ABC"); var obj2 = new Testing(3, "NA"); var obj3 = new Testing(2, "ABCDE"); var obj4 = new Testing(5, "NA"); var obj5 = new Testing(7, null); var comparer = new EqualityComparerStateful <int>((lhs, rhs) => lhs == rhs, obj => obj.GetHashCode()); Assert.Equal(comparer.GetHashCode(5), 5.GetHashCode()); Assert.Equal(comparer.GetHashCode(3), 3.GetHashCode()); Assert.Equal(comparer.GetHashCode(67), 67.GetHashCode()); var stringComparer = new EqualityComparerStateful <string>((lhs, rhs) => lhs == rhs); Assert.Equal(stringComparer.GetHashCode("ABC"), "ABC".GetHashCode()); Assert.Equal(stringComparer.GetHashCode("CBA"), "CBA".GetHashCode()); Assert.Equal(stringComparer.GetHashCode("abcd"), "abcd".GetHashCode()); Assert.Equal(stringComparer.GetHashCode(null), 0); var objComparer = new EqualityComparerStateful <Testing>((lhs, rhs) => lhs.A == rhs.A, obj => obj.A.GetHashCode() * 2); Assert.Equal(objComparer.GetHashCode(obj1), obj1.A.GetHashCode() * 2); Assert.Equal(objComparer.GetHashCode(obj2), obj2.A.GetHashCode() * 2); Assert.Equal(objComparer.GetHashCode(obj3), obj3.A.GetHashCode() * 2); Assert.Equal(objComparer.GetHashCode(obj4), obj4.A.GetHashCode() * 2); Assert.Equal(objComparer.GetHashCode(obj5), obj5.A.GetHashCode() * 2); }
public void LargeClassTest() { byte[] array = { 0xFF, 0xFF, 0xFF, 0xFF, 0xFF, 0xD7, 0xBF, 0xC0, 0xC9, 0xFD, 0xA3, 0x00, 0x00, 0x00, 0x00, 0x0D, }; var reader = new PackedBitReader(array); var serializer = new PackedBitSerializer(typeof(BitPackingTest)); var result = serializer.Deserialize(reader) as BitPackingTest; var trueValues = typeof(BitPackingTest). GetProperties(BindingFlags.Instance | BindingFlags.Public). Where(p => p.Name.StartsWith("ShouldBeTrue")). Select(p => Tuple.Create(p.Name, (bool)p.GetValue(result))); Assert.All(trueValues, tv => tv.Item2); var falseValues = typeof(BitPackingTest). GetProperties(BindingFlags.Instance | BindingFlags.Public). Where(p => p.Name.StartsWith("ShouldBeFalse")). Select(p => Tuple.Create(p.Name, (bool)p.GetValue(result))); Assert.All(falseValues, fv => !fv.Item2); Assert.Equal(result.ShouldBe0, (byte)0); Assert.Equal(result.ShouldBe0Second, (uint)0); Assert.Equal(result.ShouldBe0Third, (byte)0); Assert.Equal(result.ShouldBe4, (byte)4); }
public void As() { Assert.ThrowsExact <ArgumentNullException>(() => EnumExtensions.As <int>(null)); Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <double>(EnumVanilla.None)); Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <Guid>(EnumVanilla.None)); Assert.ThrowsExact <InvalidOperationException>(() => EnumExtensions.As <DateTime>(EnumVanilla.None)); int value = 0; value = 0; Assert.DoesNotThrow(() => value = EnumVanilla.One.As <sbyte>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = EnumVanilla.One.As <short>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = EnumVanilla.One.As <int>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <long>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = EnumVanilla.One.As <byte>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = EnumVanilla.One.As <ushort>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <uint>()); Assert.Equal(value, 1); value = 0; Assert.DoesNotThrow(() => value = (int)EnumVanilla.One.As <ulong>()); Assert.Equal(value, 1); value = 0; Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <byte>()); Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <sbyte>()); Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <ushort>()); Assert.ThrowsExact <OverflowException>(() => value = EnumVanilla.Huge.As <short>()); Assert.ThrowsExact <OverflowException>(() => value = ((EnumVanilla)(-1)).As <ushort>()); value = 0; Assert.DoesNotThrow(() => value = default(EnumNoValues).As <byte>()); Assert.Equal(value, 0); }
public new void GetHashCode() { var obj1 = new Testing(5, "ABC"); var obj2 = new Testing(3, "NA"); var obj3 = new Testing(2, "ABCDE"); var obj4 = new Testing(5, "NA"); var obj5 = new Testing(7, null); var comparer = new EqualityComparerStateless <Testing, int>(obj => obj.A); Assert.Equal(comparer.GetHashCode(obj1), obj1.A.GetHashCode()); Assert.Equal(comparer.GetHashCode(obj2), obj2.A.GetHashCode()); Assert.Equal(comparer.GetHashCode(obj3), obj3.A.GetHashCode()); Assert.Equal(comparer.GetHashCode(obj4), obj4.A.GetHashCode()); Assert.Equal(comparer.GetHashCode(obj5), obj5.A.GetHashCode()); var stringComparer = new EqualityComparerStateless <Testing, string>(obj => obj.B); Assert.Equal(stringComparer.GetHashCode(obj1), obj1.B.GetHashCode()); Assert.Equal(stringComparer.GetHashCode(obj2), obj2.B.GetHashCode()); Assert.Equal(stringComparer.GetHashCode(obj3), obj3.B.GetHashCode()); Assert.Equal(stringComparer.GetHashCode(obj4), obj4.B.GetHashCode()); Assert.Equal(stringComparer.GetHashCode(obj5), 0); }
public void Basics() { PackedBitReader reader = null; bool boolValue = false; sbyte signedByte = 0; byte unsignedByte = 0; short signedShort = 0; ushort unsignedShort = 0; int signedInt = 0; uint unsignedInt = 0; long signedLong = 0; ulong unsignedLong = 0; string stringValue = null; double doubleValue = 0; float floatValue = 0; bool success = false; Assert.DoesNotThrow(() => reader = new PackedBitReader(sPackedBitWriterOutput)); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(4, out signedByte)); Assert.True(success); Assert.Equal(signedByte, (sbyte)3); Assert.DoesNotThrow(() => success = reader.TryRead(4, out unsignedByte)); Assert.True(success); Assert.Equal(unsignedByte, (byte)4); Assert.DoesNotThrow(() => success = reader.TryRead(6, out signedShort)); Assert.True(success); Assert.Equal(signedShort, (short)13); Assert.DoesNotThrow(() => success = reader.TryRead(6, out unsignedShort)); Assert.True(success); Assert.Equal(unsignedShort, (ushort)14); Assert.DoesNotThrow(() => success = reader.TryRead(6, out signedInt)); Assert.True(success); Assert.Equal(signedInt, (int)23); Assert.DoesNotThrow(() => success = reader.TryRead(6, out unsignedInt)); Assert.True(success); Assert.Equal(unsignedInt, (uint)24); Assert.DoesNotThrow(() => success = reader.TryRead(8, out signedLong)); Assert.True(success); Assert.Equal(signedLong, (long)33); Assert.DoesNotThrow(() => success = reader.TryRead(8, out unsignedLong)); Assert.True(success); Assert.Equal(unsignedLong, (ulong)34); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.False(boolValue); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.True(success); Assert.True(boolValue); Assert.DoesNotThrow(() => success = reader.TryReadSigned(4, out signedByte)); Assert.True(success); Assert.Equal(signedByte, (sbyte)-3); Assert.DoesNotThrow(() => success = reader.TryRead(out doubleValue)); Assert.True(success); Assert.Equal(doubleValue, 2.4d); Assert.DoesNotThrow(() => success = reader.TryRead(out floatValue)); Assert.True(success); Assert.Equal(floatValue, 88.4f); Assert.DoesNotThrow(() => success = reader.TryReadSigned(6, out signedShort)); Assert.True(success); Assert.Equal(signedShort, (short)-13); Assert.DoesNotThrow(() => success = reader.TryReadSigned(6, out signedInt)); Assert.True(success); Assert.Equal(signedInt, (int)-23); Assert.DoesNotThrow(() => success = reader.TryReadSigned(8, out signedLong)); Assert.True(success); Assert.Equal(signedLong, (long)-33); Assert.DoesNotThrow(() => success = reader.TryRead(out stringValue)); Assert.True(success); Assert.Equal(stringValue, "datà"); Assert.DoesNotThrow(() => success = reader.TryRead(out stringValue, Encoding.ASCII)); Assert.True(success); Assert.Equal(stringValue, "data"); Assert.DoesNotThrow(() => success = reader.TryRead(2, out signedByte)); Assert.True(success); Assert.Equal(signedByte, (sbyte)3); Assert.DoesNotThrow(() => success = reader.TryRead(out boolValue)); Assert.False(success); }
public void Basics() { PackedBitWriter writer = null; Assert.DoesNotThrow(() => writer = new PackedBitWriter()); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(4, (sbyte)3)); Assert.DoesNotThrow(() => writer.Write(4, (byte)4)); Assert.DoesNotThrow(() => writer.Write(6, (short)13)); Assert.DoesNotThrow(() => writer.Write(6, (ushort)14)); Assert.DoesNotThrow(() => writer.Write(6, (int)23)); Assert.DoesNotThrow(() => writer.Write(6, (uint)24)); Assert.DoesNotThrow(() => writer.Write(8, (long)33)); Assert.DoesNotThrow(() => writer.Write(8, (ulong)34)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(false)); Assert.DoesNotThrow(() => writer.Write(true)); Assert.DoesNotThrow(() => writer.WriteSigned(4, (sbyte)-3)); Assert.DoesNotThrow(() => writer.Write(2.4d)); Assert.DoesNotThrow(() => writer.Write(88.4f)); Assert.DoesNotThrow(() => writer.WriteSigned(6, (short)-13)); Assert.DoesNotThrow(() => writer.WriteSigned(6, (int)-23)); Assert.DoesNotThrow(() => writer.WriteSigned(8, (long)-33)); Assert.DoesNotThrow(() => writer.Write("datà")); Assert.DoesNotThrow(() => writer.Write("data", Encoding.ASCII)); Assert.DoesNotThrow(() => writer.Write(2, (sbyte)3)); byte[] result = null; // byteQuantity must have an exact bit count in order to be correct // Please ensure that the test above produces an exact bit count // Please also ensure that PackedBitReader is updated upon changes here int byteQuantity = ( 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 4 + 4 + 6 + 6 + 6 + 6 + 8 + 8 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 1 + 64 + 32 + 5 + 7 + 7 + 9 + // datà should be 5 bytes in UTF8 // length should always be 32 bits 32 + 8 + 8 + 8 + 8 + 8 + // Since ASCII is 7-bit, should be chars = bytes 32 + 8 + 8 + 8 + 8 + 2 ) / Constants.BitsInByte; Assert.DoesNotThrow(() => result = writer.ToArray()); Assert.NotNull(result); Assert.Equal(result.Length, byteQuantity); var hex = string.Join( ", ", result.Select(v => "0x" + v.ToString("X2"))); }