/*!
 * \file test_spi_impl.cpp
 *
 * Copyright (C) 2018 Christos Choutouridis
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU Lesser General Public License as
 * published by the Free Software Foundation, either version 3
 * of the License, or (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *
 */
#include <gtest/gtest.h>
#include <utl/com/spi_bb.h>


/*!
 * \warning
 *    This is not right way of testing communication interfaces. We have to
 *    implement a Mock object to simulate the slave's behavior. Until then
 *    we have the following.
 */
namespace test_spi {
   using namespace utl;

   // implementer class stub
   class SPI : public spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW> {
      friend spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW>;
      void MOSI (bool st) { (void)st; }            // Imaginary pin functionality
      bool MISO () { return true; }                // All reads become 0xFF
      void SCLK (bool st) { (void)st; }            // Imaginary pin functionality
      void delay (uint32_t nsec) { (void)nsec; }   // Imaginary delay functionality

   public:
      SPI (uint32_t clk =100000) noexcept :  // expect 100000 default constructed clock
         spi_bb_i<SPI, spi::cpol::LOW, spi::cpha::LOW>(clk) {
      }
   };

   // fixture
   class Test_spi_impl : public ::testing::Test {
   protected:
      //void SetUp() override { }
      //void TearDown() override { }
      SPI spi {};
   };

   TEST_F(Test_spi_impl, TestConcept) {
//      EXPECT_EQ(Spi_i<SPI>, true);
   }

   TEST_F(Test_spi_impl, TestConstruction) {
      EXPECT_EQ(spi.clock(), 100000UL);
   }

   TEST_F (Test_spi_impl, TestFunctionality) {
      uint8_t b = 42;
      uint8_t bb[] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
      uint8_t bbb[sizeof bb];

      spi.clock(500000UL);
      EXPECT_EQ(spi.clock(), 500000UL);

      EXPECT_EQ(spi.tx_data(b), 0xFF);
      EXPECT_EQ(spi.tx_data(bb, bbb, sizeof(bb)), sizeof (bb));
      EXPECT_EQ(spi.rx_data(), 0xFF);

      spi.rx_data(bbb, sizeof bb);
      for (unsigned int i=0 ; i<sizeof bb ; ++i) {
         EXPECT_EQ (bbb[i], 0xFF);
      }
   }
}
