|
| 1 | +// |
| 2 | +// Copyright (c) .NET Foundation and Contributors |
| 3 | +// Portions Copyright (c) Microsoft Corporation. All rights reserved. |
| 4 | +// See LICENSE file in the project root for full license information. |
| 5 | +// |
| 6 | + |
| 7 | +using nanoFramework.TestFramework; |
| 8 | +using System.Diagnostics; |
| 9 | + |
| 10 | +namespace NFUnitTestClasses |
| 11 | +{ |
| 12 | + [TestClass] |
| 13 | + class UnitTestHashCodeTests |
| 14 | + { |
| 15 | + [TestMethod] |
| 16 | + public void HashCode_Test_01() |
| 17 | + { |
| 18 | + // generate a hash code for a class with fields |
| 19 | + SomeKey key = new(42); |
| 20 | + |
| 21 | + int hashCode = key.GetHashCode(); |
| 22 | + |
| 23 | + OutputHelper.WriteLine($"Hash code for SomeKey(42) is {hashCode}"); |
| 24 | + OutputHelper.WriteLine($"Hash code for SomeKey(42) is {key.GetHashCode()}"); |
| 25 | + |
| 26 | + Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0"); |
| 27 | + Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode()); |
| 28 | + } |
| 29 | + |
| 30 | + [TestMethod] |
| 31 | + public void HashCode_Test_02() |
| 32 | + { |
| 33 | + // generate a hash code for a class without fields |
| 34 | + SomeOtherKey key = new(); |
| 35 | + |
| 36 | + int hashCode = key.GetHashCode(); |
| 37 | + |
| 38 | + OutputHelper.WriteLine($"Hash code for SomeOtherKey is {hashCode}"); |
| 39 | + OutputHelper.WriteLine($"Hash code for SomeOtherKey is {key.GetHashCode()}"); |
| 40 | + |
| 41 | + Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0"); |
| 42 | + Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode()); |
| 43 | + } |
| 44 | + |
| 45 | + [TestMethod] |
| 46 | + public void HashCode_Test_03() |
| 47 | + { |
| 48 | + // generate a hash code for a class with fields with different types |
| 49 | + SomeOtherFunnyKey key = new(); |
| 50 | + |
| 51 | + int hashCode = key.GetHashCode(); |
| 52 | + |
| 53 | + OutputHelper.WriteLine($"Hash code for SomeOtherFunnyKey is {hashCode}"); |
| 54 | + OutputHelper.WriteLine($"Hash code for SomeOtherFunnyKey is {key.GetHashCode()}"); |
| 55 | + |
| 56 | + Assert.AreNotEqual(0, key.GetHashCode(), "Hash code for a class should not be 0"); |
| 57 | + Assert.AreEqual($"{key.GetHashCode()}".GetHashCode(), $"{hashCode}".GetHashCode()); |
| 58 | + } |
| 59 | + |
| 60 | + public class SomeKey |
| 61 | + { |
| 62 | + public SomeKey(int value) |
| 63 | + { |
| 64 | + Value = value; |
| 65 | + } |
| 66 | + public int Value { get; } |
| 67 | + } |
| 68 | + |
| 69 | + public class SomeOtherKey |
| 70 | + { |
| 71 | + public SomeOtherKey() |
| 72 | + { |
| 73 | + } |
| 74 | + } |
| 75 | + |
| 76 | + public class SomeOtherFunnyKey |
| 77 | + { |
| 78 | + public string Name { get; set; } |
| 79 | + |
| 80 | + public int GetValue { get; set; } |
| 81 | + |
| 82 | + public SomeOtherFunnyKey() |
| 83 | + { |
| 84 | + } |
| 85 | + } |
| 86 | + |
| 87 | + } |
| 88 | +} |
0 commit comments