To add two numbers-NModel program

Mar 9, 2010 at 9:21 AM
Edited Mar 9, 2010 at 9:21 AM

Hi Friends,

I am using the NModel tool to jus generate test cases for the addition of two numbers. I have two numbers A and B and a variable called Sum.I shud get the I/P in A and B and find the sum.I wrote the following model and implementation programs.I don get any error.But I jus get a single state in the o/p of the mpv and no test cases in the test suite generated.This is the model prog add.cs


using System;

using System.Text;

using System.Collections.Generic;

using NModel;

using NModel.Attributes;

using NModel.Execution;



namespace addit
{
    public static class Add
    {
        //types for each state variable
        public static int A = 0;
        public static int B = 0;
        public static int SUM = 0;



        // Actions: enabling condition, then action method

        public static bool getAenabled()
        {
            return true;
        }
        public static bool getBenabled()
        {
            return true;
        }
        public static bool getSUMenabled()
        {
            return true;
        }

        //[Action]

        public static int getA(int x)
        {

            A = x;
            return A;

        }
        public static int getB(int y)
        {

            B = y;
            return B;

        }
        public static int getSUM(int x, int y)
        {

            SUM = x + y;
            return SUM;

        }
    }

        public static class Factory
        {

            /// <summary>

            /// Factory method

            /// </summary>

            public static ModelProgram Addition()
            {

                return new LibraryModelProgram(typeof(Factory).Assembly, "addit");

            }

        }

    }

the implementation file addimpl.cs is dis:

using System;
using System.Collections.Generic;
using NModel.Conformance;
using NModel.Terms;
using NModel;
using Action = NModel.Terms.CompoundTerm;

namespace addit.Impl
{
    public static class Add
    {


        // Types for each state variable
        public static int x = 0;
        public static int y =0;
        public static int sum = 0;

        

        // Reset needed by test harness
        public static void Reset()
        {
            x = 0;
            y = 0;
            sum = 0;
        }
        // Actions
        public static void getA()
        {
            if (x == 0)
            {
                Console.WriteLine("Enter A");

            }
            else throw new ApplicationException("Get A not enabled");
        }
        public static void getB()
        {
            if (y == 0)
            {
                Console.WriteLine("Enter B");

            }
            else throw new ApplicationException("Get B  not enabled");
        }
        public static void getSUM()
        {
            if (sum == 0)
            {
                Console.WriteLine("Calculate sum");

            }
            else throw new ApplicationException("Calculate sum not enabled");
        }


    }
}

   and the stepper.cs file is:


using System;
using NModel.Conformance;
using NModel.Terms;
using Action = NModel.Terms.CompoundTerm;
namespace addit.Impl{    
    public class Stepper: IStepper    {    
        public CompoundTerm DoAction(CompoundTerm action)    {        
            switch (action.FunctionSymbol.ToString())        {            
                case("Tests"): return null; // first action in test seq.            
                case ("getA"):
                    Add.getA(); return null;
                case ("getB"):
                    Add.getB(); return null;
                case ("getsum"):
                    Add.getSUM(); return null;            
                default:
                    throw new Exception("Unexpected action " + action);            
            }        
        }        

        public void Reset()        
        {        
            Add.Reset();        
        }        
        
        public static IStepper Make()        
        {            
            return new Stepper();        
        }    
    }
}

Can anyone tel me what is the error that I have committed or how it should be written ,the new code?please help..

Developer
Mar 11, 2010 at 1:39 PM

Hi Sujithra!

First of all, all methods that are actions in your model should have the [Action] attribute. Your example just has one in even that is commented out.

Second, actions correspond to method calls as long as they have the void return type. In your case your actions have int return types which means that each such action gets rewritten into a _Start and _Finish action by the toolkit. Those actions are called split actions and are discussed on p 141 in "Model-Based Software Testing and Analysis with C#". To be honest, your simple example does not require delving into split actions.

You could easily rename your getA and getB actions to setA and setB and remove the return statement, as you actually set the values in the action bodies. And the sum action could have just one argument, the result, and void return type. In addition, the sum action needs a guard that says that the sum action is only enabled when the argument is equal to the sum of A and B.

A good way of proceeding is to make the model explorable in MPV. That involves defining finite domains on the arguments. Have a look at the EmptyCoffeeMachine example and play around with MPV to build some intuition. If you have more questions after playing around with MPV then just ask.

jpe