Making code suitable for unit tests

AlHal2
AlHal2 used Ask the Experts™
on
In this code the user presses a button cmdPythagoras which calls the FindAllTriples method.  How would I unit test this code?  The FindAllTriples method doesn't return a single value.

 private void cmdPythagoras_Click(object sender, EventArgs e)
        {

            FindAllTriples();
        }
        public void FindAllTriples()
        {
            try
            {
                List<Pythagoras> pt = new List<Pythagoras>();
                int Small = 0;
                int Med = 0;
                int Big = 0;
                CheckPrimes cp = new CheckPrimes();

                for (Small = 2; Small <= 100; Small++)

                {
                    for (Med = Small + 1; Med <= 1000; Med++)
                    {

                        {
                            for (Big = Small + 1; Big <= 1000; Big++)
                                if ((Small * Small) + (Med * Med) == (Big * Big))
                                {
                                    //Example using delegate
                                    myDelegate dg = new myDelegate(cp.CheckIfNumberIsPrime);
                                    Pythagoras p = new Pythagoras();
                                    p.Smallest = Small;
                                    p.Medium = Med;
                                    p.Biggest = Big;
                                    if (dg(Small) == true || dg(Med) == true || dg(Big) == true)
                                    {
                                        p.Comment = "At least one prime";
                                    }
                                    else if (cp.GCDRecursive(Small,
                                        Med) == 1 && cp.GCDRecursive(Small, Big) == 1 && cp.GCDRecursive(Med, Big) == 1)
                                    {
                                        p.Comment = "Numbers have no highest common factor";
                                    }
                                    else
                                    {
                                        p.Comment = "";
                                    }
                                    pt.Add(p);
                                }
                        }
                    }
                }
                using (StreamWriter sw = new StreamWriter(@"D:\Documents\MathsPrograms\Pythagoras.csv", false))
                {
                    foreach (Pythagoras p in pt)
                    {
                        sw.WriteLine(p.Smallest + "," + p.Medium + "," + p.Biggest + "," + p.Comment);

                    }
                    Environment.Exit(0);
                }
            }

            catch (Exception ex)
            {
                Console.WriteLine(ex.Message);
                Console.ReadKey();
            }
        }
        public Boolean CheckIfNumberIsPrime(int CheckThisNumber)
        {
            int Counter = 2;

                Counter = 2;
                //for (Counter = 2; Counter*Counter <= CheckThisNumber; Counter++)
                do
                {
                    if (CheckThisNumber % Counter == 0)
                    {
                        return false;
                    }
                    else
                    {

                    }
                    Counter += 1;
                } while (Counter * Counter < CheckThisNumber);
                if (Counter * Counter > CheckThisNumber)
                {
                    return true;
                }
                else
                {
                    return false;
                }

        }
public int GCDRecursive(int a, int b)
        {
            //article on http://www.vcskicks.com/euclidean-gcd.php
            if (a == 0)
                return b;
            if (b == 0)
                return a;

            if (a > b)
                return GCDRecursive(a % b, b);
            else
                return GCDRecursive(a, b % a);
        }
    }

Open in new window


I wrote this unit test for the GCDRecursive method.
using System;
using Microsoft.VisualStudio.TestTools.UnitTesting;
using MathsProg;
namespace UnitTestProject1
{
    [TestClass]
    public class UnitTest1
    {
        [TestMethod]
        public void TestHightestCommonDivisor()
        {
            int FirstInteger = 20;
            int secondInteger = 24;
            int expected = 4;
            CheckPrimes cp = new CheckPrimes();
            int actual=cp.GCDRecursive(FirstInteger, secondInteger);
            Assert.AreEqual(expected, actual);

        }
    }
}

Open in new window

Comment
Watch Question

Do more with

Expert Office
EXPERT OFFICE® is a registered trademark of EXPERTS EXCHANGE®
Senior Developer
Commented:
Normally UI is the view in the Model-View-Controller (MVC), Model-View-ViewModel (MVVP) or Model-View-Presenter (MVP) architecture pattern.

These architecural patterns separate UI from logic. So that you can test the logic. Views are not tested with unit tests. Only the model or the controllor.

So you need a class CheckPrimes separate from your form and unit test.

Author

Commented:
Thanks.

Do more with

Expert Office
Submit tech questions to Ask the Experts™ at any time to receive solutions, advice, and new ideas from leading industry professionals.

Start 7-Day Free Trial