Example #1
0
        public void EvaluationAssignmentOfVariable()
        {
            Initialize(@"form ExampleBlock {
                question Q1 (number) ""Give me a number"";

                if (Q1==2){
                   statement S1 (number, Q1) ""you wrote 2"";
                    }
	            else {
                        statement S2 (number, Q1) ""you didnt write 2"";                    
                     };
                
                }
            ");
            Assert.IsTrue(Builder.RunEvaluators());
            Identifier i = new Identifier("Q1");

            NumberWrapper nw = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValue(i);

            nw.Value = 2;
            Assert.IsTrue(Builder.RunEvaluators());

            Identifier    S1       = new Identifier("S1");
            NumberWrapper S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValue(S1);

            Assert.AreEqual(nw.Value, S1_value.Value);
        }
Example #2
0
        public void EvaluationOfUnavailableBlock()
        {
            Initialize(@"form ExampleBlock {
                question Q1 (number) ""Give me a number"";

                if (Q1==2){
                   statement S1 (number, (123+(123/Q1))) ""you wrote 2"";
                    };
                }
            ");
            Assert.IsTrue(Builder.RunEvaluators());
            NumberWrapper nw = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("Q1");

            nw.Value = 1; // new answer to the question
            Assert.IsTrue(Builder.RunEvaluators(), "reevaluation failed");

            NumberWrapper S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");

            Assert.IsNotNull(S1_value);
            nw.Value = 0; // new answer to the question, statement should not be evaluated
            Assert.IsTrue(Builder.RunEvaluators(), "division by zero exception should not be thrown");
            bool correctException = false;

            try
            {
                Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");
            }
            catch (EvaluationError)
            {
                correctException = true;
            }
            Assert.IsTrue(correctException, "exception not caught, identifier should be removed");
        }
Example #3
0
        public void Maths()
        {
            Initialize(@"form ExampleBlock {
                question Q1 (number) ""Give me a number"";
                statement S1 (number, (1+(2-(3-(4*Q1))))) ""bla"";
                
                }
            ");
            Assert.IsTrue(Builder.RunEvaluators(), "evaluation");

            NumberWrapper nw = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("Q1");

            Assert.IsNotNull(nw);
            nw.Value = 42; // answer to the question
            Assert.IsTrue(Builder.RunEvaluators(), "reevaluation");
            NumberWrapper S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");

            Assert.IsNotNull(S1_value);
            Assert.AreEqual((1 + (2 - (3 - (4 * 42)))), S1_value.Value);
            nw.Value = -6; // answer to the question
            Assert.IsTrue(Builder.RunEvaluators(), "reevaluation");
            S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");
            Assert.IsNotNull(S1_value);
            Assert.AreEqual((1 + (2 - (3 - (4 * (-6))))), S1_value.Value);
        }
Example #4
0
        public void PlusOperatorTest()
        {
            //we expect that yesno should throw and exception but number and text should add, resp. concat.

            NumberWrapper na = new NumberWrapper(12345);
            NumberWrapper nb = new NumberWrapper(12345);

            TextWrapper ta = new TextWrapper("bla");
            TextWrapper tb = new TextWrapper("ble");


            Assert.AreEqual(24690, (na + nb).Value);
            Assert.AreEqual("blable", (ta + tb).Value);
        }
Example #5
0
        public void DivisionByZeroTest()
        {
            //we expect that yesno should throw and exception but number and text should add, resp. concat.

            NumberWrapper na = new NumberWrapper(12345);
            NumberWrapper nb = new NumberWrapper(0);
            bool          exceptionRaised = false;

            try{
                var c = na / nb;
            }
            catch (Exception e) {
                Assert.IsInstanceOfType(e, typeof(DivisionByZeroError));
                exceptionRaised = true;
            }
            Assert.IsTrue(exceptionRaised, "No exception has  been raised");
        }
Example #6
0
        public void EvaluationReassignmentOfVariable()
        {
            Initialize(@"form ExampleBlock {
                question Q1 (number) ""Give me a number"";

                if (Q1==2){
                   statement S1 (number, (123+(Q1*2))) ""you wrote 2"";
                    }
	            else {
                        statement S2 (number, Q1) ""you didnt write 2"";                    
                     };
                
                }
            ");
            Assert.IsTrue(Builder.RunEvaluators(), "First evaluation");
            NumberWrapper S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");

            Assert.IsNull(S1_value, "S1 should not exist");
            NumberWrapper S2_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S2");

            Assert.IsNull(S2_value, "S2 should not exist");

            NumberWrapper nw = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("Q1");

            nw.Value = 2; // answer to the question so the if condition is true

            Assert.IsTrue(Builder.RunEvaluators(), "Second evaluation");
            S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");
            Assert.IsNotNull(S1_value, "S1 variable was not evaluated correctly");
            S2_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S2");
            Assert.IsNull(S2_value, "S2 variable is evaluated, shouldnt be");
            Assert.AreEqual(2 * 2 + 123, S1_value.Value, "Bad maths");

            nw       = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("Q1");
            nw.Value = 31; // answer to the question so the if condition is false

            Assert.IsTrue(Builder.RunEvaluators(), "Third evaluation");
            S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");
            Assert.IsNull(S1_value, "S1 shouldnt exist anymore");
            S2_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S2");
            Assert.IsNotNull(S2_value, "S2 should appear");
        }
Example #7
0
        public void EvaluatedValueComparisson()
        {
            YesnoWrapper  yes = new YesnoWrapper(true);
            YesnoWrapper  no  = new YesnoWrapper(false);
            NumberWrapper a   = new NumberWrapper(12345);
            NumberWrapper b   = new NumberWrapper(12345);

            Assert.IsTrue((a == b).ToBool());
            Number n = new Number();

            n.Value = 12345;
            NumberWrapper c = new NumberWrapper(n);

            Assert.IsTrue((c == a).ToBool());
            Assert.IsFalse((c != a).ToBool());

            b.Value = 23456;
            Assert.IsFalse((c == b).ToBool());
            Assert.IsTrue((c != b).ToBool());
        }
Example #8
0
        public void EvaluationDivisionByZeroReassignment()
        {
            Initialize(@"form ExampleBlock {
                question Q1 (number) ""Give me a number"";
                statement S1 (number, (123+(123/Q1))) ""you wrote 2"";
                
                }
            ");
            Assert.IsTrue(Builder.RunEvaluators());
            NumberWrapper nw = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("Q1");

            nw.Value = 0; // new answer to the question, division by zero
            Assert.IsFalse(Builder.RunEvaluators(), "division by zero");

            Assert.IsInstanceOfType(Builder.DataContext.ASTHandlerExceptions[0], typeof(DivisionByZeroError), "incorrect exception");
            nw.Value = 1; // new answer to the question
            Assert.IsTrue(Builder.RunEvaluators(), "reevaluation failed");

            NumberWrapper S1_value = (NumberWrapper)Builder.DataContext.ValueReferenceTable.GetValueOrNull("S1");

            Assert.IsNotNull(S1_value);
            Assert.AreEqual(123 + (123 / 1), S1_value.Value);
        }
Example #9
0
        //This region contains all HUD data
        #region HUD data
        void initializeHUD()
        {
            hud = new HUDComponent(this, GraphicsDevice);

            BaseTextType temp;

            temp = new BaseTextType("Score:", "Fonts/LindseySmall", new Vector2(10, 5), Color.Blue);
            hud.addText(temp);

            temp = new BaseTextType("Score Number", "Fonts/LindseySmall", new Vector2(55, 5), Color.Blue);
            temp = new NumberWrapper(temp, score);
            hud.addText(temp);

            temp = new BaseTextType("Lives:", "Fonts/LindseySmall", new Vector2(100, 5), Color.Blue);
            hud.addText(temp);

            temp = new BaseTextType("Lives Number", "Fonts/LindseySmall", new Vector2(145, 5), Color.Blue);
            temp = new NumberWrapper(temp, lives);
            hud.addText(temp);

            temp = new BaseTextType("Level:", "Fonts/LindseySmall", new Vector2(170, 5), Color.Blue);
            hud.addText(temp);

            temp = new BaseTextType("Level Number", "Fonts/LindseySmall", new Vector2(220, 5), Color.Blue);
            temp = new NumberWrapper(temp, levelNumber);
            hud.addText(temp);

            temp = new BaseTextType("Add Score Number", "Fonts/LindseySmall", new Vector2(60, 15), Color.Yellow);
            temp = new NumberWrapper(temp, 0);
            temp = new FloatWrapper(temp, new Vector2(0, 3), 1.0f);
            temp = new FadeWrapper(temp, 1.0f, -1);
            temp = new ScaleWrapper(temp, 1.02f, 1.0f, 1.0f);
            hud.addText(temp);

            Components.Add(hud);
        }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<double>(2);
     var b = new NumberWrapper<double>(3);
     a.RaisedTo(b).Should().Be(new NumberWrapper<double>(8));
 }
Example #11
0
        /// <summary>
        /// Uses a native library to load an image and extract answers from it.
        /// Works with both image bytes as well as just an image file path.
        /// This method is unsafe.
        /// </summary>
        /// <param name="sheet">The path to the image containing answers to be extracted as a string or an array of image bytes.</param>
        /// <param name="studentNumberAnswers">This parameter will contain the student number answers extracted from the student number table.</param>
        /// <param name="extractedAnswers">This parameter will contain the answers extracted from the answer tables.</param>
        /// <returns>True if the process has succeeded and false otherwise.</returns>
        bool ExtractAnswers <T>(T sheet, out bool[,,] studentNumberAnswers, out bool[,,] extractedAnswers)
        {
            extractedAnswers     = new bool[3, 8, 5];
            studentNumberAnswers = new bool[1, 5, 10];

            // the first row and the first column of the original tables were removed as they do not contain any answers
            int studentNumberRows    = parameters.StudentTableRows - 1;
            int studentNumberColumns = parameters.StudentTableColumns - 1;
            int answerRows           = parameters.AnswerTableRows - 1;
            int answerColumns        = parameters.AnswerTableColumns - 1;

            NumberWrapper numberWrapper = new NumberWrapper();
            AnswerWrapper answerWrapper = new AnswerWrapper();
            bool          success       = false;

            unsafe
            {
                bool *numberPtr  = numberWrapper.number;
                bool *answersPtr = answerWrapper.answers;
                bool *successPtr = &success;

                string sheetFilename;
                byte[] sheetImageBytes;

                // if the sheet has been passed as a filename
                if ((sheetFilename = sheet as string) != null)
                {
                    NativeAPIWrapper.extract_answers_path_api(sheetFilename, parameters, numberPtr, answersPtr, successPtr);
                }
                // if the sheet has been passed as image bytes
                else if ((sheetImageBytes = sheet as byte[]) != null)
                {
                    // the image is saved in PNG (or another format) so we need to convert it to BMP which the library can read
                    Image sheetImage = ImageHandling.GetBitmap(sheetImageBytes);
                    int   imageRows  = sheetImage.Height;
                    int   imageCols  = sheetImage.Width;

                    byte[] sheetImageBitmapBytes = ImageHandling.GetImageBytes(sheetImage, ImageFormat.Bmp);
                    sheetImage.Dispose();

                    fixed(byte *imageBitmapBytesPtr = sheetImageBitmapBytes)
                    {
                        NativeAPIWrapper.extract_answers_image_api(imageBitmapBytesPtr, imageRows, imageCols, parameters, numberPtr, answersPtr, successPtr);
                    }
                }
                else
                {
                    return(false);
                }

                if (!success)
                {
                    return(false);
                }

                // convert the student number values from a C-style array of answers to a C# multi-dimensional array
                for (int row = 0; row < studentNumberRows; row++)
                {
                    for (int col = 0; col < studentNumberColumns; col++)
                    {
                        if (numberPtr[row * studentNumberColumns + col] == true)
                        {
                            studentNumberAnswers[0, row, col] = true;
                        }
                        else
                        {
                            studentNumberAnswers[0, row, col] = false;
                        }
                    }
                }

                // convert the answer table values from a C-style array to a C# multi-dimensional array
                for (int table = 0; table < parameters.TableCount - 1; table++)
                {
                    for (int row = 0; row < answerRows; row++)
                    {
                        for (int col = 0; col < answerColumns; col++)
                        {
                            if (answersPtr[table * answerRows * answerColumns + row * answerColumns + col] == true)
                            {
                                extractedAnswers[table, row, col] = true;
                            }
                            else
                            {
                                extractedAnswers[table, row, col] = false;
                            }
                        }
                    }
                }
            }

            return(true);
        }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<double>(-34.4);
     var b = new NumberWrapper<double>(-1);
     a.Plus(b).Should().Be(new NumberWrapper<double>(-35.4));
 }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<double>(-34.4);
     a.Floor().Should().Be(new NumberWrapper<double>(-35));
 }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<double>(12);
     var b = new NumberWrapper<double>(3);
     a.DividedBy(b).Should().Be(new NumberWrapper<double>(4));
 }
Example #15
0
 public WidgetBase GetWidget(QuestionUnit unit, NumberWrapper typeWrapper)
 {
     unit.InitialiseValue(typeWrapper);
     return(new NumberWidget(unit));
 }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<int>(34);
     var b = new NumberWrapper<int>(15);
     a.Remainder(b).Should().Be(new NumberWrapper<int>(4));
 }
 public void NumberWrapper()
 {
     var a = new NumberWrapper<double>(-34.4);
     a.Abs().Should().Be(new NumberWrapper<double>(34.4));
 }
Example #18
0
        //This region contains code for the HUD component, such as initializing and loading, as well as Event Handlers
        #region HUD Data
        void initializeHUD()
        {
            hud = new HUDComponent(this, GraphicsDevice);

            BaseTextType temp;

            temp = new TextType("Score", new Vector2(190, 90), Color.Red);
            hud.addText(temp);

            temp = new TextType("Score Number", new Vector2(190, 105), Color.Red);
            temp = new NumberWrapper(temp, score);
            hud.addText(temp);

            temp = new TextType("Level", new Vector2(190, 130), Color.Red);
            hud.addText(temp);

            temp = new TextType("Level Number", new Vector2(210, 145), Color.Red);
            temp = new NumberWrapper(temp, level);
            hud.addText(temp);

            temp = new TextType("Bonus", new Vector2(190, 170), Color.Red);
            hud.addText(temp);

            temp = new TextType("Multiplier Number", new Vector2(210, 185), Color.Red);
            temp = new NumberWrapper(temp, multiplier);
            hud.addText(temp);

            temp = new TextType("High\nScore", new Vector2(190, 210), Color.Red);
            hud.addText(temp);

            temp = new TextType("High Score Number", new Vector2(190, 245), Color.Red);
            temp = new NumberWrapper(temp, highScores.getHighScore());
            hud.addText(temp);

            temp = new TextType("Next Level", new Vector2(25, 160), Color.White);
            temp = new FloatWrapper(temp, new Vector2(0, -3), 3.0f);
            hud.addText(temp);

            temp = new TextType("Next Level Number", new Vector2(90, 190), Color.White);
            temp = new NumberWrapper(temp, level);
            temp = new FloatWrapper(temp, new Vector2(0, -3), 3.0f);
            hud.addText(temp);

            temp = new TextType("Add Score Number", new Vector2(80, 240), Color.Yellow);
            temp = new NumberWrapper(temp, 0);
            temp = new FloatWrapper(temp, new Vector2(0, -3), 1.0f);
            hud.addText(temp);

            temp = new TextType("Multiplier Bonus", new Vector2(100, 100), Color.Orange);
            temp = new ScaleWrapper(temp, 1, 1.5f, 0.5f, 3);
            hud.addText(temp);

            temp = new TextType("Add Multiplier Number", new Vector2(100, 130), Color.Orange);
            temp = new NumberWrapper(temp, level);
            temp = new ScaleWrapper(temp, 1, 1.5f, 0.5f, 3);
            hud.addText(temp);

            temp = new TextType("Bonus Expired", new Vector2(25, 120), Color.Orange);
            temp = new FadeWrapper(temp, 4);
            hud.addText(temp);

            Components.Add(hud);
        }