|
| 1 | +using Fernandezja.ColorHashSharp; |
| 2 | +using Fernandezja.ColorHashSharp.Interfaces; |
| 3 | +using System; |
| 4 | +using Xunit; |
| 5 | + |
| 6 | +namespace ColorHashSharp.Tests |
| 7 | +{ |
| 8 | + /// <summary> |
| 9 | + /// Custom simple hash function for testing |
| 10 | + /// </summary> |
| 11 | + public class SimpleCharCodeHash : IHashFunction |
| 12 | + { |
| 13 | + public ulong Generate(string value) |
| 14 | + { |
| 15 | + if (string.IsNullOrEmpty(value)) |
| 16 | + return 0; |
| 17 | + |
| 18 | + ulong hash = 0; |
| 19 | + foreach (char c in value) |
| 20 | + { |
| 21 | + hash += (ulong)c; |
| 22 | + } |
| 23 | + return hash; |
| 24 | + } |
| 25 | + } |
| 26 | + |
| 27 | + public class CustomHashFunctionTest |
| 28 | + { |
| 29 | + [Fact(DisplayName = "CustomHashFunction_ShouldWork")] |
| 30 | + public void CustomHashFunction_ShouldWork() |
| 31 | + { |
| 32 | + // Arrange |
| 33 | + var customHash = new SimpleCharCodeHash(); |
| 34 | + var options = new Options |
| 35 | + { |
| 36 | + HashFunction = customHash |
| 37 | + }; |
| 38 | + var colorHash = new ColorHash(options); |
| 39 | + |
| 40 | + // Act |
| 41 | + var hsl = colorHash.Hsl("Hello World"); |
| 42 | + var rgb = colorHash.Rgb("Hello World"); |
| 43 | + var hex = colorHash.Hex("Hello World"); |
| 44 | + |
| 45 | + // Assert |
| 46 | + Assert.NotNull(hsl); |
| 47 | + Assert.NotNull(rgb); |
| 48 | + Assert.NotNull(hex); |
| 49 | + Assert.True(hsl.H >= 0 && hsl.H < 360); |
| 50 | + Assert.True(hsl.S >= 0 && hsl.S <= 1); |
| 51 | + Assert.True(hsl.L >= 0 && hsl.L <= 1); |
| 52 | + } |
| 53 | + |
| 54 | + [Fact(DisplayName = "CustomHashFunction_ShouldProduceDifferentResults")] |
| 55 | + public void CustomHashFunction_ShouldProduceDifferentResults() |
| 56 | + { |
| 57 | + // Arrange - Default BKDRHash |
| 58 | + var defaultColorHash = new ColorHash(); |
| 59 | + var defaultHex = defaultColorHash.Hex("Test"); |
| 60 | + |
| 61 | + // Arrange - Custom simple hash |
| 62 | + var customHash = new SimpleCharCodeHash(); |
| 63 | + var options = new Options |
| 64 | + { |
| 65 | + HashFunction = customHash |
| 66 | + }; |
| 67 | + var customColorHash = new ColorHash(options); |
| 68 | + var customHex = customColorHash.Hex("Test"); |
| 69 | + |
| 70 | + // Assert - Different hash functions should produce different results |
| 71 | + Assert.NotEqual(defaultHex, customHex); |
| 72 | + } |
| 73 | + |
| 74 | + [Fact(DisplayName = "CustomHashFunction_ShouldBeConsistent")] |
| 75 | + public void CustomHashFunction_ShouldBeConsistent() |
| 76 | + { |
| 77 | + // Arrange |
| 78 | + var customHash = new SimpleCharCodeHash(); |
| 79 | + var options = new Options |
| 80 | + { |
| 81 | + HashFunction = customHash |
| 82 | + }; |
| 83 | + var colorHash = new ColorHash(options); |
| 84 | + |
| 85 | + // Act |
| 86 | + var hex1 = colorHash.Hex("Consistent"); |
| 87 | + var hex2 = colorHash.Hex("Consistent"); |
| 88 | + |
| 89 | + // Assert - Same input should always produce same output |
| 90 | + Assert.Equal(hex1, hex2); |
| 91 | + } |
| 92 | + |
| 93 | + [Fact(DisplayName = "Options_WithoutCustomHash_ShouldUseDefaultBKDRHash")] |
| 94 | + public void Options_WithoutCustomHash_ShouldUseDefaultBKDRHash() |
| 95 | + { |
| 96 | + // Arrange |
| 97 | + var options = new Options(); // No custom hash |
| 98 | + var colorHash = new ColorHash(options); |
| 99 | + |
| 100 | + // Act |
| 101 | + var hex = colorHash.Hex("Hello World"); |
| 102 | + |
| 103 | + // Assert - Should produce expected result from BKDRHash |
| 104 | + Assert.NotNull(hex); |
| 105 | + Assert.Equal(6, hex.Length); |
| 106 | + } |
| 107 | + } |
| 108 | +} |
0 commit comments