예제 #1
0
        public void CFG()
        {
            const int COUNT  = 6;
            Parser    parser = new Parser(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\cfg.gngr"));

            parser.parse();
            ASTVisitor  astv = new ASTVisitor(parser.ast);
            TestVisitor tv   = new TestVisitor(astv.cfg);

            // create new instance to fool compiler
            CFGEntry      entry = new CFGEntry();
            CFGBasicBlock b1    = new CFGBasicBlock();
            CFGBasicBlock b2    = new CFGBasicBlock();
            CFGBasicBlock b3    = new CFGBasicBlock();
            CFGBasicBlock b4    = new CFGBasicBlock();

            for (int i = 0; i < COUNT; i++)
            {
                switch (i)
                {
                case 0:
                    entry = (CFGEntry)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(entry, typeof(CFGEntry), $"{i}/1");
                    break;

                case 1:
                    b1 = (CFGBasicBlock)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(b1, typeof(CFGBasicBlock), $"{i}/1");
                    Assert.AreEqual(entry, b1.parents[0], $"{i}/1");
                    break;

                case 2:
                    b2 = (CFGBasicBlock)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(b2, typeof(CFGBasicBlock), $"{i}/1");
                    Assert.AreEqual(b1, b2.parents[0], $"{i}/2");
                    break;

                case 3:
                    b3 = (CFGBasicBlock)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(b3, typeof(CFGBasicBlock), $"{i}/1");
                    Assert.AreEqual(b2, b3.parents[0], $"{i}/2");
                    Assert.AreEqual(b1, b3.parents[1], $"{i}/3");
                    break;

                case 4:
                    b4 = (CFGBasicBlock)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(b4, typeof(CFGBasicBlock), $"{i}/1");
                    Assert.AreEqual(b3, b4.parents[0], $"{i}/2");
                    break;

                case 6:
                    CFGExit exit = (CFGExit)tv.visitedNodes[i];
                    Assert.IsInstanceOfType(exit, typeof(CFGExit), $"{i}/1");
                    Assert.AreEqual(b4, exit.parents[0], $"{i}/2");
                    Assert.AreEqual(b3, exit.parents[1], $"{i}/2");
                    break;
                }
            }
        }
예제 #2
0
        public void BlockSurroundedIf()
        {
            const int COUNT = 7;
            Parser parser = new Parser(Path.Combine(AppDomain.CurrentDomain.BaseDirectory, @"Data\BlockSurroundedIf.gngr"));
            parser.parse();
            ASTVisitor astv = new ASTVisitor(parser.ast);
            TestVisitor tv = new TestVisitor(astv.cfg);

            // create new instance to fool compiler
            CFGEntry entry = new CFGEntry();
            CFGBasicBlock b1 = new CFGBasicBlock();
            CFGBasicBlock b2 = new CFGBasicBlock();
            CFGBasicBlock b3 = new CFGBasicBlock();
            CFGBasicBlock b4 = new CFGBasicBlock();
            CFGBasicBlock b5 = new CFGBasicBlock();

            for (int i = 0; i < COUNT; i++)
            {
                switch (i)
                {
                    case 0:
                        entry = (CFGEntry)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(entry, typeof(CFGEntry), $"{i}/1");
                        break;
                    case 1:
                        b1 = (CFGBasicBlock)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(b1, typeof(CFGBasicBlock), $"{i}/1");
                        Assert.AreEqual(entry, b1.parents[0], $"{i}/1");
                        break;
                    case 2:
                        b2 = (CFGBasicBlock)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(b2, typeof(CFGBasicBlock), $"{i}/1");
                        Assert.AreEqual(b1, b2.parents[0], $"{i}/2");
                        break;
                    case 3:
                        b3 = (CFGBasicBlock)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(b3, typeof(CFGBasicBlock), $"{i}/1");
                        Assert.AreEqual(b2, b3.parents[0], $"{i}/2");
                        break;
                    case 4:
                        b4 = (CFGBasicBlock)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(b4, typeof(CFGBasicBlock), $"{i}/1");
                        Assert.AreEqual(b3, b4.parents[0], $"{i}/2");
                        Assert.AreEqual(b2, b4.parents[1], $"{i}/3");
                        break;
                    case 5:
                        b5 = (CFGBasicBlock)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(b5, typeof(CFGBasicBlock), $"{i}/1");
                        Assert.AreEqual(b4, b5.parents[0], $"{i}/2");
                        Assert.AreEqual(b1, b5.parents[1], $"{i}/3");
                        break;
                    case 6:
                        CFGExit exit = (CFGExit)tv.visitedNodes[i];
                        Assert.IsInstanceOfType(exit, typeof(CFGExit), $"{i}/1");
                        Assert.AreEqual(b5, exit.parents[0], $"{i}/2");
                        break;
                }
            }
        }