test_gate_polarity.py

tests/test_gate_polarity.py

A single-symbol `#if defined(X)` has a polarity, and so does its `#else` (UPERF T5; 2.0.0).

test_a_compound_or_elif_condition_stays_unevaluated

None test_gate_polarity.test_a_compound_or_elif_condition_stays_unevaluated()

`&&` and `#elif` keep `if_expr`: their presence depends on more than one symbol.

tests/test_gate_polarity.py:46

Show on the map →

Called by none

Calls _gates

Checks

  • assert {form for _m, form, _l, _e in rows} == {"if_expr"}, rows

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-007measuredtest_a_compound_or_elif_condition_stays_unevaluated → _gates → gate_harvester
Source · 6 lines
def test_a_compound_or_elif_condition_stays_unevaluated() -> None:
    """@brief `&&` and `#elif` keep `if_expr`: their presence depends on more than one symbol."""
    rows = _gates("#if defined(A) && defined(B)\nint a;\n#elif defined(C)\nint c;\n#endif\n")
    assert {form for _m, form, _l, _e in rows} == {"if_expr"}, rows

Activity · 2 steps
lines 48–48rows = _gates("#if defined(A) && defined(B)\nint a;\n#elif defined(C)\nint c;\n#endif\n")lines 49–49assert {form for _m, form, _l, _e in rows} == {"if_expr"}, rows

test_a_declared_config_header_outvotes_heuristic_ones

None test_gate_polarity.test_a_declared_config_header_outvotes_heuristic_ones(tmp_path)

Declared rows, where a macro has any, are the only votes.

tests/test_gate_polarity.py:166

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert macro_definitions(conn)["mem_calloc"] == [11], "declared off wins over a heuristic on"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-013measured
Source · 19 lines
def test_a_declared_config_header_outvotes_heuristic_ones(tmp_path) -> None:
    """THE OPERATOR CAN SAY WHICH HEADER IS THE BUILD'S. `config_defaults` already tiers each row:
    `declared` for a header the repository states, `heuristic` for one that merely looks like a
    config header. mbedtls's motivating case, MBEDTLS_PLATFORM_MEMORY, is off in mbedtls_config.h
    and on in an alternative profile, so it is undecided by unanimity alone; with the shipped
    header declared, the declared rows decide and the other profiles do not vote.

    @brief Declared rows, where a macro has any, are the only votes.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    conn.execute("UPDATE config_defaults SET origin = 'declared'")
    conn.execute(
        "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'profile.h', 1, '', 'heuristic')"
    )
    assert macro_definitions(conn)["mem_calloc"] == [11], "declared off wins over a heuristic on"

Activity · 5 steps
lines 175–175from clew.macro_refs import macro_definitionslines 177–177conn = _macro_db(tmp_path)lines 178–178conn.execute("UPDATE config_defaults SET origin = 'declared'")lines 179–181conn.execute( "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'profile.h', 1, '', 'heuristic')" )lines 182–182assert macro_definitions(conn)["mem_calloc"] == [11], "declared off wins over a heuristic on"

test_a_same_name_macro_resolves_to_the_definition_the_default_build_compiles

None test_gate_polarity.test_a_same_name_macro_resolves_to_the_definition_the_default_build_compiles(tmp_path)

The default build picks the definition.

tests/test_gate_polarity.py:82

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert macro_definitions(conn)["mem_calloc"] == [11]
  • assert macro_definitions(conn)["mem_calloc"] == [10]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-013measured
Source · 14 lines
def test_a_same_name_macro_resolves_to_the_definition_the_default_build_compiles(tmp_path) -> None:
    """mbedtls F3: `{name: rowid}` kept whichever definition was read last. With MEM off as
    shipped, the `#else` definition is the one compiled.

    @brief The default build picks the definition.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    assert macro_definitions(conn)["mem_calloc"] == [11]
    conn.execute("UPDATE config_defaults SET state = 'on'")
    assert macro_definitions(conn)["mem_calloc"] == [10]

Activity · 5 steps
lines 88–88from clew.macro_refs import macro_definitionslines 90–90conn = _macro_db(tmp_path)lines 91–91assert macro_definitions(conn)["mem_calloc"] == [11]lines 92–92conn.execute("UPDATE config_defaults SET state = 'on'")lines 93–93assert macro_definitions(conn)["mem_calloc"] == [10]

test_a_single_defined_test_is_an_ifdef_or_ifndef

None test_gate_polarity.test_a_single_defined_test_is_an_ifdef_or_ifndef(str condition, str form)

`#if [!]defined(X)` carries X's polarity, and the `#else` the inverse.

tests/test_gate_polarity.py:39

Show on the map →

Called by none

Calls _gates

Checks

  • assert rows == [("A", form, 1, 3), ("A", inverse, 3, 4)], rows

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-007measuredtest_a_single_defined_test_is_an_ifdef_or_ifndef → _gates → gate_harvester
Source · 7 lines
def test_a_single_defined_test_is_an_ifdef_or_ifndef(condition: str, form: str) -> None:
    """@brief `#if [!]defined(X)` carries X's polarity, and the `#else` the inverse."""
    inverse = {"ifdef": "ifndef", "ifndef": "ifdef"}[form]
    rows = _gates(f"#if {condition}\nint a;\n#else\nint b;\n#endif\n")
    assert rows == [("A", form, 1, 3), ("A", inverse, 3, 4)], rows

Activity · 3 steps
lines 41–41inverse = {"ifdef": "ifndef", "ifndef": "ifdef"}[form]lines 42–42rows = _gates(f"#if {condition}\nint a;\n#else\nint b;\n#endif\n")lines 43–43assert rows == [("A", form, 1, 3), ("A", inverse, 3, 4)], rows

test_a_spelled_out_include_guard_is_still_not_a_gate

None test_gate_polarity.test_a_spelled_out_include_guard_is_still_not_a_gate()

`#if !defined(FOO_H)` + `#define FOO_H` is an include guard, like `#ifndef FOO_H`.

tests/test_gate_polarity.py:52

Show on the map →

Called by none

Calls _gates

Checks

  • assert _gates("#if !defined(FOO_H)\n#define FOO_H\nint a;\n#endif\n") == []

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-007measuredtest_a_spelled_out_include_guard_is_still_not_a_gate → _gates → gate_harvester
Source · 5 lines
def test_a_spelled_out_include_guard_is_still_not_a_gate() -> None:
    """@brief `#if !defined(FOO_H)` + `#define FOO_H` is an include guard, like `#ifndef FOO_H`."""
    assert _gates("#if !defined(FOO_H)\n#define FOO_H\nint a;\n#endif\n") == []

Activity · 1 steps
lines 54–54assert _gates("#if !defined(FOO_H)\n#define FOO_H\nint a;\n#endif\n") == []

test_an_undecided_symbol_keeps_every_definition

None test_gate_polarity.test_an_undecided_symbol_keeps_every_definition(tmp_path)

Unknown activity keeps both.

tests/test_gate_polarity.py:96

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
Source · 12 lines
def test_an_undecided_symbol_keeps_every_definition(tmp_path) -> None:
    """No config row: the build's choice is unknown here, so neither definition is dropped.

    @brief Unknown activity keeps both.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    conn.execute("DELETE FROM config_defaults")
    assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]

Activity · 4 steps
lines 101–101from clew.macro_refs import macro_definitionslines 103–103conn = _macro_db(tmp_path)lines 104–104conn.execute("DELETE FROM config_defaults")lines 105–105assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]

test_conflicting_or_test_only_states_decide_nothing

None test_gate_polarity.test_conflicting_or_test_only_states_decide_nothing(tmp_path)

Disagreeing or test-only states are unknown, so both definitions are kept.

tests/test_gate_polarity.py:124

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], "off vs on: undecided"
  • assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], ("a test config decides nothing" )

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-013measured
Source · 27 lines
def test_conflicting_or_test_only_states_decide_nothing(tmp_path) -> None:
    """MEASURED ON MBEDTLS: `config_defaults` holds rows from alternative profiles, the
    `config_adjust_*` headers and test configs — 92 macros with conflicting states, and
    `MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP` "on" from a test-driver config alone. Taking the last row
    dropped definitions the default build compiles. A symbol is decided only when every row
    outside the test files agrees.

    @brief Disagreeing or test-only states are unknown, so both definitions are kept.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    conn.execute(
        "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'profile.h', 1, '', 'source')"
    )
    assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], "off vs on: undecided"
    conn.execute("DELETE FROM config_defaults")
    conn.executescript(
        "INSERT INTO path VALUES (9, 'tests/cfg.h');"
        "CREATE TABLE test_scope (path_rowid INTEGER PRIMARY KEY); INSERT INTO test_scope VALUES (9);"
        "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'tests/cfg.h', 1, '', 'source');"
    )
    assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], (
        "a test config decides nothing"
    )

Activity · 7 steps
lines 133–133from clew.macro_refs import macro_definitionslines 135–135conn = _macro_db(tmp_path)lines 136–138conn.execute( "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'profile.h', 1, '', 'source')" )lines 139–139assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], "off vs on: undecided"lines 140–140conn.execute("DELETE FROM config_defaults")lines 141–145conn.executescript( "INSERT INTO path VALUES (9, 'tests/cfg.h');" "CREATE TABLE test_scope (path_rowid INTEGER PRIMARY KEY); INSERT INTO test_scope VALUES (9);" "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'tests/cfg.h', 1, '', 'source');" )lines 146–148assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11], ( "a test config decides nothing" )

test_the_build_s_test_patterns_apply_before_test_scope_exists

None test_gate_polarity.test_the_build_s_test_patterns_apply_before_test_scope_exists(tmp_path)

Patterns, not the table, at build time.

tests/test_gate_polarity.py:151

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert sorted(macro_definitions(conn, ["tests/*"])["mem_calloc"]) == [10, 11]
  • assert macro_definitions(conn)["mem_calloc"] == [11], "no patterns, no table: the row decides"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-013measured
Source · 15 lines
def test_the_build_s_test_patterns_apply_before_test_scope_exists(tmp_path) -> None:
    """The macro stage runs before `test_scope` is marked, so the build passes its patterns.
    Measured on mbedtls: with only the table consulted, a test-driver config still decided
    `MBEDTLS_PSA_ACCEL_ALG_RSA_OAEP` at build time and dropped definitions of MBEDTLS_RSA_C.

    @brief Patterns, not the table, at build time.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    conn.execute("UPDATE config_defaults SET file_path = 'tests/cfg.h'")
    assert sorted(macro_definitions(conn, ["tests/*"])["mem_calloc"]) == [10, 11]
    assert macro_definitions(conn)["mem_calloc"] == [11], "no patterns, no table: the row decides"

Activity · 5 steps
lines 158–158from clew.macro_refs import macro_definitionslines 160–160conn = _macro_db(tmp_path)lines 161–161conn.execute("UPDATE config_defaults SET file_path = 'tests/cfg.h'")lines 162–162assert sorted(macro_definitions(conn, ["tests/*"])["mem_calloc"]) == [10, 11]lines 163–163assert macro_definitions(conn)["mem_calloc"] == [11], "no patterns, no table: the row decides"

test_the_shipped_header_decides_over_an_alternative_profile

None test_gate_polarity.test_the_shipped_header_decides_over_an_alternative_profile(tmp_path)

A configs/ profile does not outvote the shipped header.

tests/test_gate_polarity.py:185

Show on the map →

Called by none

Calls _macro_db

Checks

  • assert macro_definitions(conn)["mem_calloc"] == [11], "shipped off wins over a configs/ on"

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-013measured
Source · 17 lines
def test_the_shipped_header_decides_over_an_alternative_profile(tmp_path) -> None:
    """THE QUERY LAYER'S RULE, AT BUILD TIME TOO (2.0.0, T5 served). `query.kconfig._with_defaults`
    lets the shipped header settle a disagreement with an alternative profile, so a dossier said
    MBEDTLS_PLATFORM_MEMORY is off while `macro_refs`, deciding by unanimity alone, kept both
    `mbedtls_calloc` definitions. One rule now serves both (`query._common.decided_default`).

    @brief A configs/ profile does not outvote the shipped header.
    """
    from clew.macro_refs import macro_definitions

    conn = _macro_db(tmp_path)
    conn.execute(
        "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'configs/profile.h', 1, '', 'heuristic')"
    )
    assert macro_definitions(conn)["mem_calloc"] == [11], "shipped off wins over a configs/ on"

Activity · 4 steps
lines 193–193from clew.macro_refs import macro_definitionslines 195–195conn = _macro_db(tmp_path)lines 196–198conn.execute( "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'configs/profile.h', 1, '', 'heuristic')" )lines 199–199assert macro_definitions(conn)["mem_calloc"] == [11], "shipped off wins over a configs/ on"

test_two_shipped_headers_that_disagree_decide_nothing_on_either_side

None test_gate_polarity.test_two_shipped_headers_that_disagree_decide_nothing_on_either_side(tmp_path)

Disagreement within a tier is undecided, at build time and at query time.

tests/test_gate_polarity.py:202

Show on the map →

Called by none

Calls _macro_db, _with_defaults

Checks

  • assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]
  • assert served.default_state == "" and served.in_default_build is None

Result: passed

Verifies

requirementlinkedpath to the implementer
REQ-DDB-PIPE-003measured
REQ-DDB-QUERY-003measuredtest_two_shipped_headers_that_disagree_decide_nothing_on_either_side → _with_defaults → table_exists
REQ-DDB-QUERY-004inferredtest_two_shipped_headers_that_disagree_decide_nothing_on_either_side → _with_defaults → _in_default_build
REQ-DDB-QUERY-013measuredtest_two_shipped_headers_that_disagree_decide_nothing_on_either_side → _with_defaults → decided_default
Source · 20 lines
def test_two_shipped_headers_that_disagree_decide_nothing_on_either_side(tmp_path) -> None:
    """NO LAST-ROW-WINS ANYWHERE. Both sides used to differ here: the query layer kept whichever
    shipped row it met last, the build kept both definitions. Now both say undecided.

    @brief Disagreement within a tier is undecided, at build time and at query time.
    """
    from clew.macro_refs import macro_definitions
    from clew.query.kconfig import _with_defaults
    from clew.query.models import KconfigGate

    conn = _macro_db(tmp_path)
    conn.execute(
        "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'include/adjust.h', 1, '', 'heuristic')"
    )
    assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]
    gate = KconfigGate(symbol="MEM", macro="MEM", form="ifdef", file_path="platform.h", line=1)
    (served,) = _with_defaults(conn, (gate,))
    assert served.default_state == "" and served.in_default_build is None

Activity · 9 steps
lines 208–208from clew.macro_refs import macro_definitionslines 209–209from clew.query.kconfig import _with_defaultslines 210–210from clew.query.models import KconfigGatelines 212–212conn = _macro_db(tmp_path)lines 213–215conn.execute( "INSERT INTO config_defaults VALUES ('MEM', 'on', '', 'include/adjust.h', 1, '', 'heuristic')" )lines 216–216assert sorted(macro_definitions(conn)["mem_calloc"]) == [10, 11]lines 217–217gate = KconfigGate(symbol="MEM", macro="MEM", form="ifdef", file_path="platform.h", line=1)lines 218–218(served,) = _with_defaults(conn, (gate,))lines 219–219assert served.default_state == "" and served.in_default_build is None