コード例 #1
0
        public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual()
        {
            // Arrange
            var package1 = new SystemInformation();
            var package2 = new SystemInformation();

            // Act
            int hashCodeObject1 = package1.GetHashCode();
            int hashCodeObject2 = package2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
コード例 #2
0
        public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual()
        {
            // Arrange
            var timestamp = DateTime.UtcNow;
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = timestamp,
                    SystemPerformance = new SystemPerformanceData()
                };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
コード例 #3
0
        public void GetHashCode_SameHashCodeIsReturnedEveryTimeTheMethodIsCalled_AsLongAsTheObjectDoesNotChange()
        {
            // Arrange
            var machineName = Environment.MachineName;
            var timeStamp = DateTime.UtcNow;
            var object1 = new SystemInformation
                {
                    MachineName = machineName,
                    Timestamp = timeStamp,
                    SystemPerformance = new SystemPerformanceData()
                };

            int expectedHashcode = object1.GetHashCode();

            for (var i = 0; i < 100; i++)
            {
                // Act
                object1.MachineName = machineName;
                object1.Timestamp = timeStamp;
                int generatedHashCode = object1.GetHashCode();

                // Assert
                Assert.AreEqual(expectedHashcode, generatedHashCode);
            }
        }
コード例 #4
0
        public void GetHashCode_TwoDistinctObjects_HashCodesAreDifferent()
        {
            // Arrange
            var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName,
                    Timestamp = DateTime.UtcNow,
                    SystemPerformance = new SystemPerformanceData()
                };
            var object2 = new SystemInformation
            {
                MachineName = "Different Machine",
                Timestamp = DateTime.UtcNow,
                SystemPerformance = new SystemPerformanceData()
            };

            // Act
            int hashCodeObject1 = object1.GetHashCode();
            int hashCodeObject2 = object2.GetHashCode();

            // Assert
            Assert.AreNotEqual(hashCodeObject1, hashCodeObject2);
        }
コード例 #5
0
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            var timeStamp = DateTime.UtcNow;
            var hashCodes = new Dictionary<int, SystemInformation>();

            for (var i = 0; i < 1000; i++)
            {
                // Act
                var object1 = new SystemInformation
                {
                    MachineName = Environment.MachineName + i,
                    Timestamp = timeStamp,
                    SystemPerformance = new SystemPerformanceData()
                };

                int generatedHashCode = object1.GetHashCode();

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