L6: the @Test annotation indicates that this method is used for unit testing.
L7: methods used for unit testing must be public.
L8: tests the default constructor
assertEquals(): compares two parameters where the left parameter is the expected value and the right parameter is the actual value.
L12-15: tests the constructor with a string parameter.
L18-19: tests the copy constructor.
When should we use import static instead of import?
When you run this test, you see a prompt similar to the followings:
Tests passed: 1 of 1 test
Test: multiply()
Let us define another method for testing the multiply() method:
@TestpublicvoidtestMultiply() {LongInteger a =newLongInteger("123456789");a.multiply(newLongInteger("1"));assertEquals("123456789",a.toString());a.multiply(newLongInteger("-1"));assertEquals("-123456789",a.toString());a.multiply(newLongInteger("-1234567890123456789"));assertEquals("152415787517146788750190521",a.toString());a.multiply(newLongInteger("0"));assertEquals("0",a.toString());a.multiply(newLongInteger("-0"));assertEquals("-0",a.toString());}
Test: compareTo()
Let us define a method for testing the compareTo() method:
assertTrue(): passes if the parameter returns true.
Unit testing provides an effective way of ensuring the correctness of your program. Making a unit test for every method is standard practice, although this standard is often dismissed due to time pressure.
L11-12: a can hold the integer 152415787517146788750190521 (≈287), which is much larger than the maximum value of long that is 263−1.