Esempio n. 1
0
 def testMidPoint(self):
     p = cfg.Program()
     x = p.NewVariable()
     y = p.NewVariable()
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x1 = x.AddBinding("1", source_set=[], where=n1)
     y1 = y.AddBinding("1", source_set=[x1], where=n1)
     n2 = n1.ConnectNew("n2")
     x2 = x.AddBinding("2", source_set=[], where=n2)
     n3 = n2.ConnectNew("n3")
     self.assertTrue(n3.HasCombination([y1, x2]))
     self.assertTrue(n3.HasCombination([x2, y1]))
Esempio n. 2
0
    def testFilter1(self):
        #                    x.ab = A()
        #               ,---+------------.
        #               |   n3           |
        #  x = X()      |    x.ab = B()  |
        #  +------------+---+------------+------------+
        #  n1           n2  n4           n5           n6
        p = cfg.Program()
        n1 = p.NewCFGNode("n1")
        n2 = n1.ConnectNew("n2")
        n3 = n2.ConnectNew("n3")
        n4 = n2.ConnectNew("n4")
        n5 = n3.ConnectNew("n5")
        n4.ConnectTo(n5)
        n6 = n5.ConnectNew("n6")
        n5.ConnectTo(n6)

        all_x = p.NewVariable()
        x = all_x.AddBinding({}, source_set=[], where=n1)
        ab = p.NewVariable()
        x.data["ab"] = ab
        a = ab.AddBinding("A", source_set=[], where=n3)
        b = ab.AddBinding("B", source_set=[], where=n4)

        p.entrypoint = n1
        self.assertFalse(a.IsVisible(n1) or b.IsVisible(n1))
        self.assertFalse(a.IsVisible(n2) or b.IsVisible(n2))
        self.assertTrue(a.IsVisible(n3))
        self.assertTrue(b.IsVisible(n4))
        self.assertEquals(ab.Filter(n1), [])
        self.assertEquals(ab.Filter(n2), [])
        self.assertEquals(ab.FilteredData(n3), ["A"])
        self.assertEquals(ab.FilteredData(n4), ["B"])
        self.assertSameElements(["A", "B"], ab.FilteredData(n5))
        self.assertSameElements(["A", "B"], ab.FilteredData(n6))
Esempio n. 3
0
 def testMergeOneVariable(self):
     p = cfg.Program()
     n0 = p.NewCFGNode("n0")
     u = p.NewVariable([0], [], n0)
     self.assertIs(p.MergeVariables(n0, [u]), u)
     self.assertIs(p.MergeVariables(n0, [u, u]), u)
     self.assertIs(p.MergeVariables(n0, [u, u, u]), u)
Esempio n. 4
0
 def testProgramFreeze(self):
     p = cfg.Program()
     n = p.NewCFGNode("n")
     self._Freeze(p, entrypoint=n)
     self.assertRaises(AssertionError, p.NewCFGNode)
     self.assertRaises(AssertionError, p.NewCFGNode, "named")
     self.assertRaises(AssertionError, p.NewCFGNode, name="named")
Esempio n. 5
0
 def testFindNodeBackwards(self):
     # +-->n2--.       +--->n6--.
     # |   c3  v       |    c3  v
     # n1      n4 --> n5<---+   n8
     # |       ^c1   c2|    |   ^
     # +-->n3--'       +--->n7--'
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     c1 = p.NewVariable().AddBinding("1", source_set=[], where=n1)
     c2 = p.NewVariable().AddBinding("2", source_set=[], where=n1)
     c3 = p.NewVariable().AddBinding("3", source_set=[], where=n1)
     n2 = n1.ConnectNew("n2", c3)
     n3 = n1.ConnectNew("n3")
     n4 = p.NewCFGNode("n4", c1)
     n2.ConnectTo(n4)
     n3.ConnectTo(n4)
     n5 = n4.ConnectNew("n5", c2)
     n6 = n5.ConnectNew("n6", c3)
     n7 = n5.ConnectNew("n7")
     n7.ConnectTo(n5)
     n8 = p.NewCFGNode("n8")
     n6.ConnectTo(n8)
     n7.ConnectTo(n8)
     f = p.CreateSolver()._path_finder
     self.assertEqual((True, [n5, n4]), f.FindNodeBackwards(n8, n1, ()))
     self.assertFalse(f.FindNodeBackwards(n8, n1, (n4, ))[0])
     self.assertEqual((True, [n5]), f.FindNodeBackwards(n8, n5, ()))
     self.assertEqual((True, [n5, n4]), f.FindNodeBackwards(n5, n4, ()))
     self.assertEqual((True, [n5, n4, n2]), f.FindNodeBackwards(n5, n2, ()))
     self.assertEqual((True, [n5, n4]), f.FindNodeBackwards(n5, n3, ()))
Esempio n. 6
0
 def testConditionsAreOrdered(self):
     # The error case in this test is non-deterministic. The test tries to verify
     # that the list returned by _PathFinder.FindNodeBackwards is ordered from
     # child to parent.
     # The error case would be a random order or the reverse order.
     # To guarantee that this test is working go to FindNodeBackwards and reverse
     # the order of self._on_path before generating the returned list.
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x1 = p.NewVariable().AddBinding("1", source_set=[], where=n1)
     n2 = n1.ConnectNew("n2",
                        condition=p.NewVariable().AddBinding("1",
                                                             source_set=[],
                                                             where=n1))
     n3 = n2.ConnectNew("n3",
                        condition=p.NewVariable().AddBinding("1",
                                                             source_set=[],
                                                             where=n2))
     n4 = n3.ConnectNew("n3",
                        condition=p.NewVariable().AddBinding("1",
                                                             source_set=[],
                                                             where=n3))
     # Strictly speaking n1, n2 and n3 would be enough to expose errors. n4 is
     # added to increase the chance of a failure if the order is random.
     self.assertTrue(n4.HasCombination([x1]))
Esempio n. 7
0
 def setUp(self):
   # n1------->n2
   #  |        |
   #  v        v
   # n3------->n4
   # [n2] x = a; y = a
   # [n3] x = b; y = b
   # [n4] z = x & y
   self.p = typegraph.Program()
   self.n1 = self.p.NewCFGNode("n1")
   self.n2 = self.n1.ConnectNew("n2")
   self.n3 = self.n1.ConnectNew("n3")
   self.n4 = self.n2.ConnectNew("n4")
   self.n3.ConnectTo(self.n4)
   self.x = self.p.NewVariable("x")
   self.y = self.p.NewVariable("y")
   self.z = self.p.NewVariable("z")
   self.w = self.p.NewVariable("w")
   self.xa = self.x.AddValue("a", source_set=[], where=self.n2)
   self.ya = self.y.AddValue("a", source_set=[], where=self.n2)
   self.xb = self.x.AddValue("b", source_set=[], where=self.n3)
   self.yb = self.y.AddValue("b", source_set=[], where=self.n3)
   self.za = self.z.AddValue("a", source_set=[self.xa, self.ya], where=self.n4)
   self.zb = self.z.AddValue("b", source_set=[self.xb, self.yb], where=self.n4)
   self.zab = self.z.AddValue("a&b")
   self.zab.AddOrigin(source_set=[self.xa, self.yb], where=self.n4)
   self.zab.AddOrigin(source_set=[self.xb, self.ya], where=self.n4)
   self.p.entrypoint = self.n1
   self.p.Freeze()
Esempio n. 8
0
 def testVariableSet(self):
     p = cfg.Program()
     node1 = p.NewCFGNode()
     node2 = node1.ConnectNew()
     d = p.NewVariable("d")
     d.AddValue("v1", source_set=[], where=node1)
     d.AddValue("v2", source_set=[], where=node2)
     self.assertEquals(len(d.values), 2)
Esempio n. 9
0
 def testBindingPrettyPrint(self):
     p = cfg.Program()
     node = p.NewCFGNode()
     u = p.NewVariable()
     v1 = u.AddBinding(1, source_set=[], where=node)
     v2 = u.AddBinding(2, source_set=[v1], where=node)
     v3 = u.AddBinding(3, source_set=[v2], where=node)
     v3.PrettyPrint()  # smoke test
Esempio n. 10
0
 def testPasteBinding(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x = p.NewVariable()
     ax = x.AddBinding("a", source_set=[], where=n1)
     y = p.NewVariable()
     y.PasteBinding(ax)
     self.assertEqual(x.data, y.data)
Esempio n. 11
0
 def testVariableSet(self):
     p = cfg.Program()
     node1 = p.NewCFGNode("n1")
     node2 = node1.ConnectNew("n2")
     d = p.NewVariable()
     d.AddBinding("v1", source_set=[], where=node1)
     d.AddBinding("v2", source_set=[], where=node2)
     self.assertEquals(len(d.bindings), 2)
Esempio n. 12
0
 def testEntryPoint(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     x = p.NewVariable("x")
     a = x.AddBinding("a", source_set=[], where=n1)
     a = x.AddBinding("b", source_set=[], where=n2)
     self._Freeze(p, entrypoint=n1)
     self.assertTrue(n2.HasCombination([a]))
Esempio n. 13
0
 def testNewVariable(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = p.NewCFGNode("n2")
     x, y, z = "x", "y", "z"
     variable = p.NewVariable(bindings=[x, y], source_set=[], where=n1)
     variable.AddBinding(z, source_set=variable.bindings, where=n2)
     self.assertSameElements([x, y, z], [v.data for v in variable.bindings])
     self.assertTrue(any(len(e.origins) for e in variable.bindings))
Esempio n. 14
0
 def testNewVariable(self):
     p = cfg.Program()
     n1 = p.NewCFGNode()
     n2 = p.NewCFGNode()
     x, y, z = "x", "y", "z"
     variable = p.NewVariable("xyz", values=[x, y], source_set=[], where=n1)
     variable.AddValue(z, source_set=variable.values, where=n2)
     self.assertSameElements([x, y, z], [v.data for v in variable.values])
     self.assertTrue(any(len(e.origins) for e in variable.values))
Esempio n. 15
0
 def testNonFrozenSolving(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     x = p.NewVariable()
     a = x.AddBinding("a", source_set=[], where=n1)
     a = x.AddBinding("b", source_set=[], where=n2)
     p.entrypoint = n1
     self.assertTrue(n2.HasCombination([a]))
Esempio n. 16
0
 def testValueValue(self):
     p = cfg.Program()
     node = p.NewCFGNode()
     u = p.NewVariable("v")
     v1 = u.AddValue(None, source_set=[], where=node)
     v2 = u.AddValue(u"data", source_set=[], where=node)
     v3 = u.AddValue({1: 2}, source_set=[], where=node)
     self.assertEquals(v1.data, None)
     self.assertEquals(v2.data, u"data")
     self.assertEquals(v3.data, {1: 2})
Esempio n. 17
0
 def testConditionOnStartNode(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     n3 = p.NewCFGNode("n3")
     a = p.NewVariable().AddBinding("a", source_set=[], where=n3)
     b = p.NewVariable().AddBinding("b", source_set=[], where=n1)
     n2.condition = a
     self.assertFalse(n2.HasCombination([b]))
     self.assertTrue(n1.HasCombination([b]))
Esempio n. 18
0
 def testUnsatisfiableCondition(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x = p.NewVariable()
     x1 = x.AddBinding("1", source_set=[], where=n1)
     n2 = n1.ConnectNew("n2")
     x2 = x.AddBinding("2", source_set=[], where=n2)
     n3 = n2.ConnectNew("n3", condition=x2)
     n4 = n3.ConnectNew("n4")
     self.assertFalse(n4.HasCombination([x1]))
Esempio n. 19
0
 def testFilter2(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = p.NewCFGNode("n2")
     n1.ConnectTo(n2)
     x = p.NewVariable("x")
     a = x.AddValue("a", source_set=[], where=n2)
     self._Freeze(p, entrypoint=n1)
     self.assertEquals(x.Filter(n1), [])
     self.assertEquals(x.Filter(n2), [a])
Esempio n. 20
0
 def testAsciiTree(self):
     p = cfg.Program()
     node1 = p.NewCFGNode("n1")
     node2 = node1.ConnectNew("n2")
     node3 = node2.ConnectNew("n3")
     _ = node3.ConnectNew()
     # Just check sanity. Actual verification of the drawing algorithm is
     # done in utils_test.py.
     self.assertIsInstance(node1.AsciiTree(), str)
     self.assertIsInstance(node1.AsciiTree(forward=True), str)
Esempio n. 21
0
 def testMergeBindings(self):
     p = cfg.Program()
     n0 = p.NewCFGNode("n0")
     u = p.NewVariable()
     u1 = u.AddBinding("1", source_set=[], where=n0)
     v2 = u.AddBinding("2", source_set=[], where=n0)
     w1 = p.MergeBindings(None, [u1, v2])
     w2 = p.MergeBindings(n0, [u1, v2])
     self.assertItemsEqual(w1.data, ["1", "2"])
     self.assertItemsEqual(w2.data, ["1", "2"])
Esempio n. 22
0
 def testFilter2(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = p.NewCFGNode("n2")
     n1.ConnectTo(n2)
     x = p.NewVariable()
     a = x.AddBinding("a", source_set=[], where=n2)
     p.entrypoint = n1
     self.assertEquals(x.Filter(n1), [])
     self.assertEquals(x.Filter(n2), [a])
Esempio n. 23
0
 def testLoop(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     n2.ConnectTo(n1)
     x = p.NewVariable()
     a = x.AddBinding("a")
     b = x.AddBinding("b")
     a.AddOrigin(n1, [b])
     b.AddOrigin(n2, [a])
     self.assertTrue(n2.HasCombination([b]))
Esempio n. 24
0
 def testHiddenConflict2(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     x = p.NewVariable()
     y = p.NewVariable()
     x_a = x.AddBinding("a", source_set=[], where=n1)
     x_b = x.AddBinding("b", source_set=[], where=n1)
     y_b = y.AddBinding("b", source_set=[x_b], where=n1)
     p.entrypoint = n1
     self.assertFalse(n2.HasCombination([y_b, x_a]))
Esempio n. 25
0
 def testAssignToNewNoNode(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x = p.NewVariable()
     ax = x.AddBinding("a", source_set=[], where=n1)
     y = ax.AssignToNewVariable()
     z = x.AssignToNewVariable()
     ox, = x.bindings[0].origins
     oy, = y.bindings[0].origins
     oz, = z.bindings[0].origins
     self.assertEqual(ox, oy, oz)
Esempio n. 26
0
 def testBindingBinding(self):
     p = cfg.Program()
     node = p.NewCFGNode()
     u = p.NewVariable("v")
     v1 = u.AddBinding(None, source_set=[], where=node)
     v2 = u.AddBinding(u"data", source_set=[], where=node)
     v3 = u.AddBinding({1: 2}, source_set=[], where=node)
     self.assertEquals(v1.data, None)
     self.assertEquals(v2.data, u"data")
     self.assertEquals(v3.data, {1: 2})
     self.assertEquals("$0=#%d" % id(v3.data), str(v3))
Esempio n. 27
0
 def testConflictingBindingsFromCondition(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = n1.ConnectNew("n2")
     n3 = n2.ConnectNew("n3")
     x = p.NewVariable()
     x_a = x.AddBinding("a", source_set=[], where=n1)
     x_b = x.AddBinding("b", source_set=[], where=n1)
     p.entrypoint = n1
     n2.condition = x_a
     self.assertFalse(n3.HasCombination([x_b]))
Esempio n. 28
0
 def testConflicting(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     x = p.NewVariable()
     a = x.AddBinding("a", source_set=[], where=n1)
     b = x.AddBinding("b", source_set=[], where=n1)
     p.entrypoint = n1
     # At n1, x can either be a or b, but not both.
     self.assertTrue(n1.HasCombination([a]))
     self.assertTrue(n1.HasCombination([b]))
     self.assertFalse(n1.HasCombination([a, b]))
Esempio n. 29
0
 def testId(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("n1")
     n2 = p.NewCFGNode("n2")
     x = p.NewVariable()
     y = p.NewVariable()
     self.assertIsInstance(x.id, int)
     self.assertIsInstance(y.id, int)
     self.assertLess(x.id, y.id)
     self.assertIsInstance(n1.id, int)
     self.assertIsInstance(n2.id, int)
     self.assertLess(n1.id, n2.id)
Esempio n. 30
0
 def testNodeBindings(self):
     p = cfg.Program()
     n1 = p.NewCFGNode("node1")
     n2 = n1.ConnectNew("node2")
     self.assertEquals(n1.name, "node1")
     self.assertEquals(n2.name, "node2")
     u = p.NewVariable()
     a1 = u.AddBinding(1, source_set=[], where=n1)
     a2 = u.AddBinding(2, source_set=[], where=n1)
     a3 = u.AddBinding(3, source_set=[], where=n1)
     a4 = u.AddBinding(4, source_set=[], where=n1)
     self.assertSameElements([a1, a2, a3, a4], n1.bindings)