File size: 3,711 Bytes
8800c81
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
from sandbox import run_python_sandbox


class TestPythonSandbox:
    def test_solution_class_all_passed(self):
        """Standard LeetCode Solution class passing all test cases."""
        user_code = """
class Solution:
    def twoSum(self, nums, target):
        lookup = {}
        for i, num in enumerate(nums):
            diff = target - num
            if diff in lookup:
                return [lookup[diff], i]
            lookup[num] = i
        return []
"""
        test_cases = [
            {"input": [[2, 7, 11, 15], 9], "expected": [0, 1]},
            {"input": [[3, 2, 4], 6], "expected": [1, 2]},
        ]
        result = run_python_sandbox(user_code, test_cases, timeout=3.0)

        assert result["success"] is True
        results = result["results"]
        assert len(results) == 2
        assert results[0]["status"] == "passed"
        assert results[0]["got"] == [0, 1]
        assert results[1]["status"] == "passed"
        assert results[1]["got"] == [1, 2]

    def test_standalone_function_entry_point(self):
        """User code providing a standalone solve() function."""
        user_code = """
def solve(n):
    return n * n
"""
        test_cases = [
            {"input": [4], "expected": 16},
            {"input": [5], "expected": 25},
        ]
        result = run_python_sandbox(user_code, test_cases, timeout=3.0)

        assert result["success"] is True
        results = result["results"]
        assert len(results) == 2
        assert all(r["status"] == "passed" for r in results)

    def test_failing_test_case_detection(self):
        """Incorrect logic should flag individual test cases as failed."""
        user_code = """
def add(a, b):
    return a - b  # Bug: subtraction instead of addition
"""
        test_cases = [
            {"input": [2, 3], "expected": 5},
        ]
        result = run_python_sandbox(user_code, test_cases, timeout=3.0)

        assert result["success"] is True
        results = result["results"]
        assert len(results) == 1
        assert results[0]["status"] == "failed"
        assert results[0]["got"] == -1
        assert results[0]["expected"] == 5

    def test_runtime_error_inside_user_code(self):
        """Code that raises exceptions during execution should record error status."""
        user_code = """
def divide(a, b):
    return a // b
"""
        test_cases = [
            {"input": [10, 0], "expected": 0},
        ]
        result = run_python_sandbox(user_code, test_cases, timeout=3.0)

        assert result["success"] is True
        results = result["results"]
        assert len(results) == 1
        assert results[0]["status"] == "error"
        assert "division by zero" in results[0]["error"].lower()

    def test_no_callable_function(self):
        """Code without any callable function should yield an error payload."""
        user_code = "x = 42\ny = 100"
        test_cases = [{"input": [1], "expected": 1}]
        result = run_python_sandbox(user_code, test_cases, timeout=3.0)

        assert result["success"] is True
        results = result["results"]
        assert results[0]["status"] == "error"
        assert "No callable function found" in results[0]["error"]

    def test_execution_timeout(self):
        """Long-running or infinite loops should be killed and return timeout status."""
        user_code = """
import time
def slow_function(x):
    time.sleep(2)
    return x
"""
        test_cases = [{"input": [1], "expected": 1}]
        # Set a short timeout (0.5s) so the test runs swiftly
        result = run_python_sandbox(user_code, test_cases, timeout=0.5)

        assert result["success"] is False
        assert "timed out" in result["error"].lower()