package nxm.sys.test.junit.lib;

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

/** This will test the SDDSPacket class.
    @author  Neon Ngo
    @version $Id: SDDSPacketTest.java,v 1.2 2011/02/03 18:03:06 ntn Exp $
 */
public final class SDDSPacketTest extends NMTestCase {

  private static final byte[] TEST_BUFFER = new byte[24];
  private static Time TEST_TIME;

  /** (1 tic / 250-picoseconds) = (1 tic / 250e-12 sec) = (1 tic / 25e-9 sec) = 4e9 tics/second */
  private static final long ticsPerSecond = 4000000000L;

  static {
    // initialize test buffer for Bug 1893 data is in network byte order = IEEE
    TEST_BUFFER[12]   = (byte)0xff; // flag that we have timecode
    TEST_BUFFER[16+0] = 0;          // set timecode to test value (64-bit integer = 8 bytes),
    TEST_BUFFER[16+1] = 1;          //     big-endian (network byte order)
    TEST_BUFFER[16+2] = 2;
    TEST_BUFFER[16+3] = 3;
    TEST_BUFFER[16+4] = 4;
    TEST_BUFFER[16+5] = 5;
    TEST_BUFFER[16+6] = 6;
    TEST_BUFFER[16+7] = 7;

    long tics = 0x0001020304050607L; // corresponds to above TEST_BUFFER[16-23]
    long wsec = tics / ticsPerSecond;
    long fsec = tics - (wsec*ticsPerSecond);
    TEST_TIME = new Time((double)wsec,((double)fsec)*250e-12);
  }

  /** public constructor to allow running this test case directly with JUnit. */
  public SDDSPacketTest (String methodName) { this(methodName, null); }

  /** This constructor is required by NeXtMidas Unit Testing framework.
      @param methodName The name of the method being called.
      @param midas      The midas context to use when running the tests.
   */
  public SDDSPacketTest (String methodName, Midas midas) {
    super(methodName, midas);
  }

  public void testGetTC () {
    // test case for Bug 1893 data is in network byte order = IEEE
    Time time = SDDSPacket.getTC(TEST_BUFFER, 0, 0);
    assertEquals("geTC() - timecode",TEST_TIME,time);
  }

  public void testSetTC () {
    // test case for Bug 1893 data is in network byte order = IEEE
    SDDSPacket sddsPacket = new SDDSPacket();
    int offset = 0;
    double delta = 0.1; // this needs to be greater than 0 to set timecode for call to setTC(..)
    sddsPacket.setTC(offset, delta, TEST_TIME.getWSec(), TEST_TIME.getFSec());
    assertEquals("seTC(..) - timecode",TEST_TIME,sddsPacket.getTC());
  }

}
