Skip to content

Commit c8d43eb

Browse files
committed
[test] Cover Hasher's top-level hashing functions
1 parent 5b80cf1 commit c8d43eb

File tree

1 file changed

+127
-2
lines changed

1 file changed

+127
-2
lines changed

validation-test/stdlib/Hashing.swift

+127-2
Original file line numberDiff line numberDiff line change
@@ -48,10 +48,10 @@ HashingTestSuite.test("Hasher/DefaultKey") {
4848

4949
let defaultHash = _hashValue(for: value)
5050

51-
var rawHash = value._rawHashValue(seed: Hasher._seed)
51+
let rawHash = value._rawHashValue(seed: Hasher._seed)
5252
expectEqual(rawHash, defaultHash)
5353

54-
var oneShotHash = Hasher._hash(seed: Hasher._seed, value)
54+
let oneShotHash = Hasher._hash(seed: Hasher._seed, value)
5555
expectEqual(oneShotHash, defaultHash)
5656

5757
var defaultHasher = Hasher()
@@ -63,4 +63,129 @@ HashingTestSuite.test("Hasher/DefaultKey") {
6363
expectEqual(customHasher.finalize(), defaultHash)
6464
}
6565

66+
HashingTestSuite.test("Hashing/TopLevelHashing/UInt64") {
67+
func checkTopLevelHash(
68+
for value: UInt64,
69+
seed: (UInt64, UInt64),
70+
file: String = #file,
71+
line: UInt = #line) {
72+
var hasher = Hasher(_seed: seed)
73+
hasher._combine(value)
74+
let expected = hasher.finalize()
75+
let actual = Hasher._hash(seed: seed, value)
76+
expectEqual(actual, expected, file: file, line: line)
77+
}
78+
checkTopLevelHash(for: 0, seed: (0, 0))
79+
checkTopLevelHash(for: 1, seed: (0, 0))
80+
checkTopLevelHash(for: 1, seed: (1, 0))
81+
checkTopLevelHash(for: 1, seed: (1, 1))
82+
checkTopLevelHash(for: 0x0102030405060708, seed: (1, 1))
83+
checkTopLevelHash(
84+
for: 0x0102030405060708,
85+
seed: (0x0807060504030201, 0x090a0b0c0d0e0f))
86+
checkTopLevelHash(for: UInt64.max, seed: (1, 1))
87+
checkTopLevelHash(for: UInt64.max, seed: (UInt64.max, UInt64.max))
88+
}
89+
90+
HashingTestSuite.test("Hashing/TopLevelHashing/UInt") {
91+
func checkTopLevelHash(
92+
for value: UInt,
93+
seed: (UInt64, UInt64),
94+
file: String = #file,
95+
line: UInt = #line) {
96+
var hasher = Hasher(_seed: seed)
97+
hasher._combine(value)
98+
let expected = hasher.finalize()
99+
let actual = Hasher._hash(seed: seed, value)
100+
expectEqual(actual, expected, file: file, line: line)
101+
}
102+
checkTopLevelHash(for: 0, seed: (0, 0))
103+
checkTopLevelHash(for: 1, seed: (0, 0))
104+
checkTopLevelHash(for: 1, seed: (1, 0))
105+
checkTopLevelHash(for: 1, seed: (1, 1))
106+
checkTopLevelHash(
107+
for: UInt(truncatingIfNeeded: 0x0102030405060708 as UInt64),
108+
seed: (1, 1))
109+
checkTopLevelHash(
110+
for: UInt(truncatingIfNeeded: 0x0102030405060708 as UInt64),
111+
seed: (0x8877665544332211, 0x1122334455667788))
112+
checkTopLevelHash(for: UInt.max, seed: (1, 1))
113+
checkTopLevelHash(for: UInt.max, seed: (UInt64.max, UInt64.max))
114+
}
115+
116+
HashingTestSuite.test("Hashing/TopLevelHashing/PartialUInt64") {
117+
func checkTopLevelHash(
118+
for value: UInt64,
119+
count: Int,
120+
seed: (UInt64, UInt64),
121+
file: String = #file,
122+
line: UInt = #line) {
123+
var hasher = Hasher(_seed: seed)
124+
hasher._combine(bytes: value, count: count)
125+
let expected = hasher.finalize()
126+
let actual = Hasher._hash(seed: seed, bytes: value, count: count)
127+
expectEqual(
128+
actual,
129+
expected,
130+
"seed: \(seed), value: \(value), count: \(count)",
131+
file: file,
132+
line: line)
133+
}
134+
for seed: (UInt64, UInt64) in [
135+
(0, 0),
136+
(1, 0),
137+
(1, 1),
138+
(0x1827364554637281, 0xf9e8d7c6b5a49382)
139+
] {
140+
for count in 1 ..< 8 {
141+
checkTopLevelHash(for: 0, count: count, seed: seed)
142+
}
143+
checkTopLevelHash(for: 0x01, count: 1, seed: seed)
144+
checkTopLevelHash(for: 0x0102, count: 2, seed: seed)
145+
checkTopLevelHash(for: 0x010203, count: 3, seed: seed)
146+
checkTopLevelHash(for: 0x01020304, count: 4, seed: seed)
147+
checkTopLevelHash(for: 0x0102030405, count: 5, seed: seed)
148+
checkTopLevelHash(for: 0x010203040506, count: 6, seed: seed)
149+
checkTopLevelHash(for: 0x01020304050607, count: 7, seed: seed)
150+
}
151+
}
152+
153+
HashingTestSuite.test("Hashing/TopLevelHashing/UnsafeRawBufferPointer") {
154+
func checkTopLevelHash(
155+
for buffer: [UInt8],
156+
seed: (UInt64, UInt64),
157+
file: String = #file,
158+
line: UInt = #line) {
159+
var hasher = Hasher(_seed: seed)
160+
buffer.withUnsafeBytes { buffer in
161+
hasher.combine(bytes: buffer)
162+
}
163+
let expected = hasher.finalize()
164+
let actual = buffer.withUnsafeBytes { buffer in
165+
Hasher._hash(seed: seed, bytes: buffer)
166+
}
167+
expectEqual(
168+
actual,
169+
expected,
170+
"seed: \(seed), buffer: \(buffer)",
171+
file: file,
172+
line: line)
173+
}
174+
for seed: (UInt64, UInt64) in [
175+
(0, 0),
176+
(1, 0),
177+
(1, 1),
178+
(0x1827364554637281, 0xf9e8d7c6b5a49382)
179+
] {
180+
var zeros: [UInt8] = []
181+
var integers: [UInt8] = []
182+
for i: UInt8 in 0 ..< 20 {
183+
zeros.append(0)
184+
checkTopLevelHash(for: zeros, seed: seed)
185+
integers.append(i)
186+
checkTopLevelHash(for: integers, seed: seed)
187+
}
188+
}
189+
}
190+
66191
runAllTests()

0 commit comments

Comments
 (0)