Jump to content

Hello, I've started programing with C#. Programing itself is kind a fun. But I have no idea how testing works. I'm working with VS2013

using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
 
namespace LaboratorinisDarbasNr._2._5
{
    public class Program
    {
        static void Main()
        {
            double First_Nr, Second_Nr, Third_Nr;
            double Point_x1, Point_y1, Point_x2, Point_y2, Point_x3, Point_y3, min = 0;
 
            
            Console.Write("Pirmas skaičius(A): ");  
            First_Nr = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Antras skaičius(B ): ");  
            Second_Nr = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Trečias skaičius©: ");  
            Third_Nr = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Pirmas taškas (x1): ");  
            Point_x1 = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Pirmas taškas (y1): ");  
            Point_y1 = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Antras taškas (x2): ");  
            Point_x2 = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Antras taškas (y2): ");  
            Point_y2 = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Trečias taškas (x3): ");  
            Point_x3 = Convert.ToDouble(Console.ReadLine());   
            Console.Write("Trečias taškas (y3): ");  
            Point_y3 = Convert.ToDouble(Console.ReadLine());   
           
            var objektas = new Program();
            bool Test = objektas.m_WillitCount(First_Nr, Second_Nr);
            if (Test == true)
            {
                double distance_1 = objektas.m_Calculation(Point_x1, Point_y1, First_Nr, Second_Nr, Third_Nr);
                Console.WriteLine(distance_1);
 
                double distance_2 = objektas.m_Calculation(Point_x2, Point_y2, First_Nr, Second_Nr, Third_Nr);
                Console.WriteLine(distance_2);
 
                double distance_3 = objektas.m_Calculation(Point_x3, Point_y3, First_Nr, Second_Nr, Third_Nr);
                Console.WriteLine(distance_3);
                m_ClosestPoint(distance_1, distance_2, distance_3, min);
                Console.ReadKey();
            }
            else
            {
                Main();
            }
        }
 
        public static void m_ClosestPoint(double distance_1, double distance_2, double distance_3, double min)
        {
           
            if (distance_1<distance_2)
            {
                if (distance_1 < distance_3)
                {
                    min = distance_1;
                    Console.WriteLine("Mažiausiai nutolęs yra pirmas taškas");
                }
                else
                {
                    min = distance_3;
                    Console.WriteLine("Mažiausiai nutolęs yra trečias taškas");
                }
             
            }
            else 
            {
                if (distance_2 < distance_3)
                {
                    min = distance_2;
                    Console.WriteLine("Mažiausiai nutolęs yra antras taškas");
                }
                else
                {
                    min = distance_3;
                     Console.WriteLine("Mažiausiai nutolęs yra trečias taškas");
                }
            }
         }
            
            
        public double m_Calculation(double x, double y, double A,double B, double C)
        {
 
            return  Math.Abs(A * x + B * y + C) / (Math.Sqrt(A * A + B * B ));
            //return Math.Abs(1 / (Math.Sqrt(A * A + B * B )) * (A * x + B * y + C));
        }
 
        public bool m_WillitCount(double First_Nr, double Second_Nr)
        {
            if (First_Nr == 0 && Second_Nr == 0)
            {
                
                Console.WriteLine("A ir Bvienu metu negali būti = 0");
                Console.WriteLine("Įveskite duomentis iš naujo");
                return false;
            }
            else
            {
 
 
                return true;
            }
        }
    }
}
 

 

This is my program. it works probably not the best possible way.  What numbers do I chose for it? Should I still test when A=0 and B=0, if I made to call main when this happens? And once I have xlsx file, how do I even write test? :/

Laptop: Acer V3-772G  CPU: i5 4200M GPU: GT 750M SSD: Crucial MX100 256GB
DesktopCPU: R7 1700x GPU: RTX 2080 SSDSamsung 860 Evo 1TB 

Link to comment
https://linustechtips.com/topic/233382-c-unit-testing/
Share on other sites

Link to post
Share on other sites

As for the general procedure of testing - there's documentation for it and it's better than anything anyone could write on this Forum anyway. :)

 

http://msdn.microsoft.com/en-us/library/ms182532.aspx

http://www.codeproject.com/Articles/391465/Creating-Unit-tests-for-your-csharp-code

 

Don't be afraid to use the documentation, noone can know everything and you always start somewhere.

 

As for the logic of tests:

 

You always have to test those cases that might cause your program to fuck up. Zero Divisor, boundary values, etc.

DayZ Forum Moderator, DayZ Developer, ARMA 3: 2017 Developer, System-Admin, Gameserver-Admin, always interested to learn something new as well as new people.

Link to comment
https://linustechtips.com/topic/233382-c-unit-testing/#findComment-3188915
Share on other sites

Link to post
Share on other sites

Unit test frameworks to use:

NUnit: actually sets up the tests with setups and teardowns and tests themselves, watch a few tutorials on it

FakeItEasy: an almost natural language for faking out the units you aren't testing

Fluent Assertions: an almost natural language for making the test assertions

 

The general idea is this (with a very contrived example):

public class Circle{    int _radius;        public Circle(int radius)    {        _radius = radius;    }    public double area() // This is intentionally incorrect    {        return _radius * Math.Pi;    }}public class Cylinder{    int _height;    Circle _base;    public Cylinder(int height, int radius)    {        _height = height;        _base = new Circle(radius);    }    public double area()    {        return _height * _base.area();    }}

Say you want to test that Cylinder.area works, but normally that would also involve testing Circle.area which you don't want to do here as this is unit testing, testing a single unit of your program and that would be testing two units.

 

So what you do is use something like FakeItEasy to fake what Circle.area returns so it doesn't matter if Circle.area is actually correct.

 

Like so:

[TestFixture(5, 50)] // These are from NUnit[TestFixture(1, 10)] // Passing in arguments here passes the arguments into the constructor[TestFixture(0, 10)]public class TestCylinder(){    Circle _fakeCircle;    Cylinder _cylinder;    int _correctResult;    int _height;    int _radius;    public TestCylinder(int height, int radius)    {        _height = height;        _radius = radius;        _correctResult = _height * 2 * Math.Pi * Math.Pow(_radius, 2);    }    [TestFixtureSetUp]    public void Setup()    {        _cylinder = new Cylinder(_height);        _circle = A.Fake<Circle>(); // Faked using FakeItEasy because we aren't testing it        A.CallTo(() => _fakeCircle.area()).Returns(_radius);    }    [Test]    public void ThenAreaShouldBeCorrect()    {        _cylinder.area().Should().Be(_correctResult); // Using FluentAssertions    }}

This will run this test three times with the data provided in the TestFixture (unless I did something unintentionally incorrect).

Despite the fact that Circle.area is implemented incorrectly, we now know that Cylinder.area works and that assuming Circle.area is implemented correctly, our overall program would work. However, we know that Circle.area isn't implemented correctly, but this should be tested in a separate unit test.

 

Note: the test data I chose is pretty poor, it doesn't test the extreme ranges etc but this isn't about choosing sensible test data, just about how to write unit tests.

Link to comment
https://linustechtips.com/topic/233382-c-unit-testing/#findComment-3191566
Share on other sites

Link to post
Share on other sites

Create an account or sign in to comment

You need to be a member in order to leave a comment

Create an account

Sign up for a new account in our community. It's easy!

Register a new account

Sign in

Already have an account? Sign in here.

Sign In Now

×