From 5dee497fe33705698d9b748b34eba58d05258f16 Mon Sep 17 00:00:00 2001 From: tmeissner Date: Fri, 29 May 2015 19:10:31 +0200 Subject: [PATCH] added example code of chapter 03 --- tdd_for_embedded_c/chapter03/Makefile | 74 +++++++++++++++++++ tdd_for_embedded_c/chapter03/src/LedDriver.c | 25 +++++++ tdd_for_embedded_c/chapter03/src/LedDriver.h | 8 ++ tdd_for_embedded_c/chapter03/test/AllTests.c | 15 ++++ .../chapter03/test/LedDriverTest.c | 46 ++++++++++++ .../chapter03/test/LedDriverTestRunner.c | 10 +++ 6 files changed, 178 insertions(+) create mode 100644 tdd_for_embedded_c/chapter03/Makefile create mode 100644 tdd_for_embedded_c/chapter03/src/LedDriver.c create mode 100644 tdd_for_embedded_c/chapter03/src/LedDriver.h create mode 100644 tdd_for_embedded_c/chapter03/test/AllTests.c create mode 100644 tdd_for_embedded_c/chapter03/test/LedDriverTest.c create mode 100644 tdd_for_embedded_c/chapter03/test/LedDriverTestRunner.c diff --git a/tdd_for_embedded_c/chapter03/Makefile b/tdd_for_embedded_c/chapter03/Makefile new file mode 100644 index 0000000..73766bf --- /dev/null +++ b/tdd_for_embedded_c/chapter03/Makefile @@ -0,0 +1,74 @@ +# ========================================== +# Unity Project - A Test Framework for C +# Copyright (c) 2007 Mike Karlesky, Mark VanderVoord, Greg Williams +# [Released under MIT License. Please refer to license.txt for details] +# ========================================== + +#We try to detect the OS we are running on, and adjust commands as needed +ifeq ($(OS),Windows_NT) + CLEANUP = del /F /Q + MKDIR = mkdir + TARGET_EXTENSION=.exe +else + CLEANUP = rm -rf + MKDIR = mkdir -p + TARGET_EXTENSION= +endif + + +UNITY_ROOT=../../../../../Git/Unity +C_COMPILER=gcc + +CFLAGS = -std=c11 +CFLAGS += -Wall +#CFLAGS += -Wextra +CFLAGS += -Werror +CFLAGS += -Wpointer-arith +CFLAGS += -Wcast-align +CFLAGS += -Wwrite-strings +CFLAGS += -Wswitch-default +CFLAGS += -Wunreachable-code +CFLAGS += -Winit-self +CFLAGS += -Wmissing-field-initializers +CFLAGS += -Wno-unknown-pragmas +CFLAGS += -Wstrict-prototypes +CFLAGS += -Wundef +CFLAGS += -Wold-style-definition +CFLAGS += -Wmissing-prototypes +CFLAGS += -Wmissing-declarations +CFLAGS += -g +CFLAGS += -ftest-coverage +CFLAGS += -fprofile-arcs + +TARGET_BASE=test_leddriver +TARGET = $(TARGET_BASE)$(TARGET_EXTENSION) +SRC_FILES=\ + $(UNITY_ROOT)/extras/fixture/src/unity_fixture.c \ + $(UNITY_ROOT)/src/unity.c \ + ../test/LedDriverTest.c \ + ../test/LedDriverTestRunner.c \ + ../test/AllTests.c \ + ../src/LedDriver.c + +INC_DIRS=-I$(UNITY_ROOT)/src -I$(UNITY_ROOT)/extras/fixture/src +SYMBOLS= + + +all: clean tests + + +.PHONY: tests +tests: build/$(TARGET) + +build/$(TARGET): test/*.c src/*.h src/*.c + @echo "Compiling"; cd build; $(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES) -o $(TARGET) + @echo "Running tests"; ./build/$(TARGET) + @echo "Preparing coverage"; cp build/LedDriver.gc* src/ + @echo "Running coverage"; cd src; gcovr -r . -b -p --html --html-details -o ../build/cover/test.html + @echo "Clearing intermediate files"; rm src/*.gc* build/*.gc* + + +.PHONY: clean +clean: + @echo "Cleaning $<"; $(CLEANUP) build/$(TARGET) build/*.* build/cover/* + diff --git a/tdd_for_embedded_c/chapter03/src/LedDriver.c b/tdd_for_embedded_c/chapter03/src/LedDriver.c new file mode 100644 index 0000000..990cf04 --- /dev/null +++ b/tdd_for_embedded_c/chapter03/src/LedDriver.c @@ -0,0 +1,25 @@ +#include "LedDriver.h" + + + +static uint16_t *ledsAddress; + + +void LedDriver_Create(uint16_t *address) { + ledsAddress = address; + *ledsAddress = 0x0000; +} + + +void LedDriver_Destroy(uint16_t *address) { +} + + +void LedDriver_TurnOn(int ledNumber) { + *ledsAddress = 0x0001; +} + + +void LedDriver_TurnOff(int ledNumber) { + *ledsAddress = 0x0000; +} \ No newline at end of file diff --git a/tdd_for_embedded_c/chapter03/src/LedDriver.h b/tdd_for_embedded_c/chapter03/src/LedDriver.h new file mode 100644 index 0000000..662d639 --- /dev/null +++ b/tdd_for_embedded_c/chapter03/src/LedDriver.h @@ -0,0 +1,8 @@ +#include + + + +void LedDriver_Create(uint16_t *address); +void LedDriver_Destroy(uint16_t *address); +void LedDriver_TurnOn(int ledNumber); +void LedDriver_TurnOff(int ledNumber); \ No newline at end of file diff --git a/tdd_for_embedded_c/chapter03/test/AllTests.c b/tdd_for_embedded_c/chapter03/test/AllTests.c new file mode 100644 index 0000000..c138024 --- /dev/null +++ b/tdd_for_embedded_c/chapter03/test/AllTests.c @@ -0,0 +1,15 @@ +#include "unity_fixture.h" + + +static void RunAllTests(void) +{ + + RUN_TEST_GROUP(LedDriver); + +} + + +int main(int argc, const char * argv[]) +{ + return UnityMain(argc, argv, RunAllTests); +} diff --git a/tdd_for_embedded_c/chapter03/test/LedDriverTest.c b/tdd_for_embedded_c/chapter03/test/LedDriverTest.c new file mode 100644 index 0000000..f8cb4c3 --- /dev/null +++ b/tdd_for_embedded_c/chapter03/test/LedDriverTest.c @@ -0,0 +1,46 @@ +#include "unity_fixture.h" +#include "../src/LedDriver.h" + + +TEST_GROUP(LedDriver); + + +static uint16_t virtualLeds; + + +TEST_SETUP(LedDriver) { + LedDriver_Create(&virtualLeds); +} + + +TEST_TEAR_DOWN(LedDriver) { +} + + +TEST(LedDriver, LedsOffAfterCreate) { + + uint16_t virtualLeds = 0xFFFF; + LedDriver_Create(&virtualLeds); + + TEST_ASSERT_EQUAL_HEX16(0x0000, virtualLeds); + +} + + +TEST(LedDriver, TurnOnLedOne) { + + LedDriver_TurnOn(1); + + TEST_ASSERT_EQUAL_HEX16(0x0001, virtualLeds); + +} + + +TEST(LedDriver, TurnOffLedOne) { + + LedDriver_TurnOn(1); + LedDriver_TurnOff(1); + + TEST_ASSERT_EQUAL_HEX16(0x0000, virtualLeds); + +} \ No newline at end of file diff --git a/tdd_for_embedded_c/chapter03/test/LedDriverTestRunner.c b/tdd_for_embedded_c/chapter03/test/LedDriverTestRunner.c new file mode 100644 index 0000000..e8107ee --- /dev/null +++ b/tdd_for_embedded_c/chapter03/test/LedDriverTestRunner.c @@ -0,0 +1,10 @@ +#include "unity_fixture.h" + + +TEST_GROUP_RUNNER(LedDriver) { + + RUN_TEST_CASE(LedDriver, LedsOffAfterCreate); + RUN_TEST_CASE(LedDriver, TurnOnLedOne); + RUN_TEST_CASE(LedDriver, TurnOffLedOne); + +} \ No newline at end of file