Skip to content

Commit 7d41b52

Browse files
jgarzikclaude
andcommitted
Import test_bytes, test_builtin, test_types
New tests: - test_bytes.py: 8 tests — literals, indexing, slicing, comparison, decode, methods - test_builtin.py: 23 tests — abs, bool, chr/ord, divmod, hash, hex/oct/bin, id, int, isinstance, len, max/min, pow, range, repr, round, sorted, str, sum, type, zip, enumerate, map/filter, callable - test_types.py: 21 tests, 1 skip — truth values, type checks, conversions (int, float, str, bool, list, tuple, set) Total CPython test suites: 37 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent f4a92cd commit 7d41b52

4 files changed

Lines changed: 338 additions & 0 deletions

File tree

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -124,6 +124,12 @@ gen-cpython-tests:
124124
@$(PYTHON) -m py_compile tests/cpython/test_property.py
125125
@echo "Compiling tests/cpython/test_string.py..."
126126
@$(PYTHON) -m py_compile tests/cpython/test_string.py
127+
@echo "Compiling tests/cpython/test_bytes.py..."
128+
@$(PYTHON) -m py_compile tests/cpython/test_bytes.py
129+
@echo "Compiling tests/cpython/test_builtin.py..."
130+
@$(PYTHON) -m py_compile tests/cpython/test_builtin.py
131+
@echo "Compiling tests/cpython/test_types.py..."
132+
@$(PYTHON) -m py_compile tests/cpython/test_types.py
127133
@echo "Done."
128134

129135
check-cpython: $(TARGET) gen-cpython-tests
@@ -195,3 +201,9 @@ check-cpython: $(TARGET) gen-cpython-tests
195201
@./apython tests/cpython/__pycache__/test_property.cpython-312.pyc
196202
@echo "Running CPython test_string.py..."
197203
@./apython tests/cpython/__pycache__/test_string.cpython-312.pyc
204+
@echo "Running CPython test_bytes.py..."
205+
@./apython tests/cpython/__pycache__/test_bytes.cpython-312.pyc
206+
@echo "Running CPython test_builtin.py..."
207+
@./apython tests/cpython/__pycache__/test_builtin.cpython-312.pyc
208+
@echo "Running CPython test_types.py..."
209+
@./apython tests/cpython/__pycache__/test_types.cpython-312.pyc

tests/cpython/test_builtin.py

Lines changed: 147 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,147 @@
1+
"""Tests for builtin functions — adapted from CPython test_builtin.py"""
2+
3+
import unittest
4+
5+
6+
class BuiltinTest(unittest.TestCase):
7+
8+
def test_abs(self):
9+
self.assertEqual(abs(0), 0)
10+
self.assertEqual(abs(-1), 1)
11+
self.assertEqual(abs(1), 1)
12+
self.assertAlmostEqual(abs(-1.5), 1.5)
13+
14+
def test_bool(self):
15+
self.assertIs(bool(0), False)
16+
self.assertIs(bool(1), True)
17+
self.assertIs(bool(""), False)
18+
self.assertIs(bool("x"), True)
19+
self.assertIs(bool([]), False)
20+
self.assertIs(bool([1]), True)
21+
self.assertIs(bool(None), False)
22+
23+
def test_chr_ord(self):
24+
self.assertEqual(chr(65), 'A')
25+
self.assertEqual(chr(97), 'a')
26+
self.assertEqual(ord('A'), 65)
27+
self.assertEqual(ord('a'), 97)
28+
29+
def test_divmod(self):
30+
self.assertEqual(divmod(7, 3), (2, 1))
31+
self.assertEqual(divmod(-7, 3), (-3, 2))
32+
self.assertEqual(divmod(7, -3), (-3, -2))
33+
34+
def test_hash(self):
35+
self.assertEqual(hash(42), hash(42))
36+
self.assertEqual(hash("hello"), hash("hello"))
37+
self.assertIsInstance(hash(42), int)
38+
39+
def test_hex_oct_bin(self):
40+
self.assertEqual(hex(255), '0xff')
41+
self.assertEqual(hex(-1), '-0x1')
42+
self.assertEqual(oct(8), '0o10')
43+
self.assertEqual(bin(10), '0b1010')
44+
45+
def test_id(self):
46+
a = [1, 2]
47+
b = a
48+
c = [1, 2]
49+
self.assertEqual(id(a), id(b))
50+
self.assertNotEqual(id(a), id(c))
51+
52+
def test_int(self):
53+
self.assertEqual(int(), 0)
54+
self.assertEqual(int(3.5), 3)
55+
self.assertEqual(int("42"), 42)
56+
self.assertEqual(int("-10"), -10)
57+
self.assertEqual(int("ff", 16), 255)
58+
self.assertEqual(int("10", 2), 2)
59+
60+
def test_isinstance_issubclass(self):
61+
self.assertTrue(isinstance(1, int))
62+
self.assertTrue(isinstance("x", str))
63+
self.assertTrue(isinstance([], list))
64+
self.assertTrue(issubclass(bool, int))
65+
self.assertFalse(issubclass(str, int))
66+
67+
def test_len(self):
68+
self.assertEqual(len([]), 0)
69+
self.assertEqual(len([1, 2, 3]), 3)
70+
self.assertEqual(len("hello"), 5)
71+
self.assertEqual(len({}), 0)
72+
self.assertEqual(len({1: 2}), 1)
73+
74+
def test_max_min(self):
75+
self.assertEqual(max(1, 2, 3), 3)
76+
self.assertEqual(min(1, 2, 3), 1)
77+
self.assertEqual(max([1, 2, 3]), 3)
78+
self.assertEqual(min([1, 2, 3]), 1)
79+
80+
def test_pow(self):
81+
self.assertEqual(pow(2, 10), 1024)
82+
self.assertEqual(pow(3, 3, 8), 3)
83+
84+
def test_range(self):
85+
self.assertEqual(list(range(5)), [0, 1, 2, 3, 4])
86+
self.assertEqual(list(range(1, 5)), [1, 2, 3, 4])
87+
self.assertEqual(list(range(0, 10, 2)), [0, 2, 4, 6, 8])
88+
self.assertEqual(list(range(5, 0, -1)), [5, 4, 3, 2, 1])
89+
90+
def test_repr(self):
91+
self.assertEqual(repr(42), '42')
92+
self.assertEqual(repr("hello"), "'hello'")
93+
self.assertEqual(repr([1, 2]), '[1, 2]')
94+
self.assertEqual(repr(None), 'None')
95+
96+
def test_round(self):
97+
self.assertEqual(round(3.5), 4)
98+
self.assertEqual(round(4.5), 4) # banker's rounding
99+
self.assertEqual(round(3.14159, 2), 3.14)
100+
101+
def test_sorted(self):
102+
self.assertEqual(sorted([3, 1, 2]), [1, 2, 3])
103+
self.assertEqual(sorted("cba"), ['a', 'b', 'c'])
104+
self.assertEqual(sorted([3, 1, 2], reverse=True), [3, 2, 1])
105+
106+
def test_str(self):
107+
self.assertEqual(str(42), '42')
108+
self.assertEqual(str(3.14), '3.14')
109+
self.assertEqual(str(True), 'True')
110+
self.assertEqual(str(None), 'None')
111+
self.assertEqual(str([1, 2]), '[1, 2]')
112+
113+
def test_sum(self):
114+
self.assertEqual(sum([1, 2, 3]), 6)
115+
self.assertEqual(sum([], 10), 10)
116+
self.assertEqual(sum(range(10)), 45)
117+
118+
def test_type(self):
119+
self.assertIs(type(42), int)
120+
self.assertIs(type("x"), str)
121+
self.assertIs(type([]), list)
122+
self.assertIs(type({}), dict)
123+
self.assertIs(type(()), tuple)
124+
self.assertIs(type(True), bool)
125+
self.assertIs(type(None), type(None))
126+
127+
def test_zip(self):
128+
self.assertEqual(list(zip([1, 2], [3, 4])), [(1, 3), (2, 4)])
129+
self.assertEqual(list(zip()), [])
130+
self.assertEqual(list(zip([1])), [(1,)])
131+
132+
def test_enumerate(self):
133+
self.assertEqual(list(enumerate('ab')), [(0, 'a'), (1, 'b')])
134+
self.assertEqual(list(enumerate('ab', 5)), [(5, 'a'), (6, 'b')])
135+
136+
def test_map_filter(self):
137+
self.assertEqual(list(map(str, [1, 2, 3])), ['1', '2', '3'])
138+
self.assertEqual(list(filter(lambda x: x > 2, [1, 2, 3, 4])), [3, 4])
139+
140+
def test_callable(self):
141+
self.assertTrue(callable(len))
142+
self.assertTrue(callable(lambda: None))
143+
self.assertFalse(callable(42))
144+
145+
146+
if __name__ == "__main__":
147+
unittest.main()

tests/cpython/test_bytes.py

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
"""Tests for bytes — adapted from CPython test_bytes.py"""
2+
3+
import unittest
4+
5+
6+
class BytesTest(unittest.TestCase):
7+
8+
def test_literal(self):
9+
self.assertEqual(b"hello", b"hello")
10+
self.assertEqual(b"", b"")
11+
12+
def test_len(self):
13+
self.assertEqual(len(b""), 0)
14+
self.assertEqual(len(b"hello"), 5)
15+
16+
def test_indexing(self):
17+
b = b"hello"
18+
self.assertEqual(b[0], 104) # ord('h')
19+
self.assertEqual(b[-1], 111) # ord('o')
20+
21+
def test_slicing(self):
22+
b = b"hello"
23+
self.assertEqual(b[1:3], b"el")
24+
self.assertEqual(b[:3], b"hel")
25+
self.assertEqual(b[3:], b"lo")
26+
27+
def test_comparison(self):
28+
self.assertTrue(b"abc" == b"abc")
29+
self.assertTrue(b"abc" != b"abd")
30+
31+
def test_decode(self):
32+
self.assertEqual(b"hello".decode(), "hello")
33+
34+
def test_iteration(self):
35+
result = []
36+
for x in b"abc":
37+
result.append(x)
38+
self.assertEqual(result, [97, 98, 99])
39+
40+
def test_methods(self):
41+
self.assertTrue(b"hello".startswith(b"hel"))
42+
self.assertTrue(b"hello".endswith(b"llo"))
43+
self.assertEqual(b"hello".find(b"ll"), 2)
44+
45+
46+
if __name__ == "__main__":
47+
unittest.main()

tests/cpython/test_types.py

Lines changed: 132 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,132 @@
1+
"""Tests for type system basics — adapted from CPython test_types.py"""
2+
3+
import unittest
4+
5+
6+
class TypeTest(unittest.TestCase):
7+
8+
def test_truth_values(self):
9+
# Falsy values
10+
self.assertFalse(bool(None))
11+
self.assertFalse(bool(0))
12+
self.assertFalse(bool(0.0))
13+
self.assertFalse(bool(''))
14+
self.assertFalse(bool([]))
15+
self.assertFalse(bool(()))
16+
self.assertFalse(bool({}))
17+
self.assertFalse(bool(set()))
18+
self.assertFalse(bool(False))
19+
self.assertFalse(bool(b''))
20+
21+
# Truthy values
22+
self.assertTrue(bool(1))
23+
self.assertTrue(bool(-1))
24+
self.assertTrue(bool(0.1))
25+
self.assertTrue(bool('x'))
26+
self.assertTrue(bool([0]))
27+
self.assertTrue(bool((0,)))
28+
self.assertTrue(bool({0: 0}))
29+
self.assertTrue(bool({0}))
30+
self.assertTrue(bool(True))
31+
self.assertTrue(bool(b'x'))
32+
33+
def test_none_type(self):
34+
self.assertIs(type(None), type(None))
35+
self.assertEqual(repr(None), 'None')
36+
self.assertEqual(str(None), 'None')
37+
38+
def test_int_type(self):
39+
self.assertIs(type(1), int)
40+
self.assertIs(type(True), bool)
41+
self.assertTrue(issubclass(bool, int))
42+
43+
def test_float_type(self):
44+
self.assertIs(type(1.0), float)
45+
46+
def test_str_type(self):
47+
self.assertIs(type(""), str)
48+
49+
def test_list_type(self):
50+
self.assertIs(type([]), list)
51+
52+
def test_dict_type(self):
53+
self.assertIs(type({}), dict)
54+
55+
def test_tuple_type(self):
56+
self.assertIs(type(()), tuple)
57+
58+
def test_set_type(self):
59+
self.assertIs(type(set()), set)
60+
61+
def test_bytes_type(self):
62+
self.assertIs(type(b""), bytes)
63+
64+
def test_function_type(self):
65+
def f(): pass
66+
self.assertEqual(type(f).__name__, 'function')
67+
68+
def test_method_type(self):
69+
class C:
70+
def f(self): pass
71+
obj = C()
72+
self.assertTrue(callable(obj.f))
73+
74+
def test_type_of_type(self):
75+
self.assertIs(type(int), type)
76+
self.assertIs(type(str), type)
77+
self.assertIs(type(list), type)
78+
79+
80+
class ConversionTest(unittest.TestCase):
81+
82+
def test_int_conversions(self):
83+
self.assertEqual(int(3.9), 3)
84+
self.assertEqual(int(-3.9), -3)
85+
self.assertEqual(int("100"), 100)
86+
self.assertEqual(int("0xff", 16), 255)
87+
self.assertEqual(int(True), 1)
88+
self.assertEqual(int(False), 0)
89+
90+
def test_float_conversions(self):
91+
self.assertEqual(float(3), 3.0)
92+
self.assertEqual(float("3.14"), 3.14)
93+
self.assertEqual(float(True), 1.0)
94+
self.assertEqual(float(False), 0.0)
95+
96+
def test_str_conversions(self):
97+
self.assertEqual(str(42), "42")
98+
self.assertEqual(str(3.14), "3.14")
99+
self.assertEqual(str(True), "True")
100+
self.assertEqual(str(False), "False")
101+
self.assertEqual(str(None), "None")
102+
self.assertEqual(str([1, 2]), "[1, 2]")
103+
self.assertEqual(str((1, 2)), "(1, 2)")
104+
105+
def test_bool_conversions(self):
106+
self.assertIs(bool(0), False)
107+
self.assertIs(bool(1), True)
108+
self.assertIs(bool(""), False)
109+
self.assertIs(bool("x"), True)
110+
111+
def test_list_conversions(self):
112+
self.assertEqual(list("abc"), ['a', 'b', 'c'])
113+
self.assertEqual(list((1, 2, 3)), [1, 2, 3])
114+
self.assertEqual(list(range(3)), [0, 1, 2])
115+
self.assertEqual(list({1, 2, 3}), sorted([1, 2, 3]))
116+
117+
def test_tuple_conversions(self):
118+
self.assertEqual(tuple([1, 2, 3]), (1, 2, 3))
119+
self.assertEqual(tuple("abc"), ('a', 'b', 'c'))
120+
self.assertEqual(tuple(range(3)), (0, 1, 2))
121+
122+
def test_set_conversions(self):
123+
self.assertEqual(set([1, 2, 2, 3]), {1, 2, 3})
124+
self.assertEqual(frozenset([1, 2, 3]), frozenset({1, 2, 3}))
125+
126+
@unittest.skip("dict() from kwargs not implemented")
127+
def test_dict_conversions(self):
128+
pass
129+
130+
131+
if __name__ == "__main__":
132+
unittest.main()

0 commit comments

Comments
 (0)