2023-06-03 14:55:13 +00:00
|
|
|
from typing import Type, Union
|
|
|
|
|
|
|
|
import pytest
|
|
|
|
from pyo3_pytests.comparisons import Eq, EqDefaultNe, Ordered, OrderedDefaultNe
|
|
|
|
from typing_extensions import Self
|
|
|
|
|
|
|
|
|
|
|
|
class PyEq:
|
|
|
|
def __init__(self, x: int) -> None:
|
|
|
|
self.x = x
|
|
|
|
|
|
|
|
def __eq__(self, other: Self) -> bool:
|
|
|
|
return self.x == other.x
|
|
|
|
|
|
|
|
def __ne__(self, other: Self) -> bool:
|
|
|
|
return self.x != other.x
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("ty", (Eq, PyEq), ids=("rust", "python"))
|
|
|
|
def test_eq(ty: Type[Union[Eq, PyEq]]):
|
|
|
|
a = ty(0)
|
|
|
|
b = ty(0)
|
|
|
|
c = ty(1)
|
|
|
|
|
|
|
|
assert a == b
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a != b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert a != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
|
|
|
|
assert b == a
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a != b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert b != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (b == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a <= b
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a >= b
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a < c
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert c > a
|
|
|
|
|
|
|
|
|
|
|
|
class PyEqDefaultNe:
|
|
|
|
def __init__(self, x: int) -> None:
|
|
|
|
self.x = x
|
|
|
|
|
|
|
|
def __eq__(self, other: Self) -> bool:
|
|
|
|
return self.x == other.x
|
|
|
|
|
|
|
|
|
2023-10-11 08:23:25 +00:00
|
|
|
@pytest.mark.parametrize("ty", (EqDefaultNe, PyEqDefaultNe), ids=("rust", "python"))
|
2023-06-03 14:55:13 +00:00
|
|
|
def test_eq_default_ne(ty: Type[Union[EqDefaultNe, PyEqDefaultNe]]):
|
|
|
|
a = ty(0)
|
|
|
|
b = ty(0)
|
|
|
|
c = ty(1)
|
|
|
|
|
|
|
|
assert a == b
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a != b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert a != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
|
|
|
|
assert b == a
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a != b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert b != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (b == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a <= b
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a >= b
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert a < c
|
|
|
|
|
|
|
|
with pytest.raises(TypeError):
|
|
|
|
assert c > a
|
|
|
|
|
|
|
|
|
|
|
|
class PyOrdered:
|
|
|
|
def __init__(self, x: int) -> None:
|
|
|
|
self.x = x
|
|
|
|
|
|
|
|
def __lt__(self, other: Self) -> bool:
|
|
|
|
return self.x < other.x
|
|
|
|
|
|
|
|
def __le__(self, other: Self) -> bool:
|
|
|
|
return self.x <= other.x
|
|
|
|
|
|
|
|
def __eq__(self, other: Self) -> bool:
|
|
|
|
return self.x == other.x
|
|
|
|
|
|
|
|
def __ne__(self, other: Self) -> bool:
|
|
|
|
return self.x != other.x
|
|
|
|
|
|
|
|
def __gt__(self, other: Self) -> bool:
|
|
|
|
return self.x >= other.x
|
|
|
|
|
|
|
|
def __ge__(self, other: Self) -> bool:
|
|
|
|
return self.x >= other.x
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize("ty", (Ordered, PyOrdered), ids=("rust", "python"))
|
|
|
|
def test_ordered(ty: Type[Union[Ordered, PyOrdered]]):
|
|
|
|
a = ty(0)
|
|
|
|
b = ty(0)
|
|
|
|
c = ty(1)
|
|
|
|
|
|
|
|
assert a == b
|
|
|
|
assert a <= b
|
|
|
|
assert a >= b
|
|
|
|
assert a != c
|
|
|
|
assert a <= c
|
|
|
|
|
|
|
|
assert b == a
|
|
|
|
assert b <= a
|
|
|
|
assert b >= a
|
|
|
|
assert b != c
|
|
|
|
assert b <= c
|
|
|
|
|
|
|
|
assert c != a
|
|
|
|
assert c != b
|
|
|
|
assert c > a
|
|
|
|
assert c >= a
|
|
|
|
assert c > b
|
|
|
|
assert c >= b
|
|
|
|
|
|
|
|
|
|
|
|
class PyOrderedDefaultNe:
|
|
|
|
def __init__(self, x: int) -> None:
|
|
|
|
self.x = x
|
|
|
|
|
|
|
|
def __lt__(self, other: Self) -> bool:
|
|
|
|
return self.x < other.x
|
|
|
|
|
|
|
|
def __le__(self, other: Self) -> bool:
|
|
|
|
return self.x <= other.x
|
|
|
|
|
|
|
|
def __eq__(self, other: Self) -> bool:
|
|
|
|
return self.x == other.x
|
|
|
|
|
|
|
|
def __gt__(self, other: Self) -> bool:
|
|
|
|
return self.x >= other.x
|
|
|
|
|
|
|
|
def __ge__(self, other: Self) -> bool:
|
|
|
|
return self.x >= other.x
|
|
|
|
|
|
|
|
|
|
|
|
@pytest.mark.parametrize(
|
|
|
|
"ty", (OrderedDefaultNe, PyOrderedDefaultNe), ids=("rust", "python")
|
|
|
|
)
|
|
|
|
def test_ordered_default_ne(ty: Type[Union[OrderedDefaultNe, PyOrderedDefaultNe]]):
|
|
|
|
a = ty(0)
|
|
|
|
b = ty(0)
|
|
|
|
c = ty(1)
|
|
|
|
|
|
|
|
assert a == b
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a != b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert a <= b
|
|
|
|
assert a >= b
|
|
|
|
assert a != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (a == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert a <= c
|
|
|
|
|
|
|
|
assert b == a
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (b != a)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert b <= a
|
|
|
|
assert b >= a
|
|
|
|
assert b != c
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (b == c)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert b <= c
|
|
|
|
|
|
|
|
assert c != a
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (c == a)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert c != b
|
2023-10-11 08:23:25 +00:00
|
|
|
assert not (c == b)
|
2023-06-03 14:55:13 +00:00
|
|
|
assert c > a
|
|
|
|
assert c >= a
|
|
|
|
assert c > b
|
|
|
|
assert c >= b
|