Exemple #1
0
        public static bool Compare(XElement expected, XElement actual)
        {
            var expectedNodeEnumerator = new EnumeratorAdapter<XElement>(expected.DescendantsAndSelf().GetEnumerator());
            var actualNodeEnumerator = new EnumeratorAdapter<XElement>(actual.DescendantsAndSelf().GetEnumerator());
            while (expectedNodeEnumerator.HasNext && actualNodeEnumerator.HasNext)
            {
                var expectedElement = expectedNodeEnumerator.Next();
                var actualElement = actualNodeEnumerator.Next();

                if (expectedElement != null && actualElement != null)
                {
                    isMatchSuccessful = expectedElement.Name == actualElement.Name;

                    if (!isMatchSuccessful)
                    {
                        Console.Write("Expected node {0} was not present in actual for parent {1}",
                                      expectedElement.Name.LocalName, expectedElement.Parent != null ? expectedElement.Parent.Name.LocalName : null);
                        return isMatchSuccessful;
                    }

                    if (expectedElement.HasElements)
                        continue;

                    var xElementName = actualElement.Name.LocalName;
                    var xelementValue = actualElement.Value;
                    if ((!string.IsNullOrEmpty(expectedElement.Value) ||
                         !string.IsNullOrEmpty(xelementValue)) && xelementValue != expectedElement.Value)
                    {
                        if (!actualElement.HasElements)
                            Console.Write("Expected {0}:{1} but actual was {2}:{3} for parent {4}",
                                          xElementName, xelementValue,
                                          expectedElement.Name.LocalName,
                                          expectedElement.Value, expectedElement.Parent.Name.LocalName);
                        else
                            Console.Write(
                                "Expected does not contain the node {0} that is in actual for parent {1}",
                                actualElement.Elements().First().Name.LocalName, actualElement.Name.LocalName);
                        return false;
                    }
                }
            }

            if (!expectedNodeEnumerator.HasNext && actualNodeEnumerator.HasNext)
            {
                var element2 = actualNodeEnumerator.Next();
                if (element2 != null)
                    Console.Write(
                        "Expected does not contain the node {0} that is in actual for parent {1}",
                        element2.Name.LocalName, element2.Parent != null ? element2.Parent.Name.LocalName : null);
                return false;
            }

            if (expectedNodeEnumerator.HasNext && !actualNodeEnumerator.HasNext)
            {
                var element1 = expectedNodeEnumerator.Next();
                Console.Write("Expected node {0} was not present in actual for parent {1}",
                                      element1.Name.LocalName, element1.Parent != null ? element1.Parent.Name.LocalName : null);
                return false;
            }

            return isMatchSuccessful;
        }
Exemple #2
0
 public void Test()
 {
     Func<TestCase, string> f = delegate(TestCase c)
     {
         try
         {
             IEnumerator<Token> tks; EnumeratorAdapter tenm; Token t;
             tks = SFList.FromText(c.Input + "0End\r\n").Map<Token>(Token.FromVG).NotNulls().GetEnumerator();
             tenm = new EnumeratorAdapter(tks);
             Analyzer.Init(tenm);
             t = Analyzer.Analyze();
             return TokenEx.ToTree(t);
         }
         catch (Exception e)
         {
             return e.Message;
         }
     };
     new TestCase("", Inp, Epc, f).Run();
 }
 public static void ExtendSynchronizedRangeToMonitorexit(ControlFlowGraph graph)
 {
     while (true)
     {
         bool range_extended = false;
         foreach (ExceptionRangeCFG range in graph.GetExceptions())
         {
             HashSet <BasicBlock> setPreds = new HashSet <BasicBlock>();
             foreach (BasicBlock block in range.GetProtectedRange())
             {
                 Sharpen.Collections.AddAll(setPreds, block.GetPreds());
             }
             setPreds.ExceptWith(range.GetProtectedRange());
             if (setPreds.Count != 1)
             {
                 continue;
             }
             // multiple predecessors, obfuscated range
             var                 setPredsEnumerator = new EnumeratorAdapter <BasicBlock>(setPreds.GetEnumerator());
             BasicBlock          predBlock          = setPredsEnumerator.Next();
             InstructionSequence predSeq            = predBlock.GetSeq();
             if (predSeq.IsEmpty() || predSeq.GetLastInstr().opcode != ICodeConstants.opc_monitorenter)
             {
                 continue;
             }
             // not a synchronized range
             bool monitorexit_in_range = false;
             HashSet <BasicBlock> setProtectedBlocks = new HashSet <BasicBlock>();
             Sharpen.Collections.AddAll(setProtectedBlocks, range.GetProtectedRange());
             setProtectedBlocks.Add(range.GetHandler());
             foreach (BasicBlock block in setProtectedBlocks)
             {
                 InstructionSequence blockSeq = block.GetSeq();
                 for (int i = 0; i < blockSeq.Length(); i++)
                 {
                     if (blockSeq.GetInstr(i).opcode == ICodeConstants.opc_monitorexit)
                     {
                         monitorexit_in_range = true;
                         break;
                     }
                 }
                 if (monitorexit_in_range)
                 {
                     break;
                 }
             }
             if (monitorexit_in_range)
             {
                 continue;
             }
             // protected range already contains monitorexit
             HashSet <BasicBlock> setSuccs = new HashSet <BasicBlock>();
             foreach (BasicBlock block in range.GetProtectedRange())
             {
                 Sharpen.Collections.AddAll(setSuccs, block.GetSuccs());
             }
             setSuccs.ExceptWith(range.GetProtectedRange());
             if (setSuccs.Count != 1)
             {
                 continue;
             }
             // non-unique successor
             BasicBlock          succBlock = new Sharpen.EnumeratorAdapter <BasicBlock>(setSuccs.GetEnumerator()).Next();
             InstructionSequence succSeq   = succBlock.GetSeq();
             int succ_monitorexit_index    = -1;
             for (int i = 0; i < succSeq.Length(); i++)
             {
                 if (succSeq.GetInstr(i).opcode == ICodeConstants.opc_monitorexit)
                 {
                     succ_monitorexit_index = i;
                     break;
                 }
             }
             if (succ_monitorexit_index < 0)
             {
                 continue;
             }
             // monitorexit not found in the single successor block
             BasicBlock handlerBlock = range.GetHandler();
             if (handlerBlock.GetSuccs().Count != 1)
             {
                 continue;
             }
             // non-unique handler successor
             BasicBlock          succHandler    = handlerBlock.GetSuccs()[0];
             InstructionSequence succHandlerSeq = succHandler.GetSeq();
             if (succHandlerSeq.IsEmpty() || succHandlerSeq.GetLastInstr().opcode != ICodeConstants
                 .opc_athrow)
             {
                 continue;
             }
             // not a standard synchronized range
             int handler_monitorexit_index = -1;
             for (int i = 0; i < succHandlerSeq.Length(); i++)
             {
                 if (succHandlerSeq.GetInstr(i).opcode == ICodeConstants.opc_monitorexit)
                 {
                     handler_monitorexit_index = i;
                     break;
                 }
             }
             if (handler_monitorexit_index < 0)
             {
                 continue;
             }
             // monitorexit not found in the handler successor block
             // checks successful, prerequisites satisfied, now extend the range
             if (succ_monitorexit_index < succSeq.Length() - 1)
             {
                 // split block
                 SimpleInstructionSequence seq = new SimpleInstructionSequence();
                 for (int counter = 0; counter < succ_monitorexit_index; counter++)
                 {
                     seq.AddInstruction(succSeq.GetInstr(0), -1);
                     succSeq.RemoveInstruction(0);
                 }
                 // build a separate block
                 BasicBlock newblock = new BasicBlock(++graph.last_id);
                 newblock.SetSeq(seq);
                 // insert new block
                 foreach (BasicBlock block in succBlock.GetPreds())
                 {
                     block.ReplaceSuccessor(succBlock, newblock);
                 }
                 newblock.AddSuccessor(succBlock);
                 graph.GetBlocks().AddWithKey(newblock, newblock.id);
                 succBlock = newblock;
             }
             // copy exception edges and extend protected ranges (successor block)
             BasicBlock rangeExitBlock = succBlock.GetPreds()[0];
             for (int j = 0; j < rangeExitBlock.GetSuccExceptions().Count; j++)
             {
                 BasicBlock hd = rangeExitBlock.GetSuccExceptions()[j];
                 succBlock.AddSuccessorException(hd);
                 ExceptionRangeCFG rng = graph.GetExceptionRange(hd, rangeExitBlock);
                 rng.GetProtectedRange().Add(succBlock);
             }
             // copy instructions (handler successor block)
             InstructionSequence handlerSeq = handlerBlock.GetSeq();
             for (int counter = 0; counter < handler_monitorexit_index; counter++)
             {
                 handlerSeq.AddInstruction(succHandlerSeq.GetInstr(0), -1);
                 succHandlerSeq.RemoveInstruction(0);
             }
             range_extended = true;
             break;
         }
         if (!range_extended)
         {
             break;
         }
     }
 }