diff --git a/tdd_for_embedded_c/chapter02/Makefile b/tdd_for_embedded_c/chapter02/Makefile new file mode 100644 index 0000000..4f50349 --- /dev/null +++ b/tdd_for_embedded_c/chapter02/Makefile @@ -0,0 +1,72 @@ +# ========================================== +# 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_sprintf +TARGET = $(TARGET_BASE)$(TARGET_EXTENSION) +SRC_FILES=\ + $(UNITY_ROOT)/extras/fixture/src/unity_fixture.c \ + $(UNITY_ROOT)/src/unity.c \ + ../test/SprintfTest.c \ + ../test/SprintfTestRunner.c \ + ../test/AllTests.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 + @echo "Compiling"; cd build; $(C_COMPILER) $(CFLAGS) $(INC_DIRS) $(SYMBOLS) $(SRC_FILES) -o $(TARGET) + @echo "Running tests"; ./build/$(TARGET) + @echo "Preparing coverage"; cp build/*.gc* test/ + @echo "Running coverage"; cd test; gcovr -r . -b -p --html --html-details -o ../build/cover/$(TARGET).html + @echo "Clearing intermediate files"; rm test/*.gc* build/*.gc* + + +.PHONY: clean +clean: + @echo "Cleaning $<"; $(CLEANUP) build/$(TARGET) build/cover/* + diff --git a/tdd_for_embedded_c/chapter02/test/AllTests.c b/tdd_for_embedded_c/chapter02/test/AllTests.c new file mode 100644 index 0000000..31d489c --- /dev/null +++ b/tdd_for_embedded_c/chapter02/test/AllTests.c @@ -0,0 +1,15 @@ +#include "unity_fixture.h" + + +static void RunAllTests(void) +{ + + RUN_TEST_GROUP(sprintf); + +} + + +int main(int argc, const char * argv[]) +{ + return UnityMain(argc, argv, RunAllTests); +} diff --git a/tdd_for_embedded_c/chapter02/test/SprintfTest.c b/tdd_for_embedded_c/chapter02/test/SprintfTest.c new file mode 100644 index 0000000..f30d7d2 --- /dev/null +++ b/tdd_for_embedded_c/chapter02/test/SprintfTest.c @@ -0,0 +1,58 @@ +#include +#include "unity.h" +#include "unity_fixture.h" + + +TEST_GROUP(sprintf); + + +static char output[100]; +static const char *expected; + + + +// This is run before each test +TEST_SETUP(sprintf) { + + memset(output, 0xaa, sizeof(output)); + expected = ""; + +} + + +// This id run after each test +TEST_TEAR_DOWN(sprintf) { +} + + +static void expect(const char *s) { + + expected = s; + +} + + +static void given(int charsWritten) { + + TEST_ASSERT_EQUAL(strlen(expected), charsWritten); + TEST_ASSERT_EQUAL_STRING(expected, &output[1]); + TEST_ASSERT_EQUAL_HEX8(0xaa, output[strlen(expected)+2]); + TEST_ASSERT_EQUAL_HEX8(0xaa, output[0]); + +} + + +TEST(sprintf, NoFormatOperations) { + + expect("hey"); + given(sprintf(&output[1], "%s", expected)); + +} + + +TEST(sprintf, InsertString) { + + expect("Hello world\n"); + given(sprintf(&output[1], "Hello %s\n", "world")); + +} \ No newline at end of file diff --git a/tdd_for_embedded_c/chapter02/test/SprintfTestRunner.c b/tdd_for_embedded_c/chapter02/test/SprintfTestRunner.c new file mode 100644 index 0000000..88c6219 --- /dev/null +++ b/tdd_for_embedded_c/chapter02/test/SprintfTestRunner.c @@ -0,0 +1,10 @@ +#include "unity.h" +#include "unity_fixture.h" + + +TEST_GROUP_RUNNER(sprintf) { + + RUN_TEST_CASE(sprintf, NoFormatOperations); + RUN_TEST_CASE(sprintf, InsertString); + +} \ No newline at end of file