/*!
 * \file test_i2c_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/i2c_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_i2c {
   using namespace utl;

   // implementer class stub
   class I2C : public i2c_bb_i<I2C> {
      friend i2c_bb_i<I2C>;
      void SCL (bool x) { (void)x; }      // imaginary SCL pin functionality
      bool SDA  (SDAMode mode, bool x) {
         (void)mode;
         (void)x;
         // always read 0 back (which means always ACK)
         return 0;
      }
      void delay (uint32_t u) {
         (void)u;
         // Pseudo-delay
         for (int i =0 ; i<10 ; ++i)
            ;
      }

   public:
      I2C (uint32_t clk =100000) noexcept
         : i2c_bb_i<I2C>(clk) {
         start();
      }
   };

   TEST(Test_i2c_impl, TestConcept) {
      I2C i2c {};
//      EXPECT_EQ(I2c_i<I2C>, true);
   }

   TEST(Test_i2c_impl, TestConstruction) {
      I2C i2c {};
      EXPECT_EQ(i2c.clock(), 100000UL);
   }

   TEST(Test_i2c_impl, TestFunctionality) {
      I2C i2c {};
      uint8_t b = 42;

      i2c.clock(200000UL);
      //EXPECT_EQ(i2c.clock(), 200000UL);

      EXPECT_EQ(i2c.tx_data(b), true);
      EXPECT_EQ(i2c.rx_data(true), 0x00);
      EXPECT_EQ(i2c.rx_data(false), 0x00);
   }
}
