troubleshooting Question

Making code suitable for unit tests

Avatar of AlHal2
AlHal2Flag for United Kingdom of Great Britain and Northern Ireland asked on
* unit testingC#
2 Comments1 Solution106 ViewsLast Modified:
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);
        }
    }

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);

        }
    }
}
ASKER CERTIFIED SOLUTION
ste5an
Senior Developer

Our community of experts have been thoroughly vetted for their expertise and industry experience.

Join our community to see this answer!
Unlock 1 Answer and 2 Comments.
Start Free Trial
Learn from the best

Network and collaborate with thousands of CTOs, CISOs, and IT Pros rooting for you and your success.

Andrew Hancock - VMware vExpert
See if this solution works for you by signing up for a 7 day free trial.
Unlock 1 Answer and 2 Comments.
Try for 7 days

”The time we save is the biggest benefit of E-E to our team. What could take multiple guys 2 hours or more each to find is accessed in around 15 minutes on Experts Exchange.

-Mike Kapnisakis, Warner Bros