Skip to content

Commit c2dee48

Browse files
jgarzikclaude
andcommitted
Import test_format, test_slice_ops, test_numeric, test_comprehensions
New tests: - test_format.py: 24 tests — f-strings (expressions, conversions, adjacent), % formatting (string, int, repr, hex, multiple), string methods - test_slice_ops.py: 14 tests — list/tuple/string slicing, step, negative indices, slice assign/delete, extended slicing - test_numeric.py: 17 tests — int edge cases (large, conversion, bitwise), float (special values, NaN, rounding), bool (is-int, arithmetic) - test_comprehensions.py: 22 tests — list/dict/set comps, genexps with filter, nested, closure, empty, max/min/sum/any/all Total CPython test suites: 56 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent b4df012 commit c2dee48

5 files changed

Lines changed: 420 additions & 0 deletions

File tree

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,10 @@ gen-cpython-tests:
155155
@$(PYTHON) -m py_compile tests/cpython/test_assignment.py
156156
@$(PYTHON) -m py_compile tests/cpython/test_exceptions_extra.py
157157
@$(PYTHON) -m py_compile tests/cpython/test_generators_extra.py
158+
@$(PYTHON) -m py_compile tests/cpython/test_format.py
159+
@$(PYTHON) -m py_compile tests/cpython/test_slice_ops.py
160+
@$(PYTHON) -m py_compile tests/cpython/test_numeric.py
161+
@$(PYTHON) -m py_compile tests/cpython/test_comprehensions.py
158162
@echo "Done."
159163

160164
check-cpython: $(TARGET) gen-cpython-tests
@@ -262,3 +266,11 @@ check-cpython: $(TARGET) gen-cpython-tests
262266
@./apython tests/cpython/__pycache__/test_exceptions_extra.cpython-312.pyc
263267
@echo "Running CPython test_generators_extra.py..."
264268
@./apython tests/cpython/__pycache__/test_generators_extra.cpython-312.pyc
269+
@echo "Running CPython test_format.py..."
270+
@./apython tests/cpython/__pycache__/test_format.cpython-312.pyc
271+
@echo "Running CPython test_slice_ops.py..."
272+
@./apython tests/cpython/__pycache__/test_slice_ops.cpython-312.pyc
273+
@echo "Running CPython test_numeric.py..."
274+
@./apython tests/cpython/__pycache__/test_numeric.cpython-312.pyc
275+
@echo "Running CPython test_comprehensions.py..."
276+
@./apython tests/cpython/__pycache__/test_comprehensions.cpython-312.pyc
Lines changed: 106 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,106 @@
1+
"""Comprehensive tests for all comprehension forms"""
2+
3+
import unittest
4+
5+
6+
class ListCompTest(unittest.TestCase):
7+
8+
def test_basic(self):
9+
self.assertEqual([x for x in range(5)], [0, 1, 2, 3, 4])
10+
11+
def test_with_filter(self):
12+
self.assertEqual([x for x in range(10) if x % 2 == 0], [0, 2, 4, 6, 8])
13+
14+
def test_with_expression(self):
15+
self.assertEqual([x * x for x in range(5)], [0, 1, 4, 9, 16])
16+
17+
def test_nested(self):
18+
self.assertEqual([(i, j) for i in range(2) for j in range(3)],
19+
[(0,0), (0,1), (0,2), (1,0), (1,1), (1,2)])
20+
21+
def test_nested_with_filter(self):
22+
self.assertEqual([(i, j) for i in range(3) for j in range(3) if i != j],
23+
[(0,1), (0,2), (1,0), (1,2), (2,0), (2,1)])
24+
25+
def test_string_input(self):
26+
self.assertEqual([c.upper() for c in "abc"], ['A', 'B', 'C'])
27+
28+
def test_nested_comp(self):
29+
self.assertEqual([[j for j in range(i)] for i in range(4)],
30+
[[], [0], [0, 1], [0, 1, 2]])
31+
32+
def test_closure(self):
33+
n = 10
34+
self.assertEqual([x + n for x in range(3)], [10, 11, 12])
35+
36+
def test_empty(self):
37+
self.assertEqual([x for x in []], [])
38+
self.assertEqual([x for x in range(10) if x > 100], [])
39+
40+
41+
class DictCompTest(unittest.TestCase):
42+
43+
def test_basic(self):
44+
d = {k: v for k, v in [('a', 1), ('b', 2)]}
45+
self.assertEqual(d['a'], 1)
46+
self.assertEqual(d['b'], 2)
47+
48+
def test_from_range(self):
49+
d = {x: x * x for x in range(4)}
50+
self.assertEqual(len(d), 4)
51+
self.assertEqual(d[3], 9)
52+
53+
def test_with_filter(self):
54+
d = {x: x for x in range(10) if x % 2 == 0}
55+
self.assertEqual(sorted(d.keys()), [0, 2, 4, 6, 8])
56+
57+
def test_swap_keys_values(self):
58+
original = {'a': 1, 'b': 2, 'c': 3}
59+
swapped = {v: k for k, v in original.items()}
60+
self.assertEqual(swapped[1], 'a')
61+
self.assertEqual(swapped[2], 'b')
62+
63+
64+
class SetCompTest(unittest.TestCase):
65+
66+
def test_basic(self):
67+
s = {x for x in range(5)}
68+
self.assertEqual(len(s), 5)
69+
70+
def test_with_filter(self):
71+
s = {x for x in range(10) if x % 3 == 0}
72+
self.assertEqual(sorted(list(s)), [0, 3, 6, 9])
73+
74+
def test_dedup(self):
75+
s = {x % 3 for x in range(10)}
76+
self.assertEqual(len(s), 3)
77+
78+
79+
class GenExpTest(unittest.TestCase):
80+
81+
def test_sum(self):
82+
self.assertEqual(sum(x for x in range(5)), 10)
83+
84+
def test_any_all(self):
85+
self.assertTrue(any(x > 3 for x in range(5)))
86+
self.assertFalse(any(x > 10 for x in range(5)))
87+
self.assertTrue(all(x < 5 for x in range(5)))
88+
89+
def test_list_from_gen(self):
90+
self.assertEqual(list(x * 2 for x in range(4)), [0, 2, 4, 6])
91+
92+
def test_nested_gen(self):
93+
self.assertEqual(list((i, j) for i in range(2) for j in range(2)),
94+
[(0,0), (0,1), (1,0), (1,1)])
95+
96+
def test_filter(self):
97+
self.assertEqual(list(x for x in range(10) if x % 3 == 0),
98+
[0, 3, 6, 9])
99+
100+
def test_max_min(self):
101+
self.assertEqual(max(x * x for x in range(5)), 16)
102+
self.assertEqual(min(x * x for x in range(1, 5)), 1)
103+
104+
105+
if __name__ == "__main__":
106+
unittest.main()

tests/cpython/test_format.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
"""Tests for string formatting — f-strings and basic %"""
2+
3+
import unittest
4+
5+
6+
class FStringTest(unittest.TestCase):
7+
8+
def test_simple(self):
9+
x = 42
10+
self.assertEqual(f"{x}", "42")
11+
12+
def test_expression(self):
13+
self.assertEqual(f"{2 + 3}", "5")
14+
self.assertEqual(f"{len('abc')}", "3")
15+
16+
def test_string_expr(self):
17+
self.assertEqual(f"{'hello'}", "hello")
18+
19+
def test_multiple(self):
20+
a, b = 1, 2
21+
self.assertEqual(f"{a} + {b} = {a + b}", "1 + 2 = 3")
22+
23+
def test_nested_quotes(self):
24+
name = "world"
25+
self.assertEqual(f"hello {name}!", "hello world!")
26+
27+
def test_repr_conversion(self):
28+
self.assertEqual(f"{'hi'!r}", "'hi'")
29+
30+
def test_str_conversion(self):
31+
self.assertEqual(f"{42!s}", "42")
32+
33+
def test_empty_fstring(self):
34+
self.assertEqual(f"", "")
35+
36+
def test_no_expressions(self):
37+
self.assertEqual(f"plain text", "plain text")
38+
39+
def test_adjacent(self):
40+
x = 1
41+
self.assertEqual(f"{x}{x}{x}", "111")
42+
43+
44+
class PercentFormatTest(unittest.TestCase):
45+
46+
def test_string(self):
47+
self.assertEqual("hello %s" % "world", "hello world")
48+
49+
def test_int(self):
50+
self.assertEqual("%d items" % 5, "5 items")
51+
52+
def test_repr(self):
53+
self.assertEqual("%r" % "test", "'test'")
54+
55+
def test_multiple(self):
56+
self.assertEqual("%s=%d" % ("x", 42), "x=42")
57+
58+
def test_hex(self):
59+
self.assertEqual("%x" % 255, "ff")
60+
61+
62+
class StrMethodsTest(unittest.TestCase):
63+
64+
def test_join(self):
65+
self.assertEqual(", ".join(["a", "b", "c"]), "a, b, c")
66+
67+
def test_split(self):
68+
self.assertEqual("a,b,c".split(","), ["a", "b", "c"])
69+
self.assertEqual(" hello world ".split(), ["hello", "world"])
70+
71+
def test_replace(self):
72+
self.assertEqual("aabbcc".replace("bb", "XX"), "aaXXcc")
73+
74+
def test_strip(self):
75+
self.assertEqual(" hi ".strip(), "hi")
76+
77+
def test_upper_lower(self):
78+
self.assertEqual("Hello".upper(), "HELLO")
79+
self.assertEqual("Hello".lower(), "hello")
80+
81+
def test_startswith_endswith(self):
82+
self.assertTrue("hello".startswith("hel"))
83+
self.assertTrue("hello".endswith("llo"))
84+
85+
def test_find_count(self):
86+
self.assertEqual("hello".find("ll"), 2)
87+
self.assertEqual("hello".find("zz"), -1)
88+
self.assertEqual("banana".count("an"), 2)
89+
90+
def test_isdigit_isalpha(self):
91+
self.assertTrue("123".isdigit())
92+
self.assertFalse("12a".isdigit())
93+
self.assertTrue("abc".isalpha())
94+
95+
def test_zfill(self):
96+
self.assertEqual("42".zfill(5), "00042")
97+
98+
99+
if __name__ == "__main__":
100+
unittest.main()

tests/cpython/test_numeric.py

Lines changed: 113 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,113 @@
1+
"""Tests for numeric types — int, float, bool edge cases"""
2+
3+
import unittest
4+
5+
6+
class IntEdgeCaseTest(unittest.TestCase):
7+
8+
def test_zero(self):
9+
self.assertEqual(0 + 0, 0)
10+
self.assertEqual(0 * 100, 0)
11+
self.assertEqual(0 ** 0, 1)
12+
13+
def test_negative(self):
14+
self.assertEqual(-(-5), 5)
15+
self.assertEqual(abs(-42), 42)
16+
self.assertTrue(-1 < 0)
17+
18+
def test_large(self):
19+
big = 10 ** 20
20+
self.assertEqual(big + 1, 10 ** 20 + 1)
21+
self.assertEqual(big * 2, 2 * 10 ** 20)
22+
23+
def test_int_from_string(self):
24+
self.assertEqual(int("42"), 42)
25+
self.assertEqual(int("-10"), -10)
26+
self.assertEqual(int("0"), 0)
27+
self.assertEqual(int("ff", 16), 255)
28+
self.assertEqual(int("77", 8), 63)
29+
self.assertEqual(int("1010", 2), 10)
30+
31+
def test_int_from_float(self):
32+
self.assertEqual(int(3.7), 3)
33+
self.assertEqual(int(-3.7), -3)
34+
self.assertEqual(int(0.0), 0)
35+
36+
def test_int_from_bool(self):
37+
self.assertEqual(int(True), 1)
38+
self.assertEqual(int(False), 0)
39+
40+
def test_divmod(self):
41+
self.assertEqual(divmod(17, 5), (3, 2))
42+
self.assertEqual(divmod(-17, 5), (-4, 3))
43+
self.assertEqual(divmod(17, -5), (-4, -3))
44+
45+
def test_bit_length(self):
46+
self.assertEqual((0).bit_length(), 0)
47+
self.assertEqual((1).bit_length(), 1)
48+
self.assertEqual((255).bit_length(), 8)
49+
self.assertEqual((-1).bit_length(), 1)
50+
51+
52+
class FloatEdgeCaseTest(unittest.TestCase):
53+
54+
def test_special_values(self):
55+
inf = float('inf')
56+
self.assertTrue(inf > 0)
57+
self.assertTrue(-inf < 0)
58+
self.assertTrue(inf == inf)
59+
60+
def test_nan(self):
61+
nan = float('nan')
62+
self.assertFalse(nan == nan)
63+
self.assertTrue(nan != nan)
64+
65+
def test_float_from_string(self):
66+
self.assertEqual(float("3.14"), 3.14)
67+
self.assertEqual(float("-0.5"), -0.5)
68+
self.assertEqual(float("0"), 0.0)
69+
self.assertEqual(float("1e10"), 1e10)
70+
71+
def test_float_int_equality(self):
72+
self.assertTrue(1.0 == 1)
73+
self.assertTrue(0.0 == 0)
74+
self.assertFalse(1.5 == 1)
75+
76+
def test_rounding(self):
77+
self.assertEqual(round(3.14159, 2), 3.14)
78+
self.assertEqual(round(2.5), 2) # banker's rounding
79+
self.assertEqual(round(3.5), 4)
80+
81+
82+
class BoolTest(unittest.TestCase):
83+
84+
def test_bool_is_int(self):
85+
self.assertTrue(isinstance(True, int))
86+
self.assertTrue(isinstance(False, int))
87+
self.assertTrue(issubclass(bool, int))
88+
89+
def test_bool_arithmetic(self):
90+
self.assertEqual(True + True, 2)
91+
self.assertEqual(True * 10, 10)
92+
self.assertEqual(False + 1, 1)
93+
94+
def test_bool_from_values(self):
95+
self.assertIs(bool(0), False)
96+
self.assertIs(bool(1), True)
97+
self.assertIs(bool(""), False)
98+
self.assertIs(bool("x"), True)
99+
self.assertIs(bool([]), False)
100+
self.assertIs(bool([0]), True)
101+
self.assertIs(bool(None), False)
102+
103+
def test_bool_operators(self):
104+
self.assertEqual(True and True, True)
105+
self.assertEqual(True and False, False)
106+
self.assertEqual(False or True, True)
107+
self.assertEqual(False or False, False)
108+
self.assertEqual(not True, False)
109+
self.assertEqual(not False, True)
110+
111+
112+
if __name__ == "__main__":
113+
unittest.main()

0 commit comments

Comments
 (0)