Exemplo n.º 1
0
        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);
        }
Exemplo n.º 2
0
        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);
        }
Exemplo n.º 3
0
        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);
        }
Exemplo n.º 4
0
        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();
        }
Exemplo n.º 5
0
        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);
        }
        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 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);
     }
 }
Exemplo n.º 8
0
 public void Equal_Content_Should_Have_Same_HashCode()
 {
     Assert.AreEqual(comparer.GetHashCode(x), comparer.GetHashCode(y));
 }
Exemplo n.º 9
0
 public void Equal_Content_Should_Have_Same_HashCode()
 {
     Assert.Equal(_comparer.GetHashCode(_x), _comparer.GetHashCode(_y));
 }