package nxm.sys.test.junit.libm;

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

/** This will test the MMath class.

    @author  J. Czechowski IV
    @version $Id: MMathTest.java,v 1.2 2012/10/17 16:14:04 ntn Exp $
 */
public final class MMathTest extends NMTestCase {
  /** public constructor to allow running this test case directly with JUnit. */
  public MMathTest (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 MMathTest(String methodName, Midas midas) {
    super(methodName, midas);
  }

  //////////////////////////////////////////////////////////////////////////////
  private static final float FAST_LOG_TOL  = 0.0002f;
  private static final float FAST_LOG_MIN  = -1f; // All negatives are NaN anyhow
  private static final float FAST_LOG_MAX  = 1000f;
  private static final float FAST_LOG_STEP = 0.01f;

  public void testFastLog() {
    for (float val = -1; val < FAST_LOG_MAX; val+=FAST_LOG_STEP) {
      float exp = (float)Math.log(val);
      float act = MMath.fastLog(val);

      if (Float.isNaN(exp)) {
        assertTrue("MMath.fastLog("+val+") = NaN", Float.isNaN(act));
      } else {
        assertEquals("MMath.fastLog("+val+") = Math.log("+val+") +/-"+FAST_LOG_TOL, exp, act, FAST_LOG_TOL);
      }
    }
  }

  public void testFastLog2() {
    double log2 = Math.log(2.0);

    for (float val = -1; val < FAST_LOG_MAX; val+=FAST_LOG_STEP) {
      float exp = (float)(Math.log(val) / log2);
      float act = MMath.fastLog2(val);

      if (Float.isNaN(exp)) {
        assertTrue("MMath.fastLog2("+val+") = NaN", Float.isNaN(act));
      } else {
        assertEquals("MMath.fastLog2("+val+") = Math.log("+val+")/log(2) +/-"+FAST_LOG_TOL, exp, act, FAST_LOG_TOL);
      }
    }
  }
}
