public void TestWhile1_Loop0_FaultedCondition()
        {
            WhileCondition whileCondition = new WhileCondition(0, DelegateBehavior.Faulted);
            WhileBody whileBody = new WhileBody();

            // declaring these makes it clear we are testing the correct overload
            Func<bool> condition = whileCondition.Evaluate;
            Func<Task> body = whileBody.ExecuteAsync;

            Task whileTask = null;

            try
            {
                whileTask = TaskBlocks.While(condition, body);
                whileTask.Wait();
                Assert.Fail("Expected an exception");
            }
            catch (AggregateException ex)
            {
                Assert.IsNotNull(whileTask);
                Assert.AreEqual(TaskStatus.Faulted, whileTask.Status);
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                Assert.AreSame(whileCondition.ExpectedException, ex.InnerExceptions[0]);

                Assert.AreEqual(whileCondition.TotalIterations + 1, whileCondition.EvaluateCount);
                Assert.AreEqual(whileCondition.TotalIterations, whileBody.SyncPartExecutionCount);
                Assert.AreEqual(whileCondition.TotalIterations, whileBody.ExecutionCount);
            }
        }
Beispiel #2
0
        public void TestWhile1_Loop3_NullTaskBody()
        {
            WhileCondition whileCondition = WhileCondition.True();
            WhileBody      whileBody      = new WhileBody(3, DelegateBehavior.NullTask);

            // declaring these makes it clear we are testing the correct overload
            Func <bool> condition = whileCondition.Evaluate;
            Func <Task> body      = whileBody.ExecuteAsync;

            Task whileTask = null;

            try
            {
                whileTask = TaskBlocks.While(condition, body);
                whileTask.Wait();
                Assert.Fail("Expected an exception");
            }
            catch (AggregateException ex)
            {
                Assert.IsNotNull(whileTask);
                Assert.AreEqual(TaskStatus.Faulted, whileTask.Status);
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                Assert.IsInstanceOfType(ex.InnerExceptions[0], typeof(InvalidOperationException));

                Assert.AreEqual(whileBody.MaxExecutions, whileCondition.EvaluateCount);
                Assert.AreEqual(whileBody.MaxExecutions, whileBody.SyncPartExecutionCount);
                Assert.AreEqual(whileBody.MaxExecutions - 1, whileBody.ExecutionCount);
            }
        }
Beispiel #3
0
        public void TestWhile1_Loop3_FaultedCondition()
        {
            WhileCondition whileCondition = new WhileCondition(3, DelegateBehavior.Faulted);
            WhileBody      whileBody      = new WhileBody();

            // declaring these makes it clear we are testing the correct overload
            Func <bool> condition = whileCondition.Evaluate;
            Func <Task> body      = whileBody.ExecuteAsync;

            Task whileTask = null;

            try
            {
                whileTask = TaskBlocks.While(condition, body);
                whileTask.Wait();
                Assert.Fail("Expected an exception");
            }
            catch (AggregateException ex)
            {
                Assert.IsNotNull(whileTask);
                Assert.AreEqual(TaskStatus.Faulted, whileTask.Status);
                Assert.AreEqual(1, ex.InnerExceptions.Count);
                Assert.AreSame(whileCondition.ExpectedException, ex.InnerExceptions[0]);

                Assert.AreEqual(whileCondition.TotalIterations + 1, whileCondition.EvaluateCount);
                Assert.AreEqual(whileCondition.TotalIterations, whileBody.SyncPartExecutionCount);
                Assert.AreEqual(whileCondition.TotalIterations, whileBody.ExecutionCount);
            }
        }
Beispiel #4
0
        public new WhileCondition Create(AIMember aiMember)
        {
            _aiMember = aiMember;

            WhileCondition instance = new WhileCondition(Comparison);

            OnGetInstance(instance);
            return(instance);
        }
Beispiel #5
0
 public override void Run()
 {
     whileCondition = stepSetting as WhileCondition;
     while (ExecuteExpression() && statements != null)
     {
         foreach (var item in statements)
         {
             item.Run();
         }
     }
 }
Beispiel #6
0
        public void InvokeWhile(WhileCondition condition)
        {
            int index = Listeners.Count - 1;

            while (index >= 0)
            {
                if (!condition.Invoke())
                {
                    break;
                }

                Listeners[index].Value.Invoke();
                index--;
            }
        }
Beispiel #7
0
        public void TestWhile1_Loop3_Success()
        {
            WhileCondition whileCondition = new WhileCondition(3);
            WhileBody      whileBody      = new WhileBody();

            // declaring these makes it clear we are testing the correct overload
            Func <bool> condition = whileCondition.Evaluate;
            Func <Task> body      = whileBody.ExecuteAsync;

            Task whileTask = TaskBlocks.While(condition, body);

            whileTask.Wait();

            Assert.AreEqual(whileCondition.TotalIterations + 1, whileCondition.EvaluateCount);
            Assert.AreEqual(whileCondition.TotalIterations, whileBody.SyncPartExecutionCount);
            Assert.AreEqual(whileCondition.TotalIterations, whileBody.ExecutionCount);
        }
Beispiel #8
0
        public void TestWhile1_NullBodyFunction()
        {
            WhileCondition whileCondition = new WhileCondition(0);

            // declaring these makes it clear we are testing the correct overload
            Func <bool> condition = whileCondition.Evaluate;
            Func <Task> body      = null;

            try
            {
                TaskBlocks.While(condition, body);
            }
            catch
            {
                Assert.AreEqual(0, whileCondition.EvaluateCount);
                throw;
            }
        }
        /// <summary>
        /// Sets the <see cref="ICSharpOutputNode.WriteDown"/> flag.
        /// </summary>
        public override void SetWriteDown()
        {
            if (WriteDown)
            {
                return;
            }

            WriteDown = true;

            foreach (ICSharpInstruction Instruction in InitInstructionList)
            {
                Instruction.SetWriteDown();
            }

            WhileCondition.SetWriteDown();

            foreach (ICSharpScopeAttributeFeature Item in EntityDeclarationList)
            {
                Item.SetWriteDown();
            }

            foreach (ICSharpInstruction Instruction in LoopInstructionList)
            {
                Instruction.SetWriteDown();
            }

            foreach (ICSharpInstruction Instruction in IterationInstructionList)
            {
                Instruction.SetWriteDown();
            }

            foreach (ICSharpAssertion Assertion in InvariantList)
            {
                Assertion.SetWriteDown();
            }

            if (VariantExpression != null)
            {
                VariantExpression.SetWriteDown();
            }
        }
        public void TestWhile1_Loop0_Success()
        {
            WhileCondition whileCondition = new WhileCondition(0);
            WhileBody whileBody = new WhileBody();

            // declaring these makes it clear we are testing the correct overload
            Func<bool> condition = whileCondition.Evaluate;
            Func<Task> body = whileBody.ExecuteAsync;

            Task whileTask = TaskBlocks.While(condition, body);
            whileTask.Wait();

            Assert.AreEqual(whileCondition.TotalIterations + 1, whileCondition.EvaluateCount);
            Assert.AreEqual(whileCondition.TotalIterations, whileBody.SyncPartExecutionCount);
            Assert.AreEqual(whileCondition.TotalIterations, whileBody.ExecutionCount);
        }
        public void TestWhile1_NullBodyFunction()
        {
            WhileCondition whileCondition = new WhileCondition(0);

            // declaring these makes it clear we are testing the correct overload
            Func<bool> condition = whileCondition.Evaluate;
            Func<Task> body = null;

            try
            {
                TaskBlocks.While(condition, body);
            }
            catch
            {
                Assert.AreEqual(0, whileCondition.EvaluateCount);
                throw;
            }
        }
        /// <summary>
        /// Writes down the C# instruction.
        /// </summary>
        /// <param name="writer">The stream on which to write.</param>
        public override void WriteCSharp(ICSharpWriter writer)
        {
            Debug.Assert(WriteDown);

            bool UseCurlyBrackets = false;

            /*TODO
             * List<AttachmentAlias> AttachmentVariableTable = new List<AttachmentAlias>();
             * foreach (IInstruction Item in InitInstructionList)
             *  Item.AddAttachmentVariables(Context, AttachmentVariableTable);
             * foreach (IInstruction Item in LoopInstructionList)
             *  Item.AddAttachmentVariables(Context, AttachmentVariableTable);
             * foreach (IInstruction Item in IterationInstructionList)
             *  Item.AddAttachmentVariables(Context, AttachmentVariableTable);
             */

            if (EntityDeclarationList.Count > 0 /* || AttachmentVariableTable.Count > 0*/ || InitInstructionList.Count > 1)
            {
                UseCurlyBrackets = true;
            }

            if (UseCurlyBrackets)
            {
                writer.WriteIndentedLine("{");
                writer.IncreaseIndent();
            }

            foreach (ICSharpScopeAttributeFeature Item in EntityDeclarationList)
            {
                Item.WriteCSharp(writer);
            }

            if (Source.Variant.IsAssigned)
            {
                writer.WriteIndentedLine("double LoopVariant = double.NaN;");
            }

            /*
             * foreach (AttachmentAlias AliasItem in AttachmentVariableTable)
             * {
             *  string AttachedVariableName = AliasItem.EntityName;
             *  string AttachmentTypeString = CSharpTypes.Type2CSharpString(AliasItem.EntityType, Context, AliasItem.AttachmentFormat, CSharpNamespaceFormats.None);
             *
             *  writer.WriteIndentedLine(AttachmentTypeString + " " + AttachedVariableName + ";");
             *  Context.AttachmentVariableTable.Add(AliasItem);
             * }*/

            if (EntityDeclarationList.Count > 0 /* || AttachmentVariableTable.Count > 0*/)
            {
                writer.WriteEmptyLine();
            }

            foreach (ICSharpInstruction Item in InitInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            WriteCSharpInvariant(writer);

            if (InvariantList.Count > 0)
            {
                writer.WriteEmptyLine();
            }

            ICSharpExpressionContext SourceExpressionContext = new CSharpExpressionContext();

            WhileCondition.WriteCSharp(writer, SourceExpressionContext, -1);

            string WhileString = SourceExpressionContext.ReturnValue;

            writer.WriteIndentedLine($"while ({WhileString})");
            writer.WriteIndentedLine("{");
            writer.IncreaseIndent();

            foreach (ICSharpInstruction Item in LoopInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            if (LoopInstructionList.Count > 0 && IterationInstructionList.Count > 0)
            {
                writer.WriteEmptyLine();
            }

            foreach (ICSharpInstruction Item in IterationInstructionList)
            {
                Item.WriteCSharp(writer);
            }

            if (VariantExpression != null)
            {
                ICSharpExpressionContext VariantExpressionContext = new CSharpExpressionContext();
                VariantExpression.WriteCSharp(writer, VariantExpressionContext, -1);

                string ExpressionText = VariantExpressionContext.ReturnValue;

                writer.WriteIndentedLine($"double NewVariantResult = {ExpressionText};");
                writer.WriteIndentedLine("if (NewVariantResult >= LoopVariant)// Takes advantage of the fact that 'x >= NaN' is always false");
                writer.IncreaseIndent();
                writer.WriteIndentedLine("throw new InvalidOperationException();");
                writer.DecreaseIndent();
                writer.WriteIndentedLine("LoopVariant = NewVariantResult;");
            }

            WriteCSharpInvariant(writer);

            writer.DecreaseIndent();
            writer.WriteIndentedLine("}");

            /*
             * foreach (AttachmentAlias AliasItem in AttachmentVariableTable)
             *  Context.AttachmentVariableTable.Remove(AliasItem);
             */

            if (UseCurlyBrackets)
            {
                writer.DecreaseIndent();
                writer.WriteIndentedLine("}");
            }
        }