From 62673c4137abd04a309115440899431bdb9909e3 Mon Sep 17 00:00:00 2001 From: Bent Bisballe Nyeng Date: Sat, 19 Sep 2020 18:26:41 +0200 Subject: Add README and getBaud example. --- .gitignore | 3 ++ README | 14 ++++++ examples/ExampleTest.cc | 112 ++++++++++++++++++++++++++++++++++++++++++++++++ examples/Makefile | 25 +++++++++++ examples/getbaud.cc | 52 ++++++++++++++++++++++ examples/getbaud.h | 12 ++++++ 6 files changed, 218 insertions(+) create mode 100644 .gitignore create mode 100644 README create mode 100644 examples/ExampleTest.cc create mode 100644 examples/Makefile create mode 100644 examples/getbaud.cc create mode 100644 examples/getbaud.h diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..edf1514 --- /dev/null +++ b/.gitignore @@ -0,0 +1,3 @@ +examples/test_* +examples/result_*.xml +examples/*.log \ No newline at end of file diff --git a/README b/README new file mode 100644 index 0000000..15fd5da --- /dev/null +++ b/README @@ -0,0 +1,14 @@ +µUnit (micro-unit) is a minimalistic, header only, unit-test framework for C++. + +For an example of how to use it with a simple Makefile project see the examples +folder. + +It outputs CppUnit compatible xml and can be used with for example the xUnit +plugin for jenkins. + +µUnit was originally developed by Bent Bisballe Nyeng for the DrumGizmo project +under the name dgUnit but has since then changed name and moved into its own +project for easier re-use in other C++ projects. + +µUnit is released under the CC0-1.0 license. See the CC0-1.0 file for further +details. \ No newline at end of file diff --git a/examples/ExampleTest.cc b/examples/ExampleTest.cc new file mode 100644 index 0000000..bc5e2f7 --- /dev/null +++ b/examples/ExampleTest.cc @@ -0,0 +1,112 @@ +#include + +#include "getbaud.h" + +class ExampleTest + : public uUnit +{ +public: + ExampleTest() + { + uUNIT_TEST(ExampleTest::boundaryTests); + uUNIT_TEST(ExampleTest::exceptionTests); + } + + void boundaryTests() + { + uUNIT_ASSERT_EQUAL(B0, getBaud(0)); + uUNIT_ASSERT_EQUAL(B0, getBaud(1)); + uUNIT_ASSERT_EQUAL(B0, getBaud(49)); + + uUNIT_ASSERT_EQUAL(B50, getBaud(50)); + uUNIT_ASSERT_EQUAL(B50, getBaud(51)); + uUNIT_ASSERT_EQUAL(B50, getBaud(74)); + + uUNIT_ASSERT_EQUAL(B75, getBaud(75)); + uUNIT_ASSERT_EQUAL(B75, getBaud(76)); + uUNIT_ASSERT_EQUAL(B75, getBaud(109)); + + uUNIT_ASSERT_EQUAL(B110, getBaud(110)); + uUNIT_ASSERT_EQUAL(B110, getBaud(111)); + uUNIT_ASSERT_EQUAL(B110, getBaud(133)); + + uUNIT_ASSERT_EQUAL(B134, getBaud(134)); + uUNIT_ASSERT_EQUAL(B134, getBaud(135)); + uUNIT_ASSERT_EQUAL(B134, getBaud(149)); + + uUNIT_ASSERT_EQUAL(B150, getBaud(150)); + uUNIT_ASSERT_EQUAL(B150, getBaud(151)); + uUNIT_ASSERT_EQUAL(B150, getBaud(199)); + + uUNIT_ASSERT_EQUAL(B200, getBaud(200)); + uUNIT_ASSERT_EQUAL(B200, getBaud(201)); + uUNIT_ASSERT_EQUAL(B200, getBaud(299)); + + uUNIT_ASSERT_EQUAL(B300, getBaud(300)); + uUNIT_ASSERT_EQUAL(B300, getBaud(301)); + uUNIT_ASSERT_EQUAL(B300, getBaud(599)); + + uUNIT_ASSERT_EQUAL(B600, getBaud(600)); + uUNIT_ASSERT_EQUAL(B600, getBaud(601)); + uUNIT_ASSERT_EQUAL(B600, getBaud(1199)); + + uUNIT_ASSERT_EQUAL(B1200, getBaud(1200)); + uUNIT_ASSERT_EQUAL(B1200, getBaud(1201)); + uUNIT_ASSERT_EQUAL(B1200, getBaud(1799)); + + uUNIT_ASSERT_EQUAL(B1800, getBaud(1800)); + uUNIT_ASSERT_EQUAL(B1800, getBaud(1801)); + uUNIT_ASSERT_EQUAL(B1800, getBaud(2399)); + + uUNIT_ASSERT_EQUAL(B2400, getBaud(2400)); + uUNIT_ASSERT_EQUAL(B2400, getBaud(2401)); + uUNIT_ASSERT_EQUAL(B2400, getBaud(4799)); + + uUNIT_ASSERT_EQUAL(B4800, getBaud(4800)); + uUNIT_ASSERT_EQUAL(B4800, getBaud(4801)); + uUNIT_ASSERT_EQUAL(B4800, getBaud(9599)); + + uUNIT_ASSERT_EQUAL(B9600, getBaud(9600)); + uUNIT_ASSERT_EQUAL(B9600, getBaud(9601)); + uUNIT_ASSERT_EQUAL(B9600, getBaud(19199)); + + uUNIT_ASSERT_EQUAL(B19200, getBaud(19200)); + uUNIT_ASSERT_EQUAL(B19200, getBaud(19201)); + uUNIT_ASSERT_EQUAL(B19200, getBaud(38399)); + + uUNIT_ASSERT_EQUAL(B38400, getBaud(38400)); + uUNIT_ASSERT_EQUAL(B38400, getBaud(38401)); + uUNIT_ASSERT_EQUAL(B38400, getBaud(57599)); + + uUNIT_ASSERT_EQUAL(B57600, getBaud(57600)); + uUNIT_ASSERT_EQUAL(B57600, getBaud(57601)); + uUNIT_ASSERT_EQUAL(B57600, getBaud(115199)); + + uUNIT_ASSERT_EQUAL(B115200, getBaud(115200)); + uUNIT_ASSERT_EQUAL(B115200, getBaud(115201)); + uUNIT_ASSERT_EQUAL(B115200, getBaud(230399)); + + uUNIT_ASSERT_EQUAL(B230400, getBaud(230400)); + uUNIT_ASSERT_EQUAL(B230400, getBaud(230401)); + } + + void exceptionTests() + { + try + { + getBaud(-1); + uUNIT_ASSERT(false); // exception should be thrown for invalid value + } + catch(const bad_speed& e) + { + // Excpected outcome + } + catch(...) + { + uUNIT_ASSERT(false); // Unknown exception were thrown + } + } +}; + +// Registers the fixture into the 'registry' +static ExampleTest test; diff --git a/examples/Makefile b/examples/Makefile new file mode 100644 index 0000000..17c811f --- /dev/null +++ b/examples/Makefile @@ -0,0 +1,25 @@ +# Add more tests here +TESTS=\ + test_ExampleTest + +CXX ?= g++ +CXXFLAGS ?= -g -std=c++17 -I.. + +all: ${TESTS} + +check: all + @F="0"; \ + for T in ${TESTS}; do echo -en "\n$$T: "; ./$$T 2> $$T.log; F=$$(($$F+$$?)); done; \ + echo -e '\n-----'; echo "Failed tests: $$F"; echo '-----'; \ + [ $$F -eq 0 ] + +EXAMPLE_TEST_SRC = \ + getbaud.cc \ + ExampleTest.cc +test_ExampleTest: $(EXAMPLE_TEST_SRC) + $(CXX) $(CXXFLAGS) -DOUTPUT=\"$@\" -o $@ ../uunit.cc $(EXAMPLE_TEST_SRC) + +clean: + rm -f ${TESTS} + rm -f *.xml + rm -f *.log diff --git a/examples/getbaud.cc b/examples/getbaud.cc new file mode 100644 index 0000000..e90c4f6 --- /dev/null +++ b/examples/getbaud.cc @@ -0,0 +1,52 @@ +#include "getbaud.h" + +speed_t getBaud(int speed) +{ + struct + { + int value; + speed_t baud; + } + speeds[] = + { + { 0, B0 }, + { 50, B50 }, + { 75, B75 }, + { 110, B110 }, + { 134, B134 }, + { 150, B150 }, + { 200, B200 }, + { 300, B300 }, + { 600, B600 }, + { 1200, B1200 }, + { 1800, B1800 }, + { 2400, B2400 }, + { 4800, B4800 }, + { 9600, B9600 }, + { 19200, B19200 }, + { 38400, B38400 }, + { 57600, B57600 }, + { 115200, B115200 }, + { 230400, B230400 }, + }; + + if(speed < 0) + { + throw bad_speed(); + } + + for(const auto& s : speeds) + { + if(speed == s.value) + { + return s.baud; + } + + if(speed < s.value) + { + return (*((&s)-1)).baud; + } + } + + return speeds[sizeof(speeds) / sizeof(*speeds) - 1].baud; +} diff --git a/examples/getbaud.h b/examples/getbaud.h new file mode 100644 index 0000000..db7b092 --- /dev/null +++ b/examples/getbaud.h @@ -0,0 +1,12 @@ +#pragma once + +#include + +//! Exception +struct bad_speed {}; + +//! Convert requested int based speed to corresponding speed_t enum value. +//! The retuened enum value will be the closed value not bigger than the +//! requested value. +//! If no matching enum value could be found a bad_speed exception is thrown. +speed_t getBaud(int speed); -- cgit v1.2.3