codon/test/stdlib/operator_test.codon

621 lines
13 KiB
Python
Raw Normal View History

2021-09-28 02:02:44 +08:00
from operator import *
import operator
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
def apply1(f, x, expected):
return f(x) == expected
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
def apply2(f, lhs, rhs, expected):
return f(lhs, rhs) == expected
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
n_eq = 0
n_ne = 0
n_lt = 0
n_le = 0
n_gt = 0
n_ge = 0
n_len = 0
n_bool = 0
n_abs = 0
n_pos = 0
n_neg = 0
n_inv = 0
n_index = 0
n_add = 0
n_sub = 0
n_mul = 0
n_matmul = 0
n_truediv = 0
n_floordiv = 0
n_mod = 0
n_pow = 0
n_and = 0
n_or = 0
n_xor = 0
n_lshift = 0
n_rshift = 0
n_iadd = 0
n_isub = 0
n_imul = 0
n_imatmul = 0
n_itruediv = 0
n_ifloordiv = 0
n_imod = 0
n_ipow = 0
n_iand = 0
n_ior = 0
n_ixor = 0
n_ilshift = 0
n_irshift = 0
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class C:
def __eq__(self, other: C):
global n_eq
n_eq += 1
return False
def __ne__(self, other: C):
global n_ne
n_ne += 1
return False
def __lt__(self, other: C):
global n_lt
n_lt += 1
return False
def __le__(self, other: C):
global n_le
n_le += 1
return False
def __gt__(self, other: C):
global n_gt
n_gt += 1
return False
def __ge__(self, other: C):
global n_ge
n_ge += 1
return False
def __len__(self):
global n_len
n_len += 1
return 42
def __bool__(self):
global n_bool
n_bool += 1
return True
def __abs__(self):
global n_abs
n_abs += 1
2022-01-24 18:32:33 +08:00
return "abs"
2021-09-28 02:02:44 +08:00
def __pos__(self):
global n_pos
n_pos += 1
2022-01-24 18:32:33 +08:00
return "pos"
2021-09-28 02:02:44 +08:00
def __neg__(self):
global n_neg
n_neg += 1
2022-01-24 18:32:33 +08:00
return "neg"
2021-09-28 02:02:44 +08:00
def __invert__(self):
global n_inv
n_inv += 1
2022-01-24 18:32:33 +08:00
return "inv"
2021-09-28 02:02:44 +08:00
def __index__(self):
global n_index
n_index += 1
2022-01-24 18:32:33 +08:00
return "index"
2021-09-28 02:02:44 +08:00
def __add__(self, other: C):
global n_add
n_add += 1
2022-01-24 18:32:33 +08:00
return "add"
2021-09-28 02:02:44 +08:00
def __sub__(self, other: C):
global n_sub
n_sub += 1
2022-01-24 18:32:33 +08:00
return "sub"
2021-09-28 02:02:44 +08:00
def __mul__(self, other: C):
global n_mul
n_mul += 1
2022-01-24 18:32:33 +08:00
return "mul"
2021-09-28 02:02:44 +08:00
def __matmul__(self, other: C):
global n_matmul
n_matmul += 1
2022-01-24 18:32:33 +08:00
return "matmul"
2021-09-28 02:02:44 +08:00
def __truediv__(self, other: C):
global n_truediv
n_truediv += 1
2022-01-24 18:32:33 +08:00
return "truediv"
2021-09-28 02:02:44 +08:00
def __floordiv__(self, other: C):
global n_floordiv
n_floordiv += 1
2022-01-24 18:32:33 +08:00
return "floordiv"
2021-09-28 02:02:44 +08:00
def __mod__(self, other: C):
global n_mod
n_mod += 1
2022-01-24 18:32:33 +08:00
return "mod"
2021-09-28 02:02:44 +08:00
def __pow__(self, other: C):
global n_pow
n_pow += 1
2022-01-24 18:32:33 +08:00
return "pow"
2021-09-28 02:02:44 +08:00
def __and__(self, other: C):
global n_and
n_and += 1
2022-01-24 18:32:33 +08:00
return "and"
2021-09-28 02:02:44 +08:00
def __or__(self, other: C):
global n_or
n_or += 1
2022-01-24 18:32:33 +08:00
return "or"
2021-09-28 02:02:44 +08:00
def __xor__(self, other: C):
global n_xor
n_xor += 1
2022-01-24 18:32:33 +08:00
return "xor"
2021-09-28 02:02:44 +08:00
def __lshift__(self, other: C):
global n_lshift
n_lshift += 1
2022-01-24 18:32:33 +08:00
return "lshift"
2021-09-28 02:02:44 +08:00
def __rshift__(self, other: C):
global n_rshift
n_rshift += 1
2022-01-24 18:32:33 +08:00
return "rshift"
2021-09-28 02:02:44 +08:00
def __iadd__(self, other: C):
global n_iadd
n_iadd += 1
return self
def __isub__(self, other: C):
global n_isub
n_isub += 1
return self
def __imul__(self, other: C):
global n_imul
n_imul += 1
return self
def __imatmul__(self, other: C):
global n_imatmul
n_imatmul += 1
return self
def __itruediv__(self, other: C):
global n_itruediv
n_itruediv += 1
return self
def __ifloordiv__(self, other: C):
global n_ifloordiv
n_ifloordiv += 1
return self
def __imod__(self, other: C):
global n_imod
n_imod += 1
return self
def __ipow__(self, other: C):
global n_ipow
n_ipow += 1
return self
def __iand__(self, other: C):
global n_iand
n_iand += 1
return self
def __ior__(self, other: C):
global n_ior
n_ior += 1
return self
def __ixor__(self, other: C):
global n_ixor
n_ixor += 1
return self
def __ilshift__(self, other: C):
global n_ilshift
n_ilshift += 1
return self
def __irshift__(self, other: C):
global n_irshift
n_irshift += 1
2022-01-24 18:32:33 +08:00
return "rshift"
2021-09-28 02:02:44 +08:00
@test
def test_comparisons():
assert apply2(eq, 1, 2, False)
assert apply2(eq, 2, 1, False)
assert apply2(eq, 1, 1, True)
assert apply2(eq, 1, 1.1, False)
assert apply2(operator.__eq__, 1, 2, False)
assert apply2(operator.__eq__, 2, 1, False)
assert apply2(operator.__eq__, 1, 1, True)
assert apply2(operator.__eq__, 1, 1.1, False)
assert apply2(ne, 1, 2, True)
assert apply2(ne, 2, 1, True)
assert apply2(ne, 1, 1, False)
assert apply2(ne, 1, 1.1, True)
assert apply2(operator.__ne__, 1, 2, True)
assert apply2(operator.__ne__, 2, 1, True)
assert apply2(operator.__ne__, 1, 1, False)
assert apply2(operator.__ne__, 1, 1.1, True)
assert apply2(lt, 1, 2, True)
assert apply2(lt, 2, 1, False)
assert apply2(lt, 1, 1, False)
assert apply2(lt, 1, 1.1, True)
assert apply2(operator.__lt__, 1, 2, True)
assert apply2(operator.__lt__, 2, 1, False)
assert apply2(operator.__lt__, 1, 1, False)
assert apply2(operator.__lt__, 1, 1.1, True)
assert apply2(le, 1, 2, True)
assert apply2(le, 2, 1, False)
assert apply2(le, 1, 1, True)
assert apply2(le, 1, 1.1, True)
assert apply2(operator.__le__, 1, 2, True)
assert apply2(operator.__le__, 2, 1, False)
assert apply2(operator.__le__, 1, 1, True)
assert apply2(operator.__le__, 1, 1.1, True)
assert apply2(gt, 1, 2, False)
assert apply2(gt, 2, 1, True)
assert apply2(gt, 1, 1, False)
assert apply2(gt, 1, 1.1, False)
assert apply2(operator.__gt__, 1, 2, False)
assert apply2(operator.__gt__, 2, 1, True)
assert apply2(operator.__gt__, 1, 1, False)
assert apply2(operator.__gt__, 1, 1.1, False)
assert apply2(ge, 1, 2, False)
assert apply2(ge, 2, 1, True)
assert apply2(ge, 1, 1, True)
assert apply2(ge, 1, 1.1, False)
assert apply2(operator.__ge__, 1, 2, False)
assert apply2(operator.__ge__, 2, 1, True)
assert apply2(operator.__ge__, 1, 1, True)
assert apply2(operator.__ge__, 1, 1.1, False)
assert apply2(eq, C(), C(), False)
assert n_eq == 1
assert apply2(ne, C(), C(), False)
assert n_ne == 1
assert apply2(lt, C(), C(), False)
assert n_lt == 1
assert apply2(le, C(), C(), False)
assert n_le == 1
assert apply2(gt, C(), C(), False)
assert n_gt == 1
assert apply2(ge, C(), C(), False)
assert n_ge == 1
assert apply2(operator.__eq__, C(), C(), False)
assert n_eq == 2
assert apply2(operator.__ne__, C(), C(), False)
assert n_ne == 2
assert apply2(operator.__lt__, C(), C(), False)
assert n_lt == 2
assert apply2(operator.__le__, C(), C(), False)
assert n_le == 2
assert apply2(operator.__gt__, C(), C(), False)
assert n_gt == 2
assert apply2(operator.__ge__, C(), C(), False)
assert n_ge == 2
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
test_comparisons()
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class A1:
def __bool__(self):
return True
def __len__(self):
return 0
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class A2:
def __len__(self):
return 42
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
@test
def test_truthiness():
assert truth(1)
assert not truth(0)
assert not_(False)
assert not not_(True)
assert not not_(A1())
assert not not_(A2())
a = A1()
b = A1()
assert is_(a, a)
assert not is_(a, b)
assert is_not(a, b)
assert not is_not(a, a)
assert truth(C())
assert n_bool == 1
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
test_truthiness()
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
@test
def test_ops():
x = C()
y = C()
2022-01-24 18:32:33 +08:00
assert abs(x) == "abs"
2021-09-28 02:02:44 +08:00
assert n_abs == 1
2022-01-24 18:32:33 +08:00
assert operator.__abs__(x) == "abs"
2021-09-28 02:02:44 +08:00
assert n_abs == 2
2022-01-24 18:32:33 +08:00
assert pos(x) == "pos"
2021-09-28 02:02:44 +08:00
assert n_pos == 1
2022-01-24 18:32:33 +08:00
assert operator.__pos__(x) == "pos"
2021-09-28 02:02:44 +08:00
assert n_pos == 2
2022-01-24 18:32:33 +08:00
assert neg(x) == "neg"
2021-09-28 02:02:44 +08:00
assert n_neg == 1
2022-01-24 18:32:33 +08:00
assert operator.__neg__(x) == "neg"
2021-09-28 02:02:44 +08:00
assert n_neg == 2
2022-01-24 18:32:33 +08:00
assert inv(x) == "inv"
2021-09-28 02:02:44 +08:00
assert n_inv == 1
2022-01-24 18:32:33 +08:00
assert operator.__inv__(x) == "inv"
2021-09-28 02:02:44 +08:00
assert n_inv == 2
2022-01-24 18:32:33 +08:00
assert invert(x) == "inv"
2021-09-28 02:02:44 +08:00
assert n_inv == 3
2022-01-24 18:32:33 +08:00
assert operator.__invert__(x) == "inv"
2021-09-28 02:02:44 +08:00
assert n_inv == 4
2022-01-24 18:32:33 +08:00
assert index(x) == "index"
2021-09-28 02:02:44 +08:00
assert n_index == 1
2022-01-24 18:32:33 +08:00
assert operator.__index__(x) == "index"
2021-09-28 02:02:44 +08:00
assert n_index == 2
2022-01-24 18:32:33 +08:00
assert add(x, y) == "add"
2021-09-28 02:02:44 +08:00
assert n_add == 1
2022-01-24 18:32:33 +08:00
assert operator.__add__(x, y) == "add"
2021-09-28 02:02:44 +08:00
assert n_add == 2
assert iadd(x, y) is x
assert n_iadd == 1
assert operator.__iadd__(x, y) is x
assert n_iadd == 2
2022-01-24 18:32:33 +08:00
assert sub(x, y) == "sub"
2021-09-28 02:02:44 +08:00
assert n_sub == 1
2022-01-24 18:32:33 +08:00
assert operator.__sub__(x, y) == "sub"
2021-09-28 02:02:44 +08:00
assert n_sub == 2
assert isub(x, y) is x
assert n_isub == 1
assert operator.__isub__(x, y) is x
assert n_isub == 2
2022-01-24 18:32:33 +08:00
assert mul(x, y) == "mul"
2021-09-28 02:02:44 +08:00
assert n_mul == 1
2022-01-24 18:32:33 +08:00
assert operator.__mul__(x, y) == "mul"
2021-09-28 02:02:44 +08:00
assert n_mul == 2
assert imul(x, y) is x
assert n_imul == 1
assert operator.__imul__(x, y) is x
assert n_imul == 2
2022-01-24 18:32:33 +08:00
assert matmul(x, y) == "matmul"
2021-09-28 02:02:44 +08:00
assert n_matmul == 1
2022-01-24 18:32:33 +08:00
assert operator.__matmul__(x, y) == "matmul"
2021-09-28 02:02:44 +08:00
assert n_matmul == 2
assert imatmul(x, y) is x
assert n_imatmul == 1
assert operator.__imatmul__(x, y) is x
assert n_imatmul == 2
2022-01-24 18:32:33 +08:00
assert truediv(x, y) == "truediv"
2021-09-28 02:02:44 +08:00
assert n_truediv == 1
2022-01-24 18:32:33 +08:00
assert operator.__truediv__(x, y) == "truediv"
2021-09-28 02:02:44 +08:00
assert n_truediv == 2
assert itruediv(x, y) is x
assert n_itruediv == 1
assert operator.__itruediv__(x, y) is x
assert n_itruediv == 2
2022-01-24 18:32:33 +08:00
assert floordiv(x, y) == "floordiv"
2021-09-28 02:02:44 +08:00
assert n_floordiv == 1
2022-01-24 18:32:33 +08:00
assert operator.__floordiv__(x, y) == "floordiv"
2021-09-28 02:02:44 +08:00
assert n_floordiv == 2
assert ifloordiv(x, y) is x
assert n_ifloordiv == 1
assert operator.__ifloordiv__(x, y) is x
assert n_ifloordiv == 2
2022-01-24 18:32:33 +08:00
assert mod(x, y) == "mod"
2021-09-28 02:02:44 +08:00
assert n_mod == 1
2022-01-24 18:32:33 +08:00
assert operator.__mod__(x, y) == "mod"
2021-09-28 02:02:44 +08:00
assert n_mod == 2
assert imod(x, y) is x
assert n_imod == 1
assert operator.__imod__(x, y) is x
assert n_imod == 2
2022-01-24 18:32:33 +08:00
assert pow(x, y) == "pow"
2021-09-28 02:02:44 +08:00
assert n_pow == 1
2022-01-24 18:32:33 +08:00
assert operator.__pow__(x, y) == "pow"
2021-09-28 02:02:44 +08:00
assert n_pow == 2
assert ipow(x, y) is x
assert n_ipow == 1
assert operator.__ipow__(x, y) is x
assert n_ipow == 2
2022-01-24 18:32:33 +08:00
assert and_(x, y) == "and"
2021-09-28 02:02:44 +08:00
assert n_and == 1
2022-01-24 18:32:33 +08:00
assert operator.__and__(x, y) == "and"
2021-09-28 02:02:44 +08:00
assert n_and == 2
assert iand(x, y) is x
assert n_iand == 1
assert operator.__iand__(x, y) is x
assert n_iand == 2
2022-01-24 18:32:33 +08:00
assert or_(x, y) == "or"
2021-09-28 02:02:44 +08:00
assert n_or == 1
2022-01-24 18:32:33 +08:00
assert operator.__or__(x, y) == "or"
2021-09-28 02:02:44 +08:00
assert n_or == 2
assert ior(x, y) is x
assert n_ior == 1
assert operator.__ior__(x, y) is x
assert n_ior == 2
2022-01-24 18:32:33 +08:00
assert xor(x, y) == "xor"
2021-09-28 02:02:44 +08:00
assert n_xor == 1
2022-01-24 18:32:33 +08:00
assert operator.__xor__(x, y) == "xor"
2021-09-28 02:02:44 +08:00
assert n_xor == 2
assert ixor(x, y) is x
assert n_ixor == 1
assert operator.__ixor__(x, y) is x
assert n_ixor == 2
2022-01-24 18:32:33 +08:00
assert lshift(x, y) == "lshift"
2021-09-28 02:02:44 +08:00
assert n_lshift == 1
2022-01-24 18:32:33 +08:00
assert operator.__lshift__(x, y) == "lshift"
2021-09-28 02:02:44 +08:00
assert n_lshift == 2
assert ilshift(x, y) is x
assert n_ilshift == 1
assert operator.__ilshift__(x, y) is x
assert n_ilshift == 2
2022-01-24 18:32:33 +08:00
assert rshift(x, y) == "rshift"
2021-09-28 02:02:44 +08:00
assert n_rshift == 1
2022-01-24 18:32:33 +08:00
assert operator.__rshift__(x, y) == "rshift"
2021-09-28 02:02:44 +08:00
assert n_rshift == 2
assert irshift(x, y) is x
assert n_irshift == 1
assert operator.__irshift__(x, y) is x
assert n_irshift == 2
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
test_ops()
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class B1:
def __length_hint__(self):
return 101
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class B2:
def __length_hint__(self):
return 202
def __len__(self):
return 303
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
class B3:
pass
2022-01-24 18:32:33 +08:00
2021-09-28 02:02:44 +08:00
@test
def test_sequence_ops():
2022-01-24 18:32:33 +08:00
assert concat([1, 2], [3, 4]) == [1, 2, 3, 4]
assert operator.__concat__([1, 2], [3, 4]) == [1, 2, 3, 4]
assert contains([1, 2, 3], 2)
assert not contains([1, 2, 3], 0)
assert operator.__contains__([1, 2, 3], 2)
assert not operator.__contains__([1, 2, 3], 0)
assert countOf([1, 2, 1, 1], 1) == 3
v = [1, 2, 3]
2021-09-28 02:02:44 +08:00
assert getitem(v, 1) == 2
delitem(v, 1)
assert v == [1, 3]
setitem(v, 1, 99)
assert v == [1, 99]
assert length_hint(B1()) == 101
assert length_hint(B2()) == 303
assert length_hint(B3()) == 0
assert length_hint(B3(), default=404) == 404
2022-01-24 18:32:33 +08:00
assert itemgetter(-1)([11, 22, 33]) == 33
assert itemgetter(-1, -2, -3)([11, 22, 33]) == (33, 22, 11)
2021-09-28 02:02:44 +08:00
test_sequence_ops()
Dynamic Polymorphism (#58) * Use Static[] for static inheritance * Support .seq extension * Fix #36 * Polymorphic typechecking; vtables [wip] * v-table dispatch [wip] * vtable routing [wip; bug] * vtable routing [MVP] * Fix texts * Add union type support * Update FAQs * Clarify * Add BSL license * Add makeUnion * Add IR UnionType * Update union representation in LLVM * Update README * Update README.md * Update README * Update README.md * Add benchmarks * Add more benchmarks and README * Add primes benchmark * Update benchmarks * Fix cpp * Clean up list * Update faq.md * Add binary trees benchmark * Add fannkuch benchmark * Fix paths * Add PyPy * Abort on fail * More benchmarks * Add cpp word_count * Update set_partition cpp * Add nbody cpp * Add TAQ cpp; fix word_count timing * Update CODEOWNERS * Update README * Update README.md * Update CODEOWNERS * Fix bench script * Update binary_trees.cpp * Update taq.cpp * Fix primes benchmark * Add mandelbrot benchmark * Fix OpenMP init * Add Module::unsafeGetUnionType * UnionType [wip] [skip ci] * Integrate IR unions and Union * UnionType refactor [skip ci] * Update README.md * Update docs * UnionType [wip] [skip ci] * UnionType and automatic unions * Add Slack * Update faq.md * Refactor types * New error reporting [wip] * New error reporting [wip] * peglib updates [wip] [skip_ci] * Fix parsing issues * Fix parsing issues * Fix error reporting issues * Make sure random module matches Python * Update releases.md * Fix tests * Fix #59 * Fix #57 * Fix #50 * Fix #49 * Fix #26; Fix #51; Fix #47; Fix #49 * Fix collection extension methods * Fix #62 * Handle *args/**kwargs with Callable[]; Fix #43 * Fix #43 * Fix Ptr.__sub__; Fix polymorphism issues * Add typeinfo * clang-format * Upgrade fmtlib to v9; Use CPM for fmtlib; format spec support; __format__ support * Use CPM for semver and toml++ * Remove extension check * Revamp str methods * Update str.zfill * Fix thunk crashes [wip] [skip_ci] * Fix str.__reversed__ * Fix count_with_max * Fix vtable memory allocation issues * Add poly AST tests * Use PDQsort when stability does not matter * Fix dotted imports; Fix issues * Fix kwargs passing to Python * Fix #61 * Fix #37 * Add isinstance support for unions; Union methods return Union type if different * clang-format * Nicely format error tracebacks * Fix build issues; clang-format * Fix OpenMP init * Fix OpenMP init * Update README.md * Fix tests * Update license [skip ci] * Update license [ci skip] * Add copyright header to all source files * Fix super(); Fix error recovery in ClassStmt * Clean up whitespace [ci skip] * Use Python 3.9 on CI * Print info in random test * Fix single unions * Update random_test.codon * Fix polymorhic thunk instantiation * Fix random test * Add operator.attrgetter and operator.methodcaller * Add code documentation * Update documentation * Update README.md * Fix tests * Fix random init Co-authored-by: A. R. Shajii <ars@ars.me>
2022-12-05 08:45:21 +08:00
class C:
foo: int
def __init__(self, foo):
self.foo = foo
def bar(self, k, m, n):
return self.foo + k + m*100 + n*1000
@test
def test_getter_ops():
v = ['a']
operator.attrgetter('append')(v)('hello')
operator.methodcaller('append', 'goodbye')(v)
assert v == ['a', 'hello', 'goodbye']
c = C(10)
assert operator.attrgetter('foo')(c) == 10
assert operator.methodcaller('bar', 9, n=7, m=3)(c) == 7319
test_getter_ops()