Spaces:
Running
Running
Download tests/unit/test_sandbox.py from iamfebin/algospaced-dsa: direct link, hf CLI and curl.
- Browser
- Download file 3.71 kB
-
https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_sandbox.py
- Command line
-
hf download hf://spaces/iamfebin/algospaced-dsa/tests/unit/test_sandbox.py
-
curl -L -o test_sandbox.py https://huggingface.co/spaces/iamfebin/algospaced-dsa/resolve/main/tests/unit/test_sandbox.py
3.71 kB
| 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() | |