Skip to content

Commit

Permalink
Implement equality comparisons on System.Array
Browse files Browse the repository at this point in the history
  • Loading branch information
BCSharp committed Nov 29, 2024
1 parent aefaecb commit 1aa22d1
Show file tree
Hide file tree
Showing 2 changed files with 67 additions and 0 deletions.
28 changes: 28 additions & 0 deletions Src/IronPython/Runtime/Operations/ArrayOps.cs
Original file line number Diff line number Diff line change
Expand Up @@ -87,6 +87,34 @@ public static object __new__(CodeContext context, PythonType pythonType, object
return res;
}

[StaticExtensionMethod]
public static object __eq__(CodeContext context, Array self, [NotNone] Array other) {
if (self is null) throw PythonOps.TypeError("expected Array, got None");
if (other is null) throw PythonOps.TypeError("expected Array, got None");

if (self.GetType() != other.GetType()) return ScriptingRuntimeHelpers.False;
if (self.Length != other.Length) return ScriptingRuntimeHelpers.False;
for (int i = 0; i < self.Rank; i++) {
if (self.GetLowerBound(i) != other.GetLowerBound(i)) return ScriptingRuntimeHelpers.False;
}

return ScriptingRuntimeHelpers.BooleanToObject(
((IStructuralEquatable)self).Equals(other, context.LanguageContext.EqualityComparerNonGeneric)
);
}

[StaticExtensionMethod]
[return: MaybeNotImplemented]
public static object __eq__(CodeContext context, object self, object? other) => NotImplementedType.Value;

[StaticExtensionMethod]
public static object __ne__(CodeContext context, Array self, [NotNone] Array other)
=> ScriptingRuntimeHelpers.BooleanToObject(ReferenceEquals(__eq__(context, self, other), ScriptingRuntimeHelpers.False));

[StaticExtensionMethod]
[return: MaybeNotImplemented]
public static object __ne__(CodeContext context, object self, object? other) => NotImplementedType.Value;

/// <summary>
/// Multiply two object[] arrays - slow version, we need to get the type, etc...
/// </summary>
Expand Down
39 changes: 39 additions & 0 deletions Tests/test_array.py
Original file line number Diff line number Diff line change
Expand Up @@ -279,4 +279,43 @@ def test_tuple_indexer(self):
array1[0,0] = 5
self.assertEqual(array1[0,0], array1[(0,0)])

def test_equality(self):
a = System.Array.CreateInstance(int, 5)
a2 = System.Array.CreateInstance(int, 5) # same as a
b = System.Array.CreateInstance(int, 5, 6) # different rank
c = System.Array.CreateInstance(int, 6) # different length
d = System.Array.CreateInstance(int, (5,), (1,)) # different base
e = System.Array.CreateInstance(System.Int32, 5) # different element type
l = [0] * 5 # different type

self.assertTrue(a == a2)
self.assertTrue(a2 == a)
self.assertFalse(a != a2)
self.assertFalse(a2 != a)

self.assertFalse(a == b)
self.assertFalse(b == a)
self.assertTrue(a != b)
self.assertTrue(b != a)

self.assertFalse(a == c)
self.assertFalse(c == a)
self.assertTrue(a != c)
self.assertTrue(c != a)

self.assertFalse(a == d)
self.assertFalse(d == a)
self.assertTrue(a != d)
self.assertTrue(d != a)

self.assertFalse(a == e)
self.assertFalse(e == a)
self.assertTrue(a != e)
self.assertTrue(e != a)

self.assertFalse(a == l)
self.assertFalse(l == a)
self.assertTrue(a != l)
self.assertTrue(l != a)

run_test(__name__)

0 comments on commit 1aa22d1

Please sign in to comment.