mirror of
https://github.com/exaloop/codon.git
synced 2025-06-03 15:03:52 +08:00
* 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>
134 lines
4.5 KiB
Python
134 lines
4.5 KiB
Python
# Copyright (C) 2022 Exaloop Inc. <https://exaloop.io>
|
|
# Simplified version of Python's unittest.TestCase to allow
|
|
# copy/pasting tests directly from CPython's test suite.
|
|
|
|
class TestCase:
|
|
def fail(self, standard_message: str, special_message: str = ""):
|
|
print("TEST FAILED:", special_message if special_message else standard_message)
|
|
|
|
def assertTrue(self, obj, msg=""):
|
|
if not bool(obj):
|
|
self.fail(f"expected object to be true: {obj}", msg)
|
|
|
|
def assertFalse(self, obj, msg=""):
|
|
if bool(obj):
|
|
self.fail(f"expected object to be false: {obj}", msg)
|
|
|
|
def assertEqual(self, first, second, msg=""):
|
|
result = first == second
|
|
if not result:
|
|
self.fail(f"expected equality of:\n 1: {first}\n 2: {second}", msg)
|
|
|
|
def assertNotEqual(self, first, second, msg=""):
|
|
result = first != second
|
|
if not result:
|
|
self.fail(f"expected inequality of:\n 1: {first}\n 2: {second}", msg)
|
|
|
|
def assertSequenceEqual(self, seq1, seq2, msg=""):
|
|
len1 = len(seq1)
|
|
len2 = len(seq2)
|
|
if len1 != len2:
|
|
self.fail(
|
|
f"expected equality of sequences (len1={len1}, len2={len2}):\n 1: {seq1}\n 2: {seq2}",
|
|
msg,
|
|
)
|
|
|
|
for i in range(len1):
|
|
a, b = seq1[i], seq2[i]
|
|
if a != b:
|
|
self.fail(
|
|
f"expected equality of sequences (diff at elem {i}):\n 1: {seq1}\n 2: {seq2}",
|
|
msg,
|
|
)
|
|
|
|
def assertIn(self, member, container, msg=""):
|
|
if member not in container:
|
|
self.fail(f"expected {member} to be in {container}", msg)
|
|
|
|
def assertNotIn(self, member, container, msg=""):
|
|
if member in container:
|
|
self.fail(f"expected {member} to not be in {container}", msg)
|
|
|
|
def assertIs(self, expr1, expr2, msg=""):
|
|
if expr1 is not expr2:
|
|
self.fail(f"expected {expr1} to be identical to {expr2}", msg)
|
|
|
|
def assertIsNot(self, expr1, expr2, msg=""):
|
|
if expr1 is expr2:
|
|
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
|
|
|
|
def assertIsNot(self, expr1, expr2, msg=""):
|
|
if expr1 is expr2:
|
|
self.fail(f"expected {expr1} to not be identical to {expr2}", msg)
|
|
|
|
def assertCountEqual(self, first, second, msg=""):
|
|
from collections import Counter
|
|
|
|
first_seq, second_seq = list(first), list(second)
|
|
|
|
first_counter = Counter(first_seq)
|
|
second_counter = Counter(second_seq)
|
|
|
|
if first_counter != second_counter:
|
|
self.fail(f"expected equal counts:\n 1: {first}\n 2: {second}", msg)
|
|
|
|
def assertLess(self, a, b, msg=""):
|
|
if not (a < b):
|
|
self.fail(f"expected less-than:\n 1: {a}\n 2: {b}", msg)
|
|
|
|
def assertLessEqual(self, a, b, msg=""):
|
|
if not (a <= b):
|
|
self.fail(f"expected less-than-or-equal:\n 1: {a}\n 2: {b}", msg)
|
|
|
|
def assertGreater(self, a, b, msg=""):
|
|
if not (a > b):
|
|
self.fail(f"expected greater-than:\n 1: {a}\n 2: {b}", msg)
|
|
|
|
def assertGreaterEqual(self, a, b, msg=""):
|
|
if not (a >= b):
|
|
self.fail(f"expected greater-than-or-equal:\n 1: {a}\n 2: {b}", msg)
|
|
|
|
def assertIsNone(self, obj, msg=""):
|
|
if obj is not None:
|
|
self.fail(f"expected {obj} to be None", msg)
|
|
|
|
def assertIsNotNone(self, obj, msg=""):
|
|
if obj is None:
|
|
self.fail(f"expected {obj} to not be None", msg)
|
|
|
|
def assertRaises(self, exception: type, function, *args, **kwargs):
|
|
try:
|
|
function(*args, **kwargs)
|
|
except exception:
|
|
return
|
|
except:
|
|
pass
|
|
self.fail(f"call to function did not raise the given exception")
|
|
|
|
def assertAlmostEqual(
|
|
self, first, second, places: int = 0, msg="", delta=None
|
|
):
|
|
if first == second:
|
|
# shortcut
|
|
return
|
|
if places <= 0 and delta is None:
|
|
raise ValueError("specify delta or places not both")
|
|
|
|
standard_msg = ""
|
|
diff = abs(first - second)
|
|
if delta is not None:
|
|
if diff <= delta:
|
|
return
|
|
standard_msg = (
|
|
f"{first} != {second} within {delta} delta ({diff} difference)"
|
|
)
|
|
else:
|
|
if places <= 0:
|
|
places = 7
|
|
if round(diff, places) == 0:
|
|
return
|
|
standard_msg = (
|
|
f"{first} != {second} within {places} places ({diff} difference)"
|
|
)
|
|
self.fail(standard_msg, msg)
|