|
| 1 | +import os |
| 2 | +from importlib import import_module |
| 3 | +from types import ModuleType |
| 4 | + |
| 5 | +import pytest |
| 6 | + |
| 7 | +from recirq.algo_benchmark_library import BENCHMARKS, get_all_algo_configs |
| 8 | + |
| 9 | +RECIRQ_DIR = os.path.abspath(os.path.dirname(__file__) + '/../') |
| 10 | + |
| 11 | + |
| 12 | +@pytest.mark.parametrize('algo', BENCHMARKS) |
| 13 | +def test_domain(algo): |
| 14 | + # By convention, the domain should be a recirq module. |
| 15 | + assert algo.domain.startswith('recirq.') |
| 16 | + mod = import_module(algo.domain) |
| 17 | + assert isinstance(mod, ModuleType) |
| 18 | + |
| 19 | + |
| 20 | +def test_benchmark_name_unique_in_domain(): |
| 21 | + # In a given domain, all benchmark names should be unique |
| 22 | + pairs = [(algo.domain, algo.name) for algo in BENCHMARKS] |
| 23 | + assert len(set(pairs)) == len(pairs) |
| 24 | + |
| 25 | + |
| 26 | +@pytest.mark.parametrize('algo', BENCHMARKS) |
| 27 | +def test_executable_family_is_formulaic(algo): |
| 28 | + # Check consistency in the AlgoBenchmark dataclass: |
| 29 | + assert algo.executable_family == algo.spec_class.executable_family |
| 30 | + |
| 31 | + # By convention, we set this to be the module name. By further convention, |
| 32 | + # {algo.domain}.{algo.name} should be the module name. |
| 33 | + assert algo.executable_family == f'{algo.domain}.{algo.name}' |
| 34 | + |
| 35 | + # Check the convention that it should give a module |
| 36 | + mod = import_module(algo.executable_family) |
| 37 | + assert isinstance(mod, ModuleType) |
| 38 | + |
| 39 | + |
| 40 | +@pytest.mark.parametrize('algo', BENCHMARKS) |
| 41 | +def test_classes_and_funcs(algo): |
| 42 | + # The various class objects should exist in the module |
| 43 | + mod = import_module(algo.executable_family) |
| 44 | + assert algo.spec_class == getattr(mod, algo.spec_class.__name__) |
| 45 | + assert algo.data_class == getattr(mod, algo.data_class.__name__) |
| 46 | + assert algo.gen_func == getattr(mod, algo.gen_func.__name__) |
| 47 | + |
| 48 | + |
| 49 | +def test_globally_unique_executable_family(): |
| 50 | + # Each entry should have a unique executable family |
| 51 | + fams = [algo.executable_family for algo in BENCHMARKS] |
| 52 | + assert len(set(fams)) == len(fams) |
| 53 | + |
| 54 | + |
| 55 | +def test_globally_unique_config_full_name(): |
| 56 | + full_names = [config.full_name for algo, config in get_all_algo_configs()] |
| 57 | + assert len(set(full_names)) == len(full_names) |
| 58 | + |
| 59 | + |
| 60 | +@pytest.mark.parametrize('algo_config', get_all_algo_configs()) |
| 61 | +def test_gen_script(algo_config): |
| 62 | + algo, config = algo_config |
| 63 | + |
| 64 | + # Make sure it's formulaic |
| 65 | + assert config.gen_script == f'gen-{config.short_name}.py' |
| 66 | + |
| 67 | + # Make sure it exists |
| 68 | + gen_script_path = (f"{RECIRQ_DIR}/{algo.domain.replace('.', '/')}/" |
| 69 | + f"{algo.name.replace('.', '/')}/{config.gen_script}") |
| 70 | + assert os.path.exists(gen_script_path) |
0 commit comments