import torch.nn as nn
import torch.nn.functional as F
class SimpleConvNet3(nn.Module):
    def __init__(self):
        super(SimpleConvNet3, self).__init__()
        self.conv1 = nn.Conv2d(in_channels=3, out_channels=32, kernel_size=3, stride=1, padding=1)
        self.conv2 = nn.Conv2d(in_channels=32, out_channels=64, kernel_size=3, stride=1, padding=1)
        self.conv3 = nn.Conv2d(in_channels=64, out_channels=128, kernel_size=3, stride=1, padding=1)
        self.conv4 = nn.Conv2d(in_channels=128, out_channels=256, kernel_size=3, stride=1, padding=1)
        self.fc1 = nn.Linear(256 * 16 * 16, 512)
        self.fc2 = nn.Linear(512, 3)  # 3 output classes
    def forward(self, x):
        x = F.relu(self.conv1(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = F.relu(self.conv2(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = F.relu(self.conv3(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = F.relu(self.conv4(x))
        x = F.max_pool2d(x, kernel_size=2, stride=2)
        x = x.view(x.size(0), -1)  # Flatten the tensor
        x = F.relu(self.fc1(x))
        x = self.fc2(x)
        return x

Instruction: 3. Попробуйте добавить Dropout на слои своей сверточной сети, не используя BatchNorm.

# test_dropout_no_batchnorm.py

import pytest
import inspect
import torch.nn as nn

def find_model_class(module):
    """Locate the first nn.Module subclass in the implementation module."""
    for _, obj in inspect.getmembers(module, inspect.isclass):
        if issubclass(obj, nn.Module) and obj is not nn.Module:
            return obj
    pytest.skip(f"{module.__name__}: no nn.Module subclass found")

def get_model_instance(module):
    """Instantiate the model class, or skip if it fails."""
    ModelCls = find_model_class(module)
    try:
        return ModelCls()
    except Exception as e:
        pytest.skip(f"{module.__name__}: cannot instantiate model: {e}")

def count_dropout_and_batchnorm(model):
    """
    Walk the model graph and count how many Dropout* and BatchNorm* layers it has.
    Returns (dropout_count, batchnorm_count).
    """
    dropouts = 0
    batchnorms = 0
    for layer in model.modules():
        if isinstance(layer, (nn.Dropout, nn.Dropout1d, nn.Dropout2d, nn.Dropout3d)):
            dropouts += 1
        if isinstance(layer, (nn.BatchNorm1d, nn.BatchNorm2d, nn.BatchNorm3d)):
            batchnorms += 1
    return dropouts, batchnorms

def test_dropout_layers_present(implementation):
    """
    Model must include at least one Dropout layer.
    """
    impl_name, module = implementation
    model = get_model_instance(module)
    dropouts, _ = count_dropout_and_batchnorm(model)
    assert dropouts > 0, (
        f"{impl_name}: found {dropouts} Dropout layers; expected at least one."
    )

def test_no_batchnorm_layers(implementation):
    """
    Model must NOT include any BatchNorm layers.
    """
    impl_name, module = implementation
    model = get_model_instance(module)
    _, batchnorms = count_dropout_and_batchnorm(model)
    assert batchnorms == 0, (
        f"{impl_name}: found {batchnorms} BatchNorm layers; remove all BatchNorm uses."
    )

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 streamlit as st

# Создаем две формы для ввода данных
# В первой форме значения сохраняются в словарь form1_dict напрямую
# Во второй форме значения сохраняются в session_state и затем копируются в form2_dict

form1_dict = {}
with st.form('form1'):
    form1_dict['a'] = st.text_input('a')
    form1_dict['b'] = st.text_input('b')
    st.form_submit_button('Submit Form 1')
st.write(form1_dict)

with st.form('form2'):
    st.text_input('a', key='form2_a')
    st.text_input('b', key='form2_b')
    st.form_submit_button('Submit Form 2')

# Создаем словарь form2_dict и копируем в него значения из session_state,
# убирая префикс 'form2_' из ключей
form2_dict = {}
for key in st.session_state:
    if key.startswith('form2_'):
        form2_dict[key.removeprefix('form2_')] = st.session_state[key]

st.write(form2_dict)

Instruction: добавить print в конце, чтобы в консоли тоже выводился результат сабмита формы

import inspect
import re
from unittest.mock import patch, MagicMock
import sys
from io import StringIO
import pytest

def test_print_statements_existence(implementation):
    """Test if print statements have been added to the code."""
    impl_name, module = implementation

    # Get the source code of the module
    source_code = inspect.getsource(module)

    # Check if the code contains print statements related to form submissions
    assert 'print(' in source_code, f"{impl_name}: No print statements found in the implementation"

    # Check for form1_dict in print statements
    assert re.search(r'print\(.*form1_dict.*\)', source_code), f"{impl_name}: No print statement for form1_dict found"

    # Check for form2_dict in print statements
    assert re.search(r'print\(.*form2_dict.*\)', source_code), f"{impl_name}: No print statement for form2_dict found"

def test_print_statements_content(implementation):
    """Test if the print statements have appropriate descriptive content."""
    impl_name, module = implementation

    # Get the source code of the module
    source_code = inspect.getsource(module)

    # Look for descriptive print statements rather than just printing the dictionaries
    form1_pattern = r'print\(\s*[\"\'].*[\"\'],\s*form1_dict\s*\)'
    form1_f_pattern = r'print\(\s*f[\"\'].*{form1_dict}.*[\"\']\s*\)'

    has_descriptive_form1 = re.search(form1_pattern, source_code) or re.search(form1_f_pattern, source_code)
    assert has_descriptive_form1, f"{impl_name}: Print statement for form1_dict should include descriptive text"

    form2_pattern = r'print\(\s*[\"\'].*[\"\'],\s*form2_dict\s*\)'
    form2_f_pattern = r'print\(\s*f[\"\'].*{form2_dict}.*[\"\']\s*\)'

    has_descriptive_form2 = re.search(form2_pattern, source_code) or re.search(form2_f_pattern, source_code)
    assert has_descriptive_form2, f"{impl_name}: Print statement for form2_dict should include descriptive text"

def test_print_placement(implementation):
    """Test if print statements are placed in appropriate locations."""
    impl_name, module = implementation

    # Get the source code of the module
    source_code = inspect.getsource(module)

    # Get line numbers of key elements
    form1_dict_print_line = -1
    form2_dict_print_line = -1
    form1_dict_assignment_line = -1
    form2_dict_creation_line = -1

    lines = source_code.split('\n')
    for i, line in enumerate(lines):
        if 'form1_dict = {}' in line:
            form1_dict_assignment_line = i
        elif 'form2_dict = {}' in line:
            form2_dict_creation_line = i
        elif 'print(' in line and 'form1_dict' in line:
            form1_dict_print_line = i
        elif 'print(' in line and 'form2_dict' in line:
            form2_dict_print_line = i

    # Check that print statements are after their respective dictionary operations
    assert form1_dict_print_line > form2_dict_creation_line, \
        f"{impl_name}: form1_dict print statement should be after dictionary initialization"
    assert form2_dict_print_line > form2_dict_creation_line, \
        f"{impl_name}: form2_dict print statement should be after dictionary population"

def test_form_input_with_mocks(implementation):
    """Test the form input functionality using mocks."""
    impl_name, module = implementation

    # Create a controlled test environment with mocks
    with patch.object(module.st, 'form') as mock_form, \
         patch.object(module.st, 'text_input') as mock_text_input, \
         patch.object(module.st, 'form_submit_button') as mock_submit, \
         patch.object(module.st, 'write') as mock_write, \
         patch.object(module.st, 'session_state', {'form2_a': 'test_value_a', 'form2_b': 'test_value_b'}):

        # Set return values for mocks
        mock_form.return_value.__enter__.return_value = MagicMock()
        mock_form.return_value.__exit__.return_value = None
        mock_text_input.return_value = 'test_input'
        mock_submit.return_value = True

        # Capture printed output
        old_stdout = sys.stdout
        captured_output = StringIO()
        sys.stdout = captured_output

        # Execute the main code logic directly
        # We need to manually call the key parts of the module instead of reloading
        # Form 1 handling (extracting this logic from the module)
        form1_dict = {}
        form1_dict['a'] = 'test_input'  # Simulating what the module does with mock returns
        form1_dict['b'] = 'test_input'

        # Form 2 handling (extracting this logic from the module)
        form2_dict = {}
        for key in module.st.session_state:
            if key.startswith('form2_'):
                form2_dict[key.removeprefix('form2_')] = module.st.session_state[key]

        # Restore stdout
        sys.stdout = old_stdout

        # Check only modified versions have print output
        output = captured_output.getvalue()
        assert 'form1_dict' in output.lower() or 'form 1' in output.lower(), \
            f"{impl_name}: form1_dict not in print output"
        assert 'form2_dict' in output.lower() or 'form 2' in output.lower(), \
            f"{impl_name}: form2_dict not in print output"

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: crate sume finction from A to B

import pytest
import inspect
import types
import sys
import os
import importlib.util
from typing import Any, Callable, List, Tuple, Dict, Union

def test_implementation_exists(implementation):
    """Test that the sum_from_a_to_b function exists in the implementation."""
    impl_name, module = implementation
    # Check for function existence, but don't fail the test if it doesn't exist
    # This allows other tests to be skipped properly
    has_function = hasattr(module, "sum_from_a_to_b")
    if has_function:
        assert callable(module.sum_from_a_to_b), f"{impl_name}'s sum_from_a_to_b is not a function"
    else:
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

def test_function_signature(implementation):
    """Test that the sum_from_a_to_b function has the correct signature."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    signature = inspect.signature(module.sum_from_a_to_b)
    assert len(signature.parameters) == 2, f"{impl_name}'s sum_from_a_to_b function should accept 2 parameters"

    # Check parameter names - common conventions would be a/b or start/end
    param_names = list(signature.parameters.keys())
    assert len(param_names) == 2, f"{impl_name}'s sum_from_a_to_b should have exactly 2 parameters"

@pytest.mark.parametrize("a, b, expected", [
    (1, 5, 15),             # Simple positive range
    (5, 10, 45),            # Another positive range
    (0, 0, 0),              # Same number
    (0, 5, 15),             # Start from zero
    (-5, -1, -15),          # Negative range
    (-3, 3, 0),             # Range crossing zero
    (100, 105, 615),        # Larger numbers
])
def test_sum_calculation_basic(implementation, a, b, expected):
    """Test basic calculation of sums from a to b."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    result = module.sum_from_a_to_b(a, b)
    assert result == expected, f"{impl_name}'s sum_from_a_to_b({a}, {b}) should return {expected}, got {result}"

def test_large_range(implementation):
    """Test with a large range to check efficiency."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    a, b = 1, 1000
    # Expected sum is n(n+1)/2 where n is the count of numbers
    expected = (b * (b + 1)) // 2 - ((a - 1) * a // 2)
    result = module.sum_from_a_to_b(a, b)
    assert result == expected, f"{impl_name}'s sum_from_a_to_b({a}, {b}) should return {expected}, got {result}"

def test_reversed_parameters(implementation):
    """Test if the function handles cases where a > b."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    a, b = 10, 5
    expected_if_swapped = sum(range(b, a + 1))

    # Try calling with reversed parameters
    try:
        result = module.sum_from_a_to_b(a, b)

        # Some implementations might return 0 or another value for reversed ranges
        # Try to determine if the implementation swaps parameters or has another strategy
        if result == expected_if_swapped:
            assert True, "Implementation handles reversed parameters by swapping"
        elif result == 0:
            assert True, "Implementation returns 0 for reversed parameters"
        else:
            # If it returns something else, check if it's consistent
            # This could be returning a negative value or some other special handling
            assert result == module.sum_from_a_to_b(a, b), "Implementation is consistent for reversed parameters"
    except Exception as e:
        # If the implementation raises an error, mark the test as skipped
        pytest.skip(f"{impl_name} doesn't handle reversed parameters: {str(e)}")

def test_non_integer_input(implementation):
    """Test if the function properly handles or rejects non-integer inputs."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    # Test with float inputs that are whole numbers
    try:
        result = module.sum_from_a_to_b(1.0, 5.0)
        # If the function accepts floats, verify the result
        assert result == 15, f"{impl_name}'s sum_from_a_to_b(1.0, 5.0) should return 15, got {result}"
    except (TypeError, ValueError, AssertionError) as e:
        # If the implementation rejects float inputs, that's also valid
        pass

    # Test with string inputs that can be converted to integers
    try:
        result = module.sum_from_a_to_b("1", "5")
        # If it accepts strings, verify the result
        assert result == 15, f"{impl_name}'s sum_from_a_to_b('1', '5') should return 15, got {result}"
    except (TypeError, ValueError, AssertionError) as e:
        # If the implementation rejects string inputs, that's valid
        pass

def test_docstring_presence(implementation):
    """Test that the function has a docstring explaining what it does."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    # The docstring might be None if not present
    docstring = module.sum_from_a_to_b.__doc__

    # We won't fail the test if docstring is missing, but we'll note it
    if not docstring:
        print(f"Note: {impl_name}'s sum_from_a_to_b function is missing a docstring")
    else:
        assert len(docstring.strip()) > 0, f"{impl_name}'s docstring is empty"

def test_edge_cases(implementation):
    """Test edge cases like very large numbers."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    # Test with the max argument where sum can still be calculated precisely
    # Using smaller range to avoid very long calculations
    large_a, large_b = 9998, 10000
    expected = sum(range(large_a, large_b + 1))
    result = module.sum_from_a_to_b(large_a, large_b)
    assert result == expected, f"{impl_name}'s sum_from_a_to_b({large_a}, {large_b}) should return {expected}, got {result}"

def test_formula_vs_iteration(implementation):
    """
    Test if the implementation uses the mathematical formula rather than iteration.
    This is a bonus test to check for optimization.
    """
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    # For larger ranges, the sum formula n(n+1)/2 is much faster
    a, b = 1, 10000

    # Calculate expected result using the formula
    n = b - a + 1
    expected = (n * (a + b)) // 2

    # Time the function call
    import time
    start_time = time.time()
    result = module.sum_from_a_to_b(a, b)
    execution_time = time.time() - start_time

    assert result == expected, f"{impl_name}'s sum_from_a_to_b({a}, {b}) should return {expected}, got {result}"

    # We won't fail the test based on performance, just report it
    print(f"{impl_name}'s sum_from_a_to_b execution time for range {a} to {b}: {execution_time:.6f} seconds")

def test_performance_threshold(implementation):
    """Test if the implementation is efficient for large inputs."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    import time

    # Use a moderate-sized range to test performance
    a, b = 1, 100000

    # Calculate expected result using the formula
    n = b - a + 1
    expected = (n * (a + b)) // 2

    # Set a reasonable threshold time (in seconds)
    # Formula-based implementations should be very fast
    THRESHOLD_TIME = 0.1

    start_time = time.time()
    result = module.sum_from_a_to_b(a, b)
    execution_time = time.time() - start_time

    assert result == expected, f"{impl_name}'s sum_from_a_to_b({a}, {b}) returned incorrect result"

    # Note: We're not failing on performance, just reporting
    if execution_time > THRESHOLD_TIME:
        print(f"Note: {impl_name} implementation took {execution_time:.6f}s, which is above the ideal threshold of {THRESHOLD_TIME}s")
    else:
        print(f"{impl_name} implementation is efficient: {execution_time:.6f}s")

def test_type_hints(implementation):
    """Test if the function has proper type hints (Python 3.5+)."""
    impl_name, module = implementation
    if not hasattr(module, "sum_from_a_to_b"):
        pytest.skip(f"{impl_name} is missing the sum_from_a_to_b function")

    # This is a bonus test and won't fail if type hints aren't present
    try:
        annotations = module.sum_from_a_to_b.__annotations__
        if annotations:
            # Check if type hints are present for parameters and return value
            param_names = list(inspect.signature(module.sum_from_a_to_b).parameters.keys())
            for param in param_names:
                if param in annotations:
                    print(f"{impl_name} has type hint for parameter {param}: {annotations[param]}")

            if 'return' in annotations:
                print(f"{impl_name} has return type hint: {annotations['return']}")
    except (AttributeError, TypeError):
        # Older Python versions or implementations without type hints
        pass

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


# generate a half adder module of verilog by python

# verilog code
verilog_code = """
module half_adder(a, b, c, sum, carry);
    input a, b;
    output c, sum, carry;
    assign c = a ^ b;
    assign sum = a & b;
    assign carry = a & b;
endmodule
"""

# verilog module name
module_name = "half_adder"

# verilog module input and output ports
input_ports = ["a", "b"]
output_ports = ["c", "sum", "carry"]

# verilog module parameters
parameters = ["WIDTH"]

# verilog module parameters value
parameter_values = ["8"]

# verilog module body
module_body = """
    input a, b;
    output c, sum, carry;
    assign c = a ^ b;
    assign sum = a & b;
    assign carry = a & b;
endmodule
"""

# verilog module instantiation
module_instantiation = """
half_adder half_adder_inst(
    .a(a),
    .b(b),
    .c(c),
    .sum(sum),
    .carry(carry)
);
"""

Instruction: add more input signals

import re
import pytest

def test_input_ports_added(implementation):
    """Test that additional input ports have been added to the module_body."""
    impl_name, module = implementation

    # Skip test for implementations without module_body attribute
    if not hasattr(module, 'module_body'):
        pytest.skip(f"{impl_name}: No module_body attribute found")

    # Check if the module_body contains more than just a and b as inputs
    input_pattern = r"input\s+([^;]+);"
    input_matches = re.search(input_pattern, module.module_body)

    if not input_matches:
        pytest.fail(f"{impl_name}: Failed to find input declaration in module_body")

    input_declaration = input_matches.group(1)
    input_signals = [s.strip() for s in input_declaration.split(",")]

    # The original has only "a, b" as inputs
    assert len(input_signals) > 2, f"{impl_name}: Should have more than 2 input signals, but found {len(input_signals)}"

    # Verify the original inputs are still there
    assert "a" in input_signals, f"{impl_name}: Original input 'a' should be preserved"
    assert "b" in input_signals, f"{impl_name}: Original input 'b' should be preserved"

    # Verify new inputs have been added
    new_inputs = [signal for signal in input_signals if signal not in ["a", "b"]]
    assert len(new_inputs) > 0, f"{impl_name}: No new input signals were added"

def test_input_ports_list_updated(implementation):
    """Test that input_ports list has been updated to reflect new inputs."""
    impl_name, module = implementation

    # Skip test for implementations without required attributes
    if not hasattr(module, 'module_body') or not hasattr(module, 'input_ports'):
        pytest.skip(f"{impl_name}: Missing required attributes")

    # Extract input signals from module_body
    input_pattern = r"input\s+([^;]+);"
    input_matches = re.search(input_pattern, module.module_body)

    if not input_matches:
        pytest.fail(f"{impl_name}: Failed to find input declaration in module_body")

    input_declaration = input_matches.group(1)
    module_body_inputs = set(s.strip() for s in input_declaration.split(","))

    # Handle the case where 'c' appears both as input and output
    # The analysis shows implementations may have 'c' as both input and output
    duplicated_ports = set()
    if hasattr(module, 'output_ports'):
        duplicated_ports = module_body_inputs.intersection(set(module.output_ports))

    # Check if module.input_ports list is updated to include all new inputs from module_body
    # (excluding duplicates that are also outputs)
    module_input_ports_set = set(module.input_ports)

    # Get the inputs that are in module_body but not in input_ports list
    missing_inputs = module_body_inputs - module_input_ports_set - duplicated_ports

    # If missing inputs are found, suggest what to add
    if missing_inputs:
        # Expected updated input_ports list
        expected_inputs = sorted(list(module_input_ports_set.union(missing_inputs)))
        message = (f"{impl_name}: input_ports list missing inputs from module_body: {missing_inputs}. "
                  f"Update input_ports to include: {expected_inputs}")
        assert not missing_inputs, message

def test_verilog_code_consistency(implementation):
    """Test that the verilog_code is consistent with module_body for inputs."""
    impl_name, module = implementation

    # Skip test for implementations without required attributes
    if not hasattr(module, 'module_body') or not hasattr(module, 'verilog_code'):
        pytest.skip(f"{impl_name}: Missing required attributes")

    # Check that the original inputs are in verilog_code
    original_inputs = ["a", "b"]
    for input_name in original_inputs:
        # Look for the input name as a word boundary in verilog_code
        pattern = rf"\b{re.escape(input_name)}\b"
        assert re.search(pattern, module.verilog_code), f"{impl_name}: Original input '{input_name}' not found in verilog_code"

def test_module_instantiation_updated(implementation):
    """Test that module_instantiation has been updated to include new inputs."""
    impl_name, module = implementation

    # Skip test for implementations without required attributes
    if not hasattr(module, 'module_body') or not hasattr(module, 'module_instantiation'):
        pytest.skip(f"{impl_name}: Missing required attributes")

    # Extract input signals from module_body
    input_pattern = r"input\s+([^;]+);"
    input_matches = re.search(input_pattern, module.module_body)

    if not input_matches:
        pytest.fail(f"{impl_name}: Failed to find input declaration in module_body")

    # Just check that the original inputs are in the module_instantiation
    original_inputs = ["a", "b"]
    for input_name in original_inputs:
        # Check if the input is connected in the instantiation
        pattern = rf"\.{re.escape(input_name)}\s*\("
        assert re.search(pattern, module.module_instantiation), f"{impl_name}: Original input '{input_name}' not found in module_instantiation"

def test_logic_updated_for_new_inputs(implementation):
    """Test that the logic in the module has been updated to use the new inputs."""
    impl_name, module = implementation

    # Skip test for implementations without module_body attribute
    if not hasattr(module, 'module_body'):
        pytest.skip(f"{impl_name}: No module_body attribute found")

    # Extract input signals from module_body
    input_pattern = r"input\s+([^;]+);"
    input_matches = re.search(input_pattern, module.module_body)

    if not input_matches:
        pytest.fail(f"{impl_name}: Failed to find input declaration in module_body")

    input_declaration = input_matches.group(1)
    input_signals = [s.strip() for s in input_declaration.split(",")]

    # Original inputs
    original_inputs = ["a", "b"]
    new_inputs = [signal for signal in input_signals if signal not in original_inputs]

    if not new_inputs:
        pytest.skip(f"{impl_name}: No new input signals were found to test in logic")

    # Look for any usage of new inputs in the module body
    # Extract the logic section (everything after the port declarations)
    module_content = module.module_body

    # Remove the input and output declaration lines
    input_output_pattern = r"(input|output)\s+[^;]+;"
    logic_section = re.sub(input_output_pattern, "", module_content)

    # Check if any new input is used in the logic section
    used_inputs = set()
    for new_input in new_inputs:
        # Check if the new input appears as a word boundary in the logic section
        if re.search(rf'\b{re.escape(new_input)}\b', logic_section):
            used_inputs.add(new_input)

    # If no inputs are used, provide information about the implementation
    if not used_inputs:
        # Extract assign statements for better error messages
        assign_pattern = r"assign\s+(\w+)\s*=\s*([^;]+);"
        assigns = list(re.finditer(assign_pattern, module.module_body))

        if not assigns:
            pytest.skip(f"{impl_name}: No assign statements found to test for input usage")
        else:
            # Extract the right-hand side of assign statements
            assign_exprs = [assign.group(2) for assign in assigns]

            # Suggest how to update logic to use new inputs
            suggested_logic = []
            for i, expr in enumerate(assign_exprs):
                if i == 0:  # c
                    suggested_logic.append(f"{expr} ^ {' ^ '.join(new_inputs[:2])}")
                elif i == 1:  # sum
                    suggested_logic.append(f"({expr}) | ({' & '.join(new_inputs[:2])})")
                elif i == 2:  # carry
                    suggested_logic.append(f"{expr} & {' & '.join(new_inputs[:2])}")

            fail_msg = (f"{impl_name}: None of the new inputs ({new_inputs}) are used in the logic. "
                        f"Found assigns: {assign_exprs}. "
                        f"Consider updating to: {suggested_logic}")
            assert used_inputs, fail_msg

def test_no_invalid_input_names(implementation):
    """Test that there are no invalid input names."""
    impl_name, module = implementation

    # Skip test for implementations without module_body attribute
    if not hasattr(module, 'module_body'):
        pytest.skip(f"{impl_name}: No module_body attribute found")

    # Extract input signals from module_body
    input_pattern = r"input\s+([^;]+);"
    input_matches = re.search(input_pattern, module.module_body)

    if not input_matches:
        pytest.fail(f"{impl_name}: Failed to find input declaration in module_body")

    input_declaration = input_matches.group(1)
    input_signals = [s.strip() for s in input_declaration.split(",")]

    # Check for duplicates in input list
    input_set = set()
    duplicates = set()

    for signal in input_signals:
        if signal in input_set:
            duplicates.add(signal)
        input_set.add(signal)

    # Allow 'c' to be duplicated as it could be both input and output in these examples
    allowed_duplicates = {'c'}
    real_duplicates = duplicates - allowed_duplicates

    assert not real_duplicates, f"{impl_name}: Duplicate input signals found: {real_duplicates}"

    # Check for invalid Verilog identifiers
    invalid_identifiers = []
    for signal in input_signals:
        # Verilog identifiers can only contain letters, numbers, underscore and $
        # Must start with a letter or underscore
        if not re.match(r'^[a-zA-Z_][a-zA-Z0-9_$]*$', signal):
            invalid_identifiers.append(signal)

    assert not invalid_identifiers, f"{impl_name}: Invalid Verilog identifiers found: {invalid_identifiers}"

def test_required_attributes_exist(implementation):
    """Test that all required attributes exist in the implementation."""
    impl_name, module = implementation

    # Required attributes for a complete implementation
    required_attributes = [
        'module_body',
        'verilog_code',
        'module_instantiation',
        'input_ports',
        'output_ports'
    ]

    # For new_code2, we should check if the module has any attributes at all
    # before reporting all missing attributes
    if not any(hasattr(module, attr) for attr in required_attributes):
        pytest.skip(f"{impl_name}: Implementation appears incomplete, no required attributes found")

    missing_attributes = []
    for attr in required_attributes:
        if not hasattr(module, attr):
            missing_attributes.append(attr)

    assert not missing_attributes, f"{impl_name}: Missing required attributes: {missing_attributes}"

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: add a function to check for primes

# test_is_prime.py
import pytest
import inspect
import random

def test_is_prime_exists(implementation):
    """Test that the is_prime function exists and is callable."""
    impl_name, module = implementation
    if not hasattr(module, "is_prime"):
        pytest.skip(f"{impl_name} has no is_prime function")
    assert callable(module.is_prime), f"{impl_name}: is_prime should be callable"

def test_is_prime_signature(implementation):
    """Test that is_prime takes exactly one parameter."""
    impl_name, module = implementation
    if not hasattr(module, "is_prime"):
        pytest.skip(f"{impl_name} has no is_prime function")
    sig = inspect.signature(module.is_prime)
    assert len(sig.parameters) == 1, f"{impl_name}: is_prime should take exactly one argument"

@pytest.mark.parametrize("n,expected", [
    # small primes
    (2, True), (3, True), (5, True), (7, True), (11, True),
    # small non‑primes
    (0, False), (1, False), (4, False), (6, False), (9, False),
    # negatives
    (-1, False), (-2, False), (-17, False),
])
def test_is_prime_basic_cases(implementation, n, expected):
    """Basic known primes, non‑primes, and negatives."""
    _, module = implementation
    if not hasattr(module, "is_prime"):
        pytest.skip("no is_prime")
    assert module.is_prime(n) is expected, f"is_prime({n}) should be {expected}"

def naive_is_prime(n):
    """Reference implementation."""
    if n <= 1:
        return False
    if n <= 3:
        return True
    if n % 2 == 0 or n % 3 == 0:
        return False
    i = 5
    while i * i <= n:
        if n % i == 0 or n % (i + 2) == 0:
            return False
        i += 6
    return True

def test_is_prime_random(implementation):
    """Cross‑check is_prime against a simple naive algorithm on random inputs."""
    _, module = implementation
    if not hasattr(module, "is_prime"):
        pytest.skip("no is_prime")
    random.seed(0)
    for n in random.sample(range(0, 200), 30):
        assert module.is_prime(n) == naive_is_prime(n), f"Mismatch on {n}"

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