Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- package csc143.test.newton.TestRoots;
- import csc143.newton.*;
- import csc143.org.junit.*;
- import csc143.org.junit.Assert.*;
- /**
- * @author Vita Wiebe
- * A test class for the methods of our Roots program, which uses the
- * Newton-Raphson method to arrive at a reasonable approximation of the square or cube root
- * (as appropriate) of a given number, N.
- */
- public class RootsTest {
- // Fields, some Roots objects to test with.
- private Roots root1;
- private Roots root2;
- private Roots root3;
- private Roots root4;
- private Roots root5;
- private Roots root6;
- private Roots root7;
- private Roots root8;
- private Roots rootNeg1;
- private Roots rootNeg2;
- private Roots rootNeg3;
- private Roots rootNeg4;
- private Roots rootNeg5;
- private Roots rootNeg6;
- private Roots rootHuge1;
- private Roots rootHuge2;
- private Roots rootSmallA;
- private Roots rootSmallB;
- /** Fixture initialization
- * Instantiate instances of Roots objects for use in testing their
- * "sqrt" and "cbrt" methods.
- * These shall have differing values for N, including illegal values.
- */
- @Before
- public void setUp() {
- // A mixture of even and odd values, large and small, and some without "neat"
- // integer square/cube roots.
- root1 = new Roots(4000);
- root2 = new Roots(125);
- root3 = new Roots(100);
- root4 = new Roots(64);
- root5 = new Roots(49);
- root6 = new Roots(36);
- root7 = new Roots(16);
- root8 = new Roots(8);
- root9 = new Roots(1);
- // Test our methods with some negative values.
- // These should cause an IllegalArgumentException in sqrt method, but not cbrt.
- rootNeg1 = new Roots(-4900);
- rootNeg2 = new Roots(-55);
- rootNeg3 = new Roots(-9);
- rootNeg4 = new Roots(-125);
- rootNeg5 = new Roots(-27);
- rootNeg6 = new Roots(-1);
- // Test our methods with very, very large values.
- // This is to see whether our NonconvergenceException can get caught,
- // as well as to get a sense of what we can achieve using these methods.
- rootHuge1 = new Roots(1000000);
- rootHuge2 = new Roots(1e-100);
- // Test our methods with small values that are close to 0 and whose
- // square and cube roots aren't necessarily easy to determine with
- // accuracy.
- rootSmallA = new Roots(3);
- rootSmallB = new Roots(2);
- }
- @Test
- public void defaultTest() {
- setUp();
- Assert.assertEquals(root1().sqrt(), 200);
- Assert.assertEquals(root2().cbrt(), 5);
- Assert.assertEquals(root3().sqrt(), 10);
- Assert.assertEquals(root4().sqrt(), 8);
- Assert.assertEquals(root4().cbrt(), 4);
- Assert.assertEquals(root5().sqrt(), 7);
- Assert.assertEquals(root6().sqrt(), 6);
- Assert.assertEquals(root7().sqrt(), 4);
- Assert.assertEquals(root7().cbrt(), 2);
- Assert.assertEquals(root8().cbrt(), 2);
- Assert.assertEquals(root9().sqrt(), 1);
- Assert.assertEquals(root9().cbrt(), 1);
- Assert.assertEquals(rootNeg1().sqrt(), IllegalArgumentException);
- Assert.assertEquals(rootNeg2().sqrt(), IllegalArgumentException);
- Assert.assertEquals(rootNeg3().sqrt(), IllegalArgumentException);
- Assert.assertEquals(rootNeg4().cbrt(), -5);
- Assert.assertEquals(rootNeg5().cbrt(), -3);
- Assert.assertEquals(rootNeg6().sqrt(), IllegalArgumentException);
- Assert.assertEquals(rootNeg6().cbrt(), -1);
- Assert.assertEquals(rootHuge1().sqrt(), 1000);
- Assert.assertEquals(rootHuge1().cbrt(), 100);
- Assert.assertEquals(rootHuge2().sqrt(), 0.01);
- Assert.assertEquals(rootHuge2().cbrt(), 0.04615888);
- Assert.assertEquals(rootSmallA().sqrt(), 1.732050808);
- Assert.assertEquals(rootSmallA().cbrt(), 1.44224957);
- Assert.assertEquals(rootSmallB().sqrt(), 1.414213562);
- Assert.assertEquals(rootSmallB().cbrt(), 1.25992105);
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement