def pytest_generate_tests(metafunc): grunnur_pytest_generate_tests(metafunc) api_ids = all_api_ids() devices = get_devices(metafunc.config) device_sets = get_multi_device_sets(metafunc.config) if 'mock_or_real_context' in metafunc.fixturenames: values = (devices if len(devices) > 0 else [None]) + all_api_ids() ids = [_mock_or_real_context_make_id(value) for value in values] metafunc.parametrize('mock_or_real_context', values, ids=ids, indirect=True) if 'mock_or_real_multi_device_context' in metafunc.fixturenames: values = (device_sets if len(device_sets) > 0 else [None]) + all_api_ids() ids = [ _mock_or_real_multi_device_context_make_id(value) for value in values ] metafunc.parametrize('mock_or_real_multi_device_context', values, ids=ids, indirect=True)
def test_hash(mock_backend_factory): api_id0 = all_api_ids()[0] api_id1 = all_api_ids()[1] mock_backend_factory.mock(api_id0) mock_backend_factory.mock(api_id1) api0 = API.from_api_id(api_id0) api1 = API.from_api_id(api_id1) d = {api0: 0, api1: 1} assert d[api0] == 0 assert d[api1] == 1
def test_eq(mock_backend_factory): api_id0 = all_api_ids()[0] api_id1 = all_api_ids()[1] mock_backend_factory.mock(api_id0) mock_backend_factory.mock(api_id1) api0_v1 = API.from_api_id(api_id0) api0_v2 = API.from_api_id(api_id0) api1 = API.from_api_id(api_id1) assert api0_v1 is not api0_v2 and api0_v1 == api0_v2 assert api0_v1 != api1
def test_all(mock_backend_factory): api_id = all_api_ids()[0] mock_backend_factory.mock(api_id) apis = API.all_available() assert len(apis) == 1 assert apis[0].id == api_id
def test_from_api_id(mock_backend_factory): for api_id in all_api_ids(): with pytest.raises(ImportError): API.from_api_id(api_id) mock_backend_factory.mock(api_id) api = API.from_api_id(api_id) assert api.id == api_id
def test_all_by_shortcut_none(mock_backend_factory): api_ids = all_api_ids() for api_id in api_ids: backend = mock_backend_factory.mock(api_id) backend.add_devices(['Device1', 'Device2']) apis = API.all_by_shortcut() assert len(apis) == len(api_ids) assert set(api.id for api in apis) == set(api_ids)
def __init__(self, monkeypatch): self.monkeypatch = monkeypatch # Clear out all existing backends for api_id in all_api_ids(): self.mock(api_id, disable=True)
def mock(self, api_id, disable=False): if api_id == cuda_api_id(): return self.mock_pycuda(disable=disable) elif api_id == opencl_api_id(): return self.mock_pyopencl(disable=disable) else: raise ValueError(f"Unknown API ID: {api_id}") @pytest.fixture(scope='function') def mock_backend_factory(request, monkeypatch): yield MockBackendFactory(monkeypatch) @pytest.fixture(scope='function', params=all_api_ids(), ids=lambda api_id: f"mock_backend_{api_id.shortcut}") def mock_backend(request, mock_backend_factory): yield mock_backend_factory.mock(request.param) @pytest.fixture(scope='function') def mock_backend_pyopencl(request, mock_backend_factory): yield mock_backend_factory.mock_pyopencl() @pytest.fixture(scope='function') def mock_backend_pycuda(request, mock_backend_factory): yield mock_backend_factory.mock_pycuda()
def test_all_by_shortcut_not_available(mock_backend_factory): api_id = all_api_ids()[0] with pytest.raises(ValueError): API.all_by_shortcut(api_id.shortcut)