//----------------------------------------------------------------------------
///
/// \file   tests_retain.cpp
///
/// \brief  counter
///
/// \date   20210922
/// \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/retain.h>
#include <biwak/flash_file.hpp>
#include <lepto/log.h>


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


#define TEST_ALL
#define STOP_ON_FAIL
#define FLASH_LAYOUT_MAJOR       1
#define FLASH_LAYOUT_MINOR       1


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


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

   char buf[0x10];

   #if IS_ENABLED( BIWAK_I2C_RETAIN_ONLY )
      #error "CONFIG_BIWAK_I2C_RETAIN_ONLY" msut not be set for tests.
   #endif

   SECTION( "Standard" )
   {
      {
         CFlashFile flash( BIWAK_TEST_FLASH_FILE, false /*needToExist*/ );
         flash.eraseAll();
         
         #if IS_ENABLED( CONFIG_BIWAK_I2C_RETAIN_ONLY )
            #error Test does not work with BIWAK_I2C_RETAIN_ONLY being set
         #endif
         
         CRetain retain(flash, 0x0, 2);
         retain.setData(buf, sizeof(buf), FLASH_LAYOUT_MAJOR, FLASH_LAYOUT_MINOR);

         REQUIRE( retain.restore() == ERetainError::noValidCounter );

         memset(buf, 0, sizeof(buf));
         int container=retain.store();

         REQUIRE( container >= 0 );
         REQUIRE( container < 2 );
      }

      {
         CFlashFile flash( BIWAK_TEST_FLASH_FILE );

         CRetain retain(flash, 0x0, 2);
         retain.setData(buf, sizeof(buf), FLASH_LAYOUT_MAJOR, FLASH_LAYOUT_MINOR);

         int container=retain.restore();
         REQUIRE( container >= 0 );
         REQUIRE( container < 2 );
      }

      {
         // Check that the container with coulter '0' is 'invalid' and not
         // younger than counter '0xFE'

         CFlashFile flash( BIWAK_TEST_FLASH_FILE );
         flash.eraseAll();

         CRetain retain(flash, 0x0, 2);
         retain.setData(buf, sizeof(buf), FLASH_LAYOUT_MAJOR, FLASH_LAYOUT_MINOR);

         retain.setCounter(0xFE);
         int container=retain.store();
         REQUIRE( container >= 0 );
         REQUIRE( container < 2 );

         lWarning("Oldest: %d", retain.findOldest());
         lWarning("Newest: %d", retain.findNewest());
         for(int i1=0; i1<2; i1++)
         {
            lWarning("Counter of container %d: %d", i1, retain.getCounter(i1));
         }

         REQUIRE( retain.findOldest() != container );
         REQUIRE( retain.findOldest() == 1 - container );
      }
   }
}


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