mirror of
https://github.com/meekrosoft/fff
synced 2026-01-23 00:15:59 +01:00
Added driver testing example and cleanup the buildandtest script
This commit is contained in:
66
examples/driver_testing/Makefile
Normal file
66
examples/driver_testing/Makefile
Normal file
@@ -0,0 +1,66 @@
|
||||
$(VERBOSE).SILENT:
|
||||
|
||||
BUILD_DIR = ../../build
|
||||
TEMPLATE_PROGNAME = $(BUILD_DIR)/template
|
||||
CPP_PROGNAME_NOFFF = $(BUILD_DIR)/driver_testing
|
||||
CPP_PROGNAME_FFF = $(BUILD_DIR)/driver_testing_fff
|
||||
CC = gcc
|
||||
CC += -c
|
||||
CPP = g++
|
||||
CPP += -c
|
||||
LD = g++
|
||||
|
||||
GTEST_OBJS = $(BUILD_DIR)/gtest-all.o $(BUILD_DIR)/gtest-main.o
|
||||
C_OBJFILES = $(BUILD_DIR)/driver.o
|
||||
TEMPLATE_OBJFILES = $(BUILD_DIR)/test_suite_template.o
|
||||
FFF_OBJFILES = $(BUILD_DIR)/driver.test.fff.o $(GTEST_OBJS)
|
||||
NOFFF_OBJFILES = $(BUILD_DIR)/driver.test.o $(GTEST_OBJS)
|
||||
CPP_LIBS = -lpthread
|
||||
|
||||
|
||||
all: $(CPP_PROGNAME_NOFFF) $(CPP_PROGNAME_FFF) $(TEMPLATE_PROGNAME)
|
||||
|
||||
.PHONY: clean
|
||||
|
||||
clean:
|
||||
@echo "Cleaning object files"
|
||||
@echo " rm -f $(BUILD_DIR)/*.o"
|
||||
rm -f $(BUILD_DIR)/*.o
|
||||
@echo "Cleaning backups"
|
||||
@echo " rm -f *~"
|
||||
rm -f *~
|
||||
@echo "Removing programs"
|
||||
@echo " rm -f "$(C_PROGNAME)
|
||||
rm -f $(C_PROGNAME)
|
||||
@echo " rm -f "$(CPP_PROGNAME) $(TEMPLATE_PROGNAME)
|
||||
rm -f $(CPP_PROGNAME) $(TEMPLATE_PROGNAME)
|
||||
|
||||
|
||||
$(BUILD_DIR)/%.o: %.c
|
||||
@echo "Compiling "$@
|
||||
@echo " CC "$<
|
||||
$(CC) -o $@ $< -DTESTING
|
||||
|
||||
$(BUILD_DIR)/%.o: %.cpp
|
||||
@echo "Compiling "$@
|
||||
@echo " CPP "$<
|
||||
$(CPP) -I../.. -o $@ $< -DTESTING
|
||||
|
||||
$(TEMPLATE_PROGNAME): $(TEMPLATE_OBJFILES)
|
||||
@echo "Linking "$@
|
||||
@echo " LD -o "ctemplate" "$(TEMPLATE_OBJFILES)
|
||||
$(LD) -o $(TEMPLATE_PROGNAME) $(TEMPLATE_OBJFILES)
|
||||
|
||||
$(CPP_PROGNAME_FFF): $(FFF_OBJFILES) $(C_OBJFILES)
|
||||
@echo "Linking "$@
|
||||
@echo " LD -o "$(CPP_PROGNAME_FFF)" "$(FFF_OBJFILES)
|
||||
$(LD) -o $(CPP_PROGNAME_FFF) $(FFF_OBJFILES) $(C_OBJFILES) $(CPP_LIBS)
|
||||
|
||||
$(CPP_PROGNAME_NOFFF): $(NOFFF_OBJFILES) $(C_OBJFILES)
|
||||
@echo "Linking "$@
|
||||
@echo " LD -o "$(CPP_PROGNAME_NOFFF)" "$(NOFFF_OBJFILES)
|
||||
$(LD) -o $(CPP_PROGNAME_NOFFF) $(NOFFF_OBJFILES) $(C_OBJFILES) $(CPP_LIBS)
|
||||
|
||||
nothing:
|
||||
@echo "Nothing to do; quitting :("
|
||||
@echo "HINT: Try make all"
|
||||
24
examples/driver_testing/driver.c
Normal file
24
examples/driver_testing/driver.c
Normal file
@@ -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);
|
||||
}
|
||||
11
examples/driver_testing/driver.h
Normal file
11
examples/driver_testing/driver.h
Normal file
@@ -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*/
|
||||
52
examples/driver_testing/driver.test.cpp
Normal file
52
examples/driver_testing/driver.test.cpp
Normal file
@@ -0,0 +1,52 @@
|
||||
#if 1
|
||||
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);
|
||||
}
|
||||
|
||||
#endif
|
||||
58
examples/driver_testing/driver.test.fff.cpp
Normal file
58
examples/driver_testing/driver.test.fff.cpp
Normal file
@@ -0,0 +1,58 @@
|
||||
extern "C"{
|
||||
#include "driver.h"
|
||||
#include "registers.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 DriverTest : public testing::Test
|
||||
{
|
||||
public:
|
||||
void SetUp()
|
||||
{
|
||||
RESET_FAKE(IO_MEM_WR8);
|
||||
RESET_FAKE(IO_MEM_RD8);
|
||||
FFF_RESET_HISTORY();
|
||||
}
|
||||
|
||||
};
|
||||
|
||||
TEST_F(DriverTest, 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(DriverTest, 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(DriverTest, Given_revisionB_device_When_initialize_Then_enable_peripheral_before_initializing_it)
|
||||
{
|
||||
IO_MEM_RD8_fake.return_val = HARDWARE_REV_B;
|
||||
driver_init_device();
|
||||
// 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]);
|
||||
}
|
||||
2583
examples/driver_testing/fff.h
Normal file
2583
examples/driver_testing/fff.h
Normal file
File diff suppressed because it is too large
Load Diff
15
examples/driver_testing/hardware_abstraction.h
Normal file
15
examples/driver_testing/hardware_abstraction.h
Normal file
@@ -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 */
|
||||
13
examples/driver_testing/registers.h
Normal file
13
examples/driver_testing/registers.h
Normal file
@@ -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_ */
|
||||
Reference in New Issue
Block a user