Skip to content

Commit 1280138

Browse files
authored
Add RapidHash Algorithm Unit Tests
Signed-off-by: Xen <lordofxen@deskasoft.com>
1 parent 98d25cd commit 1280138

2 files changed

Lines changed: 283 additions & 0 deletions

File tree

Lines changed: 44 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,44 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the "Software"), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
using HashifyNet.Algorithms.RapidHash;
31+
32+
namespace HashifyNet.UnitTests.Algorithms.RapidHash
33+
{
34+
public class RapidHashConfig_Tests
35+
{
36+
[Fact]
37+
public void RapidHashConfig_Defaults_HaventChanged()
38+
{
39+
var rapidHashConfig = new RapidHashConfig();
40+
41+
Assert.Equal(64, rapidHashConfig.HashSizeInBits);
42+
}
43+
}
44+
}
Lines changed: 239 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,239 @@
1+
// *
2+
// *****************************************************************************
3+
// *
4+
// * Copyright (c) 2025 Deskasoft International
5+
// *
6+
// * Permission is hereby granted, free of charge, to any person obtaining a copy
7+
// * of this software and associated documentation files (the "Software"), to deal
8+
// * in the Software without restriction, including without limitation the rights
9+
// * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
10+
// * copies of the Software, and to permit persons to whom the Software is
11+
// * furnished to do so, subject to the following conditions:
12+
// *
13+
// * The above copyright notice and this permission notice shall be included in all
14+
// * copies or substantial portions of the Software.
15+
// *
16+
// * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
17+
// * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
18+
// * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
19+
// * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
20+
// * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
21+
// * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
22+
// * SOFTWARE.
23+
// *
24+
// *
25+
// * Please refer to LICENSE file.
26+
// *
27+
// ******************************************************************************
28+
// *
29+
30+
using HashifyNet.Algorithms.RapidHash;
31+
using HashifyNet.UnitTests.Utilities;
32+
using Moq;
33+
34+
namespace HashifyNet.UnitTests.Algorithms.RapidHash
35+
{
36+
public class RapidHash_Implementation_Tests
37+
{
38+
#region Constructor
39+
40+
#region Config
41+
42+
[Fact]
43+
public void RapidHash_Implementation_Constructor_Config_IsNull_Throws()
44+
{
45+
Assert.Equal(
46+
"config",
47+
Assert.Throws<ArgumentNullException>(
48+
() => new RapidHash_Implementation(null))
49+
.ParamName);
50+
}
51+
52+
[Fact]
53+
public void RapidHash_Implementation_Constructor_Config_IsCloned()
54+
{
55+
var rapidHashConfigMock = new Mock<IRapidHashConfig>();
56+
{
57+
rapidHashConfigMock.SetupGet(bc => bc.HashSizeInBits)
58+
.Returns(64);
59+
60+
rapidHashConfigMock.Setup(bc => bc.Clone())
61+
.Returns(new RapidHashConfig());
62+
}
63+
64+
GC.KeepAlive(
65+
new RapidHash_Implementation(rapidHashConfigMock.Object));
66+
67+
rapidHashConfigMock.Verify(bc => bc.Clone(), Times.Once);
68+
69+
rapidHashConfigMock.VerifyGet(bc => bc.HashSizeInBits, Times.Never);
70+
}
71+
72+
#region HashSize
73+
74+
[Fact]
75+
public void RapidHash_Implementation_Constructor_Config_HashSize_IsInvalid_Throws()
76+
{
77+
var invalidHashSizes = new[] { -1, 0, 63, 32, 128, 800, 801, 900, 1024 };
78+
79+
foreach (var invalidHashSize in invalidHashSizes)
80+
{
81+
var rapidHashConfigMock = new Mock<IRapidHashConfig>();
82+
{
83+
rapidHashConfigMock.SetupGet(bc => bc.HashSizeInBits)
84+
.Returns(invalidHashSize);
85+
86+
rapidHashConfigMock.Setup(bc => bc.Clone())
87+
.Returns(() => rapidHashConfigMock.Object);
88+
}
89+
90+
Assert.Equal(
91+
"config.HashSizeInBits",
92+
Assert.Throws<ArgumentOutOfRangeException>(() =>
93+
new RapidHash_Implementation(
94+
rapidHashConfigMock.Object))
95+
.ParamName);
96+
}
97+
}
98+
99+
#endregion
100+
101+
#region HashSizeInBits
102+
103+
[Fact]
104+
public void RapidHash_Implementation_HashSizeInBits_IsFromConfig()
105+
{
106+
var rapidHashConfig2 = Mock.Of<IRapidHashConfig>(bc => bc.HashSizeInBits == 64 && bc.Clone() == new RapidHashConfig());
107+
var rapidHashConfig = Mock.Of<IRapidHashConfig>(bc => bc.Clone() == rapidHashConfig2);
108+
109+
var rapidHash = new RapidHash_Implementation(rapidHashConfig);
110+
111+
Assert.Equal(64, rapidHash.Config.HashSizeInBits);
112+
}
113+
114+
#endregion
115+
116+
#endregion
117+
118+
#endregion
119+
120+
public class IHashFunction_Tests_DefaultConstructor
121+
: IStreamableHashFunction_TestBase<IRapidHash, IRapidHashConfig>
122+
{
123+
protected override IEnumerable<KnownValue> KnownValues { get; } =
124+
new KnownValue[] {
125+
new KnownValue(64, TestConstants.Empty, 232177599295442350),
126+
127+
new KnownValue(64, TestConstants.FooBar, 15523718635262960188),
128+
129+
new KnownValue(64, TestConstants.LoremIpsum, 18040269197578596193),
130+
131+
new KnownValue(64, new byte[1], 5702620981742189058),
132+
new KnownValue(64, new byte[7], 10883712246314272921),
133+
new KnownValue(64, new byte[8], 11456056956516475379),
134+
new KnownValue(64, new byte[16], 11820096523416114993),
135+
new KnownValue(64, new byte[17], 12879367262414962620),
136+
137+
new KnownValue(64, new byte[4096], 11567716601755724648),
138+
139+
new KnownValue(64, new byte[8192], 17362367839289931837),
140+
141+
new KnownValue(64, new byte[10000], 5626523013684839545),
142+
};
143+
144+
protected override IRapidHash CreateHashFunction(int hashSize) =>
145+
new RapidHash_Implementation(
146+
new RapidHashConfig());
147+
}
148+
149+
public class IHashFunction_Tests_WithSeed
150+
: IStreamableHashFunction_TestBase<IRapidHash, IRapidHashConfig>
151+
{
152+
protected override IEnumerable<KnownValue> KnownValues { get; } =
153+
new KnownValue[] {
154+
new KnownValue(64, TestConstants.Empty, 7833074399267231953),
155+
156+
new KnownValue(64, TestConstants.FooBar, 11416782446331650639),
157+
158+
new KnownValue(64, TestConstants.LoremIpsum, 10744372729903230306),
159+
160+
new KnownValue(64, new byte[4096], 18209065387139984514),
161+
162+
new KnownValue(64, new byte[8192], 18419876097751971287),
163+
164+
new KnownValue(64, new byte[10000], 16909027571307796084),
165+
};
166+
167+
protected override IRapidHash CreateHashFunction(int hashSize) =>
168+
new RapidHash_Implementation(
169+
new RapidHashConfig()
170+
{
171+
Seed = 90_01
172+
});
173+
}
174+
175+
public class IHashFunction_Tests_WithModeMicro
176+
: IStreamableHashFunction_TestBase<IRapidHash, IRapidHashConfig>
177+
{
178+
protected override IEnumerable<KnownValue> KnownValues { get; } =
179+
new KnownValue[] {
180+
new KnownValue(64, TestConstants.Empty, 232177599295442350),
181+
182+
new KnownValue(64, TestConstants.FooBar, 15523718635262960188),
183+
184+
new KnownValue(64, TestConstants.LoremIpsum, 8521930955689885920),
185+
186+
new KnownValue(64, new byte[1], 5702620981742189058),
187+
new KnownValue(64, new byte[7], 10883712246314272921),
188+
new KnownValue(64, new byte[8], 11456056956516475379),
189+
new KnownValue(64, new byte[16], 11820096523416114993),
190+
new KnownValue(64, new byte[17], 12879367262414962620),
191+
192+
new KnownValue(64, new byte[4096], 13516268575382731638),
193+
194+
new KnownValue(64, new byte[8192], 5236988562116181528),
195+
196+
new KnownValue(64, new byte[10000], 10980458045961532347),
197+
};
198+
199+
protected override IRapidHash CreateHashFunction(int hashSize) =>
200+
new RapidHash_Implementation(
201+
new RapidHashConfig()
202+
{
203+
Mode = RapidHashMode.Micro
204+
});
205+
}
206+
207+
public class IHashFunction_Tests_WithModeNano
208+
: IStreamableHashFunction_TestBase<IRapidHash, IRapidHashConfig>
209+
{
210+
protected override IEnumerable<KnownValue> KnownValues { get; } =
211+
new KnownValue[] {
212+
new KnownValue(64, TestConstants.Empty, 232177599295442350),
213+
214+
new KnownValue(64, TestConstants.FooBar, 15523718635262960188),
215+
216+
new KnownValue(64, TestConstants.LoremIpsum, 9489726530920820974),
217+
218+
new KnownValue(64, new byte[1], 5702620981742189058),
219+
new KnownValue(64, new byte[7], 10883712246314272921),
220+
new KnownValue(64, new byte[8], 11456056956516475379),
221+
new KnownValue(64, new byte[16], 11820096523416114993),
222+
new KnownValue(64, new byte[17], 12879367262414962620),
223+
224+
new KnownValue(64, new byte[4096], 16506082494102252922),
225+
226+
new KnownValue(64, new byte[8192], 15389422866100442776),
227+
228+
new KnownValue(64, new byte[10000], 5782605844267771859),
229+
};
230+
231+
protected override IRapidHash CreateHashFunction(int hashSize) =>
232+
new RapidHash_Implementation(
233+
new RapidHashConfig()
234+
{
235+
Mode = RapidHashMode.Nano
236+
});
237+
}
238+
}
239+
}

0 commit comments

Comments
 (0)