package nxm.sys.test.junit.libm;

import nxm.sys.lib.*;
import nxm.sys.libm.Tolerance;
import nxm.sys.test.junit.NMTestCase;

/** This will test the Tolerance class.

    @author  Jeanette McCall
    @since NeXtMidas 3.7.1
 */
public final class ToleranceTest extends NMTestCase {
  /** public constructor to allow running this test case directly with JUnit. */
  public ToleranceTest (String methodName) { this(methodName, null); }

  /** Creates a new instance of the class.
      @param methodName The name of the method being called.
      @param midas      The midas context to use when running the tests.
   */
  public ToleranceTest(String methodName, Midas midas) {
    super(methodName, midas);
  }

  //////////////////////////////////////////////////////////////////////////////

  public void testEquals() {
    final int TRUE  = 1;
    final int FALSE = 0;
    // dval1, dval2, expected
    double[][] dval1dval2equals= {
        {0.1,0.1, TRUE},
        {0.1,0.2, FALSE},
        {0.0,4.9E-324, TRUE},
        {1e213,1e213 + 1.4e197, TRUE},
        {1e213,1e213 + 3e197, FALSE}
    };
    // fval1, fval2, expected
    float[][] fval1fval2equals= {
        {0.1f,0.1f, TRUE},
        {0.1f,0.2f, FALSE},
        {(float)1e20,(float)(1e20 - 8e12), TRUE},
        {(float)1e20,(float)(1e20 - 16e12), FALSE}
    };

    for (int i=0; i < dval1dval2equals.length; i++) {
      double dval1 = dval1dval2equals[i][0];
      double dval2 = dval1dval2equals[i][1];
      boolean expected = dval1dval2equals[i][2]==TRUE;
      //System.out.println("dval1:"+dval1+" dval2:"+dval2+" equals:"+Tolerance.equals(dval1, dval2)+" ulp:"+Math.ulp(dval1));
      assertEquals("Tolerance.equals(double) dval1:"+dval1+"equals dval2:"+dval2,
                      expected, Tolerance.equals(dval1, dval2));
    }

    for (int i=0; i < fval1fval2equals.length; i++) {
      float fval1 = fval1fval2equals[i][0];
      float fval2 = fval1fval2equals[i][1];
      boolean expected = fval1fval2equals[i][2]==TRUE;
      //System.out.println("fval1:"+fval1+" fval2:"+fval2+" equals:"+Tolerance.equals(fval1, fval2)+" ulp:"+Math.ulp(fval1));
      assertEquals("Tolerance.equals(float) fval1:"+fval1+"equals fval2:"+fval2,
                      expected, Tolerance.equals(fval1, fval2));
    }

  }

}
