//----------------------------------------------------------------------------
///
/// \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 <string.h>

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

#include <lepto/log.h>
#include <biwak/can.hpp>
#include <biwak/linux/can.hpp>

#define TEST_ALL
#define STOP_ON_FAIL


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


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

   SECTION( "Simple" )
   {
      SCanMessage msg( 0x123, 0, nullptr );
      SCanMessage *message;

      lWarning( "Test CAN" );
      SCanConfig config;
      CCan can(config);
      int bufferSize=CAN_BUFFERS;
      int pushClearance = LEPTO_RING_PUSH_CLEARANCE;
      // Whatever the default might be at the moment: dont break on full buffer
      //can.m_buffer.setFatal(false);

      for(int i2=0; i2<0x20; i2++)
      {
         for(int i1=0; i1 < bufferSize + 2; i1++)
         {
            uint8_t data;
            msg.setId(i1);
            data=i1+0x10;
            msg.setData(1, &data);
            can.receive(msg);
         }

         can.eventLoop();

         REQUIRE ( can.m_rxBuffer.getEntryCount() + LEPTO_RING_PUSH_CLEARANCE == bufferSize );

         for(int i1=0; i1<bufferSize - LEPTO_RING_PUSH_CLEARANCE; i1++)
         {
            message=can.m_rxBuffer.bottomEntry();
            REQUIRE ( message != nullptr );

            REQUIRE ( message->getId() == i1 );
            REQUIRE ( message->getData()[0] == i1+0x10 );
            can.m_rxBuffer.dropEntry();
         }
      }
   }
}


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