From 510fe5ffa3164ee7831d12617eb4b617dea6051c Mon Sep 17 00:00:00 2001 From: Christos Houtouridis Date: Sat, 12 Jan 2019 22:33:04 +0200 Subject: [PATCH] Some more testing files --- test/{eclipse-cygwin/src => }/main.cpp | 4 +- test/tests/test_1w_impl.cpp | 70 ++++++++++++++++++ test/tests/test_i2c_impl.cpp | 74 +++++++++++++++++++ .../test_spi_impl.cpp} | 18 +++-- 4 files changed, 159 insertions(+), 7 deletions(-) rename test/{eclipse-cygwin/src => }/main.cpp (91%) create mode 100644 test/tests/test_1w_impl.cpp create mode 100644 test/tests/test_i2c_impl.cpp rename test/{eclipse-cygwin/src/test_spi_bb.cpp => tests/test_spi_impl.cpp} (79%) diff --git a/test/eclipse-cygwin/src/main.cpp b/test/main.cpp similarity index 91% rename from test/eclipse-cygwin/src/main.cpp rename to test/main.cpp index 448f9e1..7bf991a 100644 --- a/test/eclipse-cygwin/src/main.cpp +++ b/test/main.cpp @@ -1,5 +1,6 @@ /*! - * \file test/main.cpp + * \file main.cpp + * \brief Test project main file * * Copyright (C) 2018 Christos Choutouridis * @@ -20,6 +21,7 @@ #include #include + GTEST_API_ int main(int argc, char **argv) { std::cout << "Running tests from main.cpp\n"; testing::InitGoogleTest(&argc, argv); diff --git a/test/tests/test_1w_impl.cpp b/test/tests/test_1w_impl.cpp new file mode 100644 index 0000000..eae2c9b --- /dev/null +++ b/test/tests/test_1w_impl.cpp @@ -0,0 +1,70 @@ +/*! + * \file test_1w_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 . + * + */ +#include +#include + +/*! + * \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_1w { + using namespace utl; + + // implementer class stub + class OW : public _1wire_uart_i { + friend _1wire_uart_i; + byte_t UART_RW (byte_t byte) { + // return the given. This also mean no-device + return byte; + } + void UART_BR (uint32_t br) {} // imaginary baud-rate implementation + }; + + // fixture + class Test_1w_impl : public ::testing::Test { + protected: + //void SetUp() override { } + //void TearDown() override { } + OW ow; + }; + + TEST_F(Test_1w_impl, TestConcept) { + EXPECT_EQ(_1Wire_i, true); + } + + TEST_F(Test_1w_impl, TestConstruction) { + EXPECT_GT(sizeof(ow), 0UL); + } + + TEST_F (Test_1w_impl, TestFunctionality) { + byte_t b {42}; + + _1wire_id_t id = ow.first(OW::Speed::STD); + EXPECT_EQ (id != _1wire_id_t::nullDev(), false); // as long as there is no slave Mock + + EXPECT_EQ (ow.reset(OW::Speed::STD), false); // as long as there is no slave Mock + //ow.match(id, OW::Speed::STD); + EXPECT_EQ(ow.rx_data(OW::Speed::STD), 0xFF); + EXPECT_EQ(ow.tx_data(b, OW::Speed::STD), 42); + + } +} diff --git a/test/tests/test_i2c_impl.cpp b/test/tests/test_i2c_impl.cpp new file mode 100644 index 0000000..3fd1d12 --- /dev/null +++ b/test/tests/test_i2c_impl.cpp @@ -0,0 +1,74 @@ +/*! + * \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 . + * + */ +#include +#include + +/*! + * \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 { + friend i2c_bb_i; + void SCL (bool x) { } // imaginary SCL pin functionality + bool SDA (SDAMode mode, bool x) { + // always read 0 back (which means always ACK) + return 0; + } + void delay (uint32_t u) { + // Pseudo-delay + for (int i =0 ; i<10 ; ++i) + ; + } + + public: + I2C (uint32_t clk =100000) noexcept + : i2c_bb_i(clk) { + start(); + } + }; + + TEST(Test_i2c_impl, TestConcept) { + I2C i2c {}; + EXPECT_EQ(I2c_i, 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); + } +} diff --git a/test/eclipse-cygwin/src/test_spi_bb.cpp b/test/tests/test_spi_impl.cpp similarity index 79% rename from test/eclipse-cygwin/src/test_spi_bb.cpp rename to test/tests/test_spi_impl.cpp index fab84ce..212920f 100644 --- a/test/eclipse-cygwin/src/test_spi_bb.cpp +++ b/test/tests/test_spi_impl.cpp @@ -1,5 +1,5 @@ /*! - * \file test_spi_bb.cpp + * \file test_spi_impl.cpp * * Copyright (C) 2018 Christos Choutouridis * @@ -20,10 +20,16 @@ #include #include +/*! + * \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; - // derived class stub + // implementer class stub class SPI : public spi_bb_i { friend spi_bb_i; void MOSI (bool st) { } // Imaginary pin functionality @@ -38,22 +44,22 @@ namespace test_spi { }; // fixture - class Test_spi_bb : public ::testing::Test { + class Test_spi_impl : public ::testing::Test { protected: //void SetUp() override { } //void TearDown() override { } SPI spi {}; }; - TEST_F(Test_spi_bb, TestConcept) { + TEST_F(Test_spi_impl, TestConcept) { EXPECT_EQ(Spi_i, true); } - TEST_F(Test_spi_bb, TestConstruction) { + TEST_F(Test_spi_impl, TestConstruction) { EXPECT_EQ(spi.clock(), 100000UL); } - TEST_F (Test_spi_bb, TestFunctionality) { + 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];