//----------------------------------------------------------------------------
///
/// \file   main.cpp
///
/// \brief  main file for pinmux
///
/// \date   20180129
/// \author Maximilian Seesslen <mes@seesslen.net>
///
//----------------------------------------------------------------------------


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


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


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

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

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

#include "../src/board.h"
#include "../src/app.h"
#include "../src/pinmux.h"
#include "writerPlatform.h"

#define TEST_ALL
#define STOP_ON_FAIL

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


TEST_CASE( "I2C-GPIO", "[default]" ) {
   CPinmux pinmux;
   CApp app;
   CBoard board;

   SECTION( "Periphery consuming multiple GPIOs" )
   {
      /*
       * The test has an peripheral that consumes I2C and an GPIO. There are not
       * enough GPIOs for it.
       * Resolving must not succeed or something with the reserving mechanism
       * does not work.
       */
      const char *boardString=
      R"JSON(
      {
          "name": "Nucleo_f303_Overload",
          "version": "1",
          "overloadingAttributes": [ "speedo" ],
          "peripheries": [
                  { "type": "GPIO",   "periphery": "PA11" },
                  { "type": "XGPIO",   "periphery": "PA10" },
                  { "type": "GPIO",   "periphery": "PA12" },
                  {
                     "type": "I2C", "periphery": "SPI1.PA11",
                     "consumes": [
                         { "periphery": "PA11" },
                         { "periphery": "PA12" }
                     ]
                  },
                  {
                     "type": "DISPLAY",
                     "consumes": [
                         { "type": "I2C",   "name": "C" },
                         { "type": "GPIO",   "name": "D" }
                     ]
                  }
         ],
         "constructors": [
                 {
                     "type": "DISPLAY",
                     "arguments": [
                             { "type": "fixed", "value": "DISPLAY" }
                     ]
                 }
         ],
         "final": "yes"
      }
      )JSON";

      app.clear();
      board.clear();
      board.loadByteArray( QByteArray(boardString), "intern_SPI" );
      app.loadByteArray(
               QByteArray(R"JSON(
               {
                   "name": "canio",
                   "periphery": [
                      { "type": "DISPLAY", "name": "2" }
                   ]
               }
      )JSON" ), "intern" );

      pinmux.setBoardApp(board, app);
      int sta=0;
      try{
         pinmux.resolveApp();
      }
      catch( ... )
      {
         sta=1;
      }

      REQUIRE( sta == 1);
   }
}


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