Advertisement
Vita_Harvey

RootsTest_A

Feb 3rd, 2019
97
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Java 4.02 KB | None | 0 0
  1. package csc143.test.newton.TestRoots;
  2.  
  3. import csc143.newton.*;
  4. import csc143.org.junit.*;
  5. import csc143.org.junit.Assert.*;
  6.  
  7.  
  8. /**
  9.  * @author Vita Wiebe
  10.  * A test class for the methods of our Roots program, which uses the
  11.  * Newton-Raphson method to arrive at a reasonable approximation of the square or cube root
  12.  * (as appropriate) of a given number, N.
  13.  */
  14. public class RootsTest {
  15.  
  16.    // Fields, some Roots objects to test with.
  17.    private Roots root1;
  18.    private Roots root2;
  19.    private Roots root3;
  20.    private Roots root4;
  21.    private Roots root5;
  22.    private Roots root6;
  23.    private Roots root7;
  24.    private Roots root8;
  25.    
  26.    private Roots rootNeg1;
  27.    private Roots rootNeg2;
  28.    private Roots rootNeg3;
  29.    private Roots rootNeg4;
  30.    private Roots rootNeg5;
  31.    private Roots rootNeg6;
  32.    
  33.    private Roots rootHuge1;
  34.    private Roots rootHuge2;
  35.    private Roots rootSmallA;
  36.    private Roots rootSmallB;
  37.    
  38.    /** Fixture initialization
  39.     * Instantiate instances of Roots objects for use in testing their
  40.     * "sqrt" and "cbrt" methods.
  41.     * These shall have differing values for N, including illegal values.
  42.     */
  43.    @Before
  44.    public void setUp() {
  45.      
  46.       // A mixture of even and odd values, large and small, and some without "neat"
  47.       // integer square/cube roots.
  48.       root1 = new Roots(4000);
  49.       root2 = new Roots(125);
  50.       root3 = new Roots(100);
  51.       root4 = new Roots(64);
  52.       root5 = new Roots(49);
  53.       root6 = new Roots(36);
  54.       root7 = new Roots(16);
  55.       root8 = new Roots(8);
  56.       root9 = new Roots(1);
  57.      
  58.       // Test our methods with some negative values.
  59.       // These should cause an IllegalArgumentException in sqrt method, but not cbrt.
  60.       rootNeg1 = new Roots(-4900);
  61.       rootNeg2 = new Roots(-55);
  62.       rootNeg3 = new Roots(-9);
  63.       rootNeg4 = new Roots(-125);
  64.       rootNeg5 = new Roots(-27);
  65.       rootNeg6 = new Roots(-1);
  66.      
  67.       // Test our methods with very, very large values.
  68.       // This is to see whether our NonconvergenceException can get caught,
  69.       // as well as to get a sense of what we can achieve using these methods.
  70.       rootHuge1 = new Roots(1000000);
  71.       rootHuge2 = new Roots(1e-100);
  72.      
  73.       // Test our methods with small values that are close to 0 and whose
  74.       // square and cube roots aren't necessarily easy to determine with
  75.       // accuracy.
  76.       rootSmallA = new Roots(3);
  77.       rootSmallB = new Roots(2);
  78.      
  79.    }
  80.  
  81.    @Test
  82.    public void defaultTest() {
  83.      
  84.       setUp();
  85.      
  86.       Assert.assertEquals(root1().sqrt(), 200);
  87.       Assert.assertEquals(root2().cbrt(), 5);
  88.      
  89.       Assert.assertEquals(root3().sqrt(), 10);
  90.       Assert.assertEquals(root4().sqrt(), 8);
  91.       Assert.assertEquals(root4().cbrt(), 4);
  92.       Assert.assertEquals(root5().sqrt(), 7);
  93.       Assert.assertEquals(root6().sqrt(), 6);
  94.       Assert.assertEquals(root7().sqrt(), 4);
  95.       Assert.assertEquals(root7().cbrt(), 2);
  96.       Assert.assertEquals(root8().cbrt(), 2);
  97.       Assert.assertEquals(root9().sqrt(), 1);
  98.       Assert.assertEquals(root9().cbrt(), 1);
  99.      
  100.       Assert.assertEquals(rootNeg1().sqrt(), IllegalArgumentException);
  101.       Assert.assertEquals(rootNeg2().sqrt(), IllegalArgumentException);
  102.       Assert.assertEquals(rootNeg3().sqrt(), IllegalArgumentException);
  103.       Assert.assertEquals(rootNeg4().cbrt(), -5);
  104.       Assert.assertEquals(rootNeg5().cbrt(), -3);
  105.       Assert.assertEquals(rootNeg6().sqrt(), IllegalArgumentException);
  106.       Assert.assertEquals(rootNeg6().cbrt(), -1);
  107.       Assert.assertEquals(rootHuge1().sqrt(), 1000);
  108.       Assert.assertEquals(rootHuge1().cbrt(), 100);
  109.       Assert.assertEquals(rootHuge2().sqrt(), 0.01);
  110.       Assert.assertEquals(rootHuge2().cbrt(), 0.04615888);
  111.      
  112.       Assert.assertEquals(rootSmallA().sqrt(), 1.732050808);
  113.       Assert.assertEquals(rootSmallA().cbrt(), 1.44224957);
  114.       Assert.assertEquals(rootSmallB().sqrt(), 1.414213562);
  115.       Assert.assertEquals(rootSmallB().cbrt(), 1.25992105);
  116.    }
  117. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement