Ejemplo n.º 1
0
 def setUp(self):
     self.f = GRSpec(env_vars={"x"}, sys_vars={"y"},
                     env_init=["x"], sys_safety=["y"],
                     env_prog=["!x", "x"], sys_prog=["y&&!x"])
     self.triv = GRSpec(env_vars=["x"], sys_vars=["y"],
                        env_init=["x && !x"])
     self.empty = GRSpec()
Ejemplo n.º 2
0
def test_str_to_int():
    x = "a' = \"hehe\""
    s = GRSpec(sys_vars={'a': ['hehe', 'haha']}, sys_safety=[x])
    s.str_to_int()
    assert x in s._ast
    assert x in s._bool_int
    print(s._bool_int[x])
    assert s._bool_int[x] == "( ( X a ) = 0 )"
Ejemplo n.º 3
0
def test_str_to_int():
    x = "a' = \"hehe\""
    s = GRSpec(sys_vars={'a': ['hehe', 'haha']},
               sys_safety=[x])
    s.str_to_int()
    assert x in s._ast
    assert x in s._bool_int
    print(s._bool_int[x])
    assert s._bool_int[x] == "( ( X a ) = 0 )"
Ejemplo n.º 4
0
def test_declare_str_vars():
    # declaring string-valued variables
    g = GRSpec()
    # neither int nor str values
    with nt.assert_raises(TypeError):
        g.declare(i=(0, 10, 'wrong'))
    d = dict(name=['a', 'b', 'c', 'w'])
    g.declare(**d)
    assert g.sys_vars == d, g.sys_vars
    assert g.env_vars == dict(), g.env_vars
Ejemplo n.º 5
0
class GRSpec_test(object):
    def setUp(self):
        self.f = GRSpec(env_vars={"x"},
                        sys_vars={"y"},
                        env_init=["x"],
                        sys_safety=["y"],
                        env_prog=["!x", "x"],
                        sys_prog=["y&&!x"])
        self.triv = GRSpec(env_vars=["x"],
                           sys_vars=["y"],
                           env_init=["x && !x"])
        self.empty = GRSpec()

    def tearDown(self):
        self.f = None

    # def test_sym_to_prop(self):
    #    original_env_vars = copy.copy(self.f.env_vars)
    #    original_sys_vars = copy.copy(self.f.sys_vars)
    #    self.f.sym_to_prop({"x":"bar", "y":"uber||cat"})
    #    assert self.f.env_vars == original_env_vars and self.f.sys_vars == original_sys_vars
    #    assert self.f.env_prog == ["!(bar)", "(bar)"] and self.f.sys_prog == ["(uber||cat)&&!(bar)"]

    def test_or(self):
        g = GRSpec(env_vars={"z"}, env_prog=["!z"])
        h = self.f | g
        assert len(h.env_vars) == 2 and 'z' in h.env_vars
        assert (len(h.env_prog) == len(self.f.env_prog) + 1
                and '!z' in h.env_prog)

        # Domain mismatch on system variable y
        g.sys_vars = {"y": (0, 5)}
        nt.assert_raises(ValueError, self.f.__or__, g)

        # Domain mismatch on environment variable x
        g.sys_vars = dict()
        g.env_vars["x"] = (0, 3)
        nt.assert_raises(ValueError, self.f.__or__, g)

    def test_to_canon(self):
        # Fragile!
        assert (self.f.to_canon() ==
                "((x) && []<>(!x) && []<>(x)) -> ([](y) && []<>(y&&!x))")
        # N.B., for self.triv, to_canon() returns a formula missing
        # the assumption part not because it detected that the
        # assumption is false, but rather the guarantee is empty (and
        # thus interpreted as being "True").
        assert self.triv.to_canon() == "True"
        assert self.empty.to_canon() == "True"

    def test_init(self):
        assert len(self.f.env_vars) == 1 and len(self.f.sys_vars) == 1
        assert (self.f.env_vars["x"] == "boolean"
                and self.f.sys_vars["y"] == "boolean")
Ejemplo n.º 6
0
class GRSpec_test:
    def setUp(self):
        self.f = GRSpec(env_vars={"x"}, sys_vars={"y"},
                        env_init=["x"], sys_safety=["y"],
                        env_prog=["!x", "x"], sys_prog=["y&&!x"])
        self.triv = GRSpec(env_vars=["x"], sys_vars=["y"],
                           env_init=["x && !x"])
        self.empty = GRSpec()

    def tearDown(self):
        self.f = None

    # def test_sym_to_prop(self):
    #    original_env_vars = copy.copy(self.f.env_vars)
    #    original_sys_vars = copy.copy(self.f.sys_vars)
    #    self.f.sym_to_prop({"x":"bar", "y":"uber||cat"})
    #    assert self.f.env_vars == original_env_vars and self.f.sys_vars == original_sys_vars
    #    assert self.f.env_prog == ["!(bar)", "(bar)"] and self.f.sys_prog == ["(uber||cat)&&!(bar)"]

    def test_or(self):
        g = GRSpec(env_vars={"z"}, env_prog=["!z"])
        h = self.f | g
        assert len(h.env_vars) == 2 and 'z' in h.env_vars
        assert (
            len(h.env_prog) == len(self.f.env_prog) + 1 and
            '!z' in h.env_prog)

        # Domain mismatch on system variable y
        g.sys_vars = {"y": (0, 5)}
        nt.assert_raises(ValueError, self.f.__or__, g)

        # Domain mismatch on environment variable x
        g.sys_vars = dict()
        g.env_vars["x"] = (0, 3)
        nt.assert_raises(ValueError, self.f.__or__, g)

    def test_to_canon(self):
        # Fragile!
        assert (self.f.to_canon() ==
                "((x) && []<>(!x) && []<>(x)) -> ([](y) && []<>(y&&!x))")
        # N.B., for self.triv, to_canon() returns a formula missing
        # the assumption part not because it detected that the
        # assumption is false, but rather the guarantee is empty (and
        # thus interpreted as being "True").
        assert self.triv.to_canon() == "True"
        assert self.empty.to_canon() == "True"

    def test_init(self):
        assert len(self.f.env_vars) == 1 and len(self.f.sys_vars) == 1
        assert (self.f.env_vars["x"] == "boolean" and
                self.f.sys_vars["y"] == "boolean")
Ejemplo n.º 7
0
def test_compile_init():
    env_vars = {'x': (0, 0), 'y': (0, 0), 'z': (0, 1)}
    sys_vars = {'w': (0, 0)}
    env_init = ['((((y = 0))) & (x = 0))']
    sys_init = ['((w = 0))']
    spc = GRSpec(
        env_vars=env_vars, sys_vars=sys_vars,
        env_init=env_init, sys_init=sys_init)
    code = spc.compile_init(no_str=True)
    d = dict(x=0, y=0, z=0, w=0)
    assert eval(code, d)
    d = dict(x=0, y=1, z=1, w=0)
    assert eval(code, d)
    d = dict(x=0, y=0, z=0, w=1)
    assert not eval(code, d)
Ejemplo n.º 8
0
def test_declare_boolean_vars():
    # declaring Boolean-valued variables
    g = GRSpec()
    g.declare('a')
    assert g.sys_vars == dict(a='boolean'), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare('a', 'b')
    assert g.sys_vars == dict(a='boolean', b='boolean'), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare('c', env=True)
    assert g.sys_vars == dict(a='boolean', b='boolean'), g.sys_vars
    assert g.env_vars == dict(c='boolean'), g.env_vars
    # attempt to redeclare "c" as sys var
    with pytest.raises(AssertionError):
        g.declare('c')
Ejemplo n.º 9
0
    def test_or(self):
        g = GRSpec(env_vars={"z"}, env_prog=["!z"])
        h = self.f | g
        assert len(h.env_vars) == 2 and 'z' in h.env_vars
        assert (len(h.env_prog) == len(self.f.env_prog) + 1
                and '!z' in h.env_prog)

        # Domain mismatch on system variable y
        g.sys_vars = {"y": (0, 5)}
        nt.assert_raises(ValueError, self.f.__or__, g)

        # Domain mismatch on environment variable x
        g.sys_vars = dict()
        g.env_vars["x"] = (0, 3)
        nt.assert_raises(ValueError, self.f.__or__, g)
Ejemplo n.º 10
0
def test_compile_init():
    env_vars = {'x': (0, 0), 'y': (0, 0), 'z': (0, 1)}
    sys_vars = {'w': (0, 0)}
    env_init = ['((((y = 0))) & (x = 0))']
    sys_init = ['((w = 0))']
    spc = GRSpec(env_vars=env_vars,
                 sys_vars=sys_vars,
                 env_init=env_init,
                 sys_init=sys_init)
    code = spc.compile_init(no_str=True)
    d = dict(x=0, y=0, z=0, w=0)
    assert eval(code, d)
    d = dict(x=0, y=1, z=1, w=0)
    assert eval(code, d)
    d = dict(x=0, y=0, z=0, w=1)
    assert not eval(code, d)
Ejemplo n.º 11
0
    def test_or(self):
        g = GRSpec(env_vars={"z"}, env_prog=["!z"])
        h = self.f | g
        assert len(h.env_vars) == 2 and 'z' in h.env_vars
        assert (
            len(h.env_prog) == len(self.f.env_prog) + 1 and
            '!z' in h.env_prog)

        # Domain mismatch on system variable y
        g.sys_vars = {"y": (0, 5)}
        nt.assert_raises(ValueError, self.f.__or__, g)

        # Domain mismatch on environment variable x
        g.sys_vars = dict()
        g.env_vars["x"] = (0, 3)
        nt.assert_raises(ValueError, self.f.__or__, g)
Ejemplo n.º 12
0
def test_GRSpec():
    f = GRSpec(env_vars={'a'},
               sys_vars={'b'},
               env_init=['!a'],
               sys_init=['!b'],
               env_safety=['a -> X !a', '!a -> X a'],
               sys_prog=['a && b'])
    M = lily.synthesize(f)
    assert M is not None
Ejemplo n.º 13
0
def test_replace_dependent_vars():
    sys_vars = {'a': 'boolean', 'locA': (0, 4)}
    sys_safe = ['!a', 'a & (locA = 3)']
    spc = GRSpec(sys_vars=sys_vars, sys_safety=sys_safe)
    bool2form = {'a': '(locA = 0) | (locA = 2)', 'b': '(locA = 1)'}
    replace_dependent_vars(spc, bool2form)
    correct_result = ('[](( ! ( ( locA = 0 ) | ( locA = 2 ) ) )) && '
                      '[](( ( ( locA = 0 ) | ( locA = 2 ) ) & ( locA = 3 ) ))')
    assert str(spc) == correct_result
Ejemplo n.º 14
0
def test_declare_boolean_vars():
    # declaring Boolean-valued variables
    g = GRSpec()
    g.declare('a')
    assert g.sys_vars == dict(a='boolean'), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare('a', 'b')
    assert g.sys_vars == dict(a='boolean', b='boolean'), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare('c', env=True)
    assert g.sys_vars == dict(a='boolean', b='boolean'), g.sys_vars
    assert g.env_vars == dict(c='boolean'), g.env_vars
    # attempt to redeclare "c" as sys var
    with nt.assert_raises(AssertionError):
        g.declare('c')
Ejemplo n.º 15
0
def test_declare_int_vars():
    # declaring integer-valued variables
    g = GRSpec()
    g.declare(i=[0, 10])
    assert g.sys_vars == dict(i=(0, 10)), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare(j=(-5, 14), env=True)
    assert g.sys_vars == dict(i=(0, 10)), g.sys_vars
    assert g.env_vars == dict(j=(-5, 14)), g.env_vars
    # attempt to redeclare "i" as env var
    with pytest.raises(AssertionError):
        g.declare(i=(0, 10), env=True)
Ejemplo n.º 16
0
def test_declare_int_vars():
    # declaring integer-valued variables
    g = GRSpec()
    g.declare(i=[0, 10])
    assert g.sys_vars == dict(i=(0, 10)), g.sys_vars
    assert g.env_vars == dict(), g.env_vars
    g.declare(j=(-5, 14), env=True)
    assert g.sys_vars == dict(i=(0, 10)), g.sys_vars
    assert g.env_vars == dict(j=(-5, 14)), g.env_vars
    # attempt to redeclare "i" as env var
    with nt.assert_raises(AssertionError):
        g.declare(i=(0, 10), env=True)
Ejemplo n.º 17
0
def test_declare_str_vars():
    # declaring string-valued variables
    g = GRSpec()
    # neither int nor str values
    with pytest.raises(TypeError):
        g.declare(i=(0, 10, 'wrong'))
    d = dict(name=['a', 'b', 'c', 'w'])
    g.declare(**d)
    assert g.sys_vars == d, g.sys_vars
    assert g.env_vars == dict(), g.env_vars
Ejemplo n.º 18
0
 def setUp(self):
     self.f = GRSpec(env_vars={"x"},
                     sys_vars={"y"},
                     env_init=["x"],
                     sys_safety=["y"],
                     env_prog=["!x", "x"],
                     sys_prog=["y&&!x"])
     self.triv = GRSpec(env_vars=["x"],
                        sys_vars=["y"],
                        env_init=["x && !x"])
     self.empty = GRSpec()