Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
10 changes: 8 additions & 2 deletions array/view_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -195,8 +195,14 @@ test "arrayview_arbitrary" {
///|
test "arrayview_hash" {
let arr : Array[ArrayView[Int]] = @quickcheck.samples(20)
inspect(arr[5:9].hash(), content="-966877954")
inspect(arr[10:15].hash(), content="-951019668")
inspect(
Hasher::new(seed=0)..combine(arr[5:9]).finalize(),
content="-966877954",
)
inspect(
Hasher::new(seed=0)..combine(arr[10:15]).finalize(),
content="-951019668",
)
}

///|
Expand Down
2 changes: 1 addition & 1 deletion bigint/bigint.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -221,7 +221,7 @@ test "can_convert_to_int64" {
/// Example:
///
/// ```moonbit
/// let hasher = Hasher::new()
/// let hasher = Hasher::new(seed=0)
/// let big = 12345N
/// hasher.combine(big)
/// inspect(hasher.finalize(), content="890579181")
Expand Down
27 changes: 16 additions & 11 deletions bigint/bigint_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -1015,38 +1015,43 @@ test "compare_int64" {
///|
test "@bigint.BigInt::hash" {
// Test zero
inspect(0N.hash(), content="-813420232")
inspect(Hasher::new(seed=0)..combine(0N).finalize(), content="-813420232")
assert_eq(0N.hash(), (-0N).hash())

// Positive and negative of same magnitude should have different hashes
assert_not_eq(1N.hash(), (-1N).hash())

// Test small positive numbers
inspect(42N.hash(), content="2103260413")
inspect(Hasher::new(seed=0)..combine(42N).finalize(), content="2103260413")

// Test small negative numbers
inspect((-42N).hash(), content="-504604139")
inspect(Hasher::new(seed=0)..combine(-42N).finalize(), content="-504604139")

// Test larger numbers
let large = 123456789N
assert_not_eq(large.hash(), (-large).hash())
inspect(Hasher::new(seed=0)..combine(large).finalize(), content="275171670")
inspect(Hasher::new(seed=0)..combine(-large).finalize(), content="-200902609")

// Test very large numbers (multi-limb)
let very_large = 12345678901234567890123456789N
inspect(very_large.hash(), content="2115080653")
inspect((-very_large).hash(), content="-1781872667")
assert_not_eq(very_large.hash(), (-very_large).hash())
inspect(
Hasher::new(seed=0)..combine(very_large).finalize(),
content="2115080653",
)
inspect(
Hasher::new(seed=0)..combine(-very_large).finalize(),
content="-1781872667",
)

// Test that equal BigInts have equal hashes
let a = 987654321098765432N
let b = @bigint.BigInt::from_string("00987654321098765432")
inspect(a.hash(), content="-1950963429")
assert_eq(a.hash(), b.hash())
inspect(Hasher::new(seed=0)..combine(a).finalize(), content="-1950963429")
inspect(Hasher::new(seed=0)..combine(b).finalize(), content="-1950963429")

// Test that different BigInts have different hashes (usually)
let c = a + 1N
inspect(c.hash(), content="-959383658")
assert_not_eq(a.hash(), c.hash())
inspect(Hasher::new(seed=0)..combine(c).finalize(), content="-959383658")
}

///|
Expand Down
5 changes: 0 additions & 5 deletions bool/bool.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,6 @@ pub fn to_uint64(self : Bool) -> UInt64 {
}
}

///|
pub impl Hash for Bool with hash(self) {
self.to_int()
}

///|
pub impl Hash for Bool with hash_combine(self, hasher) {
hasher.combine_bool(self)
Expand Down
4 changes: 2 additions & 2 deletions bool/bool_test.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -14,8 +14,8 @@

///|
test "Bool hash function" {
inspect(true.hash(), content="1")
inspect(false.hash(), content="0")
inspect(Hasher::new(seed=0)..combine(true).finalize(), content="-205818221")
inspect(Hasher::new(seed=0)..combine(false).finalize(), content="148298089")
}

///|
Expand Down
22 changes: 1 addition & 21 deletions builtin/byte.mbt
Original file line number Diff line number Diff line change
Expand Up @@ -160,26 +160,6 @@ pub fn Byte::to_string(self : Byte) -> String {
"b'\\x\{hi}\{lo}'"
}

///|
/// Implements the `Hash` trait for `Byte` type by converting the byte to an
/// integer and using it as the hash value.
///
/// Parameters:
///
/// * `self` : The byte value to be hashed.
///
/// Returns an integer representing the hash value of the byte.
///
/// Example:
///
/// ```moonbit
/// let b = b'\x42'
/// inspect(Hash::hash(b), content="66") // ASCII value of 'B'
/// ```
pub impl Hash for Byte with hash(self) {
self.to_int()
}

///|
/// Implements the `Hash` trait for `Byte` type by providing a `hash_combine`
/// method that combines a byte value with a hasher.
Expand All @@ -193,7 +173,7 @@ pub impl Hash for Byte with hash(self) {
/// Example:
///
/// ```moonbit
/// let hasher = Hasher::new()
/// let hasher = Hasher::new(seed=0)
/// hasher.combine_byte(b'\xFF')
/// inspect(hasher.finalize(), content="1955036104")
/// ```
Expand Down
Loading
Loading