diff options
-rw-r--r-- | include/cppunit/cppunit.h | 110 | ||||
-rw-r--r-- | test/direct_bt/CMakeLists.txt | 1 | ||||
-rw-r--r-- | test/direct_bt/test_attpdu01.cpp | 35 | ||||
-rw-r--r-- | test/direct_bt/test_btaddress01.cpp | 34 | ||||
-rw-r--r-- | test/direct_bt/test_uuid.cpp | 106 | ||||
-rw-r--r-- | test/ieee11073/CMakeLists.txt | 25 | ||||
-rw-r--r-- | test/ieee11073/test_datatypes01.cpp | 65 | ||||
-rw-r--r-- | test/ieee11073/test_ieee11073_01.cpp | 52 |
8 files changed, 143 insertions, 285 deletions
diff --git a/include/cppunit/cppunit.h b/include/cppunit/cppunit.h deleted file mode 100644 index 09c60fb4..00000000 --- a/include/cppunit/cppunit.h +++ /dev/null @@ -1,110 +0,0 @@ -#ifndef CPPUNIT_H -#define CPPUNIT_H - -// Required headers, or just use #include <bits/stdc++.h> -#include <iostream> -#include <sstream> -#include <cstring> -#include <string> -#include <ctime> -#include <cmath> - - -// CPlusPlusUnit - C++ Unit testing TDD framework (github.com/cppunit/cppunit) -class Cppunit { - - private: - static float machineFloatEpsilon() { - float x = 1.0f, res; - do { - res = x; - } while (1.0f + (x /= 2.0f) > 1.0f); - return res; - } - - static double machineDoubleEpsilon() { - double x = 1.0, res; - do { - res = x; - } while (1.0 + (x /= 2.0) > 1.0); - return res; - } - - public: - - #define PRINTM(m) print(m, __FILE__, __LINE__, __FUNCTION__); - #define CHECK(a,b) check<long long>("", a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); - #define CHECKM(m,a,b) check<long long>(m, a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); - #define CHECKD(m,a,b) checkDelta<double>(m, a, b, doubleEpsilon, #a, #b, __FILE__, __LINE__, __FUNCTION__); - #define CHECKDD(m,a,b,c) checkDelta<double>(m, a, b, c, #a, #b, __FILE__, __LINE__, __FUNCTION__); - #define CHECKT(a) check<bool>("", a, true, #a, "true", __FILE__, __LINE__, __FUNCTION__); - #define CHECKTM(m,a) check<bool>(m, a, true, #a, "true", __FILE__, __LINE__, __FUNCTION__); - #define CHECKS(a,b) check<cs>("", a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); - #define CHECKSM(m,a,b) check<cs>(m, a, b, #a, #b, __FILE__, __LINE__, __FUNCTION__); - - typedef const std::string& cs; - - int checks, fails; std::ostringstream serr; std::istringstream *in; - float floatEpsilon; - double doubleEpsilon; - - Cppunit() - : checks(0), fails(0), floatEpsilon(machineFloatEpsilon()), doubleEpsilon(machineDoubleEpsilon()) - {} - - virtual ~Cppunit() {} - - void test_cin(cs s){ in = new std::istringstream(s); std::cin.rdbuf(in->rdbuf()); } - - void fail_hdr(cs stra, cs strb, cs file, int line, cs func) { - serr << "==================================================" << std::endl; - serr << "FAIL: " << func << std::endl; - serr << "--------------------------------------------------" << std::endl; - serr << "File \"" << file << "\", line " << line << " in " << func << std::endl; - serr << " Checking " << stra << " == " << strb << std::endl; - } - - static void print(cs m, cs file, int line, cs func) { - std::cerr << std::endl << m << "; file \"" << file << "\", line " << line << " in " << func << std::endl; - } - - template <typename T> void check(cs m, T a, T b, cs stra, cs strb, cs file, int line, cs func) { - checks++; if (a == b) { std::cout << "."; return; } - fails++; std::cout << "F"; fail_hdr(stra, strb, file, line, func); - serr << " Error: " << m << ": \"" << a << "\" ! = \"" << b << "\"" << std::endl << std::endl; - } - - template <typename T> void checkDelta(cs m, T a, T b, T d, cs stra, cs strb, cs file, int line, cs func) { - checks++; if ( labs ( a - b ) < d ) { std::cout << "."; return; } - fails++; std::cout << "F"; fail_hdr(stra, strb, file, line, func); - serr << " Error: " << m << ": \"" << a << "\" ! = \"" << b << "\" (delta " << d << ")" << std::endl << std::endl; - } - - virtual void single_test() {} - virtual void test_list() { single_test(); } - double dclock() { return double(clock()) / CLOCKS_PER_SEC; } - int status() { - std::cout << std::endl; if (fails) std::cout << serr.str(); - std::cout << "--------------------------------------------------" << std::endl; - std::cout << "Ran " << checks << " checks in " << dclock() << "s" << std::endl << std::endl; - if (fails) std::cout << "FAILED (failures=" << fails << ")"; else std::cout << "OK" << std::endl; - return fails > 0; - } - int run() { std::streambuf* ocin = std::cin.rdbuf(); test_list(); std::cin.rdbuf(ocin); return status(); } -}; - -template<> void Cppunit::checkDelta<float>(cs m, float a, float b, float epsilon, cs stra, cs strb, cs file, int line, cs func) { - checks++; if ( fabsf( a - b ) < epsilon ) { std::cout << "."; return; } - fails++; std::cout << "F"; fail_hdr(stra, strb, file, line, func); - serr << " Error: " << m << ": \"" << a << "\" ! = \"" << b << "\" (epsilon " << epsilon << ")" << std::endl << std::endl; -} - -template<> void Cppunit::checkDelta<double>(cs m, double a, double b, double epsilon, cs stra, cs strb, cs file, int line, cs func) { - checks++; if ( fabs( a - b ) < epsilon ) { std::cout << "."; return; } - fails++; std::cout << "F"; fail_hdr(stra, strb, file, line, func); - serr << " Error: " << m << ": \"" << a << "\" ! = \"" << b << "\" (epsilon " << epsilon << ")" << std::endl << std::endl; -} - - -#endif // CPPUNIT_H - diff --git a/test/direct_bt/CMakeLists.txt b/test/direct_bt/CMakeLists.txt index 4ec160cb..f20c0b70 100644 --- a/test/direct_bt/CMakeLists.txt +++ b/test/direct_bt/CMakeLists.txt @@ -1,5 +1,4 @@ include_directories( - ${PROJECT_SOURCE_DIR}/include/cppunit ${PROJECT_SOURCE_DIR}/jaulib/include ${PROJECT_SOURCE_DIR}/api ) diff --git a/test/direct_bt/test_attpdu01.cpp b/test/direct_bt/test_attpdu01.cpp index 68abcd4c..69f0856e 100644 --- a/test/direct_bt/test_attpdu01.cpp +++ b/test/direct_bt/test_attpdu01.cpp @@ -3,7 +3,9 @@ #include <cinttypes> #include <cstring> -#include <cppunit.h> +#define CATCH_CONFIG_MAIN +#include <catch2/catch_amalgamated.hpp> +#include <jau/test/catch2_ext.hpp> #include <direct_bt/UUID.hpp> // #include <direct_bt/BTAddress.hpp> @@ -14,28 +16,17 @@ using namespace direct_bt; -// Test examples. -class Cppunit_tests: public Cppunit { - void single_test() override { - const uuid16_t uuid16 = uuid16_t(uuid16_t(0x1234)); - const AttReadByNTypeReq req(true /* group */, 1, 0xffff, uuid16); +TEST_CASE( "ATT PDU Test 01", "[datatype][attpdu]" ) { + const uuid16_t uuid16 = uuid16_t(uuid16_t(0x1234)); + const AttReadByNTypeReq req(true /* group */, 1, 0xffff, uuid16); - std::shared_ptr<const uuid_t> uuid16_2 = req.getNType(); - CHECK(uuid16.getTypeSizeInt(), 2); - CHECK(uuid16_2->getTypeSizeInt(), 2); - CHECKT( 0 == memcmp(uuid16.data(), uuid16_2->data(), 2) ) - CHECKT( uuid16.toString() == uuid16_2->toString() ); + std::shared_ptr<const uuid_t> uuid16_2 = req.getNType(); + REQUIRE(uuid16.getTypeSizeInt() == 2); + REQUIRE(uuid16_2->getTypeSizeInt() == 2); + REQUIRE( 0 == memcmp(uuid16.data(), uuid16_2->data(), 2) ); + REQUIRE( uuid16.toString() == uuid16_2->toString() ); - CHECK(req.getStartHandle(), 1); - CHECK(req.getEndHandle(), 0xffff); - } -}; - -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - Cppunit_tests test1; - return test1.run(); + REQUIRE(req.getStartHandle() == 1); + REQUIRE(req.getEndHandle() == 0xffff); } diff --git a/test/direct_bt/test_btaddress01.cpp b/test/direct_bt/test_btaddress01.cpp index 0952511f..1bd2a7c7 100644 --- a/test/direct_bt/test_btaddress01.cpp +++ b/test/direct_bt/test_btaddress01.cpp @@ -3,7 +3,9 @@ #include <cinttypes> #include <cstring> -#include <cppunit.h> +#define CATCH_CONFIG_MAIN +#include <catch2/catch_amalgamated.hpp> +#include <jau/test/catch2_ext.hpp> #include <jau/basic_types.hpp> #include <direct_bt/BTAddress.hpp> @@ -11,27 +13,11 @@ using namespace direct_bt; using namespace jau; -// Test examples. -class Cppunit_tests : public Cppunit { - public: - void single_test() override { - { - EUI48 mac01; - PRINTM("EUI48 size: whole0 "+std::to_string(sizeof(EUI48))); - PRINTM("EUI48 size: whole1 "+std::to_string(sizeof(mac01))); - PRINTM("EUI48 size: data1 "+std::to_string(sizeof(mac01.b))); - CHECKM("EUI48 struct and data size not matching", sizeof(EUI48), sizeof(mac01)); - CHECKM("EUI48 struct and data size not matching", sizeof(mac01), sizeof(mac01.b)); - } - - } -}; - -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - Cppunit_tests test1; - return test1.run(); +TEST_CASE( "EUI48 Test 01", "[datatype][eui48]" ) { + EUI48 mac01; + INFO_STR("EUI48 size: whole0 "+std::to_string(sizeof(EUI48))); + INFO_STR("EUI48 size: whole1 "+std::to_string(sizeof(mac01))); + INFO_STR("EUI48 size: data1 "+std::to_string(sizeof(mac01.b))); + REQUIRE_MSG("EUI48 struct and data size match", sizeof(EUI48) == sizeof(mac01)); + REQUIRE_MSG("EUI48 struct and data size match", sizeof(mac01) == sizeof(mac01.b)); } - diff --git a/test/direct_bt/test_uuid.cpp b/test/direct_bt/test_uuid.cpp index dd933a0f..0ba8cc12 100644 --- a/test/direct_bt/test_uuid.cpp +++ b/test/direct_bt/test_uuid.cpp @@ -3,71 +3,59 @@ #include <cinttypes> #include <cstring> -#include <cppunit.h> +#define CATCH_CONFIG_MAIN +#include <catch2/catch_amalgamated.hpp> +#include <jau/test/catch2_ext.hpp> #include <direct_bt/UUID.hpp> using namespace direct_bt; -// Test examples. -class Cppunit_tests : public Cppunit { - public: - void single_test() override { - - std::cout << "Hello COUT" << std::endl; - std::cerr << "Hello CERR" << std::endl; - - uint8_t buffer[100]; - static uint8_t uuid128_bytes[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, - 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; - - { - const uuid128_t v01 = uuid128_t(uuid128_bytes, 0, true); - CHECK(v01.getTypeSizeInt(), 16); - CHECK(v01.getTypeSizeInt(), sizeof(v01.value)); - CHECK(v01.getTypeSizeInt(), sizeof(v01.value.data)); - CHECKT( 0 == memcmp(uuid128_bytes, v01.data(), 16) ) - - put_uuid(buffer, 0, v01, true); - std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID128_SZ, buffer, 0, true); - CHECK(v02->getTypeSizeInt(), 16); - CHECKT( 0 == memcmp(v01.data(), v02->data(), 16) ) - CHECKT( v01.toString() == v02->toString() ); - } - - { - const uuid32_t v01 = uuid32_t(uuid32_t(0x12345678)); - CHECK(v01.getTypeSizeInt(), 4); - CHECK(v01.getTypeSizeInt(), sizeof(v01.value)); - CHECK(0x12345678, v01.value); - - put_uuid(buffer, 0, v01, true); - std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID32_SZ, buffer, 0, true); - CHECK(v02->getTypeSizeInt(), 4); - CHECKT( 0 == memcmp(v01.data(), v02->data(), 4) ) - CHECKT( v01.toString() == v02->toString() ); - } - - { - const uuid16_t v01 = uuid16_t(uuid16_t(0x1234)); - CHECK(v01.getTypeSizeInt(), 2); - CHECK(v01.getTypeSizeInt(), sizeof(v01.value)); - CHECK(0x1234, v01.value); - - put_uuid(buffer, 0, v01, true); - std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID16_SZ, buffer, 0, true); - CHECK(v02->getTypeSizeInt(), 2); - CHECKT( 0 == memcmp(v01.data(), v02->data(), 2) ) - CHECKT( v01.toString() == v02->toString() ); - } +TEST_CASE( "UUID Test 01", "[datatype][uuid]" ) { + std::cout << "Hello COUT" << std::endl; + std::cerr << "Hello CERR" << std::endl; + + uint8_t buffer[100]; + static uint8_t uuid128_bytes[] = { 0x00, 0x00, 0x00, 0x00, 0x00, 0x00, 0x10, 0x00, + 0x80, 0x00, 0x00, 0x80, 0x5F, 0x9B, 0x34, 0xFB }; + + { + const uuid128_t v01 = uuid128_t(uuid128_bytes, 0, true); + REQUIRE(v01.getTypeSizeInt() == 16); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value.data)); + REQUIRE( 0 == memcmp(uuid128_bytes, v01.data(), 16) ); + + put_uuid(buffer, 0, v01, true); + std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID128_SZ, buffer, 0, true); + REQUIRE(v02->getTypeSizeInt() == 16); + REQUIRE( 0 == memcmp(v01.data(), v02->data(), 16) ); + REQUIRE( v01.toString() == v02->toString() ); } -}; -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; + { + const uuid32_t v01 = uuid32_t(uuid32_t(0x12345678)); + REQUIRE(v01.getTypeSizeInt() == 4); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE(0x12345678 == v01.value); + + put_uuid(buffer, 0, v01, true); + std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID32_SZ, buffer, 0, true); + REQUIRE(v02->getTypeSizeInt() == 4); + REQUIRE( 0 == memcmp(v01.data(), v02->data(), 4) ); + REQUIRE( v01.toString() == v02->toString() ); + } - Cppunit_tests test1; - return test1.run(); + { + const uuid16_t v01 = uuid16_t(uuid16_t(0x1234)); + REQUIRE(v01.getTypeSizeInt() == 2); + REQUIRE(v01.getTypeSizeInt() == sizeof(v01.value)); + REQUIRE(0x1234 == v01.value); + + put_uuid(buffer, 0, v01, true); + std::shared_ptr<const uuid_t> v02 = uuid_t::create(uuid_t::TypeSize::UUID16_SZ, buffer, 0, true); + REQUIRE(v02->getTypeSizeInt() == 2); + REQUIRE( 0 == memcmp(v01.data(), v02->data(), 2) ); + REQUIRE( v01.toString() == v02->toString() ); + } } - diff --git a/test/ieee11073/CMakeLists.txt b/test/ieee11073/CMakeLists.txt index 4c3b05de..2ba595ae 100644 --- a/test/ieee11073/CMakeLists.txt +++ b/test/ieee11073/CMakeLists.txt @@ -1,10 +1,27 @@ include_directories( - ${PROJECT_SOURCE_DIR}/include/cppunit + ${PROJECT_SOURCE_DIR}/jaulib/include ${PROJECT_SOURCE_DIR}/api ) -add_executable (test_datatypes01 test_datatypes01.cpp) -target_link_libraries (test_datatypes01 direct_bt) +# These examples use the standard separate compilation +set( SOURCES_IDIOMATIC_EXAMPLES + test_ieee11073_01.cpp +) + +string( REPLACE ".cpp" "" BASENAMES_IDIOMATIC_EXAMPLES "${SOURCES_IDIOMATIC_EXAMPLES}" ) +set( TARGETS_IDIOMATIC_EXAMPLES ${BASENAMES_IDIOMATIC_EXAMPLES} ) + +foreach( name ${TARGETS_IDIOMATIC_EXAMPLES} ) + add_executable(${name} ${name}.cpp) +endforeach() + +set(ALL_EXAMPLE_TARGETS + ${TARGETS_IDIOMATIC_EXAMPLES} +) -add_test (NAME datatypes01 COMMAND test_datatypes01) +foreach(name ${ALL_EXAMPLE_TARGETS}) + target_link_libraries(${name} direct_bt catch2) + add_dependencies(${name} direct_bt catch2) + add_test (NAME ${name} COMMAND ${name}) +endforeach() diff --git a/test/ieee11073/test_datatypes01.cpp b/test/ieee11073/test_datatypes01.cpp deleted file mode 100644 index 10f03027..00000000 --- a/test/ieee11073/test_datatypes01.cpp +++ /dev/null @@ -1,65 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> - -#include <cppunit.h> - -#include "ieee11073/DataTypes.hpp" - -using namespace ieee11073; - -// Test examples. -class Cppunit_tests : public Cppunit { - private: - void test_float32_IEEE11073_to_IEEE754(const std::string msg, const uint32_t raw, const float expFloat) { - const float has = FloatTypes::float32_IEEE11073_to_IEEE754(raw); - PRINTM(msg+": has '"+std::to_string(has)); - PRINTM(msg+": exp '"+std::to_string(expFloat)+"', diff "+std::to_string(fabsf(has-expFloat))); - CHECKD(msg, has, expFloat); - } - - void test_AbsoluteTime_IEEE11073(const std::string msg, const uint8_t * data_le, const int size, const std::string expStr) { - ieee11073::AbsoluteTime has(data_le, size); - const std::string has_str = has.toString(); - PRINTM(msg+": has '"+has_str+"', len "+std::to_string(has_str.length())); - PRINTM(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(has_str==expStr)); - CHECKM(msg, has_str.length(), expStr.length()); - CHECKTM(msg, has_str == expStr); - } - - public: - void single_test() override { - - { - // 0x06 670100FF E40704040B1A00 00 - // 0x06 640100FF E40704040B2C00 00 - - // 79 09 00 FE -> 24.25f - test_float32_IEEE11073_to_IEEE754("IEEE11073-float01", 0xFE000979, 24.25f); - // 670100FF -> 35.900002 - test_float32_IEEE11073_to_IEEE754("IEEE11073-float01", 0xFF000167, 35.900002f); - // 640100FF -> 35.600002 - test_float32_IEEE11073_to_IEEE754("IEEE11073-float02", 0xFF000164, 35.600002f); - - { - // E40704040B1A00 -> 2020-04-04 11:26:00 - const uint8_t input[] = { 0xE4, 0x07, 0x04, 0x04, 0x0B, 0x1A, 0x00 }; - test_AbsoluteTime_IEEE11073("IEEE11073 time01", input, 7, "2020-04-04 11:26:00"); - } - { - // E40704040B2C00 -> 2020-04-04 11:44:00 - const uint8_t input[] = { 0xE4, 0x07, 0x04, 0x04, 0x0B, 0x2C, 0x00 }; - test_AbsoluteTime_IEEE11073("IEEE11073 time02", input, 7, "2020-04-04 11:44:00"); - } - } - } -}; - -int main(int argc, char *argv[]) { - (void) argc; - (void) argv; - Cppunit_tests test1; - return test1.run(); -} - diff --git a/test/ieee11073/test_ieee11073_01.cpp b/test/ieee11073/test_ieee11073_01.cpp new file mode 100644 index 00000000..707d4f51 --- /dev/null +++ b/test/ieee11073/test_ieee11073_01.cpp @@ -0,0 +1,52 @@ +#include <iostream> +#include <cassert> +#include <cinttypes> +#include <cstring> + +#define CATCH_CONFIG_MAIN +#include <catch2/catch_amalgamated.hpp> +#include <jau/test/catch2_ext.hpp> + +#include "ieee11073/DataTypes.hpp" + +using namespace ieee11073; + +static void test_float32_IEEE11073_to_IEEE754(const std::string msg, const uint32_t raw, const float expFloat) { + const float has = FloatTypes::float32_IEEE11073_to_IEEE754(raw); + INFO_STR(msg+": has '"+std::to_string(has)); + INFO_STR(msg+": exp '"+std::to_string(expFloat)+"', diff "+std::to_string(fabsf(has-expFloat))); + REQUIRE_EPSI_MSG(msg, has, expFloat); +} + +static void test_AbsoluteTime_IEEE11073(const std::string msg, const uint8_t * data_le, const int size, const std::string expStr) { + ieee11073::AbsoluteTime has(data_le, size); + const std::string has_str = has.toString(); + INFO_STR(msg+": has '"+has_str+"', len "+std::to_string(has_str.length())); + INFO_STR(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(has_str==expStr)); + REQUIRE_MSG(msg, has_str.length() == expStr.length()); + REQUIRE_MSG(msg, has_str == expStr); +} + +TEST_CASE( "Datatype IEEE11073 Test 01", "[datatype][IEEE11073]" ) { + // 0x06 670100FF E40704040B1A00 00 + // 0x06 640100FF E40704040B2C00 00 + + // 79 09 00 FE -> 24.25f + test_float32_IEEE11073_to_IEEE754("IEEE11073-float01", 0xFE000979, 24.25f); + // 670100FF -> 35.900002 + test_float32_IEEE11073_to_IEEE754("IEEE11073-float01", 0xFF000167, 35.900002f); + // 640100FF -> 35.600002 + test_float32_IEEE11073_to_IEEE754("IEEE11073-float02", 0xFF000164, 35.600002f); + + { + // E40704040B1A00 -> 2020-04-04 11:26:00 + const uint8_t input[] = { 0xE4, 0x07, 0x04, 0x04, 0x0B, 0x1A, 0x00 }; + test_AbsoluteTime_IEEE11073("IEEE11073 time01", input, 7, "2020-04-04 11:26:00"); + } + { + // E40704040B2C00 -> 2020-04-04 11:44:00 + const uint8_t input[] = { 0xE4, 0x07, 0x04, 0x04, 0x0B, 0x2C, 0x00 }; + test_AbsoluteTime_IEEE11073("IEEE11073 time02", input, 7, "2020-04-04 11:44:00"); + } +} + |