public static void GetHashCode___Should_return_different_hash_code___For_different_byte_arrays() { // Arrange var byteArray1a = Some.Dummies <byte>().ToArray(); var byteArray1b = byteArray1a.Concat(new[] { A.Dummy <byte>() }).ToArray(); var byteArray2a = Some.Dummies <byte>().ToArray(); var byteArray2b = new[] { A.Dummy <byte>() }.Concat(byteArray2a).ToArray(); var byteArray3a = Some.Dummies <byte>(5).ToArray(); var byteArray3b = Some.Dummies <byte>(5).ToArray(); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var hash1a = systemUnderTest.GetHashCode(byteArray1a); var hash1b = systemUnderTest.GetHashCode(byteArray1b); var hash2a = systemUnderTest.GetHashCode(byteArray2a); var hash2b = systemUnderTest.GetHashCode(byteArray2b); var hash3a = systemUnderTest.GetHashCode(byteArray3a); var hash3b = systemUnderTest.GetHashCode(byteArray3b); // Assert hash1a.Should().NotBe(hash1b); hash2a.Should().NotBe(hash2b); hash3a.Should().NotBe(hash3b); }
protected bool Equals(GenericTestClassWithInternalObjectSerializer <T1, T2> other) { return(BooleanFalse == other.BooleanFalse && BooleanTrue == other.BooleanTrue && Char == other.Char && SByte == other.SByte && Byte == other.Byte && Int16 == other.Int16 && UInt16 == other.UInt16 && Int32 == other.Int32 && UInt32 == other.UInt32 && Int64 == other.Int64 && UInt64 == other.UInt64 && Single.Equals(other.Single) && Double.Equals(other.Double) && Decimal == other.Decimal && String == other.String && DateTime == other.DateTime && DateTimeOffset == other.DateTimeOffset && NonGenericType == other.NonGenericType && GenericTypeDefinition == other.GenericTypeDefinition && ClosedConstructedGenericType == other.ClosedConstructedGenericType && NullReference == other.NullReference && Enum_S8 == other.Enum_S8 && Enum_U8 == other.Enum_U8 && Enum_S16 == other.Enum_S16 && Enum_U16 == other.Enum_U16 && Enum_S32 == other.Enum_S32 && Enum_U32 == other.Enum_U32 && Enum_S64 == other.Enum_S64 && Enum_U64 == other.Enum_U64 && SerializableObject.SequenceEqual(other.SerializableObject) && ByteArrayEqualityComparer.AreEqual(Buffer1, other.Buffer1) && ByteArrayEqualityComparer.AreEqual(Buffer2, other.Buffer2)); }
/// <summary> /// Takes a state snapshot of the map. Only records it as a new state if the state has changed since the last snapshot. /// If the state has changed, all forward states are destroyed. So if you undo a few times then call this, you cannot redo those undos anymore. /// </summary> /// <returns>True if a new state snapshot was taken; false if the state has not changed.</returns> public bool Snapshot() { // Serilize the whole map as binary into memory byte[] serialized = BinaryValueWriter.CreateAndWrite(w => Map.Save(w, DynamicEntityFactory, MapBase.MapSaveFlags.DoNotSort)); // Check if the state changed since the last time if (_stateIndex >= 0 && ByteArrayEqualityComparer.AreEqual(_states[_stateIndex], serialized)) { return(false); } // Remove all forward (redo) states if (_states.Count > _stateIndex + 1) { _states.RemoveRange(_stateIndex + 1, _states.Count - _stateIndex - 1); } // Remove old states if (_states.Count > _maxStates) { _states.RemoveRange(0, _cropAmount); } // Push in new state _states.Add(serialized); _stateIndex = _states.Count - 1; return(true); }
public void DifferentLengthTest() { byte[] a = new byte [] { 1, 2, 3, 4, 5 }; byte[] b = new byte [] { 1, 2, 3, 4, 5, 6 }; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsFalse(actual); }
public void NullTest3() { byte[] a = null; byte[] b = null; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsTrue(actual); }
public void EmptyTest3() { byte[] a = new byte[0]; byte[] b = new byte[0]; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsTrue(actual); }
public void EmptyTest2() { byte[] a = new byte[] { 1, 2, 3, 4, 5 }; byte[] b = new byte[0]; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsFalse(actual); }
public void SameLengthTest() { byte[] a = new byte[] { 1, 2, 3, 4 }; byte[] b = new byte[] { 1, 2, 4, 4 }; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsFalse(actual); }
public void SameTest1() { byte[] a = new byte[] { 1, 2, 3, 4, 5, 6, 1, 3, 5 }; byte[] b = a; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsTrue(actual); }
public void NullTest2() { byte[] a = null; byte[] b = new byte[] { 1, 2, 3, 4, 5, 6 }; bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsFalse(actual); }
public static void GetHashCode___Should_not_throw___When_byte_array_is_null() { // Arrange var systemUnderTest = new ByteArrayEqualityComparer(); // Act var hash = Record.Exception(() => systemUnderTest.GetHashCode(null)); // Assert hash.Should().BeNull(); }
public static void GetHashCode___Should_return_same_hash_code___When_called_twice_with_null() { // Arrange var systemUnderTest = new ByteArrayEqualityComparer(); // Act var hash1 = systemUnderTest.GetHashCode(null); var hash2 = systemUnderTest.GetHashCode(null); // Assert hash1.Should().Be(hash2); }
public static void Equals___Should_return_true___When_both_bytes_arrays_are_the_same_object_reference() { // Arrange var byteArray = Some.Dummies <byte>().ToArray(); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var actual = systemUnderTest.Equals(byteArray, byteArray); // Assert actual.Should().BeTrue(); }
public string[] Validate(Tek[] values) { var comparer = new ByteArrayEqualityComparer(); var distinctCount = values.Select(x => x.KeyData).Distinct(comparer).Count(); if (values.Length != distinctCount) { return(new[] { $"KeyData duplicate found - Count:{values.Length - distinctCount}." }); } return(new string[0]); }
public static void Equals___Should_return_true___When_both_arrays_are_null() { // Arrange byte[] byteArray1 = null; byte[] byteArray2 = null; var systemUnderTest = new ByteArrayEqualityComparer(); // Act var result = systemUnderTest.Equals(byteArray1, byteArray2); // Assert result.Should().BeTrue(); }
public static void GetHashCode___Should_return_same_hash_code___When_called_twice_on_same_object_reference() { // Arrange var byteArray = Some.Dummies <byte>().ToArray(); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var hash1 = systemUnderTest.GetHashCode(byteArray); var hash2 = systemUnderTest.GetHashCode(byteArray); // Assert hash1.Should().Be(hash2); }
public static void Equals___Should_return_false___When_one_byte_array_is_null_and_the_other_is_not_null() { // Arrange byte[] byteArray1 = null; var byteArray2 = Some.Dummies <byte>().ToArray(); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var actual1 = systemUnderTest.Equals(byteArray1, byteArray2); var actual2 = systemUnderTest.Equals(byteArray2, byteArray1); // Assert actual1.Should().BeFalse(); actual2.Should().BeFalse(); }
public void RandomEqualTest() { SafeRandom rnd = new SafeRandom(); for (int loop = 0; loop < 100000; loop++) { byte[] a = new byte[rnd.Next(1, 100)]; rnd.NextBytes(a); byte[] b = DeepCopy(a); bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsTrue(actual); } }
public static void Equals___Should_return_true___When_byte_arrays_are_equal_but_not_the_same_object_reference() { // Arrange var byteArray1 = Some.Dummies <byte>().ToArray(); var byteArray2 = new byte[byteArray1.Length]; Buffer.BlockCopy(byteArray1, 0, byteArray2, 0, byteArray1.Length); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var actual = systemUnderTest.Equals(byteArray1, byteArray2); // Assert actual.Should().BeTrue(); }
static PublisherShimHandler() { s_markAsMatching = (pipe, data, arg) => { PublisherShimHandler self = (PublisherShimHandler)arg; Utils.Swap(self.m_identities, IndexOf(self.m_identities, pipe), self.m_matching); self.m_matching++; }; s_SendUnsubscription = (pipe, data, arg) => { }; s_byteArrayEqualityComparer = new ByteArrayEqualityComparer(); }
public static void GetHashCode___Should_return_same_hash_code___For_two_byte_arrays_that_are_equal() { // Arrange var byteArray1 = Some.Dummies <byte>().ToArray(); var byteArray2 = new byte[byteArray1.Length]; Buffer.BlockCopy(byteArray1, 0, byteArray2, 0, byteArray1.Length); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var hash1 = systemUnderTest.GetHashCode(byteArray1); var hash2 = systemUnderTest.GetHashCode(byteArray2); // Assert hash1.Should().Be(hash2); }
private void Initialize(int capacity) { if (capacity == 0) { capacity = 10; } _buckets = new int[capacity]; for (int i = 0; i < _buckets.Length; i++) { _buckets[i] = -1; } _entries = new Entry[capacity]; _comparer = ByteArrayEqualityComparer.Instance; _freeList = -1; }
public void RandomNotEqualTest() { SafeRandom rnd = new SafeRandom(); for (int loop = 0; loop < 1000000; loop++) { byte[] a = new byte[rnd.Next(1, 100)]; rnd.NextBytes(a); byte[] b = DeepCopy(a); b[rnd.Next(0, b.Length)]++; // Increment at a random index bool actual = ByteArrayEqualityComparer.AreEqual(a, b); Assert.IsFalse(actual); } }
public override int GetHashCode() { unchecked { int hashCode = BooleanFalse.GetHashCode(); hashCode = (hashCode * 397) ^ BooleanTrue.GetHashCode(); hashCode = (hashCode * 397) ^ Char.GetHashCode(); hashCode = (hashCode * 397) ^ SByte.GetHashCode(); hashCode = (hashCode * 397) ^ Byte.GetHashCode(); hashCode = (hashCode * 397) ^ Int16.GetHashCode(); hashCode = (hashCode * 397) ^ UInt16.GetHashCode(); hashCode = (hashCode * 397) ^ Int32; hashCode = (hashCode * 397) ^ (int)UInt32; hashCode = (hashCode * 397) ^ Int64.GetHashCode(); hashCode = (hashCode * 397) ^ UInt64.GetHashCode(); hashCode = (hashCode * 397) ^ Single.GetHashCode(); hashCode = (hashCode * 397) ^ Double.GetHashCode(); hashCode = (hashCode * 397) ^ Decimal.GetHashCode(); hashCode = (hashCode * 397) ^ String.GetHashCode(); hashCode = (hashCode * 397) ^ DateTime.GetHashCode(); hashCode = (hashCode * 397) ^ DateTimeOffset.GetHashCode(); hashCode = (hashCode * 397) ^ Guid.GetHashCode(); hashCode = (hashCode * 397) ^ NonGenericType.GetHashCode(); hashCode = (hashCode * 397) ^ GenericTypeDefinition.GetHashCode(); hashCode = (hashCode * 397) ^ ClosedConstructedGenericType.GetHashCode(); hashCode = (hashCode * 397) ^ (NullReference != null ? NullReference.GetHashCode() : 0); hashCode = (hashCode * 397) ^ Enum_S8.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_U8.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_S16.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_U16.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_S32.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_U32.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_S64.GetHashCode(); hashCode = (hashCode * 397) ^ Enum_U64.GetHashCode(); hashCode = (hashCode * 397) ^ SerializableObject.GetHashCode(); hashCode = (hashCode * 397) ^ ByteArrayEqualityComparer.GetHashCode(Buffer1); hashCode = (hashCode * 397) ^ ByteArrayEqualityComparer.GetHashCode(Buffer2); return(hashCode); } }
static void Main(string[] args) { int[] x = { 1, 2, 3 }; int[] y = { 1, 2, 3 }; int[] z = { 4, 5, 6 }; var comparer = new ArrayEqualityComparer <int>(); Console.WriteLine(comparer.GetHashCode(x)); Console.WriteLine(comparer.GetHashCode(y)); Console.WriteLine(comparer.GetHashCode(z)); Console.WriteLine(comparer.Equals(x, y)); Console.WriteLine(comparer.Equals(x, z)); Console.WriteLine(); Console.WriteLine("Byte comparers"); byte[] x1 = { 1, 2, 3 }; byte[] y1 = { 1, 2, 3 }; byte[] z1 = { 4, 5, 6 }; var comparer1 = new ByteArrayEqualityComparer(); Console.WriteLine(comparer1.GetHashCode(x1)); Console.WriteLine(comparer1.GetHashCode(y1)); Console.WriteLine(comparer1.GetHashCode(z1)); Console.WriteLine(comparer1.Equals(x1, y1)); Console.WriteLine(comparer1.Equals(x1, z1)); var t = new Dictionary <byte[], string>(comparer1); t.Add(x1, "x"); t.Add(z1, "z"); Console.WriteLine($"t[y] = {t[y1]} "); Console.WriteLine("Press [Enter] for continue ..."); Console.ReadLine(); }
public static void Equals___Should_return_false___When_byte_arrays_are_different() { // Arrange var byteArray1a = Some.Dummies <byte>().ToArray(); var byteArray1b = byteArray1a.Concat(new[] { A.Dummy <byte>() }).ToArray(); var byteArray2a = Some.Dummies <byte>().ToArray(); var byteArray2b = new[] { A.Dummy <byte>() }.Concat(byteArray2a).ToArray(); var byteArray3a = Some.Dummies <byte>(5).ToArray(); var byteArray3b = Some.Dummies <byte>(5).ToArray(); var systemUnderTest = new ByteArrayEqualityComparer(); // Act var actual1 = systemUnderTest.Equals(byteArray1a, byteArray1b); var actual2 = systemUnderTest.Equals(byteArray2a, byteArray2b); var actual3 = systemUnderTest.Equals(byteArray3a, byteArray3b); // Assert actual1.Should().BeFalse(); actual2.Should().BeFalse(); actual3.Should().BeFalse(); }