|
| 1 | +"""Assert-based tests for builtin call/argument passing. |
| 2 | +
|
| 3 | +Rules: only builtin functions are tested; no user-defined functions; |
| 4 | +do not use argument unpacking (`*` or `**`). |
| 5 | +""" |
| 6 | + |
| 7 | +# dict and dict.fromkeys |
| 8 | +def test_init(): |
| 9 | + assert dict() == {} |
| 10 | + #assert dict([('a', 1), ('b', 2)]) == {'a': 1, 'b': 2} |
| 11 | + assert dict(a=1, b=2) == {'a': 1, 'b': 2} |
| 12 | + assert dict.fromkeys([1, 2]) == {1: None, 2: None} |
| 13 | + assert dict.fromkeys([1, 2], 0) == {1: 0, 2: 0} |
| 14 | + |
| 15 | + # list |
| 16 | + assert list() == [] |
| 17 | + assert list((1, 2, 3)) == [1, 2, 3] |
| 18 | +test_init() |
| 19 | + |
| 20 | +# numeric builtins: max, min |
| 21 | +def test_min_max(): |
| 22 | + assert max(1, 2, 3) == 3 |
| 23 | + assert max([1, 5, 3]) == 5 |
| 24 | + assert min([1, 0, -1]) == -1 |
| 25 | + # use builtin `abs` as a key function |
| 26 | + assert max([-1, -2, 0], key=abs) == -2 |
| 27 | + |
| 28 | + # simple sanity checks combining builtin calls |
| 29 | + s = set(dict.fromkeys((3, 1, 2)).keys()) |
| 30 | + assert s == {1, 2, 3} |
| 31 | +test_min_max() |
| 32 | + |
| 33 | +# Grouped tests using `def` (each function is called so asserts execute) |
| 34 | +def test_numeric_builtins(): |
| 35 | + # # pow with two and three args |
| 36 | + # assert pow(2, 3) == 8 |
| 37 | + # assert pow(2, 3, 5) == 3 |
| 38 | + |
| 39 | + # # divmod and round |
| 40 | + # assert divmod(7, 3) == (2, 1) |
| 41 | + # assert round(2.5) in (2, 3) |
| 42 | + |
| 43 | + # abs and built-in conversions |
| 44 | + assert abs(-5) == 5 |
| 45 | + assert int('10') + 5 == 15 |
| 46 | + |
| 47 | + |
| 48 | +def test_sequence_and_sorting(): |
| 49 | + # conversions and sorted results |
| 50 | + assert list((4, 3, 2)) == [4, 3, 2] |
| 51 | + assert tuple([1, 2]) == (1, 2) |
| 52 | + assert set([1, 2, 2]) == {1, 2} |
| 53 | + assert sorted([3, 1, 2]) == [1, 2, 3] |
| 54 | + |
| 55 | + # reversed returns an iterator; collect via list() |
| 56 | + assert list(reversed([1, 2, 3])) == [3, 2, 1] |
| 57 | + |
| 58 | + |
| 59 | +def test_dictionary_methods_and_lookup(): |
| 60 | + # dict creation and lookups |
| 61 | + x = dict.fromkeys(['x', 'y'], 0) |
| 62 | + assert x == {'x': 0, 'y': 0} |
| 63 | + x2 = dict(a=1, b=2) |
| 64 | + assert x2.get('a') == 1 |
| 65 | + x2.pop('a') == 1 |
| 66 | + # ensure pop removed key |
| 67 | + assert 'a' not in x2 |
| 68 | + |
| 69 | + |
| 70 | + |
| 71 | +def test_boolean_any_all_sum(): |
| 72 | + assert any([0, '', None, 1]) is True |
| 73 | + assert all([1, True, 'nonempty']) is True |
| 74 | + assert sum([1, 2, 3]) == 6 |
| 75 | + |
| 76 | + |
| 77 | +def test_string_and_bytes(): |
| 78 | + assert str(123) == '123' |
| 79 | + b = bytes([65, 66, 67]) |
| 80 | + assert b == b'ABC' |
| 81 | + |
| 82 | + |
| 83 | +# Call grouped tests so their asserts run |
| 84 | +test_numeric_builtins() |
| 85 | +test_sequence_and_sorting() |
| 86 | +test_dictionary_methods_and_lookup() |
| 87 | +test_boolean_any_all_sum() |
| 88 | +test_string_and_bytes() |
| 89 | + |
0 commit comments