Skip to content

Commit 86f2c2a

Browse files
committed
Add hash_float_array testing helper
1 parent 04c75f4 commit 86f2c2a

1 file changed

Lines changed: 14 additions & 0 deletions

File tree

tests/utils.py

Lines changed: 14 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
from __future__ import annotations
44

5+
import struct
56
from pathlib import Path
67
from typing import TYPE_CHECKING
78

@@ -133,3 +134,16 @@ def assert_valid_field_data(data: xr.DataArray, grid: XGrid):
133134
if ax_actual is None:
134135
continue # None is ok
135136
assert ax_actual == ax_expected, f"Expected axis {ax_expected} for dimension '{dim}', got {ax_actual}"
137+
138+
139+
def hash_float_array(arr):
140+
# Adapted from https://cs.stackexchange.com/a/37965
141+
h = 1
142+
for f in arr:
143+
# Mimic Float.floatToIntBits: converts float to 4-byte binary, then interprets as int
144+
float_as_int = struct.unpack("!i", struct.pack("!f", f))[0]
145+
h = 31 * h + float_as_int
146+
147+
# Mimic Java's HashMap hash transformation
148+
h ^= (h >> 20) ^ (h >> 12)
149+
return h ^ (h >> 7) ^ (h >> 4)

0 commit comments

Comments
 (0)