summaryrefslogtreecommitdiff
path: root/deps/fff/examples/driver_testing
diff options
context:
space:
mode:
Diffstat (limited to 'deps/fff/examples/driver_testing')
-rw-r--r--deps/fff/examples/driver_testing/CMakeLists.txt31
-rw-r--r--deps/fff/examples/driver_testing/include/driver.h11
-rw-r--r--deps/fff/examples/driver_testing/include/hardware_abstraction.h15
-rw-r--r--deps/fff/examples/driver_testing/include/registers.h13
-rw-r--r--deps/fff/examples/driver_testing/src/driver.c24
-rw-r--r--deps/fff/examples/driver_testing/src/driver.test.cpp50
-rw-r--r--deps/fff/examples/driver_testing/src/driver.test.fff.cpp63
7 files changed, 207 insertions, 0 deletions
diff --git a/deps/fff/examples/driver_testing/CMakeLists.txt b/deps/fff/examples/driver_testing/CMakeLists.txt
new file mode 100644
index 0000000..3580349
--- /dev/null
+++ b/deps/fff/examples/driver_testing/CMakeLists.txt
@@ -0,0 +1,31 @@
+# Copyright 2022 Google LLC
+# SPDX-License-Identifier: Apache-2.0
+
+# Create the driver test binary
+add_executable(driver_test
+ src/driver.c
+ src/driver.test.cpp
+)
+target_include_directories(driver_test PRIVATE include)
+target_link_libraries(driver_test PRIVATE GTest::gtest_main fff)
+target_compile_definitions(driver_test PUBLIC TEST_USER_OWN_TR1_TUPLE=1 TESTING)
+
+# Create the driver fff test binary
+add_executable(driver_fff_test
+ src/driver.c
+ src/driver.test.fff.cpp
+)
+target_include_directories(driver_fff_test PRIVATE include)
+target_link_libraries(driver_fff_test PRIVATE GTest::gtest_main fff)
+target_compile_definitions(driver_fff_test PUBLIC TEST_USER_OWN_TR1_TUPLE=1 TESTING)
+
+# Add tests to ctest
+add_test(
+ NAME driver_test
+ COMMAND $<TARGET_FILE:driver_test>
+)
+
+add_test(
+ NAME driver_fff_test
+ COMMAND $<TARGET_FILE:driver_fff_test>
+)
diff --git a/deps/fff/examples/driver_testing/include/driver.h b/deps/fff/examples/driver_testing/include/driver.h
new file mode 100644
index 0000000..b7406d4
--- /dev/null
+++ b/deps/fff/examples/driver_testing/include/driver.h
@@ -0,0 +1,11 @@
+
+#ifndef DRIVER
+#define DRIVER
+
+#include <stdint.h>
+
+void driver_write(uint8_t val);
+uint8_t driver_read();
+void driver_init_device();
+
+#endif /*include guard*/
diff --git a/deps/fff/examples/driver_testing/include/hardware_abstraction.h b/deps/fff/examples/driver_testing/include/hardware_abstraction.h
new file mode 100644
index 0000000..affa92e
--- /dev/null
+++ b/deps/fff/examples/driver_testing/include/hardware_abstraction.h
@@ -0,0 +1,15 @@
+#ifndef HARDWARE_ABSTRACTION
+#define HARDWARE_ABSTRACTION
+
+#include <stdint.h>
+
+#ifndef TESTING
+#define IO_MEM_RD8(ADDR) (*((volatile uint8_t *)(ADDR)))
+#define IO_MEM_WR8(ADDR, VAL_8) (*((volatile uint8_t *)(ADDR)) = (VAL_8))
+#else
+/* In testing use fake functions to record calls to IO memory */
+uint8_t IO_MEM_RD8(uint32_t reg);
+void IO_MEM_WR8(uint32_t reg, uint8_t val);
+#endif
+
+#endif /* Include guard */
diff --git a/deps/fff/examples/driver_testing/include/registers.h b/deps/fff/examples/driver_testing/include/registers.h
new file mode 100644
index 0000000..5c9e5a9
--- /dev/null
+++ b/deps/fff/examples/driver_testing/include/registers.h
@@ -0,0 +1,13 @@
+#ifndef REGISTERS_H_
+#define REGISTERS_H_
+
+#define DRIVER_OUTPUT_REGISTER 0xFFAAu
+#define DRIVER_INPUT_REGISTER 0XFFABu
+#define DRIVER_PERIPHERAL_ENABLE_REG 0xFFACu
+#define DRIVER_PERIPHERAL_INITIALIZE_REG 0xFFACu
+
+#define HARDWARE_VERSION_REGISTER 0xFF00u
+#define HARDWARE_REV_A 0x00u
+#define HARDWARE_REV_B 0x01u
+
+#endif /* REGISTERS_H_ */
diff --git a/deps/fff/examples/driver_testing/src/driver.c b/deps/fff/examples/driver_testing/src/driver.c
new file mode 100644
index 0000000..9454ba6
--- /dev/null
+++ b/deps/fff/examples/driver_testing/src/driver.c
@@ -0,0 +1,24 @@
+
+
+#include "hardware_abstraction.h"
+#include "registers.h"
+
+void driver_write(uint8_t val)
+{
+ IO_MEM_WR8(DRIVER_OUTPUT_REGISTER, val);
+}
+
+uint8_t driver_read()
+{
+ return IO_MEM_RD8(DRIVER_INPUT_REGISTER);
+}
+
+void driver_init_device()
+{
+ uint8_t hw_version = IO_MEM_RD8(HARDWARE_VERSION_REGISTER);
+ if(HARDWARE_REV_B == hw_version)
+ {
+ IO_MEM_WR8(DRIVER_PERIPHERAL_ENABLE_REG, 1);
+ }
+ IO_MEM_WR8(DRIVER_PERIPHERAL_INITIALIZE_REG, 1);
+}
diff --git a/deps/fff/examples/driver_testing/src/driver.test.cpp b/deps/fff/examples/driver_testing/src/driver.test.cpp
new file mode 100644
index 0000000..cd80bb3
--- /dev/null
+++ b/deps/fff/examples/driver_testing/src/driver.test.cpp
@@ -0,0 +1,50 @@
+extern "C"
+{
+#include "driver.h"
+#include "registers.h"
+}
+#include "../../../fff.h"
+#include <gtest/gtest.h>
+
+
+extern "C"
+{
+ static uint8_t readVal;
+ static int readCalled;
+ static uint32_t readRegister;
+ uint8_t IO_MEM_RD8(uint32_t reg)
+ {
+ readRegister = reg;
+ readCalled++;
+ return readVal;
+ }
+
+ static uint32_t writeRegister;
+ static uint8_t writeVal;
+ static int writeCalled;
+ void IO_MEM_WR8(uint32_t reg, uint8_t val)
+ {
+ writeRegister = reg;
+ writeVal = val;
+ writeCalled++;
+ }
+}
+
+TEST(Driver, When_writing_Then_writes_data_to_DRIVER_OUTPUT_REGISTER)
+{
+ driver_write(0x34);
+ ASSERT_EQ(1u, writeCalled);
+ ASSERT_EQ(0x34u, writeVal);
+ ASSERT_EQ(DRIVER_OUTPUT_REGISTER, writeRegister);
+}
+
+
+TEST(Driver, When_reading_data_Then_reads_from_DRIVER_INPUT_REGISTER)
+{
+ readVal = 0x55;
+ uint8_t returnedValue = driver_read();
+ ASSERT_EQ(1u, readCalled);
+ ASSERT_EQ(0x55u, returnedValue);
+ ASSERT_EQ(readRegister, DRIVER_INPUT_REGISTER);
+}
+
diff --git a/deps/fff/examples/driver_testing/src/driver.test.fff.cpp b/deps/fff/examples/driver_testing/src/driver.test.fff.cpp
new file mode 100644
index 0000000..56f6192
--- /dev/null
+++ b/deps/fff/examples/driver_testing/src/driver.test.fff.cpp
@@ -0,0 +1,63 @@
+extern "C"{
+ #include "driver.h"
+ #include "registers.h"
+ #include "hardware_abstraction.h"
+}
+#include "fff.h"
+#include <gtest/gtest.h>
+
+DEFINE_FFF_GLOBALS;
+
+FAKE_VOID_FUNC(IO_MEM_WR8, uint32_t, uint8_t);
+FAKE_VALUE_FUNC(uint8_t, IO_MEM_RD8, uint32_t);
+
+class DriverTestFFF : public testing::Test
+{
+public:
+ void SetUp()
+ {
+ RESET_FAKE(IO_MEM_WR8);
+ RESET_FAKE(IO_MEM_RD8);
+ FFF_RESET_HISTORY();
+ }
+
+};
+
+TEST_F(DriverTestFFF, When_writing_Then_writes_data_to_DRIVER_OUTPUT_REGISTER)
+{
+ driver_write(0x34);
+ ASSERT_EQ(1u, IO_MEM_WR8_fake.call_count);
+ ASSERT_EQ(0x34u, IO_MEM_WR8_fake.arg1_val);
+ ASSERT_EQ(DRIVER_OUTPUT_REGISTER, IO_MEM_WR8_fake.arg0_val);
+}
+
+
+TEST_F(DriverTestFFF, When_reading_data_Then_reads_from_DRIVER_INPUT_REGISTER)
+{
+ IO_MEM_RD8_fake.return_val = 0x55;
+ uint8_t returnedValue = driver_read();
+ ASSERT_EQ(1u, IO_MEM_RD8_fake.call_count);
+ ASSERT_EQ(0x55u, returnedValue);
+ ASSERT_EQ(IO_MEM_RD8_fake.arg0_val, DRIVER_INPUT_REGISTER);
+}
+
+TEST_F(DriverTestFFF, Given_revisionB_device_When_initialize_Then_enable_peripheral_before_initializing_it)
+{
+ // Given
+ IO_MEM_RD8_fake.return_val = HARDWARE_REV_B;
+ // When
+ driver_init_device();
+
+ //Then
+ // Gets the hardware revision
+ ASSERT_EQ((void*) IO_MEM_RD8, fff.call_history[0]);
+ ASSERT_EQ(HARDWARE_VERSION_REGISTER, IO_MEM_RD8_fake.arg0_history[0]);
+ // Enables Peripheral
+ ASSERT_EQ((void*) IO_MEM_WR8, fff.call_history[1]);
+ ASSERT_EQ(DRIVER_PERIPHERAL_ENABLE_REG, IO_MEM_WR8_fake.arg0_history[0]);
+ ASSERT_EQ(1, IO_MEM_WR8_fake.arg1_history[0]);
+ // Initializes Peripheral
+ ASSERT_EQ((void*) IO_MEM_WR8, fff.call_history[2]);
+ ASSERT_EQ(DRIVER_PERIPHERAL_INITIALIZE_REG,IO_MEM_WR8_fake.arg0_history[1]);
+ ASSERT_EQ(1, IO_MEM_WR8_fake.arg1_history[1]);
+}