samedi 5 décembre 2015

Unit Testing stack based virtual machine c#

I'm currently working on a stack based virtual machine which loads commands from a text file and I need to test the class Increment and Decrement operations. I am fairly new to unit testing but I've worked on a few examples to get the hang of the syntax but now I've become a bit stuck!

This is the class I am trying to test:

 namespace SVM.SimpleMachineLanguage
{
    #region Using directives
    using System;
    using SVM.VirtualMachine;
    #endregion
    /// <summary>
    /// Implements the SML Decr  instruction
    /// Decrements the integer value stored on top of the stack, 
    /// leaving the result on the stack
    /// </summary>
    public class Decr : Instruction
    {
        public override bool Equals(object obj)
        {
            return base.Equals(obj);
        }

        public int Execute(int v)
        {
            throw new NotImplementedException();
        }

        /// <summary>
        /// Serves as a hash function for this type.
        /// </summary>
        /// <returns>A hash code for the current <see cref="System.Object">Object</see>.</returns>
        public override int GetHashCode()
        {
            return base.GetHashCode();
        }

        /// <summary>
        /// Returns a <see cref="System.String">String</see> that represents the current <see cref="System.Object">Object</see>.
        /// </summary>
        /// <returns>A <see cref="System.String">String</see> that represents the current <see cref="System.Object">Object</see>.</returns>
        public override string ToString()
        {
            return base.ToString();
        }
        #endregion

        #region IInstruction Members

        public override void Execute()
        {
            try
            {
                if (VirtualMachine.Stack.Count < 0)
                {
                    throw new SvmRuntimeException(String.Format(Instruction.StackUnderflowMessage,
                                                    this.ToString(), VirtualMachine.ProgramCounter));
                }

                int op1 = (int)VirtualMachine.Stack.Pop();
                VirtualMachine.Stack.Push(op1--);
            }
            catch (InvalidCastException)
            {
                throw new SvmRuntimeException(String.Format(Instruction.OperandOfWrongTypeMessage,
                                                    this.ToString(), VirtualMachine.ProgramCounter));
            }
        }
        #endregion

    }
}

And this is what I have come up with in the unit testing:

namespace UnitTestSVM
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void DecrTest()
        {
            //Arrange 
            Decr TestDecr = new Decr();
            //Act
            int op1 = (int)TestDecr.VirtualMachine.Stack.Pop();
            TestDecr.VirtualMachine.Stack.Push(op1--);
            //Assert
            Assert.AreEqual(-1, op1);
        }
        [TestMethod]
        public void IncrTest()
        {
            //Arrange 
            Incr TestIncr = new Incr();
            //Act
            int op2 = (int)TestIncr.VirtualMachine.Stack.Pop();
            TestIncr.VirtualMachine.Stack.Push(op2++);
            //Assert
            Assert.AreEqual(2,op2);
        }
    }
}

When I try to run the tests I get a null value exception, so I guess my next question is would I be best to use Moq or Fakes to pass a value through to test to see if it passes the expected result?

I have had a look at the Fakes assembly and this is what I came up with but I'm not sure how I would pass a System.Collections

 Decr testDecr = new SVM.VirtualMachine.Fakes.StubIVirtualMachine()
            {
                StackGet = () => { return 1; }
            };

After this I have become stuck, any help / constructive help would be excellent.

Aucun commentaire:

Enregistrer un commentaire