From f7081f3917ba0e9f8e02711735d03807782f394c Mon Sep 17 00:00:00 2001 From: Mark van Renswoude Date: Tue, 27 Sep 2016 23:28:43 +0200 Subject: [PATCH] Added a framework for CxxTest based unit tests --- lib/SegmentDisplay.cpp | 4 +++ lib/SegmentDisplay.h | 9 ++---- lib/SegmentDisplayChars.h | 50 +++++++++++++++++----------------- test/.gitignore | 2 ++ test/SegmentDisplayTestSuite.h | 49 +++++++++++++++++++++++++++++++++ test/mock/Arduino.h | 31 +++++++++++++++++++++ test/mock/Mock.cpp | 19 +++++++++++++ test/mock/SPI.h | 1 + test/mock/avr/pgmspace.h | 7 +++++ test/runner.sh | 22 +++++++++++++++ 10 files changed, 163 insertions(+), 31 deletions(-) create mode 100644 test/.gitignore create mode 100644 test/SegmentDisplayTestSuite.h create mode 100644 test/mock/Arduino.h create mode 100644 test/mock/Mock.cpp create mode 100644 test/mock/SPI.h create mode 100644 test/mock/avr/pgmspace.h create mode 100644 test/runner.sh diff --git a/lib/SegmentDisplay.cpp b/lib/SegmentDisplay.cpp index 81b90bf..b28714a 100644 --- a/lib/SegmentDisplay.cpp +++ b/lib/SegmentDisplay.cpp @@ -1,4 +1,8 @@ +#include "Arduino.h" +#include "SPI.h" + #include "SegmentDisplay.h" +#include "SegmentDisplayChars.h" void SegmentDisplay::begin() diff --git a/lib/SegmentDisplay.h b/lib/SegmentDisplay.h index a4b96a0..1b883c5 100644 --- a/lib/SegmentDisplay.h +++ b/lib/SegmentDisplay.h @@ -2,9 +2,6 @@ #define SegmentDisplay_h #include "Arduino.h" -#include "SPI.h" - -#include "SegmentDisplayChars.h" /** * Drives up to 8 digits using two 8-bit shift registers. @@ -19,7 +16,7 @@ **/ // Comment to use shiftOut. Set clockPin and dataPin instead. -#define SDSupportSPI +//#define SDSupportSPI class SegmentDisplay @@ -51,8 +48,8 @@ class SegmentDisplay #endif protected: - void writeDigit(byte value, byte digitMask); - void writeDisplay(byte characterMask, byte digitMask); + inline void writeDigit(unsigned char value, unsigned char digitMask); + inline void writeDisplay(unsigned char characterMask, unsigned char digitMask); private: int digits = 6; diff --git a/lib/SegmentDisplayChars.h b/lib/SegmentDisplayChars.h index fbc80cd..ca6caa2 100644 --- a/lib/SegmentDisplayChars.h +++ b/lib/SegmentDisplayChars.h @@ -13,7 +13,7 @@ #define SegmentG 64 // Middle -const byte SDNumberSegments[10] PROGMEM = +const unsigned char SDNumberSegments[10] PROGMEM = { /** * _ @@ -98,7 +98,7 @@ const byte SDNumberSegments[10] PROGMEM = -const byte SDCharacterSegments[26] PROGMEM = +const unsigned char SDCharacterSegments[26] PROGMEM = { /** * _ @@ -166,11 +166,11 @@ const byte SDCharacterSegments[26] PROGMEM = /** * - * | - * | + * | + * | * **/ - SegmentE | SegmentF, + SegmentB | SegmentC, /** * @@ -182,11 +182,11 @@ const byte SDCharacterSegments[26] PROGMEM = /** * + * |_| * |_ - * | * **/ - SegmentE | SegmentF | SegmentG, + SegmentB | SegmentD | SegmentE | SegmentF | SegmentG, /** * @@ -196,14 +196,6 @@ const byte SDCharacterSegments[26] PROGMEM = **/ SegmentD | SegmentE | SegmentF, - /** - * _ - * - * | | - * - **/ - SegmentA | SegmentC | SegmentE, - /** * * _ @@ -212,6 +204,14 @@ const byte SDCharacterSegments[26] PROGMEM = **/ SegmentC | SegmentE | SegmentG, + /** + * _ + * | | + * | | + * + **/ + SegmentA | SegmentB | SegmentC | SegmentE | SegmentF, + /** * _ * | | @@ -268,6 +268,14 @@ const byte SDCharacterSegments[26] PROGMEM = **/ SegmentB | SegmentC | SegmentD | SegmentE | SegmentF, + /** + * + * | | + * |_| + * + **/ + SegmentB | SegmentC | SegmentD | SegmentE | SegmentF, + /** * * @@ -278,19 +286,11 @@ const byte SDCharacterSegments[26] PROGMEM = /** * + * |_| * | | - * _ * **/ - SegmentB | SegmentD | SegmentF, - - /** - * - * _| - * | - * - **/ - SegmentB | SegmentC | SegmentG, + SegmentB | SegmentC | SegmentE | SegmentF | SegmentG, /** * diff --git a/test/.gitignore b/test/.gitignore new file mode 100644 index 0000000..d24767e --- /dev/null +++ b/test/.gitignore @@ -0,0 +1,2 @@ +runner +runner.cpp \ No newline at end of file diff --git a/test/SegmentDisplayTestSuite.h b/test/SegmentDisplayTestSuite.h new file mode 100644 index 0000000..c10a5e7 --- /dev/null +++ b/test/SegmentDisplayTestSuite.h @@ -0,0 +1,49 @@ +#include +#include "lib/SegmentDisplay.h" + +class SegmentDisplayTestSuite : public CxxTest::TestSuite +{ + public: + void testBeginEnd(void) + { + SegmentDisplay* display = new SegmentDisplay(); + display->begin(); + display->end(); + } + + void testWriteNumberFull(void) + { + // TODO test writing the full 8-digit numbers + TS_WARN("Not implemented"); + } + + void testWriteTextFull(void) + { + // TODO test writing full length text + TS_WARN("Not implemented"); + } + + void testWriteNumberPartial(void) + { + // TODO test writing a partial 5-digit numbers + TS_WARN("Not implemented"); + } + + void testWriteTextPartial(void) + { + // TODO test writing partial text + TS_WARN("Not implemented"); + } + + void testWriteNumberOverflow(void) + { + // TODO test writing a number larger than 8 digits + TS_WARN("Not implemented"); + } + + void testWriteTextOverflow(void) + { + // TODO test writing text which is too long + TS_WARN("Not implemented"); + } +}; \ No newline at end of file diff --git a/test/mock/Arduino.h b/test/mock/Arduino.h new file mode 100644 index 0000000..6f452b4 --- /dev/null +++ b/test/mock/Arduino.h @@ -0,0 +1,31 @@ +// Contains the bare minimum to get the test suite up and running +#ifndef ArduinoMock_h +#define ArduinoMock_h + +#include + +#define HIGH 0x1 +#define LOW 0x0 + +#define INPUT 0x0 +#define OUTPUT 0x1 + +#define LSBFIRST 0 +#define MSBFIRST 1 + +#define PIN_SPI_SS (17) +#define PIN_SPI_MOSI (16) +#define PIN_SPI_MISO (14) +#define PIN_SPI_SCK (15) + +static const uint8_t SS = PIN_SPI_SS; +static const uint8_t MOSI = PIN_SPI_MOSI; +static const uint8_t MISO = PIN_SPI_MISO; +static const uint8_t SCK = PIN_SPI_SCK; + + +void digitalWrite( uint32_t dwPin, uint32_t dwVal ); +void pinMode( uint32_t dwPin, uint32_t dwMode ); +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val); + +#endif \ No newline at end of file diff --git a/test/mock/Mock.cpp b/test/mock/Mock.cpp new file mode 100644 index 0000000..57c21f3 --- /dev/null +++ b/test/mock/Mock.cpp @@ -0,0 +1,19 @@ +// Provides hookable implementations for the mock headers +#include "Arduino.h" + +void digitalWrite( uint32_t dwPin, uint32_t dwVal ) +{ + +} + + +void pinMode( uint32_t dwPin, uint32_t dwMode ) +{ + +} + + +void shiftOut(uint8_t dataPin, uint8_t clockPin, uint8_t bitOrder, uint8_t val) +{ + +} diff --git a/test/mock/SPI.h b/test/mock/SPI.h new file mode 100644 index 0000000..09e0c1f --- /dev/null +++ b/test/mock/SPI.h @@ -0,0 +1 @@ +// Contains the bare minimum to get the test suite up and running \ No newline at end of file diff --git a/test/mock/avr/pgmspace.h b/test/mock/avr/pgmspace.h new file mode 100644 index 0000000..71aa6b1 --- /dev/null +++ b/test/mock/avr/pgmspace.h @@ -0,0 +1,7 @@ +// Contains the bare minimum to get the test suite up and running +#ifndef pgmspacemock_h +#define pgmspacemock_h + +#define PROGMEM __attribute__(()) + +#endif \ No newline at end of file diff --git a/test/runner.sh b/test/runner.sh new file mode 100644 index 0000000..6fca63a --- /dev/null +++ b/test/runner.sh @@ -0,0 +1,22 @@ +#!/bin/sh + +# Note: +# This works perfectly on Windows 10 Anniversary Edition using Ubuntu on Windows, +# just apt-get install g++! + +CXXTest="../../CxxTest" +CXXTestGen="$CXXTest/bin/cxxtestgen" + +$CXXTestGen --error-printer -o runner.cpp SegmentDisplayTestSuite.h +if [ $? -ne 0 ]; then + echo "Generating unit test runner failed, aborting" + exit $? +fi + +g++ -o runner -I$CXXTest -I ../ -Imock -std=c++11 mock/Mock.cpp ../lib/*.cpp runner.cpp +if [ $? -ne 0 ]; then + echo "Compiling unit test runner failed, aborting" + exit $? +fi + +./runner \ No newline at end of file