#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>

#include "ports_clocktimer.h"

int main(int argc, char **argv) {
  PORTS_WallClock_t c;
  PORTS_WallTimer_t t;

#if PORTS_Num_ClockTimer() > 1
  PORTS_Clock_t(1) c1;
  PORTS_Timer_t(1) t1;
#endif

  PORTS_ClockTimerInit(&argc, argv);

  printf ("\ntesting clock 0 (WallClock) ...\n");

  PORTS_WallClock(c);
  printf ("Start at %.16g\n", PORTS_WallClockToSeconds(c));

  PORTS_WallTimerInit(t);
  PORTS_WallTimerStart(t);
  sleep(5);
  PORTS_WallTimerStop(t);
  printf ("timer = %g seconds (should be ~5)\n", PORTS_WallTimerElapsed(t,0));

  PORTS_WallTimerStart(t);
  sleep(5);
  PORTS_WallTimerStop(t);
  printf ("timer = %g seconds (should be ~10)\n", PORTS_WallTimerElapsed(t,0));

  PORTS_WallTimerReset(t);
  PORTS_WallTimerStart(t);
  sleep(5);
  PORTS_WallTimerStop(t);
  printf ("timer = %g seconds (should be ~5)\n", PORTS_WallTimerElapsed(t,0));

  PORTS_WallTimerDestroy(t);

  PORTS_WallClock(c);
  printf ("End at %.16g (should be ~15 seconds more than start time)\n",
          PORTS_WallClockToSeconds(c));


#if PORTS_Num_ClockTimer() > 1
  printf ("\ntesting clock 1 ...\n");

  PORTS_Clock(1,c1);
  printf ("Start at %.16g\n", PORTS_ClockToSeconds(1,c1));

  PORTS_TimerInit(1,t1);
  PORTS_TimerStart(1,t1);
  sleep(5);
  PORTS_TimerStop(1,t1);
  printf ("timer = %g seconds (should be ~5)\n", PORTS_TimerElapsed(1,t1,0));

  PORTS_TimerStart(1,t1);
  sleep(5);
  PORTS_TimerStop(1,t1);
  printf ("timer = %g seconds (should be ~10)\n", PORTS_TimerElapsed(1,t1,0));

  PORTS_TimerReset(1,t1);
  PORTS_TimerStart(1,t1);
  sleep(5);
  PORTS_TimerStop(1,t1);
  printf ("timer = %g seconds (should be ~5)\n", PORTS_TimerElapsed(1,t1,0));

  PORTS_TimerDestroy(1,t1);

  PORTS_Clock(1,c1);
  printf ("End at %.16g (should be ~15 seconds more than start time)\n",
          PORTS_ClockToSeconds(1,c1));

#endif


  PORTS_ClockTimerTerm();
  exit(0);
}
