-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from crichez/floatingpoint-zero-hash-fix
Fix Floating Point Zero Hash Values
- Loading branch information
Showing
2 changed files
with
82 additions
and
3 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,79 @@ | ||
// | ||
// FloatingPointHashingTests.swift | ||
// | ||
// | ||
// Created by Christopher Richez on 2/3/22. | ||
// | ||
|
||
import FowlerNollVo | ||
import XCTest | ||
|
||
/// This test case asserts different representations of zero still return the same hash value. | ||
class FloatingPointHashingTests: XCTestCase { | ||
func test<T, Hasher>(type: T.Type, hasher: Hasher.Type) | ||
where T : BinaryFloatingPoint & FNVHashable, Hasher : FNVHasher, Hasher.Digest : Equatable { | ||
// Get both representations of zero | ||
let plusZero = T(sign: .plus, exponent: 1, significand: 0.0) | ||
let minusZero = T(sign: .minus, exponent: 1, significand: 0.0) | ||
|
||
// Hash both values | ||
var plusHasher = Hasher() | ||
plusZero.hash(into: &plusHasher) | ||
var minusHasher = Hasher() | ||
minusZero.hash(into: &minusHasher) | ||
|
||
// Compare their digests | ||
XCTAssertEqual(plusHasher.digest, minusHasher.digest) | ||
} | ||
|
||
func testFloat() { | ||
let type = Float.self | ||
test(type: type, hasher: FNV32.self) | ||
test(type: type, hasher: FNV32a.self) | ||
test(type: type, hasher: FNV64.self) | ||
test(type: type, hasher: FNV64a.self) | ||
test(type: type, hasher: FNV128.self) | ||
test(type: type, hasher: FNV128a.self) | ||
test(type: type, hasher: FNV256.self) | ||
test(type: type, hasher: FNV256a.self) | ||
test(type: type, hasher: FNV512.self) | ||
test(type: type, hasher: FNV512a.self) | ||
test(type: type, hasher: FNV1024.self) | ||
test(type: type, hasher: FNV1024a.self) | ||
} | ||
|
||
func testDouble() { | ||
let type = Double.self | ||
test(type: type, hasher: FNV32.self) | ||
test(type: type, hasher: FNV32a.self) | ||
test(type: type, hasher: FNV64.self) | ||
test(type: type, hasher: FNV64a.self) | ||
test(type: type, hasher: FNV128.self) | ||
test(type: type, hasher: FNV128a.self) | ||
test(type: type, hasher: FNV256.self) | ||
test(type: type, hasher: FNV256a.self) | ||
test(type: type, hasher: FNV512.self) | ||
test(type: type, hasher: FNV512a.self) | ||
test(type: type, hasher: FNV1024.self) | ||
test(type: type, hasher: FNV1024a.self) | ||
} | ||
|
||
#if swift(>=5.4) && !((os(macOS) || targetEnvironment(macCatalyst)) && arch(x86_64)) | ||
@available(macOS 11, iOS 14, tvOS 14, watchOS 7, *) | ||
func testFloat16() { | ||
let type = Float16.self | ||
test(type: type, hasher: FNV32.self) | ||
test(type: type, hasher: FNV32a.self) | ||
test(type: type, hasher: FNV64.self) | ||
test(type: type, hasher: FNV64a.self) | ||
test(type: type, hasher: FNV128.self) | ||
test(type: type, hasher: FNV128a.self) | ||
test(type: type, hasher: FNV256.self) | ||
test(type: type, hasher: FNV256a.self) | ||
test(type: type, hasher: FNV512.self) | ||
test(type: type, hasher: FNV512a.self) | ||
test(type: type, hasher: FNV1024.self) | ||
test(type: type, hasher: FNV1024a.self) | ||
} | ||
#endif | ||
} |