aboutsummaryrefslogtreecommitdiffstats
path: root/psp_emu/tests/test_oracle.py
blob: d2f4132a70792d06fe9515177e98482183d1ae79 (plain) (blame)
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
#!/usr/bin/env python3
"""The Probe abstraction must refuse exactly the failures that fooled us."""
import sys, os
sys.path.insert(0, os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from pspemu.oracle import Probe, Result, NotReportable


def test_blind_instrument_is_refused():
    """The flat-oracle / dead-detector shape: sees nothing, reports 'clean'."""
    p = Probe("zero hits", measure=lambda: 0, positive=lambda: False)
    r = p.run()
    assert not r.ok and "blind" in r.why
    try:
        r.get(); raise AssertionError("a blind result must not be readable")
    except NotReportable:
        pass
    print("  blind instrument refused, and .get() raises")


def test_indiscriminate_instrument_is_refused():
    """The byteswap-echo shape: fires on everything, so a hit means nothing."""
    p = Probe("hit", measure=lambda: 1, positive=lambda: True, negative=lambda: True)
    r = p.run()
    assert not r.ok and "indiscriminate" in r.why
    print("  indiscriminate instrument refused")


def test_good_probe_reports():
    p = Probe("ok", measure=lambda: 42, positive=lambda: True, negative=lambda: False)
    assert p.run().get() == 42
    print("  well-controlled probe reports its value")


def test_report_exit_codes():
    good = Probe("g", lambda: 1, lambda: True, lambda: False)
    blind = Probe("b", lambda: 0, lambda: False)
    assert good.report() == 0 and blind.report() == 1
    print("  report() exit codes: 0 reportable, 1 not")


if __name__ == "__main__":
    fails = 0
    for fn in (test_blind_instrument_is_refused, test_indiscriminate_instrument_is_refused,
               test_good_probe_reports, test_report_exit_codes):
        print(fn.__name__)
        try: fn(); print("   PASS")
        except Exception as e: fails += 1; print("   FAIL: %s" % e)
    sys.exit(1 if fails else 0)