示例#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()
示例#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 )"
示例#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 )"
示例#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
示例#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")
示例#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")
示例#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)
示例#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')
示例#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)
示例#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)
示例#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)
示例#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
示例#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
示例#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')
示例#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)
示例#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)
示例#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
示例#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()