Avatar of AlHal2
AlHal2
Flag for United Kingdom of Great Britain and Northern Ireland asked on

Making code suitable for unit tests

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

* unit testingC#

Avatar of undefined
Last Comment
AlHal2

8/22/2022 - Mon
ASKER CERTIFIED SOLUTION
ste5an

Log in or sign up to see answer
Become an EE member today7-DAY FREE TRIAL
Members can start a 7-Day Free trial then enjoy unlimited access to the platform
Sign up - Free for 7 days
or
Learn why we charge membership fees
We get it - no one likes a content blocker. Take one extra minute and find out why we block content.
Not exactly the question you had in mind?
Sign up for an EE membership and get your own personalized solution. With an EE membership, you can ask unlimited troubleshooting, research, or opinion questions.
ask a question
AlHal2

ASKER
Thanks.
I started with Experts Exchange in 2004 and it's been a mainstay of my professional computing life since. It helped me launch a career as a programmer / Oracle data analyst
William Peck