Skip to content

Commit 2bb8d24

Browse files
committed
Implement equality comparisons on System.Array
1 parent aefaecb commit 2bb8d24

File tree

2 files changed

+63
-0
lines changed

2 files changed

+63
-0
lines changed

Src/IronPython/Runtime/Operations/ArrayOps.cs

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,30 @@ public static object __new__(CodeContext context, PythonType pythonType, object
8787
return res;
8888
}
8989

90+
[StaticExtensionMethod]
91+
public static object __eq__(CodeContext context, Array self, [NotNone] Array other) {
92+
if (self is null) throw PythonOps.TypeError("expected Array, got None");
93+
if (other is null) throw PythonOps.TypeError("expected Array, got None");
94+
95+
if (self.GetType() != other.GetType()) return ScriptingRuntimeHelpers.False;
96+
97+
return ScriptingRuntimeHelpers.BooleanToObject(
98+
((IStructuralEquatable)self).Equals(other, context.LanguageContext.EqualityComparerNonGeneric)
99+
);
100+
}
101+
102+
[StaticExtensionMethod]
103+
[return: MaybeNotImplemented]
104+
public static object __eq__(CodeContext context, object self, object? other) => NotImplementedType.Value;
105+
106+
[StaticExtensionMethod]
107+
public static object __ne__(CodeContext context, Array self, [NotNone] Array other)
108+
=> ScriptingRuntimeHelpers.BooleanToObject(ReferenceEquals(__eq__(context, self, other), ScriptingRuntimeHelpers.False));
109+
110+
[StaticExtensionMethod]
111+
[return: MaybeNotImplemented]
112+
public static object __ne__(CodeContext context, object self, object? other) => NotImplementedType.Value;
113+
90114
/// <summary>
91115
/// Multiply two object[] arrays - slow version, we need to get the type, etc...
92116
/// </summary>

Tests/test_array.py

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -279,4 +279,43 @@ def test_tuple_indexer(self):
279279
array1[0,0] = 5
280280
self.assertEqual(array1[0,0], array1[(0,0)])
281281

282+
def test_equality(self):
283+
a = System.Array.CreateInstance(int, 5)
284+
a2 = System.Array.CreateInstance(int, 5) # same as a
285+
b = System.Array.CreateInstance(int, 5, 6) # different rank
286+
c = System.Array.CreateInstance(int, 6) # different length
287+
d = System.Array.CreateInstance(int, (5,), (1,)) # different base
288+
e = System.Array.CreateInstance(System.Int32, 5) # different element type
289+
l = [0] * 5 # different type
290+
291+
self.assertTrue(a == a2)
292+
self.assertTrue(a2 == a)
293+
self.assertFalse(a != a2)
294+
self.assertFalse(a2 != a)
295+
296+
self.assertFalse(a == b)
297+
self.assertFalse(b == a)
298+
self.assertTrue(a != b)
299+
self.assertTrue(b != a)
300+
301+
self.assertFalse(a == c)
302+
self.assertFalse(c == a)
303+
self.assertTrue(a != c)
304+
self.assertTrue(c != a)
305+
306+
self.assertFalse(a == d)
307+
self.assertFalse(d == a)
308+
self.assertTrue(a != d)
309+
self.assertTrue(d != a)
310+
311+
self.assertFalse(a == e)
312+
self.assertFalse(e == a)
313+
self.assertTrue(a != e)
314+
self.assertTrue(e != a)
315+
316+
self.assertFalse(a == l)
317+
self.assertFalse(l == a)
318+
self.assertTrue(a != l)
319+
self.assertTrue(l != a)
320+
282321
run_test(__name__)

0 commit comments

Comments
 (0)