Skip to content

Commit fba8ba3

Browse files
committed
Adjust tests to upstream change of NaN semantics in dictionaries
Also add more hash tests.
1 parent 5fe24d0 commit fba8ba3

File tree

2 files changed

+32
-4
lines changed

2 files changed

+32
-4
lines changed

godot-core/src/builtin/dictionary.rs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -142,6 +142,7 @@ impl Dictionary {
142142
}
143143

144144
/// Returns a 32-bit integer hash value representing the dictionary and its contents.
145+
#[must_use]
145146
pub fn hash(&self) -> u32 {
146147
self.as_inner().hash().try_into().unwrap()
147148
}

itest/rust/src/builtin_tests/containers/dictionary_test.rs

Lines changed: 31 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -95,6 +95,7 @@ fn dictionary_clone() {
9595
"foo": 0,
9696
"bar": subdictionary.clone()
9797
};
98+
9899
#[allow(clippy::redundant_clone)]
99100
let clone = dictionary.clone();
100101
Dictionary::from_variant(&clone.get("bar").unwrap()).insert("final", 4);
@@ -103,12 +104,33 @@ fn dictionary_clone() {
103104

104105
#[itest]
105106
fn dictionary_hash() {
106-
let dictionary = dict! {
107+
use godot::builtin::Vector2i;
108+
109+
let a = dict! {
107110
"foo": 0,
108111
"bar": true,
109-
"baz": "foobar"
112+
(Vector2i::new(4, -1)): "foobar",
113+
};
114+
let b = dict! {
115+
"foo": 0,
116+
"bar": true,
117+
(Vector2i::new(4, -1)): "foobar" // No comma to test macro.
110118
};
111-
dictionary.hash();
119+
let c = dict! {
120+
"foo": 0,
121+
(Vector2i::new(4, -1)): "foobar",
122+
"bar": true,
123+
};
124+
125+
assert_eq!(a.hash(), b.hash(), "equal dictionaries have same hash");
126+
assert_ne!(
127+
a.hash(),
128+
c.hash(),
129+
"dictionaries with reordered content have different hash"
130+
);
131+
132+
// NaNs are not equal (since Godot 4.2) but share same hash.
133+
assert_eq!(dict! {772: f32::NAN}.hash(), dict! {772: f32::NAN}.hash());
112134
}
113135

114136
#[itest]
@@ -328,8 +350,13 @@ fn dictionary_keys_values() {
328350
#[itest]
329351
fn dictionary_equal() {
330352
assert_eq!(dict! {"foo": "bar"}, dict! {"foo": "bar"});
331-
assert_eq!(dict! {1: f32::NAN}, dict! {1: f32::NAN}); // yes apparently Godot considers these equal
332353
assert_ne!(dict! {"foo": "bar"}, dict! {"bar": "foo"});
354+
355+
// Changed in https://github.com/godotengine/godot/pull/74588.
356+
#[cfg(before_api = "4.2")]
357+
assert_eq!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
358+
#[cfg(since_api = "4.2")]
359+
assert_ne!(dict! {1: f32::NAN}, dict! {1: f32::NAN});
333360
}
334361

335362
#[itest]

0 commit comments

Comments
 (0)