File size: 2,885 Bytes
fc3e70a
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
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
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
# This Source Code Form is subject to the terms of the Mozilla Public
# License, v. 2.0. If a copy of the MPL was not distributed with this
# file, You can obtain one at https://mozilla.org/MPL/2.0/.

"""Tests: Phase 7 — Boolean engine."""

import pytest
from semantic_engine.boolean_engine import (
    AND, OR, NOT, XOR, IMPLIES, EQUIV,
    BooleanLiteral, BooleanVar, BooleanExpression,
    var, lit, normalize, to_cnf, simplify,
    semantic_constraints_to_boolean,
)
from semantic_engine.types import BooleanOperator


def test_literal_repr():
    assert repr(BooleanLiteral(True)) == "⊤"
    assert repr(BooleanLiteral(False)) == "⊥"


def test_var_repr():
    assert repr(BooleanVar("x")) == "x"
    assert repr(BooleanVar("x", negated=True)) == "¬x"


def test_and_repr():
    e = AND(var("a"), var("b"))
    assert "∧" in repr(e)


def test_simplify_double_negation():
    e = NOT(NOT(var("x")))
    result = simplify(e)
    assert isinstance(result, BooleanVar)
    assert result.name == "x"
    assert not result.negated


def test_simplify_and_with_true():
    e = AND(BooleanLiteral(True), var("x"))
    result = simplify(e)
    assert isinstance(result, BooleanVar)
    assert result.name == "x"


def test_simplify_and_with_false():
    e = AND(BooleanLiteral(False), var("x"))
    result = simplify(e)
    assert isinstance(result, BooleanLiteral)
    assert not result.value


def test_simplify_or_with_true():
    e = OR(BooleanLiteral(True), var("x"))
    result = simplify(e)
    assert isinstance(result, BooleanLiteral)
    assert result.value


def test_implies_expansion():
    e = IMPLIES(var("a"), var("b"))
    result = simplify(e)
    assert repr(result)  # should not crash


def test_equivalence_expansion():
    e = EQUIV(var("a"), var("b"))
    result = simplify(e)
    assert repr(result)


def test_xor_expansion():
    e = XOR(var("a"), var("b"))
    result = simplify(e)
    assert repr(result)


def test_normalize_idempotent():
    e = AND(var("a"), OR(var("b"), NOT(var("c"))))
    n1 = normalize(e)
    n2 = normalize(n1)
    assert repr(n1) == repr(n2)


def test_normalize_literal_true():
    assert isinstance(normalize(BooleanLiteral(True)), BooleanLiteral)


def test_constraints_to_boolean_empty():
    bnf = semantic_constraints_to_boolean([])
    assert isinstance(bnf.normalized, BooleanLiteral)
    assert bnf.normalized.value is True


def test_constraints_to_boolean_single():
    from semantic_engine.ir import Constraint
    c = Constraint.make("deterministic", "deterministic")
    bnf = semantic_constraints_to_boolean([c])
    assert bnf.stable


def test_constraints_to_boolean_multiple():
    from semantic_engine.ir import Constraint
    cs = [Constraint.make("deterministic", "deterministic"),
          Constraint.make("immutable", "immutable")]
    bnf = semantic_constraints_to_boolean(cs)
    assert bnf.stable