Skip to content
Merged
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
16 changes: 16 additions & 0 deletions tests/utils.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,6 @@
"""General helper functions and utilies for test suite."""

import struct
from pathlib import Path

import numpy as np
Expand Down Expand Up @@ -116,3 +117,18 @@ def create_fieldset_zeros_simple(xdim=40, ydim=100, withtime=False):

def assert_empty_folder(path: Path):
assert [p.name for p in path.iterdir()] == []


def round_and_hash_float_array(arr, decimals=6):
arr = np.round(arr, decimals=decimals)

# Adapted from https://cs.stackexchange.com/a/37965
h = 1
for f in arr:
# Mimic Float.floatToIntBits: converts float to 4-byte binary, then interprets as int
float_as_int = struct.unpack("!i", struct.pack("!f", f))[0]
h = 31 * h + float_as_int

# Mimic Java's HashMap hash transformation
h ^= (h >> 20) ^ (h >> 12)
return h ^ (h >> 7) ^ (h >> 4)
Loading