GetHashCode() public method

public GetHashCode ( ) : int
return int
コード例 #1
0
        public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration();
            var repositoryConfiguration2 = new SourceRepositoryConfiguration();

            // Act
            int hashCodeObject1 = repositoryConfiguration1.GetHashCode();
            int hashCodeObject2 = repositoryConfiguration2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
コード例 #2
0
        public void GetHashCode_SameHashCodeIsReturnedEveryTimeTheMethodIsCalled_AsLongAsTheObjectDoesNotChange()
        {
            // Arrange
            string repositoryName = "Some Repository";
            string repositoryUrl = "http://example.com";
            var repositoryConfiguration = new SourceRepositoryConfiguration { Name = repositoryName, Url = new Uri(repositoryUrl) };

            int expectedHashcode = repositoryConfiguration.GetHashCode();

            for (var i = 0; i < 100; i++)
            {
                // Act
                repositoryConfiguration.Name = repositoryName;
                repositoryConfiguration.Url = new Uri(repositoryUrl);
                int generatedHashCode = repositoryConfiguration.GetHashCode();

                // Assert
                Assert.AreEqual(expectedHashcode, generatedHashCode);
            }
        }
コード例 #3
0
        public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual()
        {
            // Arrange
            var repositoryConfiguration1 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };
            var repositoryConfiguration2 = new SourceRepositoryConfiguration { Name = "Some Repository", Url = new Uri("http://example.com") };

            // Act
            int hashCodeObject1 = repositoryConfiguration1.GetHashCode();
            int hashCodeObject2 = repositoryConfiguration2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
コード例 #4
0
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            var hashCodes = new Dictionary<int, SourceRepositoryConfiguration>();

            for (var i = 0; i < 10000; i++)
            {
                // Act
                var repositoryConfiguration = new SourceRepositoryConfiguration
                    { Name = Guid.NewGuid().ToString(), Url = new Uri("http://" + Guid.NewGuid().ToString()) };

                int generatedHashCode = repositoryConfiguration.GetHashCode();

                // Assert
                Assert.IsFalse(hashCodes.ContainsKey(generatedHashCode));
                hashCodes.Add(generatedHashCode, repositoryConfiguration);
            }
        }