Version: 5.2.1

Static Methods

The first example of using the tester library: testing static methods:

At times students first learn ot program simple static methods that typically perform some mathematical calculations. For examples, we may define the following methods for shapes:

// methods that compute some information about shapes

class ShapeMethods{

 

  // compute the perimeter of a rectangle of the given width and height

  static int rectPerimeter(int width, int height){

    return 2 * width + height;

  }

 

  // compute the area of a rectangle of the given width and height

  static int rectArea(int width, int height){

    return width * height;

  }

 

  // compute the perimeter of a circle of the given radius

  static double circlePerimeter(int radius){

    return 2 * Math.PI * radius;

  }

 

  // compute the area of the circle of the given radius

  static double circleArea(int radius){

    return Math.PI * radius * radius;

  }

}

We first test the methods for rectangles as follows:

class ExamplesShapesMethods{

  ExamplesShapesMethods(){}

 

  // test the methods for rectangles

  void testRectMethods(Tester t){

    t.checkExpect(ShapeMethods.rectArea(20, 40), 800);

    t.checkExpect(ShapeMethods.rectArea(30, 10), 300);

    t.checkExpect(ShapeMethods.rectPerimeter(20, 40), 120);

    t.checkExpect(ShapeMethods.rectPerimeter(30, 10), 80);

  }

}

and get the following results:

Tester Prima v.1.5.1 - 17 June 2012

-----------------------------------

Tests defined in the class: ExamplesShapesMethods:

---------------------------

ExamplesShapesMethods:

---------------

 

 new ExamplesShapesMethods:1()

---------------

 

Ran 4 tests.

2 tests failed.

 

Full test results:

-------------------

 

Success in the test number 1

 

actual:                                 expected:

800                                               800

 

 

 

Success in the test number 2

 

actual:                                 expected:

300                                               300

 

 

 

Error in test number 3

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testRectMethods(ShapesMethods.java:33)

        at ExamplesShapesMethods.main(ShapesMethods.java:40)

actual:                                 expected:

80................................................120

 

 

 

Error in test number 4

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testRectMethods(ShapesMethods.java:34)

        at ExamplesShapesMethods.main(ShapesMethods.java:40)

actual:                                 expected:

70................................................80

 

 

 

--- END OF FULL TEST RESULTS ---

We see that even in this simple example we can make a silly error that will haunt us later on, if not detected.

We fix the problem and add the tests for the circle methods:

// test the methods for circle

void testCircleMethods(Tester t){

  t.checkExpect(ShapeMethods.circleArea(10), 314.15926);

  t.checkExpect(ShapeMethods.circleArea(20), 1256.63704);

  t.checkExpect(ShapeMethods.circlePerimeter(10), 62.831852);

  t.checkExpect(ShapeMethods.circlePerimeter(20), 125.663704);

}

but when we run the tests we see that all four test failed:

 

Error in test number 5

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testCircleMethods(ShapesMethods.java:39)

        at ExamplesShapesMethods.main(ShapesMethods.java:48)

The comparison involved inexact numbers with relative tolerance 0.0010

actual:                                 expected:

314.1592653589793.................................314.15926

 

 

 

Error in test number 6

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testCircleMethods(ShapesMethods.java:40)

        at ExamplesShapesMethods.main(ShapesMethods.java:48)

The comparison involved inexact numbers with relative tolerance 0.0010

actual:                                 expected:

1256.6370614359173................................1256.63704

 

 

 

Error in test number 7

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testCircleMethods(ShapesMethods.java:41)

        at ExamplesShapesMethods.main(ShapesMethods.java:48)

The comparison involved inexact numbers with relative tolerance 0.0010

actual:                                 expected:

62.83185307179586.................................62.831852

 

 

 

Error in test number 8

 

tester.ErrorReport: Error trace:

        at ExamplesShapesMethods.testCircleMethods(ShapesMethods.java:42)

        at ExamplesShapesMethods.main(ShapesMethods.java:48)

The comparison involved inexact numbers with relative tolerance 0.0010

actual:                                 expected:

125.66370614359172................................125.663704

The tester library informs us that these tests involved inexact comparisons. So, we need to change the methods that perform the test comparison to checkInexact as follows:

// test the methods for circle

void testCircleMethods(Tester t){

  t.checkInexact(ShapeMethods.circleArea(10), 314.15926, 0.01);

  t.checkInexact(ShapeMethods.circleArea(20), 1256.63704, 0.01);

  t.checkInexact(ShapeMethods.circlePerimeter(10), 62.831852, 0.01);

  t.checkInexact(ShapeMethods.circlePerimeter(20), 125.663704, 0.01);

}

Now all test pass, but we still will get the inexact warning, as the equality of inexact values is not transitive, and so should not be considered a true equality:

Tester Prima v.1.5.1 - 17 June 2012

-----------------------------------

Tests defined in the class: ExamplesShapesMethods:

---------------------------

ExamplesShapesMethods:

---------------

 

 new ExamplesShapesMethods:1()

---------------

 

Ran 8 tests.

All tests passed.

Issued 4 warnings of inexact comparison.

 

Full test results:

-------------------

...

 

Success in the test number 5

 

The comparison involved inexact numbers with relative tolerance 0.01

actual:                                 expected:

314.1592653589793.................................314.15926

 

 

 

Success in the test number 6

 

The comparison involved inexact numbers with relative tolerance 0.01

actual:                                 expected:

1256.6370614359173................................1256.63704

 

 

 

Success in the test number 7

 

The comparison involved inexact numbers with relative tolerance 0.01

actual:                                 expected:

62.83185307179586.................................62.831852

 

 

 

Success in the test number 8

 

The comparison involved inexact numbers with relative tolerance 0.01

actual:                                 expected:

125.66370614359172................................125.663704

 

 

 

--- END OF FULL TEST RESULTS ---

_________________________________________________________________________________