From 28153db8d1dc8a950f853d4766f9364a2c3f0ef4 Mon Sep 17 00:00:00 2001 From: bartoolina Date: Sat, 29 Aug 2020 19:06:16 +0200 Subject: [PATCH] projekt z testami od ceedling. Dodatkowo skonfigurowany pugin do vscode do testowania. Dziala tez debug testow. --- .gitignore | 1 + .vscode/c_cpp_properties.json | 18 ++++++ .vscode/launch.json | 28 ++++++++++ .vscode/settings.json | 3 + project.yml | 102 ++++++++++++++++++++++++++++++++++ src/i2c.h | 7 +++ src/lights.c | 19 +++++++ src/lights.h | 10 ++++ src/tempSensor.c | 9 +++ src/tempSensor.h | 6 ++ test/support/.gitkeep | 0 test/test_lights.c | 32 +++++++++++ test/test_tempSensor.c | 29 ++++++++++ 13 files changed, 264 insertions(+) create mode 100644 .gitignore create mode 100644 .vscode/c_cpp_properties.json create mode 100644 .vscode/launch.json create mode 100644 .vscode/settings.json create mode 100644 project.yml create mode 100644 src/i2c.h create mode 100644 src/lights.c create mode 100644 src/lights.h create mode 100644 src/tempSensor.c create mode 100644 src/tempSensor.h create mode 100644 test/support/.gitkeep create mode 100644 test/test_lights.c create mode 100644 test/test_tempSensor.c diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..d163863 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +build/ \ No newline at end of file diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..402fbd5 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,18 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/var/lib/gems/2.7.0/gems/ceedling-0.30.0/vendor/unity/src/", + "/var/lib/gems/2.7.0/gems/ceedling-0.30.0/vendor/cmock/src" + ], + "defines": [], + "compilerPath": "/usr/bin/gcc", + "cStandard": "c99", + "cppStandard": "gnu++14", + "intelliSenseMode": "gcc-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json new file mode 100644 index 0000000..5760901 --- /dev/null +++ b/.vscode/launch.json @@ -0,0 +1,28 @@ +{ + // Use IntelliSense to learn about possible attributes. + // Hover to view descriptions of existing attributes. + // For more information, visit: https://go.microsoft.com/fwlink/?linkid=830387 + "version": "0.2.0", + "configurations": [ + { + "name": "Ceedling Test Explorer Debug", + "type": "cppdbg", + "request": "launch", + "program": "${workspaceFolder}/build/test/out/${command:ceedlingExplorer.debugTestExecutable}", + "args": [], + "stopAtEntry": false, + "cwd": "${workspaceFolder}", + "environment": [], + "externalConsole": false, + "MIMode": "gdb", + "setupCommands": [ + { + "description": "Enable pretty-printing for gdb", + "text": "-enable-pretty-printing", + "ignoreFailures": true + } + ], + "miDebuggerPath": "/usr/bin/gdb" + } + ] +} \ No newline at end of file diff --git a/.vscode/settings.json b/.vscode/settings.json new file mode 100644 index 0000000..f7e0123 --- /dev/null +++ b/.vscode/settings.json @@ -0,0 +1,3 @@ +{ + "ceedlingExplorer.debugConfiguration": "Ceedling Test Explorer Debug" +} \ No newline at end of file diff --git a/project.yml b/project.yml new file mode 100644 index 0000000..2464cf3 --- /dev/null +++ b/project.yml @@ -0,0 +1,102 @@ +--- + +# Notes: +# Sample project C code is not presently written to produce a release artifact. +# As such, release build options are disabled. +# This sample, therefore, only demonstrates running a collection of unit tests. + +:project: + :use_exceptions: FALSE + :use_test_preprocessor: TRUE + :use_auxiliary_dependencies: TRUE + :build_root: build +# :release_build: TRUE + :test_file_prefix: test_ + :which_ceedling: gem + :ceedling_version: 0.30.0 + :default_tasks: + - test:all + +#:test_build: +# :use_assembly: TRUE + +#:release_build: +# :output: MyApp.out +# :use_assembly: FALSE + +:environment: + +:extension: + :executable: .out + +:paths: + :test: + - +:test/** + - -:test/support + :source: + - src/** + :support: + - test/support + :libraries: [] + +:defines: + # in order to add common defines: + # 1) remove the trailing [] from the :common: section + # 2) add entries to the :common: section (e.g. :test: has TEST defined) + :common: &common_defines [] + :test: + - *common_defines + - TEST + :test_preprocess: + - *common_defines + - TEST + +:cmock: + :mock_prefix: mock_ + :when_no_prototypes: :warn + :enforce_strict_ordering: TRUE + :plugins: + - :ignore + - :callback + :treat_as: + uint8: HEX8 + uint16: HEX16 + uint32: UINT32 + int8: INT8 + bool: UINT8 + +# Add -gcov to the plugins list to make sure of the gcov plugin +# You will need to have gcov and gcovr both installed to make it work. +# For more information on these options, see docs in plugins/gcov +:gcov: + :reports: + - HtmlDetailed + :gcovr: + :html_medium_threshold: 75 + :html_high_threshold: 90 + +#:tools: +# Ceedling defaults to using gcc for compiling, linking, etc. +# As [:tools] is blank, gcc will be used (so long as it's in your system path) +# See documentation to configure a given toolchain for use + +# LIBRARIES +# These libraries are automatically injected into the build process. Those specified as +# common will be used in all types of builds. Otherwise, libraries can be injected in just +# tests or releases. These options are MERGED with the options in supplemental yaml files. +:libraries: + :placement: :end + :flag: "-l${1}" + :path_flag: "-L ${1}" + :system: [] # for example, you might list 'm' to grab the math library + :test: [] + :release: [] + +:plugins: + :load_paths: + - "#{Ceedling.load_path}" + :enabled: + - stdout_pretty_tests_report + - module_generator + - xml_tests_report +... diff --git a/src/i2c.h b/src/i2c.h new file mode 100644 index 0000000..5c70789 --- /dev/null +++ b/src/i2c.h @@ -0,0 +1,7 @@ +#ifndef i2c_H +#define i2c_H +#include + +uint16_t i2c_readRegister(uint8_t registerAddress); + +#endif // i2c_H \ No newline at end of file diff --git a/src/lights.c b/src/lights.c new file mode 100644 index 0000000..31ee9ec --- /dev/null +++ b/src/lights.c @@ -0,0 +1,19 @@ +#include "lights.h" +#include + +static bool areLightsOn = false; + +void lights_SetHeadlightSwitchOff(void) +{ + areLightsOn = false; +} + +void lights_SetHeadlightSwitchOn(void) +{ + areLightsOn = true; +} + +bool lights_AreHeadlightsOn(void) +{ + return areLightsOn; +} \ No newline at end of file diff --git a/src/lights.h b/src/lights.h new file mode 100644 index 0000000..86f8620 --- /dev/null +++ b/src/lights.h @@ -0,0 +1,10 @@ +#ifndef LIGHTS_H +#define LIGHTS_H + +#include + +void lights_SetHeadlightSwitchOff(void); +void lights_SetHeadlightSwitchOn(void); +bool lights_AreHeadlightsOn(void); + +#endif // LIGHTS_H diff --git a/src/tempSensor.c b/src/tempSensor.c new file mode 100644 index 0000000..31522f9 --- /dev/null +++ b/src/tempSensor.c @@ -0,0 +1,9 @@ +#include "tempSensor.h" +#include "i2c.h" +#include + +float tempSensor_getTemperature(void) +{ + uint16_t rawValue = i2c_readRegister(0x03); + return -100.0f + (0.2f * (float)rawValue); +} \ No newline at end of file diff --git a/src/tempSensor.h b/src/tempSensor.h new file mode 100644 index 0000000..3eb2502 --- /dev/null +++ b/src/tempSensor.h @@ -0,0 +1,6 @@ +#ifndef TEMPSENSOR_H +#define TEMPSENSOR_H + +float tempSensor_getTemperature(void); + +#endif // TEMPSENSOR_H diff --git a/test/support/.gitkeep b/test/support/.gitkeep new file mode 100644 index 0000000..e69de29 diff --git a/test/test_lights.c b/test/test_lights.c new file mode 100644 index 0000000..d547c67 --- /dev/null +++ b/test/test_lights.c @@ -0,0 +1,32 @@ +#include "unity.h" + +#include "lights.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +// void test_lights_NeedToImplement(void) +// { +// TEST_IGNORE_MESSAGE("Need to Implement lights"); +// } + +void test_WhenTheHeadlightSwitchIsOff_ThenTheHeadLightsAreOff(void) +{ + // When the headlight switch is off... + lights_SetHeadlightSwitchOff(); + // then the headlights are off. + TEST_ASSERT_EQUAL(false, lights_AreHeadlightsOn()); +} + +void test_WhenTheHeadlightSwitchIsOn_ThenTheHeadLightsAreOn(void) +{ + // When the headlight switch is on... + lights_SetHeadlightSwitchOn(); + // then the headlights are on. + TEST_ASSERT_EQUAL(true, lights_AreHeadlightsOn()); +} \ No newline at end of file diff --git a/test/test_tempSensor.c b/test/test_tempSensor.c new file mode 100644 index 0000000..99ccce0 --- /dev/null +++ b/test/test_tempSensor.c @@ -0,0 +1,29 @@ +#include "unity.h" + +#include "tempSensor.h" +#include "mock_i2c.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +// void test_tempSensor_NeedToImplement(void) +// { +// TEST_IGNORE_MESSAGE("Need to Implement tempSensor"); +// } + +void test_whenTempRegisterReadsMaxValue_thenTheTempIsTheMaxValue(void) +{ + uint8_t tempRegisterAddress = 0x03; + float expectedTemperature = 104.6f; + float tolerance = 0.1f; + //When + i2c_readRegister_ExpectAndReturn(tempRegisterAddress, 0x3ff); + //Then + float actualTemperature = tempSensor_getTemperature(); + TEST_ASSERT_FLOAT_WITHIN(tolerance, expectedTemperature, actualTemperature); +}