Exemplo n.º 1
0
        public void GetHashCode_SameHashCodeIsReturnedEveryTimeTheMethodIsCalled_AsLongAsTheObjectDoesNotChange()
        {
            // Arrange
            var memoryStatus = new SystemMemoryInformation {
                AvailableMemoryInGB = 3
            };
            var processorStatus = new ProcessorUtilizationInformation {
                ProcessorUtilizationInPercent = 30d
            };
            var storageStatus = new SystemStorageInformation();

            var object1 = new SystemPerformanceData {
                MemoryStatus = memoryStatus, ProcessorStatus = processorStatus, StorageStatus = storageStatus
            };

            int expectedHashcode = object1.GetHashCode();

            for (var i = 0; i < 100; i++)
            {
                // Act
                object1.MemoryStatus    = memoryStatus;
                object1.ProcessorStatus = processorStatus;
                object1.StorageStatus   = storageStatus;

                int generatedHashCode = object1.GetHashCode();

                // Assert
                Assert.AreEqual(expectedHashcode, generatedHashCode);
            }
        }
Exemplo n.º 2
0
        public void GetHashCode_TwoDistinctObjects_HashCodesAreDifferent()
        {
            // Arrange
            var object1 = new SystemPerformanceData
            {
                MemoryStatus = new SystemMemoryInformation {
                    AvailableMemoryInGB = 3
                },
                ProcessorStatus = new ProcessorUtilizationInformation {
                    ProcessorUtilizationInPercent = 30d
                }
            };

            var object2 = new SystemPerformanceData
            {
                MemoryStatus = new SystemMemoryInformation {
                    AvailableMemoryInGB = 4
                },
                ProcessorStatus = new ProcessorUtilizationInformation {
                    ProcessorUtilizationInPercent = 90d
                }
            };

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

            // Assert
            Assert.AreNotEqual(hashCodeObject1, hashCodeObject2);
        }
Exemplo n.º 3
0
        public void GetHashCode_TwoIdenticalObjects_BothInitialized_HashCodesAreEqual()
        {
            // Arrange
            var object1 = new SystemPerformanceData
            {
                MemoryStatus = new SystemMemoryInformation {
                    AvailableMemoryInGB = 3
                },
                ProcessorStatus = new ProcessorUtilizationInformation {
                    ProcessorUtilizationInPercent = 30d
                }
            };

            var object2 = new SystemPerformanceData
            {
                MemoryStatus = new SystemMemoryInformation {
                    AvailableMemoryInGB = 3
                },
                ProcessorStatus = new ProcessorUtilizationInformation {
                    ProcessorUtilizationInPercent = 30d
                }
            };

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

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
Exemplo n.º 4
0
        public void GetHashCode_TwoIdenticalObjects_BothUninitialized_HashCodesAreEqual()
        {
            // Arrange
            var package1 = new SystemPerformanceData();
            var package2 = new SystemPerformanceData();

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

            // Assert
            Assert.AreEqual(hashCodeObject1, hashCodeObject2);
        }
Exemplo n.º 5
0
        public void GetHashCode_ForAllUniqueObject_AUniqueHashCodeIsReturned()
        {
            var hashCodes = new Dictionary <int, SystemPerformanceData>();

            for (var i = 0; i < 10000; i++)
            {
                // Act
                var object1 = new SystemPerformanceData
                {
                    MemoryStatus = new SystemMemoryInformation {
                        AvailableMemoryInGB = i
                    },
                    ProcessorStatus = new ProcessorUtilizationInformation {
                        ProcessorUtilizationInPercent = 30d
                    }
                };

                int generatedHashCode = object1.GetHashCode();

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