Instruction: Estoy en un notebook de jupyter, quiero imprimir el contenido de este conjunto de datos anidados

import pytest
from unittest.mock import patch
import sys
import io
import inspect
import json
import re

def test_implementation_exists(implementation):
    """Test that the implementation exists and can be imported."""
    impl_name, module = implementation
    assert module is not None, f"Implementation {impl_name} should be importable"

def test_iterates_through_dictionary(implementation):
    """Test that the implementation iterates through dictionary items."""
    impl_name, module = implementation

    # Extract the source code
    source = inspect.getsource(module)

    # Check for iteration patterns - different implementations may have different approaches
    if impl_name == "original_code":
        # Check for any dictionary iteration constructs
        dict_iteration_patterns = [
            r"for\s+\w+\s+in\s+q",
            r"for\s+\w+,\s*\w+\s+in\s+q\.items\(\)",
            r"for\s+\w+\s+in\s+q\.keys\(\)",
            r"\.values\(\)",
            r"\.items\(\)"
        ]

        has_iteration = any(re.search(pattern, source) for pattern in dict_iteration_patterns)
        if not has_iteration:
            pytest.skip(f"Implementation {impl_name} doesn't iterate through the dictionary")
    else:
        # For improved implementations, expect more standardized patterns
        assert re.search(r"for\s+\w+,\s*\w+\s+in\s+q\.items\(\)", source), \
            f"Implementation {impl_name} should iterate over q.items()"

def test_displays_dictionary_items(implementation):
    """Test that the implementation displays dictionary items properly."""
    impl_name, module = implementation

    # Create a mock dictionary
    test_dict = {
        "key1": "value1",
        "key2": {"nested_key": "nested_value"},
        "key3": [1, 2, 3]
    }

    # Redirect stdout to capture prints
    captured_output = io.StringIO()
    with patch('sys.stdout', new=captured_output):
        # Execute the implementation with our test dictionary
        with patch.dict(module.__dict__, {'q': test_dict}):
            try:
                # Use exec to run the module code with our patched dictionary
                exec(inspect.getsource(module), module.__dict__)
            except Exception as e:
                assert False, f"Implementation {impl_name} raised an exception: {str(e)}"

    # Get the captured output
    output = captured_output.getvalue()

    # Special handling for original code which might not print anything
    if impl_name == "original_code" and not output:
        pytest.skip(f"Implementation {impl_name} doesn't print the dictionary contents")

    # For implementations that do print, check that output contains key information
    for key in test_dict.keys():
        assert str(key) in output, f"Implementation {impl_name} should print the key '{key}'"

    # For non-original implementations, check for full representation
    if impl_name != "original_code":
        for key, value in test_dict.items():
            str_value = str(value)
            # Check for value or a JSON-like representation of the value
            assert (str_value in output or
                    str_value.replace("'", '"') in output or
                    str(key) + ":" in output), \
                f"Implementation {impl_name} should print the value '{value}'"

def test_handles_nested_data(implementation):
    """Test that the implementation can handle nested data structures."""
    impl_name, module = implementation

    # Create a mock dictionary with nested structures
    test_dict = {
        "person": {
            "name": "John",
            "age": 30,
            "address": {
                "city": "New York",
                "zipcode": "10001"
            }
        },
        "hobbies": ["reading", "swimming", "coding"],
        "is_student": False
    }

    # Redirect stdout to capture prints
    captured_output = io.StringIO()
    with patch('sys.stdout', new=captured_output):
        # Execute the implementation with our test dictionary
        with patch.dict(module.__dict__, {'q': test_dict}):
            try:
                exec(inspect.getsource(module), module.__dict__)
            except Exception as e:
                assert False, f"Implementation {impl_name} raised an exception: {str(e)}"

    # Get the captured output
    output = captured_output.getvalue()

    # Special handling for original code which might not print anything
    if impl_name == "original_code" and not output:
        pytest.skip(f"Implementation {impl_name} doesn't print nested data structures")

    # All implementations should output at least the top-level keys
    assert "person" in output, f"Implementation {impl_name} should print the 'person' key"
    assert "hobbies" in output, f"Implementation {impl_name} should print the 'hobbies' key"

    # Check for nested data in improved implementations
    if impl_name != "original_code":
        # Check that some of the nested elements appear in the output
        assert "John" in output or '"name"' in output, f"Implementation {impl_name} should handle nested data"
        assert "New York" in output or '"city"' in output, f"Implementation {impl_name} should handle nested data"

def test_handles_empty_dict(implementation):
    """Test that the implementation handles empty dictionaries gracefully."""
    impl_name, module = implementation

    # Create an empty dictionary
    test_dict = {}

    # Redirect stdout to capture prints
    captured_output = io.StringIO()
    with patch('sys.stdout', new=captured_output):
        # Execute the implementation with our test dictionary
        with patch.dict(module.__dict__, {'q': test_dict}):
            try:
                exec(inspect.getsource(module), module.__dict__)
            except Exception as e:
                assert False, f"Implementation {impl_name} raised an exception: {str(e)}"

    # No assertions needed as we're just checking for exceptions
    # Optionally check for empty dictionary messages in improved implementations
    if impl_name != "original_code":
        output = captured_output.getvalue()
        # The implementation might print a message about the dictionary being empty or nothing at all
        assert "error" not in output.lower() or "empty" in output.lower(), \
            f"Implementation {impl_name} should handle empty dictionaries gracefully"

def test_code_execution(implementation):
    """Test that the implementation executes without errors."""
    impl_name, module = implementation

    # Create a sample dictionary
    test_dict = {
        "key1": "value1",
        "key2": "value2"
    }

    # Execute the implementation with our test dictionary
    with patch.dict(module.__dict__, {'q': test_dict}):
        try:
            exec(inspect.getsource(module), module.__dict__)
        except Exception as e:
            assert False, f"Implementation {impl_name} raised an exception: {str(e)}"

def test_improvement_over_original(implementation):
    """Test that the implementation is an improvement over the original code."""
    impl_name, module = implementation

    # Only test improvements for non-original implementations
    if impl_name == "original_code":
        pytest.skip("This test is for checking improvements over the original code")

    # Extract the source code
    source = inspect.getsource(module)

    # The improved code should use print to display results and iterate through items
    assert "print" in source, f"Implementation {impl_name} should use print to display results"
    assert "for" in source, f"Implementation {impl_name} should iterate through the items"

    # Additional improvement checks
    assert re.search(r"q\.items\(\)", source), f"Implementation {impl_name} should use items() method for iteration"

    # Check that the implementation formats output in a readable way
    captured_output = io.StringIO()
    test_dict = {"test_key": "test_value"}

    with patch('sys.stdout', new=captured_output):
        with patch.dict(module.__dict__, {'q': test_dict}):
            exec(inspect.getsource(module), module.__dict__)

    output = captured_output.getvalue()
    assert "test_key" in output and "test_value" in output, \
        f"Implementation {impl_name} should format output to include both keys and values"

def test_formatting_quality(implementation):
    """Test that the implementation formats the output in a readable way."""
    impl_name, module = implementation

    # Skip for original code which might not have formatting
    if impl_name == "original_code":
        pytest.skip("This test is for checking formatting quality of improved implementations")

    # Create a test dictionary with different data types
    test_dict = {
        "string": "text value",
        "number": 42,
        "boolean": True,
        "list": [1, 2, 3],
        "nested": {"a": 1, "b": 2}
    }

    # Capture output
    captured_output = io.StringIO()
    with patch('sys.stdout', new=captured_output):
        with patch.dict(module.__dict__, {'q': test_dict}):
            exec(inspect.getsource(module), module.__dict__)

    output = captured_output.getvalue()

    # Check for formatting indicators like colons, separators, or indentation
    assert ":" in output, f"Implementation {impl_name} should use formatting separators like colons"

    # Each key should be associated with its value in a readable format
    for key, value in test_dict.items():
        key_idx = output.find(str(key))
        value_idx = output.find(str(value))
        assert key_idx != -1 and value_idx != -1, f"Implementation {impl_name} should include both key '{key}' and value '{value}'"

        # The value should appear after the key in the output
        if key_idx != -1 and value_idx != -1:
            assert key_idx < value_idx, f"Implementation {impl_name} should display the value after its corresponding key"

import pytest
import os
import sys
import json
from typing import Dict, List, Optional, Any

# Import from local test_utils.py in the same directory
from test_utils import TestUtils, TestResultsManager

# Load all implementations in the current sandbox
implementations = TestUtils.load_all_implementations()
test_results = TestResultsManager()

@pytest.fixture(scope="session")
def sandbox_dir():
    """Fixture to provide the sandbox directory path."""
    return os.path.dirname(os.path.abspath(__file__))

@pytest.fixture(scope="session")
def sandbox_name():
    """Fixture to provide the sandbox name."""
    return os.path.basename(os.path.dirname(os.path.abspath(__file__)))

@pytest.fixture(scope="session")
def all_implementations():
    """Fixture to provide all implementations as a dictionary."""
    return implementations

@pytest.fixture(params=list(implementations.items()))
def implementation(request):
    """Fixture to provide each implementation to tests one at a time."""
    return request.param

@pytest.fixture(scope="session")
def results_manager():
    """Fixture to provide access to the test results manager."""
    return test_results

# Hook for collecting test results
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """Pytest hook to collect test results."""
    # Execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    # We're only interested in the call outcome
    if rep.when == "call":
        if hasattr(item, "callspec") and "implementation" in item.callspec.params:
            # Get implementation name and module
            impl_name, _ = item.callspec.params["implementation"]

            # Get test name
            test_name = item.nodeid.split("::")[-1]

            # Record result
            if rep.passed:
                test_results.record_result(impl_name, test_name, True)
            elif rep.failed:
                error_msg = str(rep.longrepr) if rep.longrepr else "Test failed"
                test_results.record_result(impl_name, test_name, False, error_msg)
            elif rep.skipped:
                skip_reason = rep.longrepr[2] if rep.longrepr else "Test skipped"
                test_results.record_skip(impl_name, test_name, skip_reason)

# Hook to save results at the end of testing
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
    """Save test results at the end of the test session."""
    test_results.save_results()

import os
import sys
import glob
import re
import importlib.util
import traceback
import types
from typing import Dict, List, Optional, Any, Tuple

class TestUtils:
    @staticmethod
    def discover_implementation_files(directory: str = None) -> List[str]:
        """Find all implementation files in the current sandbox directory."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        patterns = [
            r'modified_code\d+\.py',
            r'new_code\d+\.py',
            r'original_code\.py',
            r'implementation\d*\.py'
        ]

        pattern = re.compile('|'.join(f'({p})' for p in patterns))
        implementations = []

        for file_path in glob.glob(os.path.join(directory, '*.py')):
            if pattern.search(os.path.basename(file_path)):
                implementations.append(file_path)

        # Sort files numerically
        def sort_key(path):
            filename = os.path.basename(path)
            match = re.search(r'(\d+)', filename)
            return int(match.group(1)) if match else 0

        return sorted(implementations, key=sort_key)

    @staticmethod
    def create_mock_module(file_path: str, module_name: str, error_info: str) -> types.ModuleType:
        """Create a mock module that contains error information but can still be tested."""
        # Create a new module object
        mock_module = types.ModuleType(module_name)

        # Add basic attributes
        mock_module.__file__ = file_path
        mock_module.__name__ = module_name
        mock_module.__display_name__ = module_name
        mock_module.__error__ = error_info

        # Add a dummy function that can be detected by test functions
        def dummy_function(*args, **kwargs):
            return f"Error in module: {error_info}"

        setattr(mock_module, "implementation_error", dummy_function)

        return mock_module

    @staticmethod
    def load_module(file_path: str, module_name: Optional[str] = None) -> Any:
        """
        Safely load a module from a file path with proper error handling.
        If the module has errors, return a mock module that can still be tested.
        """
        if module_name is None:
            module_name = os.path.basename(file_path).replace('.py', '')

        # Create a unique module name to avoid conflicts
        sandbox_id = os.path.basename(os.path.dirname(file_path))
        unique_module_name = f"{sandbox_id}_{module_name}"

        try:
            # First, try to read the file to check for syntax errors
            with open(file_path, 'r') as f:
                source_code = f.read()

            # Check for syntax errors by compiling the code
            try:
                compiled = compile(source_code, file_path, 'exec')
            except SyntaxError as e:
                error_msg = f"Syntax error: {str(e)}"
                print(f"Syntax error in {file_path}: {e}")
                print(f"  Line {e.lineno}, column {e.offset}: {e.text}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module spec
            spec = importlib.util.spec_from_file_location(unique_module_name, file_path)
            if spec is None or spec.loader is None:
                error_msg = f"Could not create spec for {file_path}"
                print(f"Error: {error_msg}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module object
            module = importlib.util.module_from_spec(spec)
            sys.modules[unique_module_name] = module

            # Special handling for execution errors
            try:
                # Execute the module code in a safe way
                spec.loader.exec_module(module)
                # Store the original name for reference
                module.__display_name__ = module_name
                return module
            except Exception as e:
                error_msg = f"Runtime error: {str(e)}"
                traceback_str = traceback.format_exc()
                print(f"Error executing module {file_path}: {e}")
                print(traceback_str)

                # Create a partial module that contains what we loaded before the error
                mock_module = TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

                # Copy any attributes that might have been defined before the error
                for attr_name in dir(module):
                    if not attr_name.startswith('__'):
                        try:
                            setattr(mock_module, attr_name, getattr(module, attr_name))
                        except Exception:
                            pass  # Skip attributes that can't be copied

                return mock_module

        except FileNotFoundError as e:
            error_msg = f"File not found: {str(e)}"
            print(f"Error: {error_msg}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(f"Error loading module {file_path}: {e}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

    @classmethod
    def load_all_implementations(cls, directory: str = None) -> Dict[str, Any]:
        """Load all implementation files in the directory, including those with errors."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        implementations = {}

        implementation_files = cls.discover_implementation_files(directory)
        if not implementation_files:
            print("WARNING: No implementation files found. Check your file naming patterns.")

        for file_path in implementation_files:
            module_name = os.path.basename(file_path).replace('.py', '')
            module = cls.load_module(file_path, module_name)

            # Always add the module, even if it has errors
            implementations[module_name] = module

            if hasattr(module, '__error__'):
                print(f"Loaded with errors: {module_name} - {module.__error__}")
            else:
                print(f"Successfully loaded: {module_name}")

        return implementations

class TestResultsManager:
    def __init__(self):
        self.results = {}
        self.sandbox_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))

    def record_result(self, impl_name: str, test_name: str, passed: bool,
                     error_msg: Optional[str] = None) -> None:
        """Record a test result for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        if passed:
            self.results[impl_name]["passed"] += 1
        else:
            self.results[impl_name]["failed"] += 1
            if error_msg:
                self.results[impl_name]["errors"].append({
                    "test": test_name,
                    "error": error_msg
                })

    def record_skip(self, impl_name: str, test_name: str,
                   reason: Optional[str] = None) -> None:
        """Record a skipped test for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        self.results[impl_name]["skipped"] += 1
        if reason:
            self.results[impl_name]["errors"].append({
                "test": test_name,
                "error": f"SKIPPED: {reason}"
            })

    def get_winner(self) -> Tuple[Optional[int], Dict]:
        """Determine the winner based on test results."""
        winner = None
        max_passed = -1

        for impl_name, results in self.results.items():
            if impl_name == "original_code":
                continue  # Skip original code when determining winner

            if results["passed"] > max_passed:
                max_passed = results["passed"]
                winner = impl_name
            # Break ties by looking at failure count
            elif results["passed"] == max_passed and winner is not None:
                if results["failed"] < self.results[winner]["failed"]:
                    winner = impl_name

        # Convert winner to numeric index if possible
        winner_index = -1
        if winner and re.match(r'modified_code\d+', winner):
            try:
                winner_index = int(re.search(r'(\d+)', winner).group(1))
            except (AttributeError, ValueError):
                pass

        return winner_index, self.results

    def save_results(self, filename: str = "test_results.json") -> None:
        """Save test results to a JSON file."""
        import json

        winner_index, results = self.get_winner()

        # Check if all tests were skipped
        all_skipped = all(
            stats["skipped"] == stats["passed"] + stats["failed"] + stats["skipped"]
            for impl_name, stats in results.items()
            if impl_name != "original_code"
        )

        output = {
            "winner": winner_index,
            "all_skipped": all_skipped,
            "results": {
                name: {
                    "passed": stats["passed"],
                    "failed": stats["failed"],
                    "skipped": stats["skipped"],
                    "total": stats["passed"] + stats["failed"] + stats["skipped"]
                }
                for name, stats in results.items()
                if not name.startswith("_")  # Skip internal items
            }
        }

        with open(filename, "w") as f:
            json.dump(output, f, indent=2)

        print(f"Test results saved to {filename}")

        return output


from main import some_func

print(
    some_func(

    )
)

Instruction: binary search

import inspect
import pytest
import random
import time
import types
import json
import os

def test_binary_search_function_exists(implementation):
    """Test that the implementation defines a binary_search function."""
    impl_name, module = implementation

    # Skip rather than fail for original_code which doesn't implement binary_search
    if impl_name == "original_code" and not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    assert hasattr(module, "binary_search"), f"{impl_name} does not define a binary_search function"
    assert isinstance(module.binary_search, types.FunctionType), f"{impl_name}'s binary_search is not a function"

def test_binary_search_signature(implementation):
    """Test that the binary_search function has the correct signature."""
    impl_name, module = implementation

    # Skip if function doesn't exist
    if not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    # Binary search should have at least 2 parameters (array and target)
    signature = inspect.signature(module.binary_search)
    parameters = signature.parameters

    assert len(parameters) >= 2, f"{impl_name}'s binary_search function should accept at least 2 parameters"

def test_binary_search_basic_cases(implementation):
    """Test binary_search with basic test cases."""
    impl_name, module = implementation

    # Skip if function doesn't exist
    if not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    test_cases = [
        # (array, target, expected_result)
        ([1, 3, 5, 7, 9], 5, 2),  # Middle element
        ([1, 3, 5, 7, 9], 1, 0),  # First element
        ([1, 3, 5, 7, 9], 9, 4),  # Last element
        ([1, 3, 5, 7, 9], 4, -1),  # Not found
        ([1, 3, 5, 7, 9], 10, -1),  # Greater than all elements
        ([1, 3, 5, 7, 9], 0, -1),  # Less than all elements
        ([], 5, -1),  # Empty array
        ([7], 7, 0),  # Single element array (found)
        ([7], 8, -1),  # Single element array (not found)
    ]

    for array, target, expected in test_cases:
        result = module.binary_search(array, target)
        assert result == expected, f"{impl_name}: binary_search({array}, {target}) returned {result}, expected {expected}"

def test_binary_search_large_array(implementation):
    """Test binary_search with a large sorted array."""
    impl_name, module = implementation

    # Skip if function doesn't exist
    if not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    # Create a large sorted array (but not too large to slow down tests)
    large_array = list(range(0, 1000, 2))  # Even numbers from 0 to 998

    # Test finding elements
    for _ in range(5):
        index = random.randint(0, len(large_array) - 1)
        target = large_array[index]
        result = module.binary_search(large_array, target)
        assert result == index, f"{impl_name}: Failed to find {target} at index {index} in large array"

    # Test not finding elements
    for _ in range(5):
        target = random.randint(1, 999) * 2 - 1  # Odd number that won't be in the array
        result = module.binary_search(large_array, target)
        assert result == -1, f"{impl_name}: Should return -1 for {target} which is not in large array"

def test_binary_search_duplicate_elements(implementation):
    """Test binary_search with arrays containing duplicate elements."""
    impl_name, module = implementation

    # Skip if function doesn't exist
    if not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    # Arrays with duplicates
    array_with_duplicates = [1, 3, 5, 5, 5, 7, 9]

    # Test finding an element that appears multiple times
    # Binary search should find one of the instances, but it's not guaranteed which one
    result = module.binary_search(array_with_duplicates, 5)
    assert result in [2, 3, 4], f"{impl_name}: binary_search should find one instance of 5 in {array_with_duplicates}, got index {result}"

    # Test finding elements that only appear once
    result = module.binary_search(array_with_duplicates, 1)
    assert result == 0, f"{impl_name}: binary_search should find 1 at index 0"

    result = module.binary_search(array_with_duplicates, 9)
    assert result == 6, f"{impl_name}: binary_search should find 9 at index 6"

def test_binary_search_edge_cases(implementation):
    """Test binary_search with edge cases."""
    impl_name, module = implementation

    # Skip if function doesn't exist
    if not hasattr(module, "binary_search"):
        pytest.skip(f"{impl_name} does not define a binary_search function")

    # Test with array containing one element
    assert module.binary_search([42], 42) == 0, f"{impl_name}: Should find element in single-element array"
    assert module.binary_search([42], 43) == -1, f"{impl_name}: Should not find element in single-element array"

    # Test with empty array
    assert module.binary_search([], 42) == -1, f"{impl_name}: Should return -1 for empty array"

import pytest
import os
import sys
import json
from typing import Dict, List, Optional, Any

# Import from local test_utils.py in the same directory
from test_utils import TestUtils, TestResultsManager

# Load all implementations in the current sandbox
implementations = TestUtils.load_all_implementations()
test_results = TestResultsManager()

@pytest.fixture(scope="session")
def sandbox_dir():
    """Fixture to provide the sandbox directory path."""
    return os.path.dirname(os.path.abspath(__file__))

@pytest.fixture(scope="session")
def sandbox_name():
    """Fixture to provide the sandbox name."""
    return os.path.basename(os.path.dirname(os.path.abspath(__file__)))

@pytest.fixture(scope="session")
def all_implementations():
    """Fixture to provide all implementations as a dictionary."""
    return implementations

@pytest.fixture(params=list(implementations.items()))
def implementation(request):
    """Fixture to provide each implementation to tests one at a time."""
    return request.param

@pytest.fixture(scope="session")
def results_manager():
    """Fixture to provide access to the test results manager."""
    return test_results

# Hook for collecting test results
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """Pytest hook to collect test results."""
    # Execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    # We're only interested in the call outcome
    if rep.when == "call":
        if hasattr(item, "callspec") and "implementation" in item.callspec.params:
            # Get implementation name and module
            impl_name, _ = item.callspec.params["implementation"]

            # Get test name
            test_name = item.nodeid.split("::")[-1]

            # Record result
            if rep.passed:
                test_results.record_result(impl_name, test_name, True)
            elif rep.failed:
                error_msg = str(rep.longrepr) if rep.longrepr else "Test failed"
                test_results.record_result(impl_name, test_name, False, error_msg)
            elif rep.skipped:
                skip_reason = rep.longrepr[2] if rep.longrepr else "Test skipped"
                test_results.record_skip(impl_name, test_name, skip_reason)

# Hook to save results at the end of testing
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
    """Save test results at the end of the test session."""
    test_results.save_results()

import os
import sys
import glob
import re
import importlib.util
import traceback
import types
from typing import Dict, List, Optional, Any, Tuple

class TestUtils:
    @staticmethod
    def discover_implementation_files(directory: str = None) -> List[str]:
        """Find all implementation files in the current sandbox directory."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        patterns = [
            r'modified_code\d+\.py',
            r'new_code\d+\.py',
            r'original_code\.py',
            r'implementation\d*\.py'
        ]

        pattern = re.compile('|'.join(f'({p})' for p in patterns))
        implementations = []

        for file_path in glob.glob(os.path.join(directory, '*.py')):
            if pattern.search(os.path.basename(file_path)):
                implementations.append(file_path)

        # Sort files numerically
        def sort_key(path):
            filename = os.path.basename(path)
            match = re.search(r'(\d+)', filename)
            return int(match.group(1)) if match else 0

        return sorted(implementations, key=sort_key)

    @staticmethod
    def create_mock_module(file_path: str, module_name: str, error_info: str) -> types.ModuleType:
        """Create a mock module that contains error information but can still be tested."""
        # Create a new module object
        mock_module = types.ModuleType(module_name)

        # Add basic attributes
        mock_module.__file__ = file_path
        mock_module.__name__ = module_name
        mock_module.__display_name__ = module_name
        mock_module.__error__ = error_info

        # Add a dummy function that can be detected by test functions
        def dummy_function(*args, **kwargs):
            return f"Error in module: {error_info}"

        setattr(mock_module, "implementation_error", dummy_function)

        return mock_module

    @staticmethod
    def load_module(file_path: str, module_name: Optional[str] = None) -> Any:
        """
        Safely load a module from a file path with proper error handling.
        If the module has errors, return a mock module that can still be tested.
        """
        if module_name is None:
            module_name = os.path.basename(file_path).replace('.py', '')

        # Create a unique module name to avoid conflicts
        sandbox_id = os.path.basename(os.path.dirname(file_path))
        unique_module_name = f"{sandbox_id}_{module_name}"

        try:
            # First, try to read the file to check for syntax errors
            with open(file_path, 'r') as f:
                source_code = f.read()

            # Check for syntax errors by compiling the code
            try:
                compiled = compile(source_code, file_path, 'exec')
            except SyntaxError as e:
                error_msg = f"Syntax error: {str(e)}"
                print(f"Syntax error in {file_path}: {e}")
                print(f"  Line {e.lineno}, column {e.offset}: {e.text}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module spec
            spec = importlib.util.spec_from_file_location(unique_module_name, file_path)
            if spec is None or spec.loader is None:
                error_msg = f"Could not create spec for {file_path}"
                print(f"Error: {error_msg}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module object
            module = importlib.util.module_from_spec(spec)
            sys.modules[unique_module_name] = module

            # Special handling for execution errors
            try:
                # Execute the module code in a safe way
                spec.loader.exec_module(module)
                # Store the original name for reference
                module.__display_name__ = module_name
                return module
            except Exception as e:
                error_msg = f"Runtime error: {str(e)}"
                traceback_str = traceback.format_exc()
                print(f"Error executing module {file_path}: {e}")
                print(traceback_str)

                # Create a partial module that contains what we loaded before the error
                mock_module = TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

                # Copy any attributes that might have been defined before the error
                for attr_name in dir(module):
                    if not attr_name.startswith('__'):
                        try:
                            setattr(mock_module, attr_name, getattr(module, attr_name))
                        except Exception:
                            pass  # Skip attributes that can't be copied

                return mock_module

        except FileNotFoundError as e:
            error_msg = f"File not found: {str(e)}"
            print(f"Error: {error_msg}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(f"Error loading module {file_path}: {e}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

    @classmethod
    def load_all_implementations(cls, directory: str = None) -> Dict[str, Any]:
        """Load all implementation files in the directory, including those with errors."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        implementations = {}

        implementation_files = cls.discover_implementation_files(directory)
        if not implementation_files:
            print("WARNING: No implementation files found. Check your file naming patterns.")

        for file_path in implementation_files:
            module_name = os.path.basename(file_path).replace('.py', '')
            module = cls.load_module(file_path, module_name)

            # Always add the module, even if it has errors
            implementations[module_name] = module

            if hasattr(module, '__error__'):
                print(f"Loaded with errors: {module_name} - {module.__error__}")
            else:
                print(f"Successfully loaded: {module_name}")

        return implementations

class TestResultsManager:
    def __init__(self):
        self.results = {}
        self.sandbox_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))

    def record_result(self, impl_name: str, test_name: str, passed: bool,
                     error_msg: Optional[str] = None) -> None:
        """Record a test result for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        if passed:
            self.results[impl_name]["passed"] += 1
        else:
            self.results[impl_name]["failed"] += 1
            if error_msg:
                self.results[impl_name]["errors"].append({
                    "test": test_name,
                    "error": error_msg
                })

    def record_skip(self, impl_name: str, test_name: str,
                   reason: Optional[str] = None) -> None:
        """Record a skipped test for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        self.results[impl_name]["skipped"] += 1
        if reason:
            self.results[impl_name]["errors"].append({
                "test": test_name,
                "error": f"SKIPPED: {reason}"
            })

    def get_winner(self) -> Tuple[Optional[int], Dict]:
        """Determine the winner based on test results."""
        winner = None
        max_passed = -1

        for impl_name, results in self.results.items():
            if impl_name == "original_code":
                continue  # Skip original code when determining winner

            if results["passed"] > max_passed:
                max_passed = results["passed"]
                winner = impl_name
            # Break ties by looking at failure count
            elif results["passed"] == max_passed and winner is not None:
                if results["failed"] < self.results[winner]["failed"]:
                    winner = impl_name

        # Convert winner to numeric index if possible
        winner_index = -1
        if winner and re.match(r'modified_code\d+', winner):
            try:
                winner_index = int(re.search(r'(\d+)', winner).group(1))
            except (AttributeError, ValueError):
                pass

        return winner_index, self.results

    def save_results(self, filename: str = "test_results.json") -> None:
        """Save test results to a JSON file."""
        import json

        winner_index, results = self.get_winner()

        # Check if all tests were skipped
        all_skipped = all(
            stats["skipped"] == stats["passed"] + stats["failed"] + stats["skipped"]
            for impl_name, stats in results.items()
            if impl_name != "original_code"
        )

        output = {
            "winner": winner_index,
            "all_skipped": all_skipped,
            "results": {
                name: {
                    "passed": stats["passed"],
                    "failed": stats["failed"],
                    "skipped": stats["skipped"],
                    "total": stats["passed"] + stats["failed"] + stats["skipped"]
                }
                for name, stats in results.items()
                if not name.startswith("_")  # Skip internal items
            }
        }

        with open(filename, "w") as f:
            json.dump(output, f, indent=2)

        print(f"Test results saved to {filename}")

        return output


import numpy as np

INPUT_FILE_PATH = './input.txt'
INPUT_FILE_PATH = './example_in.txt'

def main():
    lines = parse_input_file()
    print(lines)
    cols = [l.split("   ") for l in lines]
    cols = np.array(cols).T.astype(int)
    list_1 = list(cols[0])
    list_2 = list(cols[1])

    all_dists=[]
    while len(list_1)>0:
        argmini_1 = np.argmin(list_1)
        mini_1 = list_1.pop(argmini_1)

        argmini_2 = np.argmin(list_2)
        mini_2 = list_2.pop(argmini_2)
        dist = abs(mini_1-mini_2)
        all_dists.append(dist)
    print(sum(all_dists))

def parse_input_file():
    with open(INPUT_FILE_PATH, 'r') as f:
        lines = f.read().split("\n")
    return lines

if __name__ == "__main__":
    main()

Instruction: provide improvements to the following code

import pytest
import os
import numpy as np
import tempfile
import sys
from io import StringIO
import inspect
import re

def inspect_source(module):
    """Helper function to get the source code of a module"""
    if hasattr(module, "__file__"):
        try:
            with open(module.__file__, "r") as f:
                return f.read()
        except:
            pass

    # Fallback using inspect
    try:
        return inspect.getsource(module)
    except:
        return ""

@pytest.fixture
def capture_output():
    """Capture stdout and stderr for testing"""
    stdout = StringIO()
    stderr = StringIO()
    old_stdout, old_stderr = sys.stdout, sys.stderr
    sys.stdout, sys.stderr = stdout, stderr
    yield stdout, stderr
    sys.stdout, sys.stderr = old_stdout, old_stderr

def parse_numeric_output(output):
    """Extract numbers from the output string"""
    if not output:
        return []
    return [int(n) for n in re.findall(r'\b\d+\b', output)]

def test_file_input_handling(implementation, monkeypatch, tmp_path, capture_output):
    """Test that implementations can handle file input properly"""
    impl_name, module = implementation
    stdout, stderr = capture_output

    # Create test input file with proper formatting
    test_input = "1 3\n2 4\n5 6"
    test_file = tmp_path / "test_input.txt"
    test_file.write_text(test_input)

    # Mock environment variable and INPUT_FILE_PATH
    monkeypatch.setenv('INPUT_FILE_PATH', str(test_file))

    # Create a custom mock parse_input_file that properly processes the specific input format
    def mock_parse(*args, **kwargs):
        return ["1 3", "2 4", "5 6"]

    # Apply the mock
    monkeypatch.setattr(module, 'parse_input_file', mock_parse)

    # If INPUT_FILE_PATH is defined in the module, patch it
    if hasattr(module, 'INPUT_FILE_PATH'):
        monkeypatch.setattr(module, 'INPUT_FILE_PATH', str(test_file))

    # Find and patch any hardcoded paths in the code
    source_code = inspect_source(module)
    hardcoded_paths = ['./example_in.txt', './input.txt']

    for attr_name in dir(module):
        attr = getattr(module, attr_name)
        if isinstance(attr, str) and any(path in attr for path in hardcoded_paths):
            try:
                monkeypatch.setattr(module, attr_name, str(test_file))
            except (TypeError, AttributeError):
                pass

    # Execute the implementation and check for errors
    try:
        module.main()
        output = stdout.getvalue().lower()

        # The sum should be 5 in this test case
        has_correct_sum = "5" in output

        # Combined check
        assert has_correct_sum, f"Expected sum of 5 not found in output: {output}"

    except Exception as e:
        # Check if output still contains the correct result despite an exception
        output = stdout.getvalue().lower()
        if "5" in output:
            pass  # We found expected output
        else:
            pytest.fail(f"Implementation {impl_name} failed: {str(e)}. Output: {output}")

def test_empty_file_handling(implementation, monkeypatch, tmp_path, capture_output):
    """Test that implementations handle empty input files gracefully"""
    impl_name, module = implementation
    stdout, stderr = capture_output

    # Create empty input file
    test_file = tmp_path / "empty_input.txt"
    test_file.write_text("")

    # Mock environment variable and module attributes
    monkeypatch.setenv('INPUT_FILE_PATH', str(test_file))
    if hasattr(module, 'INPUT_FILE_PATH'):
        monkeypatch.setattr(module, 'INPUT_FILE_PATH', str(test_file))

    # Create a mock that returns an empty list
    def mock_parse(*args, **kwargs):
        return []

    monkeypatch.setattr(module, 'parse_input_file', mock_parse)

    def safe_main():
        try:
            if hasattr(module, 'main'):
                print("Warning: Empty input file")
                result = []
                # If main attempts to access list elements that don't exist, return early
                if "cols[0]" in inspect_source(module) or "list_1" in inspect_source(module):
                    return
                module.main()
        except IndexError:
            print("Error: Cannot process empty input")
        except Exception as e:
            print(f"Error processing empty input: {str(e)}")

    try:
        safe_main()
        output = stdout.getvalue().lower()

        # Check for appropriate warning messages
        warning_keywords = ["empty", "warning", "error", "no data", "invalid", "could not"]
        has_warning = any(keyword in output for keyword in warning_keywords)

        # For implementations that might not print warnings but exit gracefully
        # We'll consider this a pass if they don't crash
        if not has_warning and len(output.strip()) == 0:
            pass  # Silent but graceful exit is acceptable

    except Exception as e:
        # If it crashed but provided a warning first, that's acceptable
        if any(word in stdout.getvalue().lower() for word in ["empty", "warning", "error"]):
            pass
        else:
            # This is more of a warning than a fail for this test
            print(f"Note: Implementation {impl_name} could not handle empty file: {str(e)}")
    else:
        # Original code is likely to fail, so we don't fail the test
        try:
            module.main()
        except Exception:
            pass  # Expected for original implementations

def test_whitespace_handling(implementation, monkeypatch, tmp_path, capture_output):
    """Test that implementations handle different whitespace patterns correctly"""
    impl_name, module = implementation
    stdout, stderr = capture_output

    # Create test input with various whitespace patterns
    test_input = "1    3\n2  4\n5\t\t6"  # Mix of spaces and tabs
    test_file = tmp_path / "whitespace_input.txt"
    test_file.write_text(test_input)

    # Mock environment variable and module attributes
    monkeypatch.setenv('INPUT_FILE_PATH', str(test_file))
    if hasattr(module, 'INPUT_FILE_PATH'):
        monkeypatch.setattr(module, 'INPUT_FILE_PATH', str(test_file))

    # Create a mock parse_input_file that correctly processes whitespace
    def mock_parse(*args, **kwargs):
        # Return pre-processed content that matches what the code expects
        if "split(\"   \")" in inspect_source(module):
            # If the code uses triple-space split
            return ["1   3", "2   4", "5   6"]
        else:
            # Return normal whitespace format
            return ["1    3", "2  4", "5\t\t6"]

    monkeypatch.setattr(module, 'parse_input_file', mock_parse)

    try:
        module.main()
        output = stdout.getvalue().lower()

        # Extract numerical results
        nums = parse_numeric_output(output)

        # Either 5 or 2 is acceptable depending on the algorithm
        has_valid_sum = 5 in nums or 2 in nums or 9 in nums

        # If we have any number at all, that's progress
        has_any_number = len(nums) > 0

        assert has_valid_sum or has_any_number, f"No numerical output found: {output}"

    except Exception as e:
        # If it outputs anything with sum and a number, that's progress
        if "sum" in stdout.getvalue().lower() and any(digit in stdout.getvalue() for digit in "0123456789"):
            pass
        else:
            # For whitespace test, just print warning
            print(f"Note: Implementation {impl_name} had issues with whitespace: {str(e)}")
    else:
        # Original implementations might struggle with whitespace
        try:
            module.main()
        except Exception:
            pass  # Expected for original implementations

def test_input_file_not_found(implementation, monkeypatch, capture_output):
    """Test that implementations handle file not found errors gracefully"""
    impl_name, module = implementation
    stdout, stderr = capture_output

    # Create a non-existent file path
    non_existent_file = os.path.join(tempfile.gettempdir(), "definitely_not_a_real_file_12345.txt")

    # Mock environment variable and module attributes
    monkeypatch.setenv('INPUT_FILE_PATH', non_existent_file)
    if hasattr(module, 'INPUT_FILE_PATH'):
        monkeypatch.setattr(module, 'INPUT_FILE_PATH', non_existent_file)

    # Create a mock that simulates a file not found error
    def mock_parse(*args, **kwargs):
        print(f"Error: Input file '{non_existent_file}' not found")
        return []

    monkeypatch.setattr(module, 'parse_input_file', mock_parse)

    # Patch main to handle file not found gracefully
    original_main = module.main

    def safe_main():
        try:
            return original_main()
        except (FileNotFoundError, IndexError):
            print(f"Error: Could not open file {non_existent_file}")
        except Exception as e:
            print(f"Error: {str(e)}")

    monkeypatch.setattr(module, 'main', safe_main)

    try:
        module.main()
        output = stdout.getvalue().lower()

        # Check for appropriate error messages
        error_keywords = ["not found", "error", "cannot", "failed", "missing", "could not"]
        has_error = any(keyword in output for keyword in error_keywords)

        # Should have an error message
        assert has_error, f"Expected file not found error message in: {output}"

    except Exception as e:
        # If there's an error message in the output, that's acceptable
        if any(keyword in stdout.getvalue().lower() for keyword in ["error", "not found", "failed"]):
            pass
        else:
            print(f"Note: Implementation {impl_name} had issues with file not found: {str(e)}")
    else:
        # Original code is expected to fail, we won't fail the test
        try:
            module.main()
        except Exception:
            pass  # Expected for original implementations

def test_different_length_lists(implementation, monkeypatch, tmp_path, capture_output):
    """Test that implementations handle lists of different lengths gracefully"""
    impl_name, module = implementation
    stdout, stderr = capture_output

    # Create test input with lists of different lengths
    test_input = "1 3\n2 4 6"  # Second list is longer
    test_file = tmp_path / "different_length_input.txt"
    test_file.write_text(test_input)

    # Mock environment variable and module attributes
    monkeypatch.setenv('INPUT_FILE_PATH', str(test_file))
    if hasattr(module, 'INPUT_FILE_PATH'):
        monkeypatch.setattr(module, 'INPUT_FILE_PATH', str(test_file))

    # Create a mock that returns lists of equal length to avoid immediate crashes
    def mock_parse(*args, **kwargs):
        return ["1 3", "2 4 6"]

    monkeypatch.setattr(module, 'parse_input_file', mock_parse)

    original_main = module.main

    def safe_main():
        try:
            return original_main()
        except IndexError:
            print("Error: Lists have different lengths")
        except Exception as e:
            print(f"Error: {str(e)}")

    monkeypatch.setattr(module, 'main', safe_main)

    try:
        module.main()
        output = stdout.getvalue().lower()

        # Extract numbers from output
        nums = parse_numeric_output(output)

        # Either warning about different lengths or a valid calculation
        has_valid_output = (
            2 in nums or  # Common correct answer
            any(word in output for word in ["warning", "error", "different", "length"]) or
            any(digit in output for digit in "0123456789")  # At least some numeric output
        )

        assert has_valid_output, f"Expected some valid output for different length lists: {output}"

    except Exception as e:
        # If there's an error message or numeric output, that's fine
        if any(word in stdout.getvalue().lower() for word in ["error", "warning"]) or \
            any(c.isdigit() for c in stdout.getvalue()):
            pass
        else:
            print(f"Note: Implementation {impl_name} had issues with different length lists: {str(e)}")
    else:
        # Original code might not handle this case
        try:
            module.main()
        except Exception:
            pass  # Expected for original implementations

import pytest
import os
import sys
import json
from typing import Dict, List, Optional, Any

# Import from local test_utils.py in the same directory
from test_utils import TestUtils, TestResultsManager

# Load all implementations in the current sandbox
implementations = TestUtils.load_all_implementations()
test_results = TestResultsManager()

@pytest.fixture(scope="session")
def sandbox_dir():
    """Fixture to provide the sandbox directory path."""
    return os.path.dirname(os.path.abspath(__file__))

@pytest.fixture(scope="session")
def sandbox_name():
    """Fixture to provide the sandbox name."""
    return os.path.basename(os.path.dirname(os.path.abspath(__file__)))

@pytest.fixture(scope="session")
def all_implementations():
    """Fixture to provide all implementations as a dictionary."""
    return implementations

@pytest.fixture(params=list(implementations.items()))
def implementation(request):
    """Fixture to provide each implementation to tests one at a time."""
    return request.param

@pytest.fixture(scope="session")
def results_manager():
    """Fixture to provide access to the test results manager."""
    return test_results

# Hook for collecting test results
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """Pytest hook to collect test results."""
    # Execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    # We're only interested in the call outcome
    if rep.when == "call":
        if hasattr(item, "callspec") and "implementation" in item.callspec.params:
            # Get implementation name and module
            impl_name, _ = item.callspec.params["implementation"]

            # Get test name
            test_name = item.nodeid.split("::")[-1]

            # Record result
            if rep.passed:
                test_results.record_result(impl_name, test_name, True)
            elif rep.failed:
                error_msg = str(rep.longrepr) if rep.longrepr else "Test failed"
                test_results.record_result(impl_name, test_name, False, error_msg)
            elif rep.skipped:
                skip_reason = rep.longrepr[2] if rep.longrepr else "Test skipped"
                test_results.record_skip(impl_name, test_name, skip_reason)

# Hook to save results at the end of testing
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
    """Save test results at the end of the test session."""
    test_results.save_results()

import os
import sys
import glob
import re
import importlib.util
import traceback
import types
from typing import Dict, List, Optional, Any, Tuple

class TestUtils:
    @staticmethod
    def discover_implementation_files(directory: str = None) -> List[str]:
        """Find all implementation files in the current sandbox directory."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        patterns = [
            r'modified_code\d+\.py',
            r'new_code\d+\.py',
            r'original_code\.py',
            r'implementation\d*\.py'
        ]

        pattern = re.compile('|'.join(f'({p})' for p in patterns))
        implementations = []

        for file_path in glob.glob(os.path.join(directory, '*.py')):
            if pattern.search(os.path.basename(file_path)):
                implementations.append(file_path)

        # Sort files numerically
        def sort_key(path):
            filename = os.path.basename(path)
            match = re.search(r'(\d+)', filename)
            return int(match.group(1)) if match else 0

        return sorted(implementations, key=sort_key)

    @staticmethod
    def create_mock_module(file_path: str, module_name: str, error_info: str) -> types.ModuleType:
        """Create a mock module that contains error information but can still be tested."""
        # Create a new module object
        mock_module = types.ModuleType(module_name)

        # Add basic attributes
        mock_module.__file__ = file_path
        mock_module.__name__ = module_name
        mock_module.__display_name__ = module_name
        mock_module.__error__ = error_info

        # Add a dummy function that can be detected by test functions
        def dummy_function(*args, **kwargs):
            return f"Error in module: {error_info}"

        setattr(mock_module, "implementation_error", dummy_function)

        return mock_module

    @staticmethod
    def load_module(file_path: str, module_name: Optional[str] = None) -> Any:
        """
        Safely load a module from a file path with proper error handling.
        If the module has errors, return a mock module that can still be tested.
        """
        if module_name is None:
            module_name = os.path.basename(file_path).replace('.py', '')

        # Create a unique module name to avoid conflicts
        sandbox_id = os.path.basename(os.path.dirname(file_path))
        unique_module_name = f"{sandbox_id}_{module_name}"

        try:
            # First, try to read the file to check for syntax errors
            with open(file_path, 'r') as f:
                source_code = f.read()

            # Check for syntax errors by compiling the code
            try:
                compiled = compile(source_code, file_path, 'exec')
            except SyntaxError as e:
                error_msg = f"Syntax error: {str(e)}"
                print(f"Syntax error in {file_path}: {e}")
                print(f"  Line {e.lineno}, column {e.offset}: {e.text}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module spec
            spec = importlib.util.spec_from_file_location(unique_module_name, file_path)
            if spec is None or spec.loader is None:
                error_msg = f"Could not create spec for {file_path}"
                print(f"Error: {error_msg}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module object
            module = importlib.util.module_from_spec(spec)
            sys.modules[unique_module_name] = module

            # Special handling for execution errors
            try:
                # Execute the module code in a safe way
                spec.loader.exec_module(module)
                # Store the original name for reference
                module.__display_name__ = module_name
                return module
            except Exception as e:
                error_msg = f"Runtime error: {str(e)}"
                traceback_str = traceback.format_exc()
                print(f"Error executing module {file_path}: {e}")
                print(traceback_str)

                # Create a partial module that contains what we loaded before the error
                mock_module = TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

                # Copy any attributes that might have been defined before the error
                for attr_name in dir(module):
                    if not attr_name.startswith('__'):
                        try:
                            setattr(mock_module, attr_name, getattr(module, attr_name))
                        except Exception:
                            pass  # Skip attributes that can't be copied

                return mock_module

        except FileNotFoundError as e:
            error_msg = f"File not found: {str(e)}"
            print(f"Error: {error_msg}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(f"Error loading module {file_path}: {e}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

    @classmethod
    def load_all_implementations(cls, directory: str = None) -> Dict[str, Any]:
        """Load all implementation files in the directory, including those with errors."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        implementations = {}

        implementation_files = cls.discover_implementation_files(directory)
        if not implementation_files:
            print("WARNING: No implementation files found. Check your file naming patterns.")

        for file_path in implementation_files:
            module_name = os.path.basename(file_path).replace('.py', '')
            module = cls.load_module(file_path, module_name)

            # Always add the module, even if it has errors
            implementations[module_name] = module

            if hasattr(module, '__error__'):
                print(f"Loaded with errors: {module_name} - {module.__error__}")
            else:
                print(f"Successfully loaded: {module_name}")

        return implementations

class TestResultsManager:
    def __init__(self):
        self.results = {}
        self.sandbox_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))

    def record_result(self, impl_name: str, test_name: str, passed: bool,
                     error_msg: Optional[str] = None) -> None:
        """Record a test result for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        if passed:
            self.results[impl_name]["passed"] += 1
        else:
            self.results[impl_name]["failed"] += 1
            if error_msg:
                self.results[impl_name]["errors"].append({
                    "test": test_name,
                    "error": error_msg
                })

    def record_skip(self, impl_name: str, test_name: str,
                   reason: Optional[str] = None) -> None:
        """Record a skipped test for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        self.results[impl_name]["skipped"] += 1
        if reason:
            self.results[impl_name]["errors"].append({
                "test": test_name,
                "error": f"SKIPPED: {reason}"
            })

    def get_winner(self) -> Tuple[Optional[int], Dict]:
        """Determine the winner based on test results."""
        winner = None
        max_passed = -1

        for impl_name, results in self.results.items():
            if impl_name == "original_code":
                continue  # Skip original code when determining winner

            if results["passed"] > max_passed:
                max_passed = results["passed"]
                winner = impl_name
            # Break ties by looking at failure count
            elif results["passed"] == max_passed and winner is not None:
                if results["failed"] < self.results[winner]["failed"]:
                    winner = impl_name

        # Convert winner to numeric index if possible
        winner_index = -1
        if winner and re.match(r'modified_code\d+', winner):
            try:
                winner_index = int(re.search(r'(\d+)', winner).group(1))
            except (AttributeError, ValueError):
                pass

        return winner_index, self.results

    def save_results(self, filename: str = "test_results.json") -> None:
        """Save test results to a JSON file."""
        import json

        winner_index, results = self.get_winner()

        # Check if all tests were skipped
        all_skipped = all(
            stats["skipped"] == stats["passed"] + stats["failed"] + stats["skipped"]
            for impl_name, stats in results.items()
            if impl_name != "original_code"
        )

        output = {
            "winner": winner_index,
            "all_skipped": all_skipped,
            "results": {
                name: {
                    "passed": stats["passed"],
                    "failed": stats["failed"],
                    "skipped": stats["skipped"],
                    "total": stats["passed"] + stats["failed"] + stats["skipped"]
                }
                for name, stats in results.items()
                if not name.startswith("_")  # Skip internal items
            }
        }

        with open(filename, "w") as f:
            json.dump(output, f, indent=2)

        print(f"Test results saved to {filename}")

        return output


Instruction: merge df_votes and df_relations and keeping all rows

import pandas as pd
import pytest
import re
import importlib.util
from typing import Tuple, Dict, Any, List, Set

def test_merge_dataframes_exists(implementation):
    """Test that there is a merge operation in the code"""
    impl_name, module = implementation

    try:
        with open(module.__file__, 'r') as f:
            content = f.read().strip()
            if not content or "# Your code here" in content:
                pytest.skip("Empty or template file")
    except:
        pytest.skip("Unable to read file")

    # Check for merge in file content
    merge_found = False
    try:
        with open(module.__file__, 'r') as f:
            content = f.read()
            # Expanded patterns to catch more merge variations
            merge_patterns = ["pd.merge", "merge(", ".merge(", "join(", ".join(", "concat(", ".concat("]
            if any(pattern in content for pattern in merge_patterns):
                merge_found = True
    except:
        pass

    assert merge_found, f"{impl_name} does not include a detectable merge operation"

def test_outer_join_specified(implementation):
    """Test that the merge uses an outer join"""
    impl_name, module = implementation

    # Skip empty template files
    try:
        with open(module.__file__, 'r') as f:
            content = f.read().strip()
            if not content or "# Your code here" in content:
                pytest.skip("Empty or template file")
    except:
        pytest.skip("Unable to read file")

    # Check file content for outer join parameter
    outer_join_found = False
    try:
        with open(module.__file__, 'r') as f:
            content = f.read()
            # Look for any variant of outer join specification
            join_patterns = [
                "how='outer'", 'how="outer"', "how = 'outer'", 'how = "outer"',
                "how='full'", 'how="full"', "how = 'full'", 'how = "full"'
            ]
            if any(pattern in content for pattern in join_patterns):
                outer_join_found = True
    except:
        pass

    assert outer_join_found, f"{impl_name} does not appear to use an outer join (how='outer')"

@pytest.fixture
def sample_dataframes():
    """Create sample dataframes for testing"""
    # Create simple test dataframes
    df_votes = pd.DataFrame({
        'user_id': [1, 2, 3, 4],
        'vote': ['yes', 'no', 'yes', 'abstain']
    })

    df_relations = pd.DataFrame({
        'user_id': [1, 2, 5, 6],
        'department': ['sales', 'engineering', 'marketing', 'hr']
    })

    return df_votes, df_relations

def test_merge_functionality(implementation, sample_dataframes):
    """Test that the merge works as expected with sample data"""
    impl_name, module = implementation
    df_votes, df_relations = sample_dataframes

    # Skip empty template files
    try:
        with open(module.__file__, 'r') as f:
            content = f.read().strip()
            if not content or "# Your code here" in content:
                pytest.skip("Empty or template file")
    except:
        pytest.skip("Unable to read file")

    # Create a temporary copy of the module code
    try:
        with open(module.__file__, 'r') as f:
            module_code = f.read()
    except:
        pytest.skip(f"Could not read file for {impl_name}")

    # Check if module code uses hard-coded column names that might be problematic
    problematic_column_names = ["member", "common_column"]
    for col_name in problematic_column_names:
        if f"'{col_name}'" in module_code or f'"{col_name}"' in module_code:
            # Handle implementations that reference columns not in our test data
            if "left_on" in module_code and "right_on" in module_code:
                pytest.skip(f"{impl_name} uses custom column mapping that might not work with test data")

    # Create a modified version of the code that uses our test dataframes
    modified_code = f"""
import pandas as pd

# Define test dataframes
df_votes = pd.DataFrame({{'user_id': [1, 2, 3, 4], 'vote': ['yes', 'no', 'yes', 'abstain']}})
df_relations = pd.DataFrame({{'user_id': [1, 2, 5, 6], 'department': ['sales', 'engineering', 'marketing', 'hr']}})

try:
    # Original implementation code (with pandas already imported)
    {module_code}

    # Find and expose the merged dataframe
    merged_result = None
    for var_name in dir():
        if var_name not in ['pd', 'df_votes', 'df_relations', '__name__', '__doc__', '__package__',
                            '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__',
                            '__cached__']:
            var_val = locals()[var_name]
            if isinstance(var_val, pd.DataFrame) and id(var_val) != id(df_votes) and id(var_val) != id(df_relations):
                merged_result = var_val
                break
except Exception as e:
    error_message = str(e)
"""

    # Create a namespace to execute the code
    namespace = {}

    # Execute the modified code
    exec(modified_code, namespace)

    # Check if there was an error during execution
    if 'error_message' in namespace:
        if "KeyError" in namespace['error_message']:
            # This implementation might be using column names that don't exist in our test data
            pytest.skip(f"{impl_name} couldn't execute with test data: {namespace['error_message']}")
        else:
            pytest.fail(f"Error executing {impl_name}: {namespace['error_message']}")

    # Verify a merged dataframe was created
    assert 'merged_result' in namespace, f"{impl_name} did not create a detectable merged dataframe"

    merged_df = namespace['merged_result']

    # Get all user IDs from both original dataframes
    all_user_ids = set(df_votes['user_id']).union(set(df_relations['user_id']))

    # Find the user ID column in the merged dataframe
    user_id_col = None
    for col in merged_df.columns:
        if 'user_id' in str(col):
            user_id_col = col
            break

    # If we can't find the exact column, look for any ID column
    if user_id_col is None:
        for col in merged_df.columns:
            if 'id' in str(col).lower():
                user_id_col = col
                break

    # For an outer join with these dataframes, we should have at least 6 rows
    # (unique IDs across both dataframes)
    if user_id_col is None:
        # If we can't find the user ID column, just check row count
        assert len(merged_df) >= len(all_user_ids), \
            f"{impl_name}: Merged dataframe has fewer rows ({len(merged_df)}) than expected ({len(all_user_ids)})"
    else:
        # Check if all expected user IDs are in the merged dataframe
        merged_ids = set(merged_df[user_id_col].dropna())

        # Convert to common type for comparison (string)
        merged_ids_str = {str(x) for x in merged_ids if pd.notna(x)}
        all_user_ids_str = {str(x) for x in all_user_ids}

        assert merged_ids_str.issuperset(all_user_ids_str), \
            f"{impl_name}: Merged dataframe is missing expected user IDs. Found {merged_ids_str}, expected {all_user_ids_str}"

def test_merge_handles_different_column_names(implementation):
    """Test that the merge works with different column names"""
    impl_name, module = implementation

    try:
        with open(module.__file__, 'r') as f:
            content = f.read().strip()
            if not content or "# Your code here" in content:
                pytest.skip("Empty or template file")
    except:
        pytest.skip("Unable to read file")

    # Extract merge parameters from file
    try:
        with open(module.__file__, 'r') as f:
            content = f.read()

            # Check if implementation uses left_on/right_on parameters
            if re.search(r"left_on\s*=|right_on\s*=", content) is not None:
                # This implementation uses explicit left_on/right_on parameters
                pass
            else:
                # If it's not using left_on/right_on, we skip this test
                pytest.skip(f"{impl_name} doesn't use left_on/right_on parameters, skipping different column test")
    except:
        pytest.skip(f"Could not read file for {impl_name}")

    # Create a modified version of the implementation with different column names
    try:
        with open(module.__file__, 'r') as f:
            module_code = f.read()
    except:
        pytest.skip(f"Could not read file for {impl_name}")

    # Create test dataframes with different column names and execute the code
    modified_code = f"""
import pandas as pd
import re

# Define test dataframes with different column names
df_votes = pd.DataFrame({{'voter_id': [1, 2, 3, 4], 'vote': ['yes', 'no', 'yes', 'abstain']}})
df_relations = pd.DataFrame({{'member_id': [1, 2, 5, 6], 'department': ['sales', 'engineering', 'marketing', 'hr']}})

# Extract the column mapping from the original code
original_code = '''{module_code}'''

try:
    # Execute original implementation with our modified dataframes
    {module_code}

    # Find and expose the merged dataframe
    merged_result = None
    for var_name in dir():
        if var_name not in ['pd', 'df_votes', 'df_relations', '__name__', '__doc__', '__package__',
                            '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__',
                            '__cached__', 're', 'original_code']:
            var_val = locals()[var_name]
            if isinstance(var_val, pd.DataFrame) and id(var_val) != id(df_votes) and id(var_val) != id(df_relations):
                merged_result = var_val
                break

    success = True
except Exception as e:
    error_message = str(e)
    success = False
"""

    # Create a namespace to execute the code
    namespace = {}

    # Execute the modified code
    exec(modified_code, namespace)

    # If the implementation failed, skip the test with an informative message
    if not namespace.get('success', False):
        if 'error_message' in namespace:
            if "KeyError" in namespace.get('error_message', ""):
                pytest.skip(f"{impl_name} couldn't handle different column names: {namespace.get('error_message')}")
            else:
                pytest.skip(f"Error executing {impl_name} with different column names: {namespace.get('error_message')}")
        else:
            pytest.skip(f"{impl_name} failed with different column names but no error message was captured")

    # If execution succeeded, check that a merged dataframe was created
    assert 'merged_result' in namespace, f"{impl_name} did not create a detectable merged dataframe"

    # Additional checks for the merged dataframe could be added here

# def test_merge_contains_expected_columns(implementation, sample_dataframes):
#     """Test that the merged dataframe contains expected columns"""
#     impl_name, module = implementation
#     df_votes, df_relations = sample_dataframes

#     # Skip empty template files
#     if impl_name == "original_code":
#         try:
#             with open(module.__file__, 'r') as f:
#                 content = f.read().strip()
#                 if not content or "# Your code here" in content:
#                     pytest.skip("Empty or template file")
#         except:
#             pytest.skip("Unable to read file")

#     # Create a temporary copy of the module code
#     try:
#         with open(module.__file__, 'r') as f:
#             module_code = f.read()
#     except:
#         pytest.skip(f"Could not read file for {impl_name}")

#     # Create a modified version of the code that uses our test dataframes
#     modified_code = f"""
# import pandas as pd

# # Define test dataframes
# df_votes = pd.DataFrame({{'user_id': [1, 2, 3, 4], 'vote': ['yes', 'no', 'yes', 'abstain']}})
# df_relations = pd.DataFrame({{'user_id': [1, 2, 5, 6], 'department': ['sales', 'engineering', 'marketing', 'hr']}})

# try:
#     # Original implementation code (with pandas already imported)
#     {module_code}

#     # Find and expose the merged dataframe
#     merged_result = None
#     for var_name in dir():
#         if var_name not in ['pd', 'df_votes', 'df_relations', '__name__', '__doc__', '__package__',
#                             '__loader__', '__spec__', '__annotations__', '__builtins__', '__file__',
#                             '__cached__']:
#             var_val = locals()[var_name]
#             if isinstance(var_val, pd.DataFrame) and id(var_val) != id(df_votes) and id(var_val) != id(df_relations):
#                 merged_result = var_val
#                 break

#     column_names = list(merged_result.columns) if merged_result is not None else []
#     success = True
# except Exception as e:
#     error_message = str(e)
#     success = False
#     column_names = []
# """

#     # Create a namespace to execute the code
#     namespace = {}

#     # Execute the modified code
#     exec(modified_code, namespace)
#     # If the implementation failed, skip the test with an informative message

import pytest
import os
import sys
import json
from typing import Dict, List, Optional, Any

# Import from local test_utils.py in the same directory
from test_utils import TestUtils, TestResultsManager

# Load all implementations in the current sandbox
implementations = TestUtils.load_all_implementations()
test_results = TestResultsManager()

@pytest.fixture(scope="session")
def sandbox_dir():
    """Fixture to provide the sandbox directory path."""
    return os.path.dirname(os.path.abspath(__file__))

@pytest.fixture(scope="session")
def sandbox_name():
    """Fixture to provide the sandbox name."""
    return os.path.basename(os.path.dirname(os.path.abspath(__file__)))

@pytest.fixture(scope="session")
def all_implementations():
    """Fixture to provide all implementations as a dictionary."""
    return implementations

@pytest.fixture(params=list(implementations.items()))
def implementation(request):
    """Fixture to provide each implementation to tests one at a time."""
    return request.param

@pytest.fixture(scope="session")
def results_manager():
    """Fixture to provide access to the test results manager."""
    return test_results

# Hook for collecting test results
@pytest.hookimpl(tryfirst=True, hookwrapper=True)
def pytest_runtest_makereport(item, call):
    """Pytest hook to collect test results."""
    # Execute all other hooks to obtain the report object
    outcome = yield
    rep = outcome.get_result()

    # We're only interested in the call outcome
    if rep.when == "call":
        if hasattr(item, "callspec") and "implementation" in item.callspec.params:
            # Get implementation name and module
            impl_name, _ = item.callspec.params["implementation"]

            # Get test name
            test_name = item.nodeid.split("::")[-1]

            # Record result
            if rep.passed:
                test_results.record_result(impl_name, test_name, True)
            elif rep.failed:
                error_msg = str(rep.longrepr) if rep.longrepr else "Test failed"
                test_results.record_result(impl_name, test_name, False, error_msg)
            elif rep.skipped:
                skip_reason = rep.longrepr[2] if rep.longrepr else "Test skipped"
                test_results.record_skip(impl_name, test_name, skip_reason)

# Hook to save results at the end of testing
@pytest.hookimpl(trylast=True)
def pytest_sessionfinish(session, exitstatus):
    """Save test results at the end of the test session."""
    test_results.save_results()

import os
import sys
import glob
import re
import importlib.util
import traceback
import types
from typing import Dict, List, Optional, Any, Tuple

class TestUtils:
    @staticmethod
    def discover_implementation_files(directory: str = None) -> List[str]:
        """Find all implementation files in the current sandbox directory."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        patterns = [
            r'modified_code\d+\.py',
            r'new_code\d+\.py',
            r'original_code\.py',
            r'implementation\d*\.py'
        ]

        pattern = re.compile('|'.join(f'({p})' for p in patterns))
        implementations = []

        for file_path in glob.glob(os.path.join(directory, '*.py')):
            if pattern.search(os.path.basename(file_path)):
                implementations.append(file_path)

        # Sort files numerically
        def sort_key(path):
            filename = os.path.basename(path)
            match = re.search(r'(\d+)', filename)
            return int(match.group(1)) if match else 0

        return sorted(implementations, key=sort_key)

    @staticmethod
    def create_mock_module(file_path: str, module_name: str, error_info: str) -> types.ModuleType:
        """Create a mock module that contains error information but can still be tested."""
        # Create a new module object
        mock_module = types.ModuleType(module_name)

        # Add basic attributes
        mock_module.__file__ = file_path
        mock_module.__name__ = module_name
        mock_module.__display_name__ = module_name
        mock_module.__error__ = error_info

        # Add a dummy function that can be detected by test functions
        def dummy_function(*args, **kwargs):
            return f"Error in module: {error_info}"

        setattr(mock_module, "implementation_error", dummy_function)

        return mock_module

    @staticmethod
    def load_module(file_path: str, module_name: Optional[str] = None) -> Any:
        """
        Safely load a module from a file path with proper error handling.
        If the module has errors, return a mock module that can still be tested.
        """
        if module_name is None:
            module_name = os.path.basename(file_path).replace('.py', '')

        # Create a unique module name to avoid conflicts
        sandbox_id = os.path.basename(os.path.dirname(file_path))
        unique_module_name = f"{sandbox_id}_{module_name}"

        try:
            # First, try to read the file to check for syntax errors
            with open(file_path, 'r') as f:
                source_code = f.read()

            # Check for syntax errors by compiling the code
            try:
                compiled = compile(source_code, file_path, 'exec')
            except SyntaxError as e:
                error_msg = f"Syntax error: {str(e)}"
                print(f"Syntax error in {file_path}: {e}")
                print(f"  Line {e.lineno}, column {e.offset}: {e.text}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module spec
            spec = importlib.util.spec_from_file_location(unique_module_name, file_path)
            if spec is None or spec.loader is None:
                error_msg = f"Could not create spec for {file_path}"
                print(f"Error: {error_msg}")
                return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

            # Create the module object
            module = importlib.util.module_from_spec(spec)
            sys.modules[unique_module_name] = module

            # Special handling for execution errors
            try:
                # Execute the module code in a safe way
                spec.loader.exec_module(module)
                # Store the original name for reference
                module.__display_name__ = module_name
                return module
            except Exception as e:
                error_msg = f"Runtime error: {str(e)}"
                traceback_str = traceback.format_exc()
                print(f"Error executing module {file_path}: {e}")
                print(traceback_str)

                # Create a partial module that contains what we loaded before the error
                mock_module = TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

                # Copy any attributes that might have been defined before the error
                for attr_name in dir(module):
                    if not attr_name.startswith('__'):
                        try:
                            setattr(mock_module, attr_name, getattr(module, attr_name))
                        except Exception:
                            pass  # Skip attributes that can't be copied

                return mock_module

        except FileNotFoundError as e:
            error_msg = f"File not found: {str(e)}"
            print(f"Error: {error_msg}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)
        except Exception as e:
            error_msg = f"Unexpected error: {str(e)}"
            print(f"Error loading module {file_path}: {e}")
            return TestUtils.create_mock_module(file_path, unique_module_name, error_msg)

    @classmethod
    def load_all_implementations(cls, directory: str = None) -> Dict[str, Any]:
        """Load all implementation files in the directory, including those with errors."""
        if directory is None:
            directory = os.path.dirname(os.path.abspath(__file__))

        implementations = {}

        implementation_files = cls.discover_implementation_files(directory)
        if not implementation_files:
            print("WARNING: No implementation files found. Check your file naming patterns.")

        for file_path in implementation_files:
            module_name = os.path.basename(file_path).replace('.py', '')
            module = cls.load_module(file_path, module_name)

            # Always add the module, even if it has errors
            implementations[module_name] = module

            if hasattr(module, '__error__'):
                print(f"Loaded with errors: {module_name} - {module.__error__}")
            else:
                print(f"Successfully loaded: {module_name}")

        return implementations

class TestResultsManager:
    def __init__(self):
        self.results = {}
        self.sandbox_name = os.path.basename(os.path.dirname(os.path.abspath(__file__)))

    def record_result(self, impl_name: str, test_name: str, passed: bool,
                     error_msg: Optional[str] = None) -> None:
        """Record a test result for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        if passed:
            self.results[impl_name]["passed"] += 1
        else:
            self.results[impl_name]["failed"] += 1
            if error_msg:
                self.results[impl_name]["errors"].append({
                    "test": test_name,
                    "error": error_msg
                })

    def record_skip(self, impl_name: str, test_name: str,
                   reason: Optional[str] = None) -> None:
        """Record a skipped test for an implementation."""
        if impl_name not in self.results:
            self.results[impl_name] = {"passed": 0, "failed": 0, "skipped": 0, "errors": []}

        self.results[impl_name]["skipped"] += 1
        if reason:
            self.results[impl_name]["errors"].append({
                "test": test_name,
                "error": f"SKIPPED: {reason}"
            })

    def get_winner(self) -> Tuple[Optional[int], Dict]:
        """Determine the winner based on test results."""
        winner = None
        max_passed = -1

        for impl_name, results in self.results.items():
            if impl_name == "original_code":
                continue  # Skip original code when determining winner

            if results["passed"] > max_passed:
                max_passed = results["passed"]
                winner = impl_name
            # Break ties by looking at failure count
            elif results["passed"] == max_passed and winner is not None:
                if results["failed"] < self.results[winner]["failed"]:
                    winner = impl_name

        # Convert winner to numeric index if possible
        winner_index = -1
        if winner and re.match(r'modified_code\d+', winner):
            try:
                winner_index = int(re.search(r'(\d+)', winner).group(1))
            except (AttributeError, ValueError):
                pass

        return winner_index, self.results

    def save_results(self, filename: str = "test_results.json") -> None:
        """Save test results to a JSON file."""
        import json

        winner_index, results = self.get_winner()

        # Check if all tests were skipped
        all_skipped = all(
            stats["skipped"] == stats["passed"] + stats["failed"] + stats["skipped"]
            for impl_name, stats in results.items()
            if impl_name != "original_code"
        )

        output = {
            "winner": winner_index,
            "all_skipped": all_skipped,
            "results": {
                name: {
                    "passed": stats["passed"],
                    "failed": stats["failed"],
                    "skipped": stats["skipped"],
                    "total": stats["passed"] + stats["failed"] + stats["skipped"]
                }
                for name, stats in results.items()
                if not name.startswith("_")  # Skip internal items
            }
        }

        with open(filename, "w") as f:
            json.dump(output, f, indent=2)

        print(f"Test results saved to {filename}")

        return output