mojo
mojo copied to clipboard
[stdlib] Add `array_tester` with `Tester` and `Testee` for lists
Introducing two new types for testing lists and datastructures.
(Tester
and Testee
)
Testee
can be used as the element type of an array or a similar datastructure.
Tester
holds Int
fields to count the amount of moves, copy, init, del.
Multiple instances of Testee
contains an UnsafePointer
to an instance of Tester
.
Assertions can be created on the current amount of init
, copy
, del
, moves
a list does.
Additionally, Testee
have an Int
field to store a value.
That value can be used to check the equality of the elements of two lists.
Example:
def main():
var _tester = Tester()
var x = List[Testee]()
for i in range(10):
x.append(Testee(_tester, i))
for i in x: print(i[].value)
assert_equal(_tester.oninit, 10)
assert_equal(_tester.ondelete, 0)
assert_equal(_tester.oncopy, 0)
y = x[0]
assert_equal(y.value, 0)
assert_equal(_tester.oncopy, 1)
_ = x.pop()
assert_equal(_tester.ondelete, 2)
__type_of(x).__del__(x^)
assert_equal(_tester.ondelete, 11)
assert_equal(_tester.oncopy, 1)