Skip to content

Commit 593933c

Browse files
jgarzikclaude
andcommitted
Import test_exceptions_builtin, test_functions, test_range_extra, test_conditional
New tests: - test_exceptions_builtin.py: 16 tests — exception hierarchy, args, catching, error-raising builtins (TypeError, ValueError, IndexError, etc.) - test_functions.py: 18 tests — defaults, varargs, kwargs, keyword-only, recursion (factorial, fibonacci, mutual), higher-order functions - test_range_extra.py: 13 tests — range constructor, step, negative, len, contains, reversed, enumerate, sum - test_conditional.py: 15 tests — ternary expressions, short-circuit and/or, not, chained booleans, is/is not identity Total CPython test suites: 64 Co-Authored-By: Claude Opus 4.6 (1M context) <noreply@anthropic.com>
1 parent 28ff34b commit 593933c

5 files changed

Lines changed: 413 additions & 0 deletions

File tree

Makefile

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -163,6 +163,10 @@ gen-cpython-tests:
163163
@$(PYTHON) -m py_compile tests/cpython/test_walrus.py
164164
@$(PYTHON) -m py_compile tests/cpython/test_match.py
165165
@$(PYTHON) -m py_compile tests/cpython/test_datastructures.py
166+
@$(PYTHON) -m py_compile tests/cpython/test_exceptions_builtin.py
167+
@$(PYTHON) -m py_compile tests/cpython/test_functions.py
168+
@$(PYTHON) -m py_compile tests/cpython/test_range_extra.py
169+
@$(PYTHON) -m py_compile tests/cpython/test_conditional.py
166170
@echo "Done."
167171

168172
check-cpython: $(TARGET) gen-cpython-tests
@@ -286,3 +290,11 @@ check-cpython: $(TARGET) gen-cpython-tests
286290
@./apython tests/cpython/__pycache__/test_match.cpython-312.pyc
287291
@echo "Running CPython test_datastructures.py..."
288292
@./apython tests/cpython/__pycache__/test_datastructures.cpython-312.pyc
293+
@echo "Running CPython test_exceptions_builtin.py..."
294+
@./apython tests/cpython/__pycache__/test_exceptions_builtin.cpython-312.pyc
295+
@echo "Running CPython test_functions.py..."
296+
@./apython tests/cpython/__pycache__/test_functions.cpython-312.pyc
297+
@echo "Running CPython test_range_extra.py..."
298+
@./apython tests/cpython/__pycache__/test_range_extra.cpython-312.pyc
299+
@echo "Running CPython test_conditional.py..."
300+
@./apython tests/cpython/__pycache__/test_conditional.cpython-312.pyc

tests/cpython/test_conditional.py

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,102 @@
1+
"""Tests for conditional expressions and boolean logic"""
2+
3+
import unittest
4+
5+
6+
class TernaryTest(unittest.TestCase):
7+
8+
def test_true(self):
9+
self.assertEqual("yes" if True else "no", "yes")
10+
11+
def test_false(self):
12+
self.assertEqual("yes" if False else "no", "no")
13+
14+
def test_expression(self):
15+
x = 10
16+
self.assertEqual("big" if x > 5 else "small", "big")
17+
self.assertEqual("big" if x < 5 else "small", "small")
18+
19+
def test_nested(self):
20+
def classify(x):
21+
return "pos" if x > 0 else "zero" if x == 0 else "neg"
22+
self.assertEqual(classify(5), "pos")
23+
self.assertEqual(classify(0), "zero")
24+
self.assertEqual(classify(-5), "neg")
25+
26+
def test_in_list(self):
27+
result = [x if x > 0 else 0 for x in [-2, -1, 0, 1, 2]]
28+
self.assertEqual(result, [0, 0, 0, 1, 2])
29+
30+
31+
class BooleanLogicTest(unittest.TestCase):
32+
33+
def test_and_short_circuit(self):
34+
self.assertEqual(0 and 42, 0)
35+
self.assertEqual(1 and 42, 42)
36+
self.assertEqual("" and "hello", "")
37+
self.assertEqual("x" and "hello", "hello")
38+
39+
def test_or_short_circuit(self):
40+
self.assertEqual(0 or 42, 42)
41+
self.assertEqual(1 or 42, 1)
42+
self.assertEqual("" or "hello", "hello")
43+
self.assertEqual("x" or "hello", "x")
44+
45+
def test_not(self):
46+
self.assertEqual(not True, False)
47+
self.assertEqual(not False, True)
48+
self.assertEqual(not 0, True)
49+
self.assertEqual(not 1, False)
50+
self.assertEqual(not "", True)
51+
self.assertEqual(not "x", False)
52+
self.assertEqual(not None, True)
53+
54+
def test_chained_and_or(self):
55+
self.assertEqual(1 and 2 and 3, 3)
56+
self.assertEqual(1 and 0 and 3, 0)
57+
self.assertEqual(0 or 0 or 3, 3)
58+
self.assertEqual(0 or 2 or 3, 2)
59+
60+
def test_complex_boolean(self):
61+
x, y = 5, 10
62+
self.assertTrue(x > 0 and y > 0)
63+
self.assertFalse(x > 0 and y < 0)
64+
self.assertTrue(x > 0 or y < 0)
65+
self.assertFalse(x < 0 or y < 0)
66+
67+
def test_default_pattern(self):
68+
def f(x=None):
69+
return x or "default"
70+
self.assertEqual(f(), "default")
71+
self.assertEqual(f("value"), "value")
72+
self.assertEqual(f(0), "default") # 0 is falsy
73+
74+
75+
class IdentityTest(unittest.TestCase):
76+
77+
def test_is(self):
78+
a = [1, 2]
79+
b = a
80+
c = [1, 2]
81+
self.assertTrue(a is b)
82+
self.assertFalse(a is c)
83+
84+
def test_is_not(self):
85+
a = [1, 2]
86+
c = [1, 2]
87+
self.assertTrue(a is not c)
88+
self.assertFalse(a is not a)
89+
90+
def test_none_identity(self):
91+
self.assertTrue(None is None)
92+
self.assertFalse(None is not None)
93+
self.assertFalse(0 is None)
94+
95+
def test_bool_identity(self):
96+
self.assertTrue(True is True)
97+
self.assertTrue(False is False)
98+
self.assertFalse(True is False)
99+
100+
101+
if __name__ == "__main__":
102+
unittest.main()
Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""Tests for builtin exception types and their relationships"""
2+
3+
import unittest
4+
5+
6+
class ExceptionTypeTest(unittest.TestCase):
7+
8+
def test_base_hierarchy(self):
9+
self.assertTrue(issubclass(Exception, BaseException))
10+
self.assertTrue(issubclass(TypeError, Exception))
11+
self.assertTrue(issubclass(ValueError, Exception))
12+
self.assertTrue(issubclass(KeyError, LookupError))
13+
self.assertTrue(issubclass(IndexError, LookupError))
14+
self.assertTrue(issubclass(LookupError, Exception))
15+
self.assertTrue(issubclass(NotImplementedError, RuntimeError))
16+
self.assertTrue(issubclass(ZeroDivisionError, ArithmeticError))
17+
self.assertTrue(issubclass(OverflowError, ArithmeticError))
18+
19+
def test_keyboard_interrupt(self):
20+
self.assertTrue(issubclass(KeyboardInterrupt, BaseException))
21+
self.assertFalse(issubclass(KeyboardInterrupt, Exception))
22+
23+
def test_exception_args_single(self):
24+
e = ValueError("msg")
25+
self.assertEqual(e.args, ("msg",))
26+
self.assertEqual(str(e), "msg")
27+
28+
def test_exception_args_multi(self):
29+
e = ValueError(1, 2, 3)
30+
self.assertEqual(e.args, (1, 2, 3))
31+
self.assertEqual(len(e.args), 3)
32+
33+
def test_exception_args_empty(self):
34+
e = ValueError()
35+
self.assertEqual(e.args, ())
36+
37+
def test_catch_parent(self):
38+
for ExcType in [TypeError, ValueError, KeyError, IndexError]:
39+
try:
40+
raise ExcType("test")
41+
except Exception:
42+
pass # should catch all
43+
else:
44+
self.fail("%s not caught by Exception" % ExcType.__name__)
45+
46+
def test_catch_specific(self):
47+
caught = None
48+
try:
49+
raise KeyError("k")
50+
except ValueError:
51+
caught = "ValueError"
52+
except KeyError:
53+
caught = "KeyError"
54+
except TypeError:
55+
caught = "TypeError"
56+
self.assertEqual(caught, "KeyError")
57+
58+
def test_catch_tuple(self):
59+
for ExcType in [ValueError, TypeError, KeyError]:
60+
try:
61+
raise ExcType()
62+
except (ValueError, TypeError, KeyError):
63+
pass
64+
else:
65+
self.fail("%s not caught by tuple" % ExcType.__name__)
66+
67+
68+
class ErrorRaisingTest(unittest.TestCase):
69+
70+
def test_type_error(self):
71+
with self.assertRaises(TypeError):
72+
len(42)
73+
74+
def test_value_error(self):
75+
with self.assertRaises(ValueError):
76+
int("not_a_number")
77+
78+
def test_index_error(self):
79+
with self.assertRaises(IndexError):
80+
[1, 2, 3][10]
81+
82+
def test_key_error(self):
83+
with self.assertRaises(KeyError):
84+
{}["missing"]
85+
86+
def test_attribute_error(self):
87+
with self.assertRaises(AttributeError):
88+
(42).nonexistent
89+
90+
def test_zero_division(self):
91+
with self.assertRaises(ZeroDivisionError):
92+
1 / 0
93+
with self.assertRaises(ZeroDivisionError):
94+
1 // 0
95+
96+
def test_name_error(self):
97+
def f():
98+
return undefined_name
99+
with self.assertRaises(NameError):
100+
f()
101+
102+
def test_stop_iteration(self):
103+
it = iter([])
104+
with self.assertRaises(StopIteration):
105+
next(it)
106+
107+
108+
if __name__ == "__main__":
109+
unittest.main()

tests/cpython/test_functions.py

Lines changed: 127 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,127 @@
1+
"""Tests for function features — defaults, varargs, kwargs, closures, recursion"""
2+
3+
import unittest
4+
5+
6+
class FunctionBasicTest(unittest.TestCase):
7+
8+
def test_no_args(self):
9+
def f():
10+
return 42
11+
self.assertEqual(f(), 42)
12+
13+
def test_positional(self):
14+
def f(a, b, c):
15+
return a + b + c
16+
self.assertEqual(f(1, 2, 3), 6)
17+
18+
def test_default_args(self):
19+
def f(a, b=10, c=20):
20+
return a + b + c
21+
self.assertEqual(f(1), 31)
22+
self.assertEqual(f(1, 2), 23)
23+
self.assertEqual(f(1, 2, 3), 6)
24+
25+
def test_keyword_args(self):
26+
def f(a, b=0, c=0):
27+
return (a, b, c)
28+
self.assertEqual(f(1, c=3), (1, 0, 3))
29+
self.assertEqual(f(1, b=2, c=3), (1, 2, 3))
30+
31+
def test_keyword_only(self):
32+
def f(a, *, b, c=10):
33+
return a + b + c
34+
self.assertEqual(f(1, b=2), 13)
35+
self.assertEqual(f(1, b=2, c=3), 6)
36+
37+
def test_varargs(self):
38+
def f(*args):
39+
return args
40+
self.assertEqual(f(), ())
41+
self.assertEqual(f(1, 2, 3), (1, 2, 3))
42+
43+
def test_kwargs(self):
44+
def f(**kw):
45+
return sorted(kw.items())
46+
self.assertEqual(f(a=1, b=2), [('a', 1), ('b', 2)])
47+
48+
def test_mixed(self):
49+
def f(a, b, *args, **kw):
50+
return (a, b, args, sorted(kw.items()))
51+
result = f(1, 2, 3, 4, x=5)
52+
self.assertEqual(result, (1, 2, (3, 4), [('x', 5)]))
53+
54+
def test_return_none(self):
55+
def f():
56+
pass
57+
self.assertIsNone(f())
58+
59+
def test_multiple_return(self):
60+
def f():
61+
return 1, 2, 3
62+
a, b, c = f()
63+
self.assertEqual((a, b, c), (1, 2, 3))
64+
65+
66+
class RecursionTest(unittest.TestCase):
67+
68+
def test_factorial(self):
69+
def fact(n):
70+
if n <= 1:
71+
return 1
72+
return n * fact(n - 1)
73+
self.assertEqual(fact(10), 3628800)
74+
75+
def test_fibonacci(self):
76+
def fib(n):
77+
if n < 2:
78+
return n
79+
return fib(n - 1) + fib(n - 2)
80+
self.assertEqual(fib(10), 55)
81+
82+
def test_mutual_recursion(self):
83+
def is_even(n):
84+
if n == 0:
85+
return True
86+
return is_odd(n - 1)
87+
def is_odd(n):
88+
if n == 0:
89+
return False
90+
return is_even(n - 1)
91+
self.assertTrue(is_even(10))
92+
self.assertFalse(is_even(11))
93+
self.assertTrue(is_odd(7))
94+
95+
96+
class HigherOrderTest(unittest.TestCase):
97+
98+
def test_function_as_arg(self):
99+
def apply(f, x):
100+
return f(x)
101+
self.assertEqual(apply(str, 42), "42")
102+
self.assertEqual(apply(len, [1, 2, 3]), 3)
103+
104+
def test_function_as_return(self):
105+
def make_adder(n):
106+
def adder(x):
107+
return x + n
108+
return adder
109+
add10 = make_adder(10)
110+
self.assertEqual(add10(5), 15)
111+
112+
def test_map(self):
113+
result = list(map(lambda x: x * 2, [1, 2, 3]))
114+
self.assertEqual(result, [2, 4, 6])
115+
116+
def test_filter(self):
117+
result = list(filter(lambda x: x > 2, [1, 2, 3, 4, 5]))
118+
self.assertEqual(result, [3, 4, 5])
119+
120+
def test_sorted_key(self):
121+
data = ["banana", "apple", "cherry"]
122+
result = sorted(data, key=len)
123+
self.assertEqual(result, ["apple", "banana", "cherry"])
124+
125+
126+
if __name__ == "__main__":
127+
unittest.main()

0 commit comments

Comments
 (0)