summaryrefslogtreecommitdiff
path: root/examples
diff options
context:
space:
mode:
authorBent Bisballe Nyeng <deva@aasimon.org>2021-09-28 17:05:31 +0200
committerBent Bisballe Nyeng <deva@aasimon.org>2021-09-28 17:05:31 +0200
commitbc078da645412c6b36ef59e635d6c35d11088c96 (patch)
treecd337c7a4d62d13d8f587f6fd02e6cc33961104c /examples
parentc61b229a213ac1f1cc50c6c4af53131578a91d53 (diff)
Improve exampleHEADmasterdevelop
Diffstat (limited to 'examples')
-rw-r--r--examples/ExampleTest.cc140
-rw-r--r--examples/Makefile2
-rw-r--r--examples/getbaud.cc48
-rw-r--r--examples/getbaud.h33
4 files changed, 136 insertions, 87 deletions
diff --git a/examples/ExampleTest.cc b/examples/ExampleTest.cc
index 8947c9b..aebe712 100644
--- a/examples/ExampleTest.cc
+++ b/examples/ExampleTest.cc
@@ -1,7 +1,35 @@
#include <uunit.h>
+#include <iostream>
#include "getbaud.h"
+std::ostream& operator<<(std::ostream& stream, const Speed& speed)
+{
+ switch(speed)
+ {
+ case Speed::B0: stream << "Speed::B0"; break;
+ case Speed::B50: stream << "Speed::B50"; break;
+ case Speed::B75: stream << "Speed::B75"; break;
+ case Speed::B110: stream << "Speed::B110"; break;
+ case Speed::B134: stream << "Speed::B134"; break;
+ case Speed::B150: stream << "Speed::B150"; break;
+ case Speed::B200: stream << "Speed::B200"; break;
+ case Speed::B300: stream << "Speed::B300"; break;
+ case Speed::B600: stream << "Speed::B600"; break;
+ case Speed::B1200: stream << "Speed::B1200"; break;
+ case Speed::B1800: stream << "Speed::B1800"; break;
+ case Speed::B2400: stream << "Speed::B2400"; break;
+ case Speed::B4800: stream << "Speed::B4800"; break;
+ case Speed::B9600: stream << "Speed::B9600"; break;
+ case Speed::B19200: stream << "Speed::B19200"; break;
+ case Speed::B38400: stream << "Speed::B38400"; break;
+ case Speed::B57600: stream << "Speed::B57600"; break;
+ case Speed::B115200: stream << "Speed::B115200"; break;
+ case Speed::B230400: stream << "Speed::B230400"; break;
+ }
+ return stream;
+}
+
class ExampleTest
: public uUnit
{
@@ -14,80 +42,80 @@ public:
void boundaryTests()
{
- uASSERT_EQUAL(B0, getBaud(0));
- uASSERT_EQUAL(B0, getBaud(1));
- uASSERT_EQUAL(B0, getBaud(49));
+ uASSERT_EQUAL(Speed::B0, getBaud(0));
+ uASSERT_EQUAL(Speed::B0, getBaud(1));
+ uASSERT_EQUAL(Speed::B0, getBaud(49));
- uASSERT_EQUAL(B50, getBaud(50));
- uASSERT_EQUAL(B50, getBaud(51));
- uASSERT_EQUAL(B50, getBaud(74));
+ uASSERT_EQUAL(Speed::B50, getBaud(50));
+ uASSERT_EQUAL(Speed::B50, getBaud(51));
+ uASSERT_EQUAL(Speed::B50, getBaud(74));
- uASSERT_EQUAL(B75, getBaud(75));
- uASSERT_EQUAL(B75, getBaud(76));
- uASSERT_EQUAL(B75, getBaud(109));
+ uASSERT_EQUAL(Speed::B75, getBaud(75));
+ uASSERT_EQUAL(Speed::B75, getBaud(76));
+ uASSERT_EQUAL(Speed::B75, getBaud(109));
- uASSERT_EQUAL(B110, getBaud(110));
- uASSERT_EQUAL(B110, getBaud(111));
- uASSERT_EQUAL(B110, getBaud(133));
+ uASSERT_EQUAL(Speed::B110, getBaud(110));
+ uASSERT_EQUAL(Speed::B110, getBaud(111));
+ uASSERT_EQUAL(Speed::B110, getBaud(133));
- uASSERT_EQUAL(B134, getBaud(134));
- uASSERT_EQUAL(B134, getBaud(135));
- uASSERT_EQUAL(B134, getBaud(149));
+ uASSERT_EQUAL(Speed::B134, getBaud(134));
+ uASSERT_EQUAL(Speed::B134, getBaud(135));
+ uASSERT_EQUAL(Speed::B134, getBaud(149));
- uASSERT_EQUAL(B150, getBaud(150));
- uASSERT_EQUAL(B150, getBaud(151));
- uASSERT_EQUAL(B150, getBaud(199));
+ uASSERT_EQUAL(Speed::B150, getBaud(150));
+ uASSERT_EQUAL(Speed::B150, getBaud(151));
+ uASSERT_EQUAL(Speed::B150, getBaud(199));
- uASSERT_EQUAL(B200, getBaud(200));
- uASSERT_EQUAL(B200, getBaud(201));
- uASSERT_EQUAL(B200, getBaud(299));
+ uASSERT_EQUAL(Speed::B200, getBaud(200));
+ uASSERT_EQUAL(Speed::B200, getBaud(201));
+ uASSERT_EQUAL(Speed::B200, getBaud(299));
- uASSERT_EQUAL(B300, getBaud(300));
- uASSERT_EQUAL(B300, getBaud(301));
- uASSERT_EQUAL(B300, getBaud(599));
+ uASSERT_EQUAL(Speed::B300, getBaud(300));
+ uASSERT_EQUAL(Speed::B300, getBaud(301));
+ uASSERT_EQUAL(Speed::B300, getBaud(599));
- uASSERT_EQUAL(B600, getBaud(600));
- uASSERT_EQUAL(B600, getBaud(601));
- uASSERT_EQUAL(B600, getBaud(1199));
+ uASSERT_EQUAL(Speed::B600, getBaud(600));
+ uASSERT_EQUAL(Speed::B600, getBaud(601));
+ uASSERT_EQUAL(Speed::B600, getBaud(1199));
- uASSERT_EQUAL(B1200, getBaud(1200));
- uASSERT_EQUAL(B1200, getBaud(1201));
- uASSERT_EQUAL(B1200, getBaud(1799));
+ uASSERT_EQUAL(Speed::B1200, getBaud(1200));
+ uASSERT_EQUAL(Speed::B1200, getBaud(1201));
+ uASSERT_EQUAL(Speed::B1200, getBaud(1799));
- uASSERT_EQUAL(B1800, getBaud(1800));
- uASSERT_EQUAL(B1800, getBaud(1801));
- uASSERT_EQUAL(B1800, getBaud(2399));
+ uASSERT_EQUAL(Speed::B1800, getBaud(1800));
+ uASSERT_EQUAL(Speed::B1800, getBaud(1801));
+ uASSERT_EQUAL(Speed::B1800, getBaud(2399));
- uASSERT_EQUAL(B2400, getBaud(2400));
- uASSERT_EQUAL(B2400, getBaud(2401));
- uASSERT_EQUAL(B2400, getBaud(4799));
+ uASSERT_EQUAL(Speed::B2400, getBaud(2400));
+ uASSERT_EQUAL(Speed::B2400, getBaud(2401));
+ uASSERT_EQUAL(Speed::B2400, getBaud(4799));
- uASSERT_EQUAL(B4800, getBaud(4800));
- uASSERT_EQUAL(B4800, getBaud(4801));
- uASSERT_EQUAL(B4800, getBaud(9599));
+ uASSERT_EQUAL(Speed::B4800, getBaud(4800));
+ uASSERT_EQUAL(Speed::B4800, getBaud(4801));
+ uASSERT_EQUAL(Speed::B4800, getBaud(9599));
- uASSERT_EQUAL(B9600, getBaud(9600));
- uASSERT_EQUAL(B9600, getBaud(9601));
- uASSERT_EQUAL(B9600, getBaud(19199));
+ uASSERT_EQUAL(Speed::B9600, getBaud(9600));
+ uASSERT_EQUAL(Speed::B9600, getBaud(9601));
+ uASSERT_EQUAL(Speed::B9600, getBaud(19199));
- uASSERT_EQUAL(B19200, getBaud(19200));
- uASSERT_EQUAL(B19200, getBaud(19201));
- uASSERT_EQUAL(B19200, getBaud(38399));
+ uASSERT_EQUAL(Speed::B19200, getBaud(19200));
+ uASSERT_EQUAL(Speed::B19200, getBaud(19201));
+ uASSERT_EQUAL(Speed::B19200, getBaud(38399));
- uASSERT_EQUAL(B38400, getBaud(38400));
- uASSERT_EQUAL(B38400, getBaud(38401));
- uASSERT_EQUAL(B38400, getBaud(57599));
+ uASSERT_EQUAL(Speed::B38400, getBaud(38400));
+ uASSERT_EQUAL(Speed::B38400, getBaud(38401));
+ uASSERT_EQUAL(Speed::B38400, getBaud(57599));
- uASSERT_EQUAL(B57600, getBaud(57600));
- uASSERT_EQUAL(B57600, getBaud(57601));
- uASSERT_EQUAL(B57600, getBaud(115199));
+ uASSERT_EQUAL(Speed::B57600, getBaud(57600));
+ uASSERT_EQUAL(Speed::B57600, getBaud(57601));
+ uASSERT_EQUAL(Speed::B57600, getBaud(115199));
- uASSERT_EQUAL(B115200, getBaud(115200));
- uASSERT_EQUAL(B115200, getBaud(115201));
- uASSERT_EQUAL(B115200, getBaud(230399));
+ uASSERT_EQUAL(Speed::B115200, getBaud(115200));
+ uASSERT_EQUAL(Speed::B115200, getBaud(115201));
+ uASSERT_EQUAL(Speed::B115200, getBaud(230399));
- uASSERT_EQUAL(B230400, getBaud(230400));
- uASSERT_EQUAL(B230400, getBaud(230401));
+ uASSERT_EQUAL(Speed::B230400, getBaud(230400));
+ uASSERT_EQUAL(Speed::B230400, getBaud(230401));
}
void exceptionTests()
diff --git a/examples/Makefile b/examples/Makefile
index b40c09b..7ab11fa 100644
--- a/examples/Makefile
+++ b/examples/Makefile
@@ -3,7 +3,7 @@ TESTS=\
test_ExampleTest
CXX ?= g++
-CXXFLAGS ?= -g -std=c++17 -I..
+CXXFLAGS ?= -g -std=c++17 -Wall -Werror -Wextra -Wconversion -I..
all: ${TESTS}
diff --git a/examples/getbaud.cc b/examples/getbaud.cc
index e90c4f6..2488fc1 100644
--- a/examples/getbaud.cc
+++ b/examples/getbaud.cc
@@ -1,33 +1,33 @@
#include "getbaud.h"
-speed_t getBaud(int speed)
+Speed getBaud(int speed)
{
struct
{
int value;
- speed_t baud;
+ Speed speed;
}
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 },
+ { 0, Speed::B0 },
+ { 50, Speed::B50 },
+ { 75, Speed::B75 },
+ { 110, Speed::B110 },
+ { 134, Speed::B134 },
+ { 150, Speed::B150 },
+ { 200, Speed::B200 },
+ { 300, Speed::B300 },
+ { 600, Speed::B600 },
+ { 1200, Speed::B1200 },
+ { 1800, Speed::B1800 },
+ { 2400, Speed::B2400 },
+ { 4800, Speed::B4800 },
+ { 9600, Speed::B9600 },
+ { 19200, Speed::B19200 },
+ { 38400, Speed::B38400 },
+ { 57600, Speed::B57600 },
+ { 115200, Speed::B115200 },
+ { 230400, Speed::B230400 },
};
if(speed < 0)
@@ -39,14 +39,14 @@ speed_t getBaud(int speed)
{
if(speed == s.value)
{
- return s.baud;
+ return s.speed;
}
if(speed < s.value)
{
- return (*((&s)-1)).baud;
+ return (*((&s)-1)).speed;
}
}
- return speeds[sizeof(speeds) / sizeof(*speeds) - 1].baud;
+ return speeds[sizeof(speeds) / sizeof(*speeds) - 1].speed;
}
diff --git a/examples/getbaud.h b/examples/getbaud.h
index db7b092..3cd1ff3 100644
--- a/examples/getbaud.h
+++ b/examples/getbaud.h
@@ -1,12 +1,33 @@
#pragma once
-#include <termios.h>
+enum class Speed
+{
+ B0,
+ B50,
+ B75,
+ B110,
+ B134,
+ B150,
+ B200,
+ B300,
+ B600,
+ B1200,
+ B1800,
+ B2400,
+ B4800,
+ B9600,
+ B19200,
+ B38400,
+ B57600,
+ B115200,
+ B230400,
+};
//! 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);
+//! Convert requested int based speed to corresponding Speed enum class value.
+//! The returned enum class value is the closest value not bigger than the
+//! requested int value.
+//! If no matching enum class value is found a bad_speed exception is thrown.
+Speed getBaud(int speed);