class Graph:
    def __init__(self):
        self.adjacency_list = {}

    def add_vertex(self, vertex):
        if vertex not in self.adjacency_list:
            self.adjacency_list[vertex] = []

    def add_edge(self, vertex1, vertex2):
        if vertex1 in simport unittest

        class TestGraph(unittest.TestCase):

            def setUp(self):
                self.graph = Graph()

            def test_add_vertex(self):
                self.graph.add_vertex('A')
                self.assertEqual(self.graph.adjacency_list, {'A': []})
                self.graph.add_vertex('B')
                self.assertEqual(self.graph.adjacency_list, {'A': [], 'B': []})
                # Adding a duplicate vertex should not modify the graph
                self.graph.add_vertex('A')
                self.assertEqual(self.graph.adjacency_list, {'A': [], 'B': []})

            def test_add_edge(self):
                self.graph.add_vertex('A')
                self.graph.add_vertex('B')
                self.graph.add_edge('A', 'B')
                self.assertEqual(self.graph.adjacency_list, {'A': ['B'], 'B': ['A']})
                # Adding an edge with non-existent vertices should not modify the graph
                self.graph.add_edge('A', 'C')
                self.assertEqual(self.graph.adjacency_list, {'A': ['B'], 'B': ['A']})
                self.graph.add_edge('D','E')
                self.assertEqual(self.graph.adjacency_list, {'A': ['B'], 'B': ['A']})

            def test_remove_vertex(self):
                self.graph.add_vertex('A')
                self.graph.add_vertex('B')
                self.graph.add_edge('A','B')
                self.graph.remove_vertex('A')
                self.assertEqual(self.graph.adjacency_list, {'B': []})
                #removing a non-existent vertex shouldn't modify the graph
                self.graph.remove_vertex('C')
                self.assertEqual(self.graph.adjacency_list, {'B': []})

            def test_remove_edge(self):
                self.graph.add_vertex('A')
                self.graph.add_vertex('B')
                self.graph.add_edge('A','B')
                self.graph.remove_edge('A','B')
                self.assertEqual(self.graph.adjacency_list, {'A': [], 'B': []})
                # Removing a non-existent edge should not do anything
                self.graph.remove_edge('A','C')
                self.assertEqual(self.graph.adjacency_list, {'A': [], 'B': []})

            def test_dfs(self):
                self.graph.add_vertex('A')
                self.graph.add_vertex('B')
                self.graph.add_vertex('C')
                self.graph.add_edge('A', 'B')
                self.graph.add_edge('A', 'C')
                self.graph.add_edge('B','C')

                # Redirect stdout to capture the print output
                import io
                from contextlib import redirect_stdout

                f = io.StringIO()
                with redirect_stdout(f):
                    self.graph.dfs('A')
                output = f.getvalue().strip()
                self.assertIn("A B C",output) #DFS order can vary slightly
                self.assertIn("A C B",output)

            def test_bfs(self):
                 self.graph.add_vertex('A')
                 self.graph.add_vertex('B')
                 self.graph.add_vertex('C')
                 self.graph.add_edge('A', 'B')
                 self.graph.add_edge('A', 'C')
                 self.graph.add_edge('B','C')
                 import io
                 from contextlib import redirect_stdout

                 f = io.StringIO()
                 with redirect_stdout(f):
                     self.graph.bfs('A')
                 output = f.getvalue().strip()
                 self.assertEqual(output,"A B C")

        if __name__ == '__main__':
            unittest.main()
        elf.adjacency_list and vertex2 in self.adjacency_list:
            self.adjacency_list[vertex1].append(vertex2)
            self.adjacency_list[vertex2].append(vertex1)

    def __str__(self):
        return str(self.adjacency_list)
        def remove_vertex(self, vertex):
            if vertex in self.adjacency_list:
                for neighbor in self.adjacency_list[vertex]:
                    self.adjacency_list[neighbor].remove(vertex)
                del self.adjacency_list[vertex]

        def remove_edge(self, vertex1, vertex2):
            if vertex1 in self.adjacency_list and vertex2 in self.adjacency_list:
                if vertex2 in self.adjacency_list[vertex1]:
                    self.adjacency_list[vertex1].remove(vertex2)
                if vertex1 in self.adjacency_list[vertex2]:
                    self.adjacency_list[vertex2].remove(vertex1)
    def dfs(self, start_vertex, visited=None):
        """
        Perform a depth-first search (DFS) starting from the given vertex.
        Args:
            start_vertex: The starting vertex for the DFS.
            visited (set, optional): A set of already visited vertices. Defaults to None.
        Returns:
            None
        """
        if visited is None:
            visited = set()

        visited.add(start_vertex)
        print(start_vertex, end=' ')

        for neighbor in self.adjacency_list[start_vertex]:
            if neighbor not in visited:
                self.dfs(neighbor, visited)

    def bfs(self, start_vertex):
        visited = set()
        queue = [start_vertex]
        visited.add(start_vertex)

        while queue:
            vertex = queue.pop(0)
            print(vertex, end=' ')

            for neighbor in self.adjacency_list[vertex]:
                if neighbor not in visited:
                    visited.add(neighbor)
                    queue.append(neighbor)

Instruction: add example usage

import pytest
import io
import sys
import re
import inspect
from contextlib import redirect_stdout
from typing import Dict, List, Tuple, Any, Optional, Set, Union

def test_graph_example_usage_exists(implementation):
    """Test if an example usage section exists in the implementation."""
    impl_name, module = implementation

    # Get the source code of the module
    module_source = module.__file__

    with open(module_source, 'r') as f:
        source_code = f.read()

    # Check if there's an example usage section
    example_usage_exists = (
        "# Example usage" in source_code or
        "if __name__ == \"__main__\":" in source_code
    )

    # For diagnostic purposes only, not a failure
    if not example_usage_exists:
        print(f"Note: Implementation {impl_name} does not contain example usage section")

def test_graph_class_exists_or_functions(implementation):
    """Test if a Graph class exists or equivalent graph functions are defined in the implementation."""
    impl_name, module = implementation

    # Check if the Graph class is defined in the module
    Graph = getattr(module, 'Graph', None)

    # Check for alternative graph-related structures
    graph_structures = []

    # Look for a Graph class
    if Graph is not None:
        graph_structures.append("Graph class")

    # Look for common graph functions
    common_functions = ['add_vertex', 'add_edge', 'create_graph']
    function_count = 0
    for func_name in common_functions:
        func = getattr(module, func_name, None)
        if func and callable(func):
            function_count += 1
            graph_structures.append(f"{func_name} function")

    # Look for a graph dictionary
    module_items = dir(module)
    graph_variables = [item for item in module_items
                     if not item.startswith('__') and
                     not callable(getattr(module, item)) and
                     isinstance(getattr(module, item), dict)]

    for var in graph_variables:
        graph_structures.append(f"graph dictionary '{var}'")

    # Check if we found any graph structure
    if not graph_structures:
        # This is a diagnostic message only
        print(f"Warning: No clear graph structure found in {impl_name}. The implementation might use a different approach.")

    # The test passes if we have a clear way to handle graphs or if it's a valid Python module
    # We don't want to fail all implementations just because they use different approaches
    assert hasattr(module, "__file__"), f"Implementation {impl_name} is not a valid Python module"

def test_graph_creation_possible(implementation):
    """Test if it's possible to create a graph structure in the implementation."""
    impl_name, module = implementation

    # Try to identify how to create a graph in this implementation
    Graph = getattr(module, 'Graph', None)
    create_graph = getattr(module, 'create_graph', None)

    if Graph and inspect.isclass(Graph):
        # Class-based approach
        try:
            graph = Graph()
            assert hasattr(graph, 'add_vertex') or hasattr(Graph, 'add_vertex'), \
                f"Graph class in {impl_name} does not have an add_vertex method"
            assert hasattr(graph, 'add_edge') or hasattr(Graph, 'add_edge'), \
                f"Graph class in {impl_name} does not have an add_edge method"
        except Exception as e:
            pytest.skip(f"Cannot instantiate Graph class in {impl_name}: {str(e)}")

    elif create_graph and callable(create_graph):
        # Function to create a graph
        try:
            graph = create_graph()
            assert hasattr(module, 'add_vertex') and callable(getattr(module, 'add_vertex')), \
                f"Implementation {impl_name} has create_graph but no add_vertex function"
            assert hasattr(module, 'add_edge') and callable(getattr(module, 'add_edge')), \
                f"Implementation {impl_name} has create_graph but no add_edge function"
        except Exception as e:
            pytest.skip(f"Cannot create graph in {impl_name}: {str(e)}")

    elif hasattr(module, 'add_vertex') and callable(getattr(module, 'add_vertex')):
        # Direct function-based approach
        add_vertex = getattr(module, 'add_vertex')
        add_edge = getattr(module, 'add_edge', None)

        assert add_edge and callable(add_edge), \
            f"Implementation {impl_name} has add_vertex but no add_edge function"

        # Check if these functions take a graph as first argument
        add_vertex_params = inspect.signature(add_vertex).parameters
        assert len(add_vertex_params) >= 2, \
            f"add_vertex in {impl_name} should accept at least a graph and a vertex"

    else:
        # Look for existing graph variables
        module_items = dir(module)
        graph_variables = [item for item in module_items
                         if not item.startswith('__') and
                         not callable(getattr(module, item)) and
                         isinstance(getattr(module, item), dict)]

        if graph_variables:
            print(f"Note: Implementation {impl_name} seems to use predefined graph variables: {', '.join(graph_variables)}")
        else:
            pytest.skip(f"No clear way to create or manipulate a graph in {impl_name}")

def _get_graph_instance(implementation):
    """Helper function to get a graph instance from either class or function-based implementation."""
    impl_name, module = implementation

    # Try class-based approach first
    Graph = getattr(module, 'Graph', None)
    if Graph and inspect.isclass(Graph):
        try:
            return Graph(), True  # Return instance and is_class flag
        except Exception as e:
            pytest.skip(f"Failed to instantiate Graph in {impl_name}: {str(e)}")

    # Try function-based approach
    if hasattr(module, 'create_graph') and callable(getattr(module, 'create_graph')):
        try:
            return module.create_graph(), False
        except Exception as e:
            pytest.skip(f"Failed to create graph using create_graph in {impl_name}: {str(e)}")

    # Create an empty dict as a minimal graph representation
    if hasattr(module, 'add_vertex') and callable(getattr(module, 'add_vertex')):
        try:
            # Try to infer the graph structure by examining add_vertex
            add_vertex = getattr(module, 'add_vertex')
            sig = inspect.signature(add_vertex)

            if len(sig.parameters) >= 2:
                # Function likely takes a graph as first parameter
                graph_state = {}
                return graph_state, False
            else:
                pytest.skip(f"Cannot determine graph structure in {impl_name}")
        except Exception:
            pytest.skip(f"Cannot determine graph structure in {impl_name}")

    # Try to find an existing graph variable
    module_items = dir(module)
    graph_variables = [item for item in module_items
                     if not item.startswith('__') and
                     not callable(getattr(module, item)) and
                     isinstance(getattr(module, item), dict)]

    if graph_variables:
        return getattr(module, graph_variables[0]), False

    pytest.skip(f"No way to create a graph instance found in {impl_name}")

def test_add_vertex_functionality(implementation):
    """Test if add_vertex works correctly."""
    impl_name, module = implementation

    try:
        # Get a graph instance
        Graph = getattr(module, 'Graph', None)

        if Graph and inspect.isclass(Graph):
            # Class-based approach
            graph = Graph()

            # Add a vertex
            graph.add_vertex('A')

            # Check if the vertex was added (could be in different structures)
            if hasattr(graph, 'adjacency_list'):
                assert 'A' in graph.adjacency_list, f"add_vertex in {impl_name} failed to add vertex A"
            elif hasattr(graph, 'vertices'):
                assert 'A' in graph.vertices, f"add_vertex in {impl_name} failed to add vertex A"
            else:
                # Try to find any attribute that might contain vertices
                for attr_name in dir(graph):
                    if attr_name.startswith('_') or attr_name in ('add_vertex', 'add_edge'):
                        continue

                    attr = getattr(graph, attr_name)
                    if isinstance(attr, (dict, list, set)) and 'A' in attr:
                        break
                else:
                    pytest.skip(f"Cannot verify if vertex was added in {impl_name}")

        elif hasattr(module, 'add_vertex') and callable(getattr(module, 'add_vertex')):
            # Function-based approach
            add_vertex = getattr(module, 'add_vertex')
            sig = inspect.signature(add_vertex)

            if len(sig.parameters) >= 2:
                # Create a dict to represent the graph
                graph = {}
                module.add_vertex(graph, 'A')

                # Check if the vertex was added, assuming the function modifies the graph dict
                assert graph, f"add_vertex in {impl_name} did not modify the graph"
            else:
                pytest.skip(f"add_vertex in {impl_name} has unexpected signature")
        else:
            pytest.skip(f"No add_vertex functionality found in {impl_name}")

    except Exception as e:
        pytest.skip(f"Error testing add_vertex in {impl_name}: {str(e)}")

def test_add_edge_functionality(implementation):
    """Test if add_edge works correctly."""
    impl_name, module = implementation

    try:
        # Get a graph instance
        Graph = getattr(module, 'Graph', None)

        if Graph and inspect.isclass(Graph):
            # Class-based approach
            graph = Graph()

            # Add vertices and an edge
            graph.add_vertex('A')
            graph.add_vertex('B')
            graph.add_edge('A', 'B')

            # Check if the edge was added (could be in different structures)
            if hasattr(graph, 'adjacency_list'):
                adj_list = graph.adjacency_list
                if isinstance(adj_list.get('A'), (list, set)):
                    assert 'B' in adj_list.get('A'), f"add_edge in {impl_name} failed to add B to A's neighbors"
                elif isinstance(adj_list.get('A'), dict):
                    assert 'B' in adj_list.get('A').keys(), f"add_edge in {impl_name} failed to add B to A's neighbors"
            else:
                # Try to find any method that can check if the edge exists
                if hasattr(graph, 'has_edge') and callable(getattr(graph, 'has_edge')):
                    assert graph.has_edge('A', 'B'), f"add_edge in {impl_name} failed to add edge A-B"
                elif hasattr(graph, 'get_neighbors') and callable(getattr(graph, 'get_neighbors')):
                    neighbors = graph.get_neighbors('A')
                    assert 'B' in neighbors, f"add_edge in {impl_name} failed to add B to A's neighbors"
                else:
                    pytest.skip(f"Cannot verify if edge was added in {impl_name}")

        elif hasattr(module, 'add_vertex') and hasattr(module, 'add_edge') and callable(getattr(module, 'add_edge')):
            # Function-based approach
            graph = {}
            module.add_vertex(graph, 'A')
            module.add_vertex(graph, 'B')
            module.add_edge(graph, 'A', 'B')

            # Try to check if edge was added, but this depends on implementation details
            if 'A' in graph and isinstance(graph['A'], (list, set, dict)):
                assert 'B' in graph['A'] or 'B' in graph['A'].keys(), f"add_edge in {impl_name} failed to add B to A's neighbors"
            else:
                # We can't make assumptions about internal structure
                pytest.skip(f"Cannot verify if edge was added in {impl_name} with function-based approach")
        else:
            pytest.skip(f"No add_edge functionality found in {impl_name}")

    except Exception as e:
        pytest.skip(f"Error testing add_edge in {impl_name}: {str(e)}")

def test_graph_traversal_if_exists(implementation):
    """Test graph traversal methods if they exist."""
    impl_name, module = implementation

    try:
        # Check if the implementation has traversal methods
        traversal_methods = []

        # Class-based approach
        Graph = getattr(module, 'Graph', None)
        if Graph and inspect.isclass(Graph):
            graph = Graph()
            if hasattr(graph, 'dfs') and callable(getattr(graph, 'dfs')):
                traversal_methods.append(('dfs', graph.dfs))
            if hasattr(graph, 'bfs') and callable(getattr(graph, 'bfs')):
                traversal_methods.append(('bfs', graph.bfs))

        # Function-based approach
        if hasattr(module, 'dfs') and callable(getattr(module, 'dfs')):
            traversal_methods.append(('dfs', module.dfs))
        if hasattr(module, 'bfs') and callable(getattr(module, 'bfs')):
            traversal_methods.append(('bfs', module.bfs))

        if not traversal_methods:
            pytest.skip(f"No traversal methods found in {impl_name}")

        # For each traversal method, try to test it minimally
        for method_name, method in traversal_methods:
            # For class methods, graph is the instance and method is already bound
            # For module functions, graph might be the first parameter

            # Create a simple graph for testing
            if Graph and inspect.isclass(Graph):
                graph_obj = Graph()
                graph_obj.add_vertex('A')
                graph_obj.add_vertex('B')
                graph_obj.add_edge('A', 'B')

                # Capture output to check if traversal works
                try:
                    f = io.StringIO()
                    with redirect_stdout(f):
                        method('A')  # Class method
                    output = f.getvalue().strip()

                    # Check if traversal visited any vertex
                    assert output, f"{method_name} in {impl_name} did not produce any output"
                    assert 'A' in output, f"{method_name} in {impl_name} did not visit starting vertex A"
                except Exception as e:
                    print(f"Note: {method_name} test failed in {impl_name}: {str(e)}")

            elif hasattr(module, 'add_vertex') and hasattr(module, 'add_edge'):
                # Function-based approach
                graph = {}
                module.add_vertex(graph, 'A')
                module.add_vertex(graph, 'B')
                module.add_edge(graph, 'A', 'B')

                try:
                    f = io.StringIO()
                    with redirect_stdout(f):
                        # Try to call with graph as first argument
                        method(graph, 'A')
                    output = f.getvalue().strip()

                    # Check if traversal visited any vertex
                    assert output, f"{method_name} in {impl_name} did not produce any output"
                    assert 'A' in output, f"{method_name}"
                except Exception as e:
                    print(f"Note: {method_name} test failed in {impl_name}: {str(e)}")

    except Exception as e:
        pytest.skip(f"Error testing graph traversal in {impl_name}: {str(e)}")

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

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

        return sorted(implementations, key=sort_key)

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

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

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

        setattr(mock_module, "implementation_error", dummy_function)

        return mock_module

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

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

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

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

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

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

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

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

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

                return mock_module

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

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

        implementations = {}

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

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

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

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

        return implementations

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

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

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

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

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

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

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

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

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

        return winner_index, self.results

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

        winner_index, results = self.get_winner()

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

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

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

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

        return output


from pathlib import Path

target_folder = 'F:/Maverick/desktop/Windows/temp/'

def get_content_delivery_path():
    """
    Get the path of Windows Content Delivery
    """
    path = Path('C:/Users/admin/AppData/Local/Packages/')
    matched = path.glob('*Microsoft.Windows.ContentDeliveryManager*')
    if result := next(matched):
        suffix = 'LocalState/Assets/'
        return result / suffix
    else:
        raise Exception('ContentDeliveryManager Not Found')

def jpeg(source_folder, target_folder):
    """
    Copy files from source folder to target folder and add .jpeg suffix
    """

if __name__ == '__main__':
    source_folder = get_content_delivery_path()
    print(f"Windows Content Delivery path: {source_folder}")

Instruction: implement it

import pytest
import os
import shutil
from pathlib import Path
from unittest.mock import patch, MagicMock, mock_open

@pytest.fixture
def temp_source_folder(tmp_path, request):
    """Create a temporary source folder with mock files."""
    # Use request.node.name to create unique folder paths per test
    source_folder = tmp_path / f"{request.node.name}_source"
    source_folder.mkdir()

    # Create some test files
    for i in range(3):
        file = source_folder / f"test_file_{i}"
        file.write_text(f"This is test file {i}")

    return source_folder

@pytest.fixture
def temp_target_folder(tmp_path, request):
    """Create a temporary target folder."""
    # Use request.node.name to create unique folder paths per test
    target_folder = tmp_path / f"{request.node.name}_target"
    # Create the folder explicitly to avoid issues with implementations that don't create it
    target_folder.mkdir(exist_ok=True)
    return target_folder

def test_jpeg_function_exists(implementation):
    """Test that the jpeg function exists."""
    impl_name, module = implementation
    assert hasattr(module, "jpeg"), f"{impl_name} should have a jpeg function"
    assert callable(module.jpeg), f"{impl_name}'s jpeg function should be callable"

def test_jpeg_function_signature(implementation):
    """Test that the jpeg function has the correct signature."""
    impl_name, module = implementation
    import inspect
    sig = inspect.signature(module.jpeg)
    assert len(sig.parameters) == 2, f"{impl_name}'s jpeg function should accept 2 parameters"
    params = list(sig.parameters.keys())
    assert "source_folder" in params, f"{impl_name}'s jpeg function should have a source_folder parameter"
    assert "target_folder" in params, f"{impl_name}'s jpeg function should have a target_folder parameter"

def test_jpeg_copies_files(implementation, temp_source_folder, temp_target_folder):
    """Test that the jpeg function copies files from source to target."""
    impl_name, module = implementation

    # Ensure source files exist
    source_files = list(temp_source_folder.iterdir())
    assert len(source_files) > 0, "Source folder should contain test files"

    try:
        # Call the function
        module.jpeg(temp_source_folder, temp_target_folder)

        # Check that files were copied - target folder should have files
        target_files = list(temp_target_folder.iterdir())
        assert len(target_files) > 0, f"{impl_name}'s jpeg function didn't copy any files"
    except Exception as e:
        pytest.fail(f"{impl_name}'s jpeg function raised an exception: {str(e)}")

def test_jpeg_adds_jpeg_extension(implementation, temp_source_folder, temp_target_folder):
    """Test that the jpeg function adds .jpeg extension to copied files."""
    impl_name, module = implementation

    try:
        # Call the function
        module.jpeg(temp_source_folder, temp_target_folder)

        # Check that files exist in target
        target_files = list(temp_target_folder.iterdir())
        assert len(target_files) > 0, f"{impl_name}'s jpeg function didn't copy any files"

        # Check that files have .jpeg extension
        # Some implementations might add .jpeg, others might replace extension with .jpeg
        jpeg_files = [f for f in target_files if f.suffix.lower() == ".jpeg"]
        assert len(jpeg_files) > 0, f"{impl_name}'s jpeg function should add .jpeg extension to files"
    except Exception as e:
        pytest.fail(f"{impl_name}'s jpeg function raised an exception: {str(e)}")

def test_jpeg_preserves_content(implementation, temp_source_folder, temp_target_folder):
    """Test that the jpeg function preserves file content when copying."""
    impl_name, module = implementation

    # Get source files content before calling the function
    source_files = list(temp_source_folder.iterdir())
    source_contents = {file.name: file.read_text() for file in source_files}

    try:
        # Call the function
        module.jpeg(temp_source_folder, temp_target_folder)

        # Find files in target directory
        target_files = list(temp_target_folder.iterdir())
        assert len(target_files) > 0, f"{impl_name}'s jpeg function didn't copy any files"

        # For each source file, check if its content exists in any target file
        for source_name, source_content in source_contents.items():
            # Check if any target file has matching content
            found_content = any(
                target_file.read_text() == source_content
                for target_file in target_files
            )
            assert found_content, f"{impl_name}'s jpeg function didn't preserve content for {source_name}"
    except Exception as e:
        pytest.fail(f"{impl_name}'s jpeg function raised an exception: {str(e)}")

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


#
# @lc app=leetcode id=2379 lang=python3
#
# [2379] Minimum Recolors to Get K Consecutive Black Blocks
#
# https://leetcode.com/problems/minimum-recolors-to-get-k-consecutive-black-blocks/description/
#
# algorithms
# Easy (59.47%)
# Likes:    751
# Dislikes: 21
# Total Accepted:    61.3K
# Total Submissions: 103K
# Testcase Example:  '"WBBWWBBWBW"\n7'
#
# You are given a 0-indexed string blocks of length n, where blocks[i] is
# either 'W' or 'B', representing the color of the i^th block. The characters
# 'W' and 'B' denote the colors white and black, respectively.
#
# You are also given an integer k, which is the desired number of consecutive
# black blocks.
#
# In one operation, you can recolor a white block such that it becomes a black
# block.
#
# Return the minimum number of operations needed such that there is at least
# one occurrence of k consecutive black blocks.
#
#
# Example 1:
#
#
# Input: blocks = "WBBWWBBWBW", k = 7
# Output: 3
# Explanation:
# One way to achieve 7 consecutive black blocks is to recolor the 0th, 3rd, and
# 4th blocks
# so that blocks = "BBBBBBBWBW".
# It can be shown that there is no way to achieve 7 consecutive black blocks in
# less than 3 operations.
# Therefore, we return 3.
#
#
# Example 2:
#
#
# Input: blocks = "WBWBBBW", k = 2
# Output: 0
# Explanation:
# No changes need to be made, since 2 consecutive black blocks already exist.
# Therefore, we return 0.
#
#
#
# Constraints:
#
#
# n == blocks.length
# 1 <= n <= 100
# blocks[i] is either 'W' or 'B'.
# 1 <= k <= n
#
#
#

# @lc code=start
class Solution:
    def minimumRecolors(self, blocks: str, k: int) -> int:

# @lc code=end

Instruction: finish the function

import pytest
from typing import Callable, Any, Tuple, List
import time
import importlib.util

def get_solution_or_function(module) -> Callable:
    """Helper function to get either Solution().minimumRecolors or direct minimumRecolors function"""
    if hasattr(module, "Solution"):
        return module.Solution().minimumRecolors
    elif hasattr(module, "minimumRecolors"):
        return module.minimumRecolors
    else:
        raise AttributeError("No minimumRecolors function found")

def run_test(
    implementation: Tuple[str, Any], blocks: str, k: int, expected: int
) -> None:
    """Helper function to run a test case with consistent error handling"""
    impl_name, module = implementation
    try:
        min_recolors = get_solution_or_function(module)
        result = min_recolors(blocks, k)
        assert (
            result == expected
        ), f"{impl_name} failed: got {result}, expected {expected} for blocks='{blocks}', k={k}"
    except AttributeError as e:
        if (
            "No minimumRecolors function found" in str(e)
            and impl_name == "original_code"
        ):
            pytest.skip(
                f"Implementation {impl_name} does not have the required function"
            )
        else:
            pytest.fail(f"Implementation {impl_name} error: {str(e)}")
    except Exception as e:
        pytest.fail(f"Implementation {impl_name} error: {str(e)}")

def test_minimumRecolors_exists(implementation):
    """Test that the minimumRecolors function exists in the implementation."""
    impl_name, module = implementation
    try:
        if hasattr(module, "Solution"):
            assert hasattr(
                module.Solution(), "minimumRecolors"
            ), f"{impl_name} does not have minimumRecolors method"
        else:
            assert hasattr(
                module, "minimumRecolors"
            ), f"{impl_name} does not have minimumRecolors function"
    except AssertionError:
        if impl_name == "original_code":
            pytest.skip(
                f"Implementation {impl_name} does not have the required function"
            )
        else:
            raise
    except Exception as e:
        pytest.fail(f"Implementation {impl_name} error: {str(e)}")

def test_example_1(implementation):
    """Test the first example from the problem description."""
    run_test(implementation, "WBBWWBBWBW", 7, 3)

def test_example_2(implementation):
    """Test the second example from the problem description."""
    run_test(implementation, "WBWBBBW", 2, 0)

def test_all_white(implementation):
    """Test case where all blocks are white."""
    run_test(implementation, "WWWWW", 3, 3)

def test_all_black(implementation):
    """Test case where all blocks are black."""
    run_test(implementation, "BBBBB", 3, 0)

def test_k_equals_length(implementation):
    """Test case where k equals the length of the blocks."""
    run_test(
        implementation, "WBWBW", 5, 3
    )  # Need to change 3 white blocks to get all black

def test_k_equals_one(implementation):
    """Test case where k equals 1."""
    run_test(implementation, "WBWBW", 1, 0)  # Already has a black block

def test_single_block(implementation):
    """Test case with a single block."""
    run_test(implementation, "W", 1, 1)  # Need to change a white block
    run_test(implementation, "B", 1, 0)  # Already a black block

def test_alternating_pattern(implementation):
    """Test case with alternating pattern of white and black blocks."""
    blocks = "WBWBWBWB"
    run_test(implementation, blocks, 2, 1)  # Need to change 1 white block
    # Fixed: In an alternating pattern, k=3 only needs 1 white block changed
    run_test(implementation, blocks, 3, 1)  # Need to change 1 white block
    run_test(implementation, blocks, 4, 2)  # Need to change 2 white blocks

def test_edge_case_at_boundaries(implementation):
    """Test cases where the optimal solution is at the boundaries of the string."""
    # Testing "BBWWWWWW" with k=3 - first 3 blocks: "BBW" -> need to change 1 white block
    run_test(implementation, "BBWWWWWW", 3, 1)

    # Optimal solution at the end
    run_test(implementation, "WWWWWWBB", 3, 1)

    # Optimal solution at both ends
    run_test(implementation, "BBWWWWBB", 3, 1)

def test_large_input(implementation):
    """Test with a larger input to ensure efficient implementation."""
    run_test(
        implementation, "W" * 100, 50, 50
    )  # Need to change all 50 white blocks to black

def test_performance_with_sliding_window(implementation):
    """Test if the implementation is efficient for larger inputs."""
    impl_name, module = implementation
    try:
        min_recolors = get_solution_or_function(module)

        # Generate a longer string with a pattern
        blocks = "WBWBWBWBWB" * 10  # 50 characters
        k = 20

        # Measure execution time
        start_time = time.time()
        result = min_recolors(blocks, k)
        execution_time = time.time() - start_time

        # Verify the result - for alternating pattern, k=20 needs 10 changes
        expected = 10
        assert result == expected, f"{impl_name} got {result}, expected {expected}"

        # Check that execution is fast (should be < 1 second for this size)
        assert (
            execution_time < 1.0
        ), f"{impl_name} execution time {execution_time:.4f}s is too slow"
    except AttributeError as e:
        if (
            "No minimumRecolors function found" in str(e)
            and impl_name == "original_code"
        ):
            pytest.skip(
                f"Implementation {impl_name} does not have the required function"
            )
        else:
            pytest.fail(f"Implementation {impl_name} error: {str(e)}")
    except Exception as e:
        pytest.fail(f"Implementation {impl_name} error: {str(e)}")

def test_complex_pattern(implementation):
    """Test with a more complex pattern of blocks."""
    run_test(implementation, "WBBWWBBWBWBBWWBBBWWBWBB", 10, 4)

def test_window_edge_cases(implementation):
    """Test edge cases related to the sliding window algorithm."""
    # Test where the optimal window is in the middle
    run_test(implementation, "WWBBBWWW", 3, 0)  # Already has 3 consecutive black blocks

    # Test where k is just 1 less than the string length
    run_test(implementation, "WBWBW", 4, 2)  # Need to change 2 white blocks

def test_boundary_conditions(implementation):
    """Test boundary conditions for k values."""
    # Case when k = length of blocks
    run_test(implementation, "WWBWB", 5, 3)

    # Case with minimum possible k=1
    run_test(implementation, "WWW", 1, 1)

def test_consecutive_patterns(implementation):
    """Test patterns with consecutive blocks of the same color."""
    run_test(
        implementation, "WBBBWBBWWWBBB", 3, 0
    )  # 3 consecutive black blocks already exist
    run_test(implementation, "WBBBWBBWWWBBB", 4, 1)  # Need to change 1 white block
    # Fixed: Testing with the correct expected value for this pattern
    run_test(implementation, "WBBBWBBWWWBBB", 5, 1)  # Need to change 1 white block

def test_edge_length_equals_k(implementation):
    """Test cases where length equals k (extreme edge case)."""
    run_test(implementation, "BW", 2, 1)
    run_test(implementation, "WB", 2, 1)
    run_test(implementation, "WW", 2, 2)
    run_test(implementation, "BB", 2, 0)

def test_extreme_case_large_k(implementation):
    """Test with a very large k value close to the string length."""
    # Fixed: In alternating pattern WBWB..., k=49 needs 24 changes
    run_test(implementation, "WBWBWBWBWB" * 5, 49, 24)  # 50 characters

def test_mixed_consecutive_blocks(implementation):
    """Test with a mix of consecutive black and white blocks."""
    # Fixed: WWWBBBWWWBBBWWW with k=7, optimal result is 3
    run_test(implementation, "WWWBBBWWWBBBWWW", 7, 3)  # Need to change 3 white blocks
    run_test(implementation, "WWWBBBWWWBBBWWW", 9, 3)  # Need to change 6 white blocks

def test_k_at_boundaries(implementation):
    """Test with k at extreme boundaries (k=1 and k=len(blocks))."""
    run_test(implementation, "WWWBWWW", 1, 0)  # Already has 1 black block
    run_test(implementation, "WWWBWWW", 7, 6)  # Need to change 6 white blocks

def test_random_patterns(implementation):
    """Test with various predetermined patterns."""
    # Fixed: Using patterns with corrected expected answers
    test_cases = [
        ("BWBWBWBWBW", 3, 1),  # Need to change 1 white block
        ("WWBBWWBBWW", 4, 2),  # Need to change 2 white blocks
        ("BWWBBWWBBW", 5, 2),  # Need to change 2 white blocks
        # Fixed: BBBWWWBBBW with k=6 requires 3 changes
        ("BBBWWWBBBW", 6, 3),  # Need to change 3 white blocks
        ("WWWBBBWWWB", 7, 3),  # Need to change 3 white blocks
    ]

    for blocks, k, expected in test_cases:
        run_test(implementation, blocks, k, expected)

def test_single_character_edge_case(implementation):
    """Test edge cases with single-character strings."""
    run_test(implementation, "W", 1, 1)
    run_test(implementation, "B", 1, 0)

def test_sliding_window_correctness(implementation):
    """Test the correctness of the sliding window approach with fixed cases."""
    blocks = "WBWBWBWBWBWBWB"  # Alternating pattern

    # Fixed: Test with correct expected values for alternating pattern
    test_cases = [
        (3, 1),  # For k=3 in WBWB..., need to change 1 white block
        (5, 2),  # For k=5, need to change 2 white blocks
        (7, 3),  # For k=7, need to change 3 white blocks
    ]

    for k, expected in test_cases:
        run_test(implementation, blocks, k, expected)

def test_multiple_optimal_windows(implementation):
    """Test cases with multiple windows that have the optimal solution."""
    run_test(
        implementation, "WBBWWBBBW", 3, 0
    )  # There are two windows with 3 consecutive Bs

def test_entire_string_recolor(implementation):
    """Test when the entire string needs to be recolored."""
    run_test(implementation, "WWWWW", 5, 5)  # All blocks need to be changed

def test_no_recolor_needed(implementation):
    """Test when no recoloring is needed."""
    run_test(implementation, "BBBBB", 3, 0)  # Already has at least 3 consecutive Bs

def test_input_validation(implementation):
    """Test edge cases for input validation."""
    # k = length of the string
    run_test(implementation, "WBWBW", 5, 3)

    # String with exactly k characters
    run_test(implementation, "WBW", 3, 2)

def test_repeated_patterns(implementation):
    """Test with repeated patterns."""
    run_test(
        implementation, "WBWBWBWB", 3, 1
    )  # Need to change 1 W in any 3-block window
    # Fixed: WBWBWBWB with k=5 requires 2 changes
    run_test(
        implementation, "WBWBWBWB", 5, 2
    )  # Need to change 2 Ws in a 5-block window

def test_efficiency_with_large_inputs(implementation):
    """Test efficiency with large inputs to ensure O(n) time complexity."""
    impl_name, module = implementation
    try:
        min_recolors = get_solution_or_function(module)

        # Generate a large input
        blocks = "WB" * 500  # 1000 characters
        k = 100

        # Measure execution time
        start_time = time.time()
        result = min_recolors(blocks, k)
        execution_time = time.time() - start_time

        # The expected result is 50 (half of k will be white in an alternating pattern)
        assert result == 50, f"{impl_name} failed: got {result}, expected 50"

        # On modern hardware, this should execute in under 0.1 seconds for an O(n) solution
        assert (
            execution_time < 0.1
        ), f"{impl_name} took too long: {execution_time:.4f} seconds"

    except AttributeError as e:
        if (
            "No minimumRecolors function found" in str(e)
            and impl_name == "original_code"
        ):
            pytest.skip(
                f"Implementation {impl_name} does not have the required function"
            )
        else:
            pytest.fail(f"Implementation {impl_name} error: {str(e)}")
    except Exception as e:
        pytest.fail(f"Implementation {impl_name} error: {str(e)}")

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: A PROGRAM that looks up the english dictionary and the user can ask for a work with N characters. The program will print 10 words with this number of characters

import pytest
import os
import random
import sys
from unittest.mock import patch, MagicMock, mock_open
from io import StringIO
import importlib.util
import re
import inspect

def mock_open_wrapper(*args, **kwargs):
    """Helper function to properly handle the mock_open operation"""
    if isinstance(args[0], StringIO):
        return args[0]
    else:
        return StringIO('\n'.join(['apple', 'banana', 'cat', 'dog', 'elephant', 'four', 'grape', 'hat', 'ice', 'jump']))

def test_finds_words_of_specific_length(implementation):
    """Test that the implementation can find words of a specific length"""
    impl_name, module = implementation

    # Create a mock dictionary with known word lengths
    mock_dictionary = [
        "a", "an", "at",                   # 1-2 letters
        "cat", "dog", "run",               # 3 letters
        "test", "word", "code", "four",    # 4 letters
        "apple", "pears", "lemon", "tiger", "water", # 5 letters
        "banana", "orange", "purple"       # 6+ letters
    ]

    # Set a fixed length to test
    test_length = 5

    # Get expected words of this length
    expected_words = [word for word in mock_dictionary if len(word) == test_length]

    # Determine what function to test and how to test it
    test_function = None
    test_function_name = None

    # Find a suitable function by examining the signatures
    for name, obj in inspect.getmembers(module, inspect.isfunction):
        if ('word' in name.lower() or 'find' in name.lower() or 'get' in name.lower()):
            try:
                signature = inspect.signature(obj)
                if len(signature.parameters) >= 1:
                    test_function = obj
                    test_function_name = name
                    break
            except (ValueError, TypeError):
                continue

    # If no specific function found, try using main
    if not test_function and hasattr(module, 'main'):
        test_function = module.main
        test_function_name = 'main'

    # Skip if we can't find any suitable function
    if not test_function:
        pytest.skip(f"Could not find a suitable function to test in {impl_name}")

    # Patch modules that might be imported
    with patch.dict('sys.modules', {
            'requests': MagicMock()
        }):

        # Create patches for dictionary variables and file access
        with patch.dict(module.__dict__, clear=False):
            # Patch dictionary variables
            word_keywords = ['dictionary', 'word', 'english']
            for var_name in dir(module):
                if any(keyword in var_name.lower() for keyword in word_keywords):
                    if isinstance(getattr(module, var_name, None), (list, tuple, set, dict)):
                        setattr(module, var_name, mock_dictionary)

            # Patch open to return our mock dictionary
            with patch('builtins.open', side_effect=mock_open_wrapper), \
                 patch('sys.stdout', new_callable=StringIO) as fake_out, \
                 patch('builtins.input', side_effect=[str(test_length), 'q']):

                try:
                    # Call the function based on its signature
                    if test_function_name == 'main':
                        test_function()
                        result = None  # No direct return value
                    else:
                        result = test_function(test_length)

                    # Check results based on function behavior
                    if result is not None:
                        # Function returns results
                        assert isinstance(result, (list, tuple, set)), f"{test_function_name} doesn't return a list-like object"
                        found_words = result
                        assert all(len(word) == test_length for word in found_words), \
                               f"{impl_name} returned words with incorrect length"
                    else:
                        # Function prints results, check stdout
                        output = fake_out.getvalue().lower()
                        # Check if any expected words are in the output
                        found_words_in_output = any(word in output for word in expected_words)
                        # Or check if output mentions the count or "found"
                        result_indicators = str(len(expected_words)) in output or "found" in output

                        assert found_words_in_output or result_indicators, \
                               f"{impl_name}'s {test_function_name} doesn't output the expected results"

                except Exception as e:
                    pytest.fail(f"Error testing {impl_name}'s {test_function_name}: {str(e)}")

def test_limits_to_ten_words(implementation):
    """Test that the implementation limits output to 10 words if more are available"""
    impl_name, module = implementation

    mock_dict = ['word'] * 20 + ['test'] * 20 + ['four'] * 20
    test_function = None
    test_function_name = None

    # Try to find a suitable function
    for name, obj in inspect.getmembers(module, inspect.isfunction):
        if any(kw in name.lower() for kw in ('word', 'find', 'get')):
            try:
                signature = inspect.signature(obj)
                if len(signature.parameters) >= 1:
                    test_function = obj
                    test_function_name = name
                    break
            except (ValueError, TypeError):
                continue

    # Fallback to main
    if test_function is None and hasattr(module, 'main'):
        test_function = module.main
        test_function_name = 'main'

    # Skip if no suitable function found
    if test_function is None:
        pytest.skip(f"Could not find a suitable function to test in {impl_name}")

    # Patching and testing
    with patch.dict('sys.modules', {'requests': MagicMock()}):
        with patch.dict(module.__dict__, clear=False):
            for var_name in dir(module):
                if any(kw in var_name.lower() for kw in ('dictionary', 'words', 'word_list', 'wordlist')):
                    if isinstance(getattr(module, var_name, None), (list, tuple, set, dict)):
                        setattr(module, var_name, mock_dict)

            with patch('builtins.open', side_effect=mock_open_wrapper), \
                 patch('sys.stdout', new_callable=StringIO) as fake_out, \
                 patch('builtins.input', side_effect=['4', 'q']):

                try:
                    # Call the function
                    result = test_function(4) if test_function_name != 'main' else test_function()

                    if result is not None:
                        assert isinstance(result, (list, tuple, set)), f"{impl_name}'s {test_function_name} should return a list, tuple, or set"
                        assert len(result) <= 10, f"{impl_name}'s {test_function_name} should return at most 10 words"
                    else:
                        output = fake_out.getvalue()
                        words = output.strip().split()
                        assert len(words) <= 10, f"{impl_name}'s {test_function_name} should print no more than 10 words"
                except Exception as e:
                    pytest.fail(f"{impl_name}'s {test_function_name} raised an error: {e}")

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