From 628e31cf62a94f1828587181c48998d6a9225a1f Mon Sep 17 00:00:00 2001 From: Nikita-str <42584606+Nikita-str@users.noreply.github.com> Date: Sat, 28 Sep 2024 20:56:39 +0300 Subject: [PATCH] #4009: eq bug fix + tests (#4010) --- core/engine/src/value/integer.rs | 49 ++++++++++++++++++++++++++------ 1 file changed, 40 insertions(+), 9 deletions(-) diff --git a/core/engine/src/value/integer.rs b/core/engine/src/value/integer.rs index 0218fb95bc..9c2d84e5bf 100644 --- a/core/engine/src/value/integer.rs +++ b/core/engine/src/value/integer.rs @@ -74,10 +74,7 @@ impl PartialEq for IntegerOrInfinity { impl PartialEq for i64 { fn eq(&self, other: &IntegerOrInfinity) -> bool { - match other { - IntegerOrInfinity::Integer(i) => i == other, - _ => false, - } + other.eq(self) } } @@ -93,10 +90,44 @@ impl PartialOrd for IntegerOrInfinity { impl PartialOrd for i64 { fn partial_cmp(&self, other: &IntegerOrInfinity) -> Option { - match other { - IntegerOrInfinity::PositiveInfinity => Some(Ordering::Less), - IntegerOrInfinity::Integer(i) => self.partial_cmp(i), - IntegerOrInfinity::NegativeInfinity => Some(Ordering::Greater), - } + other.partial_cmp(self).map(Ordering::reverse) + } +} + +#[cfg(test)] +mod tests { + use super::*; + + #[test] + fn test_eq() { + let int: i64 = 42; + let int_or_inf = IntegerOrInfinity::Integer(10); + assert!(int != int_or_inf); + assert!(int_or_inf != int); + + let int: i64 = 10; + assert!(int == int_or_inf); + assert!(int_or_inf == int); + } + + #[test] + fn test_ord() { + let int: i64 = 42; + + let int_or_inf = IntegerOrInfinity::Integer(10); + assert!(int_or_inf < int); + assert!(int > int_or_inf); + + let int_or_inf = IntegerOrInfinity::Integer(100); + assert!(int_or_inf > int); + assert!(int < int_or_inf); + + let int_or_inf = IntegerOrInfinity::PositiveInfinity; + assert!(int_or_inf > int); + assert!(int < int_or_inf); + + let int_or_inf = IntegerOrInfinity::NegativeInfinity; + assert!(int_or_inf < int); + assert!(int > int_or_inf); } }