From 024bfb135784f437cb81c16990bad795ab81a1b9 Mon Sep 17 00:00:00 2001 From: Erik van Sebille Date: Fri, 22 Aug 2025 09:58:14 +0200 Subject: [PATCH] Adding the round_and_hash_float_array also to v3 So we can easily compare unit test results between v3 and v4 --- tests/utils.py | 16 ++++++++++++++++ 1 file changed, 16 insertions(+) diff --git a/tests/utils.py b/tests/utils.py index 35e134b21e..b1288cbda2 100644 --- a/tests/utils.py +++ b/tests/utils.py @@ -1,5 +1,6 @@ """General helper functions and utilies for test suite.""" +import struct from pathlib import Path import numpy as np @@ -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)