Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Unit-testing of a constructor

I'm working on a laboratory practical with unit-testing, and below is a piece of code from the application that i'm testing. Most of the unit-testings are finished, but regarding the constructor below I just can't figure out how to test it. For example, what exactly is the constructor doing with the array elements? What would be a good way of testing the construtor?

Is there maybe a kind soul out there who could give me a kick in the right direction?

  public struct Point { 
    public int x, y;

    public Point(int a, int b) {
      x = a;
      y = b;
    }
  }

...

  public Triangle(Point[] s) {
    sides = new double[s.Length];
    sides[0] = Math.Sqrt(Math.Pow((double)(s[1].x - s[0].x), 2.0) + Math.Pow((double)(s[1].y - s[0].y), 2.0));
    sides[1] = Math.Sqrt(Math.Pow((double)(s[1].x - s[2].x), 2.0) + Math.Pow((double)(s[1].x - s[2].x), 2.0));
    sides[2] = Math.Sqrt(Math.Pow((double)(s[2].x - s[0].x), 2.0) + Math.Pow((double)(s[2].x - s[0].x), 2.0));
  }

...

        [TestMethod()]
        public void TriangleConstructorTest1()
        {
            Point[] s = null; // TODO: Initialize to an appropriate value
            Triangle target = new Triangle(s);
            Assert.Inconclusive("TODO: Implement code to verify target");
        }
like image 335
holyredbeard Avatar asked Oct 20 '25 14:10

holyredbeard


1 Answers

Perhaps I'm missing something but won't this do it?

   [TestMethod()]
        public void PointConstructorTest1()
        {
            Point target = new Point(1.5, 2.0);
            Assert.AreEqual(1.5, target.x);
            Assert.AreEqual(2.0, target.y);
        }

Not much point in testing variable assigning though really....

like image 137
Simon Avatar answered Oct 22 '25 04:10

Simon



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!