Skip to content

combination_generator

CombinationGenerator(*discrete_domains)

Bases: IncrementalEnvironment

A generator that produces tests based on combination of ranges.

This generator creates a test case for each combination of the provided value ranges. Each value range must be associated with exactly one input feature of the model that shall be tested.

Initialize the combination generator.

Parameters:

Name Type Description Default
discrete_domains Discrete

A list of discrete domains to generate test cases from. Each domain must be associated with exactly one input feature of the model that shall be tested.

()
Source code in src/flowcean/core/tool/testing/generator/combination_generator.py
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
def __init__(self, *discrete_domains: Discrete) -> None:
    """Initialize the combination generator.

    Args:
        discrete_domains: A list of discrete domains to generate test cases
            from. Each domain must be associated with exactly one input
            feature of the model that shall be tested.
    """
    super().__init__()

    # Check if no duplicate feature names are present
    feature_names_count: dict[str, int] = {}
    for domain in discrete_domains:
        feature_names_count[domain.feature_name] = (
            feature_names_count.get(
                domain.feature_name,
                0,
            )
            + 1
        )
    if len(feature_names_count) != len(discrete_domains):
        msg = "Duplicate features found: "
        msg += ", ".join(
            f"{feature_name}: {count}"
            for feature_name, count in feature_names_count.items()
            if count > 1
        )
        raise ValueError(msg)

    self.domains = discrete_domains

    # Calculate the number of test cases
    self.number_test_cases = functools.reduce(
        lambda n, range_: n * len(range_),
        self.domains,
        1,
    )

    # Build the product iterator
    self.product_iterator = itertools.product(
        *self.domains,
    )

    # Perform the first step
    self.step()