//----------------------------------------------------------------------------
///
/// \file   tests_lrtimer.cpp
///
/// \brief  Check time validity
///
/// \date   20241120
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


//---Documentation------------------------------------------------------------


//---Includes -----------------------------------------------------------------


//---General--------------------------

#if defined ( CATCH_V3 )
   #include <catch2/catch_test_macros.hpp>
#elif defined ( CATCH_V2 )
   #include <catch2/catch.hpp>
#elif defined ( CATCH_V1 )
   #include <catch/catch.hpp>
#else
   #error "Either 'catch' or 'catch2' has to be installed"
#endif

#include <QDebug>

//--- Own ----------------------------

#include <biwak/timer.hpp>
#include <lepto/imago.hpp>


//---Defines-------------------------------------------------------------------


#define TEST_ALL
#define STOP_ON_FAIL


//---Implementation------------------------------------------------------------


TEST_CASE( "LRTimer", "[default]" ) {

   /* Let one exactly pass second.
    * The difference in the lrtimer has to be around 1000.
    */
   SECTION( "Standard" )
   {
      /* The lrtimer has an resolution of 1ms
       * The hrtimer has an resolution of 1ms/0x10000
       */
      lrtimer_t lrstart;
      lrtimer_t lrstop;
      lrtimer_t lrdiff;

      time_t t=time( nullptr );
      struct tm start=*gmtime( &t );
      struct tm stop;
      do{
         t=time( nullptr );
         stop=*gmtime( &t );
      }while( start.tm_sec == stop.tm_sec );

      lrstart=lrNow();
      start=stop;
      do{
         t=time( nullptr );
         stop=*gmtime( &t );
      }while( start.tm_sec == stop.tm_sec );

      lrstop=lrNow();

      lrdiff=lrstop - lrstart;
      INFO( "lrNow jitter" );
      // Jitter of 20 (2%) is usually ok, but build server is super busy
      REQUIRE( CImago<bool>::matchesJittered( lrdiff, 1000, 50 ) );
   }
}


TEST_CASE( "lrElapsed", "[default]" ) {
   
   /* Let one exactly pass second.
    * The difference in the lrtimer has to be around 1000.
    */
   SECTION( "Standard" )
   {
      /* Test "lrElapsedMSeconds()" 
       * Let one eplapsed event expire.
       * Let another eplapsed event expire and measure how long it took.
       */
      lrtimer_t t;
      lrtimer_t startTimer;
      lrtimer_t endTimer;
      lrtimer_t shouldDiff;
      t=lrNow();
      // Let one eplapsed event expire.
      while( !lrElapsedMSeconds(&t, MSECS_PER_SEC/2) )
      {
         usleep(1000);
      }
      startTimer=lrNow();
      while( !lrElapsedMSeconds(&t, MSECS_PER_SEC/2) )
      {
         usleep(1000);
      };
      endTimer=lrNow();
      shouldDiff=MSECS_PER_SEC/2;
      INFO( "Check lrElapsedMSeconds() to work as expected" );
      REQUIRE( CImago<bool>::approximatelyPercent(
               endTimer-startTimer, shouldDiff, 10 ) == shouldDiff );
   }
}


//---fin-----------------------------------------------------------------------
