diff options
author | Sven Gothel <[email protected]> | 2020-10-16 04:31:06 +0200 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2020-10-16 04:31:06 +0200 |
commit | b477d8d7057d367feb0d87cccb212221b5992f82 (patch) | |
tree | 6fb66f2349363f871d15a121bd583841b2b39c39 /test | |
parent | 50fcf6a3781b4f3be7899c0e4a42c7b4a1505833 (diff) |
Extract common C++ Support Library inclusive Java JNI Binding to sub-project jaucpp, namespace jau
Diffstat (limited to 'test')
-rw-r--r-- | test/direct_bt/CMakeLists.txt | 67 | ||||
-rw-r--r-- | test/direct_bt/test_basictypes01.cpp | 79 | ||||
-rw-r--r-- | test/direct_bt/test_functiondef01.cpp | 319 | ||||
-rw-r--r-- | test/direct_bt/test_lfringbuffer01.cpp | 336 | ||||
-rw-r--r-- | test/direct_bt/test_lfringbuffer11.cpp | 190 | ||||
-rw-r--r-- | test/direct_bt/test_mm_sc_drf_00.cpp | 243 | ||||
-rw-r--r-- | test/direct_bt/test_mm_sc_drf_01.cpp | 237 |
7 files changed, 6 insertions, 1465 deletions
diff --git a/test/direct_bt/CMakeLists.txt b/test/direct_bt/CMakeLists.txt index 135a2766..02bbf978 100644 --- a/test/direct_bt/CMakeLists.txt +++ b/test/direct_bt/CMakeLists.txt @@ -1,18 +1,19 @@ include_directories( ${PROJECT_SOURCE_DIR}/include/cppunit + ${PROJECT_SOURCE_DIR}/jaucpp/include ${PROJECT_SOURCE_DIR}/api ) -add_executable (test_functiondef01 test_functiondef01.cpp) -set_target_properties(test_functiondef01 +add_executable (test_basictypes01 test_basictypes01.cpp) +set_target_properties(test_basictypes01 PROPERTIES CXX_STANDARD 17 CXX_STANDARD_REQUIRED ON COMPILE_FLAGS "-Wall -Wextra -Werror" ) -target_link_libraries (test_functiondef01 direct_bt) -add_dependencies(test_functiondef01 direct_bt) -add_test (NAME functiondef01 COMMAND test_functiondef01) +target_link_libraries (test_basictypes01 direct_bt) +add_dependencies(test_basictypes01 direct_bt) +add_test (NAME basictypes01 COMMAND test_basictypes01) add_executable (test_uuid test_uuid.cpp) set_target_properties(test_uuid @@ -25,17 +26,6 @@ target_link_libraries (test_uuid direct_bt) add_dependencies(test_uuid direct_bt) add_test (NAME uuid COMMAND test_uuid) -add_executable (test_basictypes01 test_basictypes01.cpp) -set_target_properties(test_basictypes01 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "-Wall -Wextra -Werror" -) -target_link_libraries (test_basictypes01 direct_bt) -add_dependencies(test_basictypes01 direct_bt) -add_test (NAME basictypes01 COMMAND test_basictypes01) - add_executable (test_attpdu01 test_attpdu01.cpp) set_target_properties(test_attpdu01 PROPERTIES @@ -47,48 +37,3 @@ target_link_libraries (test_attpdu01 direct_bt) add_dependencies(test_attpdu01 direct_bt) add_test (NAME attpdu01 COMMAND test_attpdu01) -add_executable (test_lfringbuffer01 test_lfringbuffer01.cpp) -set_target_properties(test_lfringbuffer01 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "-Wall -Wextra -Werror" -) -target_link_libraries (test_lfringbuffer01 direct_bt) -add_dependencies(test_lfringbuffer01 direct_bt) -add_test (NAME lfringbuffer01 COMMAND test_lfringbuffer01) - -add_executable (test_lfringbuffer11 test_lfringbuffer11.cpp) -set_target_properties(test_lfringbuffer11 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "-Wall -Wextra -Werror" -) -target_link_libraries (test_lfringbuffer11 direct_bt) -add_dependencies(test_lfringbuffer11 direct_bt) -add_test (NAME lfringbuffer11 COMMAND test_lfringbuffer11) - -add_executable (test_mm_sc_drf_00 test_mm_sc_drf_00.cpp) -set_target_properties(test_mm_sc_drf_00 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "-Wall -Wextra -Werror" -) -target_link_libraries (test_mm_sc_drf_00 direct_bt) -add_dependencies(test_mm_sc_drf_00 direct_bt) -add_test (NAME mm_sc_drf_00 COMMAND test_mm_sc_drf_00) - -add_executable (test_mm_sc_drf_01 test_mm_sc_drf_01.cpp) -set_target_properties(test_mm_sc_drf_01 - PROPERTIES - CXX_STANDARD 17 - CXX_STANDARD_REQUIRED ON - COMPILE_FLAGS "-Wall -Wextra -Werror" -) -target_link_libraries (test_mm_sc_drf_01 direct_bt) -add_dependencies(test_mm_sc_drf_01 direct_bt) -add_test (NAME mm_sc_drf_01 COMMAND test_mm_sc_drf_01) - - diff --git a/test/direct_bt/test_basictypes01.cpp b/test/direct_bt/test_basictypes01.cpp deleted file mode 100644 index d44ac0c4..00000000 --- a/test/direct_bt/test_basictypes01.cpp +++ /dev/null @@ -1,79 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> - -#include <cppunit.h> - -#include <direct_bt/BasicTypes.hpp> -#include <direct_bt/BTAddress.hpp> - -using namespace direct_bt; - -// Test examples. -class Cppunit_tests : public Cppunit { - private: - - void test_int32_t(const std::string msg, const int32_t v, const int expStrLen, const std::string expStr) { - const std::string str = int32SeparatedString(v); - PRINTM(msg+": has '"+str+"', len "+std::to_string(str.length())); - PRINTM(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr)); - CHECKM(msg, str.length(), expStrLen); - CHECKTM(msg, str == expStr); - } - - void test_uint32_t(const std::string msg, const uint32_t v, const int expStrLen, const std::string expStr) { - const std::string str = uint32SeparatedString(v); - PRINTM(msg+": has '"+str+"', len "+std::to_string(str.length())); - PRINTM(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr)); - CHECKM(msg, str.length(), expStrLen); - CHECKTM(msg, str == expStr); - } - - void test_uint64_t(const std::string msg, const uint64_t v, const int expStrLen, const std::string expStr) { - const std::string str = uint64SeparatedString(v); - PRINTM(msg+": has '"+str+"', len "+std::to_string(str.length())); - PRINTM(msg+": exp '"+expStr+"', len "+std::to_string(expStr.length())+", equal: "+std::to_string(str==expStr)); - CHECKM(msg, str.length(), expStrLen); - CHECKTM(msg, str == expStr); - } - - public: - void single_test() override { - { - test_int32_t("INT32_MIN", INT32_MIN, 14, "-2,147,483,648"); - test_int32_t("int32_t -thousand", -1000, 6, "-1,000"); - test_int32_t("int32_t one", 1, 1, "1"); - test_int32_t("int32_t thousand", 1000, 5, "1,000"); - test_int32_t("INT32_MAX", INT32_MAX, 13, "2,147,483,647"); - - test_uint32_t("UINT32_MIN", 0, 1, "0"); - test_uint32_t("uint32_t one", 1, 1, "1"); - test_uint32_t("uint32_t thousand", 1000, 5, "1,000"); - test_uint32_t("UINT32_MAX", UINT32_MAX, 13, "4,294,967,295"); - - test_uint64_t("UINT64_MIN", 0, 1, "0"); - test_uint64_t("uint64_t one", 1, 1, "1"); - test_uint64_t("uint64_t thousand", 1000, 5, "1,000"); - test_uint64_t("UINT64_MAX", UINT64_MAX, 26, "18,446,744,073,709,551,615"); - } - { - 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(); -} - diff --git a/test/direct_bt/test_functiondef01.cpp b/test/direct_bt/test_functiondef01.cpp deleted file mode 100644 index 02872b66..00000000 --- a/test/direct_bt/test_functiondef01.cpp +++ /dev/null @@ -1,319 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> - -#include <cppunit.h> - -#include <direct_bt/FunctionDef.hpp> - -using namespace direct_bt; - -// Test examples. -class Cppunit_tests : public Cppunit { - private: - - int func2a_member(int i) { - int res = i+100; - return res;; - } - int func2b_member(int i) { - int res = i+1000; - return res;; - } - static int Func3a_static(int i) { - int res = i+100; - return res;; - } - static int Func3b_static(int i) { - int res = i+1000; - return res;; - } - - // template<typename R, typename... A> - typedef FunctionDef<int, int> MyClassFunction; - - struct IntOffset { - int value; - IntOffset(int v) : value(v) {} - - IntOffset(const IntOffset &o) - : value(o.value) - { - PRINTM("IntOffset::copy_ctor"); - } - IntOffset(IntOffset &&o) - : value(std::move(o.value)) - { - PRINTM("IntOffset::move_ctor"); - } - IntOffset& operator=(const IntOffset &o) { - PRINTM("IntOffset::copy_assign"); - if( &o == this ) { - return *this; - } - value = o.value; - return *this; - } - IntOffset& operator=(IntOffset &&o) { - PRINTM("IntOffset::move_assign"); - value = std::move(o.value); - (void)value; - return *this; - } - - bool operator==(const IntOffset& rhs) const { - if( &rhs == this ) { - return true; - } - return value == rhs.value; - } - - bool operator!=(const IntOffset& rhs) const - { return !( *this == rhs ); } - - }; - - void test_FunctionPointer00(std::string msg, bool expEqual, int value, int expRes, MyClassFunction & f1, MyClassFunction &f2) { - // test std::function identity - PRINTM(msg+": FunctionPointer00 Fun f1p == f2p : " + std::to_string( f1 == f2 ) + ", f1p: " + f1.toString() + ", f2 "+f2.toString() ); - int f1r = f1.invoke(value); - int f2r = f2.invoke(value); - PRINTM(msg+": FunctionPointer00 Res f1r == f2r : " + std::to_string( f1r == f2r ) + ", f1r: " + std::to_string( f1r ) + ", f2r "+std::to_string( f2r ) ); - if( expEqual ) { - CHECKM(msg, f1r, expRes); - CHECKM(msg, f2r, expRes); - CHECKTM(msg, f1 == f2); - } else { - CHECKTM(msg, f1 != f2); - } - } - void test_FunctionPointer01(std::string msg, bool expEqual, MyClassFunction & f1, MyClassFunction &f2) { - // test std::function identity - PRINTM(msg+": FunctionPointer00 Fun f1p == f2p : " + std::to_string( f1 == f2 ) + ", f1p: " + f1.toString() + ", f2 "+f2.toString() ); - if( expEqual ) { - CHECKTM(msg, f1 == f2); - } else { - CHECKTM(msg, f1 != f2); - } - } - - public: - void single_test() override { - - { - PRINTM("FuncPtr2_member: bindMemberFunc<int, Cppunit_tests, int>: START"); - // FunctionDef(Cppunit_tests &base, Func1Type func) - MyClassFunction f2a_1 = bindMemberFunc<int, Cppunit_tests, int>(this, &Cppunit_tests::func2a_member); - MyClassFunction f2a_2 = bindMemberFunc(this, &Cppunit_tests::func2a_member); - test_FunctionPointer00("FuncPtr2a_member_11", true, 1, 101, f2a_1, f2a_1); - test_FunctionPointer00("FuncPtr2a_member_12", true, 1, 101, f2a_1, f2a_2); - - MyClassFunction f2b_1 = bindMemberFunc(this, &Cppunit_tests::func2b_member); - MyClassFunction f2b_2 = bindMemberFunc(this, &Cppunit_tests::func2b_member); - test_FunctionPointer00("FuncPtr2b_member_11", true, 1, 1001, f2b_1, f2b_1); - test_FunctionPointer00("FuncPtr2b_member_12", true, 1, 1001, f2b_1, f2b_2); - - test_FunctionPointer00("FuncPtr2ab_member_11", false, 1, 0, f2a_1, f2b_1); - test_FunctionPointer00("FuncPtr2ab_member_22", false, 1, 0, f2a_2, f2b_2); - PRINTM("FuncPtr2_member: bindMemberFunc<int, Cppunit_tests, int>: END"); - } - { - PRINTM("FuncPtr3_plain: bindPlainFunc<int, int>: START"); - // FunctionDef(Func1Type func) - MyClassFunction f3a_1 = bindPlainFunc<int, int>(&Cppunit_tests::Func3a_static); - MyClassFunction f3a_2 = bindPlainFunc(&Cppunit_tests::Func3a_static); - test_FunctionPointer00("FuncPtr3a_plain_11", true, 1, 101, f3a_1, f3a_1); - test_FunctionPointer00("FuncPtr3a_plain_12", true, 1, 101, f3a_1, f3a_2); - - MyClassFunction f3b_1 = bindPlainFunc(&Cppunit_tests::Func3b_static); - MyClassFunction f3b_2 = bindPlainFunc(&Func3b_static); - test_FunctionPointer00("FuncPtr3b_plain_11", true, 1, 1001, f3b_1, f3b_1); - test_FunctionPointer00("FuncPtr3b_plain_12", true, 1, 1001, f3b_1, f3b_2); - - test_FunctionPointer00("FuncPtr3ab_plain_11", false, 1, 0, f3a_1, f3b_1); - test_FunctionPointer00("FuncPtr3ab_plain_22", false, 1, 0, f3a_2, f3b_2); - PRINTM("FuncPtr3_plain: bindPlainFunc<int, int>: END"); - } - { - PRINTM("FuncPtr4_stdlambda: bindStdFunc<int, int>: START"); - // FunctionDef(Func1Type func) <int, int> - std::function<int(int i)> func4a_stdlambda = [](int i)->int { - int res = i+100; - return res;; - }; - std::function<int(int i)> func4b_stdlambda = [](int i)->int { - int res = i+1000; - return res;; - }; - MyClassFunction f4a_1 = bindStdFunc<int, int>(100, func4a_stdlambda); - MyClassFunction f4a_2 = bindStdFunc(100, func4a_stdlambda); - test_FunctionPointer00("FuncPtr4a_stdlambda_11", true, 1, 101, f4a_1, f4a_1); - test_FunctionPointer00("FuncPtr4a_stdlambda_12", true, 1, 101, f4a_1, f4a_2); - - MyClassFunction f4b_1 = bindStdFunc(200, func4b_stdlambda); - MyClassFunction f4b_2 = bindStdFunc(200, func4b_stdlambda); - test_FunctionPointer00("FuncPtr4b_stdlambda_11", true, 1, 1001, f4b_1, f4b_1); - test_FunctionPointer00("FuncPtr4b_stdlambda_12", true, 1, 1001, f4b_1, f4b_2); - - test_FunctionPointer00("FuncPtr4ab_stdlambda_11", false, 1, 0, f4a_1, f4b_1); - test_FunctionPointer00("FuncPtr4ab_stdlambda_22", false, 1, 0, f4a_2, f4b_2); - - MyClassFunction f4a_0 = bindStdFunc<int, int>(100); - MyClassFunction f4b_0 = bindStdFunc<int, int>(200); - test_FunctionPointer01("FuncPtr4a_stdlambda_01", true, f4a_0, f4a_1); - test_FunctionPointer01("FuncPtr4a_stdlambda_02", true, f4a_0, f4a_2); - test_FunctionPointer01("FuncPtr4b_stdlambda_01", true, f4b_0, f4b_1); - test_FunctionPointer01("FuncPtr4b_stdlambda_02", true, f4b_0, f4b_2); - test_FunctionPointer01("FuncPtr4ab_stdlambda_00", false, f4a_0, f4b_0); - test_FunctionPointer01("FuncPtr4ab_stdlambda_01", false, f4a_0, f4b_1); - test_FunctionPointer01("FuncPtr4ab_stdlambda_10", false, f4a_1, f4b_0); - PRINTM("FuncPtr4_stdlambda: bindStdFunc<int, int>: END"); - } - { - PRINTM("FuncPtr5_capture: bindCaptureFunc<int, int, int>: START"); - // bindCaptureFunc(I& data, R(*func)(I&, A...)) - // FunctionDef(Func1Type func) <int, int> - int offset100 = 100; - int offset1000 = 1000; - - int(*func5a_capture)(int&, int) = [](int& offset, int i)->int { - int res = i+10000+offset; - return res; - }; - int(*func5b_capture)(int&, int) = [](int& offset, int i)->int { - int res = i+100000+offset; - return res; - }; - -#if 0 - MyClassFunction f5a_o100_0 = bindCaptureFunc<int, int, int>(offset100, - [](int& offset, int i)->int { - int res = i+10000+offset; - return res;; - } ); - test_FunctionPointer01("FuncPtr5a_o100_capture_00", true, f5a_o100_0, f5a_o100_0); -#endif - MyClassFunction f5a_o100_1 = bindCaptureFunc<int, int, int>(offset100, func5a_capture); - MyClassFunction f5a_o100_2 = bindCaptureFunc(offset100, func5a_capture); - test_FunctionPointer01("FuncPtr5a_o100_capture_12", true, f5a_o100_1, f5a_o100_2); - test_FunctionPointer00("FuncPtr5a_o100_capture_11", true, 1, 10101, f5a_o100_1, f5a_o100_1); - test_FunctionPointer00("FuncPtr5a_o100_capture_12", true, 1, 10101, f5a_o100_1, f5a_o100_2); - // test_FunctionPointer01("FuncPtr5a_o100_capture_01", false, f5a_o100_0, f5a_o100_1); - MyClassFunction f5a_o1000_1 = bindCaptureFunc(offset1000, func5a_capture); - MyClassFunction f5a_o1000_2 = bindCaptureFunc(offset1000, func5a_capture); - test_FunctionPointer01("FuncPtr5a_o1000_capture_12", true, f5a_o1000_1, f5a_o1000_2); - test_FunctionPointer01("FuncPtr5a_o100_o1000_capture_11", false, f5a_o100_1, f5a_o1000_1); - - MyClassFunction f5b_o100_1 = bindCaptureFunc(offset100, func5b_capture); - MyClassFunction f5b_o100_2 = bindCaptureFunc(offset100, func5b_capture); - test_FunctionPointer00("FuncPtr5b_o100_capture_11", true, 1, 100101, f5b_o100_1, f5b_o100_1); - test_FunctionPointer00("FuncPtr5b_o100_capture_12", true, 1, 100101, f5b_o100_1, f5b_o100_2); - - test_FunctionPointer00("FuncPtr5ab_o100_capture_11", false, 1, 0, f5a_o100_1, f5b_o100_1); - test_FunctionPointer00("FuncPtr5ab_o100_capture_22", false, 1, 0, f5a_o100_2, f5b_o100_2); - PRINTM("FuncPtr5_capture: bindCaptureFunc<int, int, int>: END"); - } - { - PRINTM("FuncPtr6_capture: bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>: START"); - // bindCaptureFunc(I& data, R(*func)(I&, A...)) - // FunctionDef(Func1Type func) <int, int> - std::shared_ptr<IntOffset> offset100(new IntOffset(100)); - std::shared_ptr<IntOffset> offset1000(new IntOffset(1000)); - - int(*func6a_capture)(std::shared_ptr<IntOffset>&, int) = [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int { - int res = i+10000+sharedOffset->value; - return res; - }; - int(*func6b_capture)(std::shared_ptr<IntOffset>&, int) = [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int { - int res = i+100000+sharedOffset->value; - return res; - }; - -#if 0 - MyClassFunction f6a_o100_0 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100, - [](std::shared_ptr<IntOffset>& sharedOffset, int i)->int { - int res = i+10000+sharedOffset->value; - return res;; - } ); - test_FunctionPointer01("FuncPtr6a_o100_capture_00", true, f6a_o100_0, f6a_o100_0); -#endif - MyClassFunction f6a_o100_1 = bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>(offset100, func6a_capture); - MyClassFunction f6a_o100_2 = bindCaptureFunc(offset100, func6a_capture); - test_FunctionPointer01("FuncPtr6a_o100_capture_12", true, f6a_o100_1, f6a_o100_2); - test_FunctionPointer00("FuncPtr6a_o100_capture_11", true, 1, 10101, f6a_o100_1, f6a_o100_1); - test_FunctionPointer00("FuncPtr6a_o100_capture_12", true, 1, 10101, f6a_o100_1, f6a_o100_2); - // test_FunctionPointer01("FuncPtr6a_o100_capture_01", false, f6a_o100_0, f6a_o100_1); - MyClassFunction f6a_o1000_1 = bindCaptureFunc(offset1000, func6a_capture); - MyClassFunction f6a_o1000_2 = bindCaptureFunc(offset1000, func6a_capture); - test_FunctionPointer01("FuncPtr6a_o1000_capture_12", true, f6a_o1000_1, f6a_o1000_2); - test_FunctionPointer01("FuncPtr6a_o100_o1000_capture_11", false, f6a_o100_1, f6a_o1000_1); - - MyClassFunction f6b_o100_1 = bindCaptureFunc(offset100, func6b_capture); - MyClassFunction f6b_o100_2 = bindCaptureFunc(offset100, func6b_capture); - test_FunctionPointer00("FuncPtr6b_o100_capture_11", true, 1, 100101, f6b_o100_1, f6b_o100_1); - test_FunctionPointer00("FuncPtr6b_o100_capture_12", true, 1, 100101, f6b_o100_1, f6b_o100_2); - - test_FunctionPointer00("FuncPtr6ab_o100_capture_11", false, 1, 0, f6a_o100_1, f6b_o100_1); - test_FunctionPointer00("FuncPtr6ab_o100_capture_22", false, 1, 0, f6a_o100_2, f6b_o100_2); - PRINTM("FuncPtr6_capture: bindCaptureFunc<int, std::shared_ptr<IntOffset>, int>: END"); - } - { - PRINTM("FuncPtr7_capture: bindCaptureFunc<int, IntOffset, int>: START"); - // bindCaptureFunc(I& data, R(*func)(I&, A...)) - // FunctionDef(Func1Type func) <int, int> - IntOffset offset100(100); - IntOffset offset1000(1000); - - int(*func7a_capture)(IntOffset&, int) = [](IntOffset& sharedOffset, int i)->int { - int res = i+10000+sharedOffset.value; - return res; - }; - int(*func7b_capture)(IntOffset&, int) = [](IntOffset& sharedOffset, int i)->int { - int res = i+100000+sharedOffset.value; - return res; - }; - -#if 0 - MyClassFunction f7a_o100_0 = bindCaptureFunc<int, IntOffset, int>(offset100, - [](IntOffset& sharedOffset, int i)->int { - int res = i+10000+sharedOffset.value; - return res;; - } ); - test_FunctionPointer01("FuncPtr7a_o100_capture_00", true, f7a_o100_0, f7a_o100_0); -#endif - PRINTM("f7a_o100_1 copy_ctor"); - MyClassFunction f7a_o100_1 = bindCaptureFunc<int, IntOffset, int>(offset100, func7a_capture); - PRINTM("f7a_o100_1 copy_ctor done"); - PRINTM("f7a_o100_2 move_ctor"); - MyClassFunction f7a_o100_2 = bindCaptureFunc(IntOffset(100), func7a_capture); - PRINTM("f7a_o100_2 move_ctor done"); - test_FunctionPointer01("FuncPtr7a_o100_capture_12", true, f7a_o100_1, f7a_o100_2); - test_FunctionPointer00("FuncPtr7a_o100_capture_11", true, 1, 10101, f7a_o100_1, f7a_o100_1); - test_FunctionPointer00("FuncPtr7a_o100_capture_12", true, 1, 10101, f7a_o100_1, f7a_o100_2); - // test_FunctionPointer01("FuncPtr7a_o100_capture_01", false, f7a_o100_0, f7a_o100_1); - MyClassFunction f7a_o1000_1 = bindCaptureFunc(offset1000, func7a_capture); - MyClassFunction f7a_o1000_2 = bindCaptureFunc(offset1000, func7a_capture); - test_FunctionPointer01("FuncPtr7a_o1000_capture_12", true, f7a_o1000_1, f7a_o1000_2); - test_FunctionPointer01("FuncPtr7a_o100_o1000_capture_11", false, f7a_o100_1, f7a_o1000_1); - - MyClassFunction f7b_o100_1 = bindCaptureFunc(offset100, func7b_capture); - MyClassFunction f7b_o100_2 = bindCaptureFunc(offset100, func7b_capture); - test_FunctionPointer00("FuncPtr7b_o100_capture_11", true, 1, 100101, f7b_o100_1, f7b_o100_1); - test_FunctionPointer00("FuncPtr7b_o100_capture_12", true, 1, 100101, f7b_o100_1, f7b_o100_2); - - test_FunctionPointer00("FuncPtr7ab_o100_capture_11", false, 1, 0, f7a_o100_1, f7b_o100_1); - test_FunctionPointer00("FuncPtr7ab_o100_capture_22", false, 1, 0, f7a_o100_2, f7b_o100_2); - PRINTM("FuncPtr7_capture: bindCaptureFunc<int, IntOffset, int>: END"); - } - } -}; - -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - Cppunit_tests test1; - return test1.run(); -} - diff --git a/test/direct_bt/test_lfringbuffer01.cpp b/test/direct_bt/test_lfringbuffer01.cpp deleted file mode 100644 index 98f3c101..00000000 --- a/test/direct_bt/test_lfringbuffer01.cpp +++ /dev/null @@ -1,336 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> -#include <memory> - -#include <cppunit.h> - -#include <direct_bt/UUID.hpp> -#include <direct_bt/Ringbuffer.hpp> -#include <direct_bt/LFRingbuffer.hpp> - -using namespace direct_bt; - -class Integer { - public: - int value; - - Integer(int v) : value(v) {} - - Integer(const Integer &o) noexcept = default; - Integer(Integer &&o) noexcept = default; - Integer& operator=(const Integer &o) noexcept = default; - Integer& operator=(Integer &&o) noexcept = default; - - operator int() const { - return value; - } - int intValue() const { return value; } - static Integer valueOf(const int i) { return Integer(i); } -}; - -std::shared_ptr<Integer> NullInteger = nullptr; - -typedef std::shared_ptr<Integer> SharedType; -typedef Ringbuffer<SharedType> SharedTypeRingbuffer; -typedef LFRingbuffer<SharedType, nullptr> SharedTypeLFRingbuffer; - -// Test examples. -class Cppunit_tests : public Cppunit { - private: - - std::shared_ptr<SharedTypeRingbuffer> createEmpty(int initialCapacity) { - return std::shared_ptr<SharedTypeRingbuffer>(new SharedTypeLFRingbuffer(initialCapacity)); - } - std::shared_ptr<SharedTypeRingbuffer> createFull(const std::vector<std::shared_ptr<Integer>> & source) { - return std::shared_ptr<SharedTypeRingbuffer>(new SharedTypeLFRingbuffer(source)); - } - - std::vector<SharedType> createIntArray(const int capacity, const int startValue) { - std::vector<SharedType> array(capacity); - for(int i=0; i<capacity; i++) { - array[i] = SharedType(new Integer(startValue+i)); - } - return array; - } - - void readTestImpl(Ringbuffer<SharedType> &rb, bool clearRef, int capacity, int len, int startValue) { - (void) clearRef; - - int preSize = rb.getSize(); - CHECKM("Wrong capacity "+rb.toString(), capacity, rb.capacity()); - CHECKTM("Too low capacity to read "+std::to_string(len)+" elems: "+rb.toString(), capacity-len >= 0); - CHECKTM("Too low size to read "+std::to_string(len)+" elems: "+rb.toString(), preSize >= len); - CHECKTM("Is empty "+rb.toString(), !rb.isEmpty()); - - for(int i=0; i<len; i++) { - SharedType svI = rb.get(); - CHECKTM("Empty at read #"+std::to_string(i+1)+": "+rb.toString(), svI!=nullptr); - CHECKM("Wrong value at read #"+std::to_string(i+1)+": "+rb.toString(), startValue+i, svI->intValue()); - } - - CHECKM("Invalid size "+rb.toString(), preSize-len, rb.getSize()); - CHECKTM("Invalid free slots after reading "+std::to_string(len)+": "+rb.toString(), rb.getFreeSlots()>= len); - CHECKTM("Is full "+rb.toString(), !rb.isFull()); - } - - void writeTestImpl(Ringbuffer<SharedType> &rb, int capacity, int len, int startValue) { - int preSize = rb.getSize(); - - CHECKM("Wrong capacity "+rb.toString(), capacity, rb.capacity()); - CHECKTM("Too low capacity to write "+std::to_string(len)+" elems: "+rb.toString(), capacity-len >= 0); - CHECKTM("Too low size to write "+std::to_string(len)+" elems: "+rb.toString(), preSize+len <= capacity); - CHECKTM("Is full "+rb.toString(), !rb.isFull()); - - for(int i=0; i<len; i++) { - std::string m = "Buffer is full at put #"+std::to_string(i)+": "+rb.toString(); - CHECKTM(m, rb.put( SharedType( new Integer(startValue+i) ) ) ); - } - - CHECKM("Invalid size "+rb.toString(), preSize+len, rb.getSize()); - CHECKTM("Is empty "+rb.toString(), !rb.isEmpty()); - } - - void moveGetPutImpl(Ringbuffer<SharedType> &rb, int pos) { - CHECKTM("RB is empty "+rb.toString(), !rb.isEmpty()); - for(int i=0; i<pos; i++) { - CHECKM("MoveFull.get failed "+rb.toString(), i, rb.get()->intValue()); - CHECKTM("MoveFull.put failed "+rb.toString(), rb.put( SharedType( new Integer(i) ) ) ); - } - } - - void movePutGetImpl(Ringbuffer<SharedType> &rb, int pos) { - CHECKTM("RB is full "+rb.toString(), !rb.isFull()); - for(int i=0; i<pos; i++) { - CHECKTM("MoveEmpty.put failed "+rb.toString(), rb.put( SharedType( new Integer(600+i) ) ) ); - CHECKM("MoveEmpty.get failed "+rb.toString(), 600+i, rb.get()->intValue()); - } - } - - void test01_FullRead() { - int capacity = 11; - std::vector<SharedType> source = createIntArray(capacity, 0); - std::shared_ptr<SharedTypeRingbuffer> rb = createFull(source); - fprintf(stderr, "test01_FullRead: Created / %s\n", rb->toString().c_str()); - CHECKM("Not full size "+rb->toString(), capacity, rb->getSize()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, true, capacity, capacity, 0); - fprintf(stderr, "test01_FullRead: PostRead / %s\n", rb->toString().c_str()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test02_EmptyWrite() { - int capacity = 11; - std::shared_ptr<Ringbuffer<SharedType>> rb = createEmpty(capacity); - fprintf(stderr, "test01_EmptyWrite: Created / %s\n", rb->toString().c_str()); - CHECKM("Not zero size "+rb->toString(), 0, rb->getSize()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - writeTestImpl(*rb, capacity, capacity, 0); - fprintf(stderr, "test01_EmptyWrite: PostWrite / %s\n", rb->toString().c_str()); - CHECKM("Not full size "+rb->toString(), capacity, rb->getSize()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, true, capacity, capacity, 0); - fprintf(stderr, "test01_EmptyWrite: PostRead / %s\n", rb->toString().c_str()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test03_FullReadReset() { - int capacity = 11; - std::vector<SharedType> source = createIntArray(capacity, 0); - std::shared_ptr<Ringbuffer<SharedType>> rb = createFull(source); - fprintf(stderr, "test01_FullReadReset: Created / %s\n", rb->toString().c_str()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - rb->reset(source); - fprintf(stderr, "test01_FullReadReset: Post Reset w/ source / %s\n", rb->toString().c_str()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - fprintf(stderr, "test01_FullReadReset: Post Read / %s\n", rb->toString().c_str()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - rb->reset(source); - fprintf(stderr, "test01_FullReadReset: Post Reset w/ source / %s\n", rb->toString().c_str()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - fprintf(stderr, "test01_FullReadReset: Post Read / %s\n", rb->toString().c_str()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test04_EmptyWriteClear() { - int capacity = 11; - std::shared_ptr<Ringbuffer<SharedType>> rb = createEmpty(capacity); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - rb->clear(); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - writeTestImpl(*rb, capacity, capacity, 0); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - rb->clear(); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - writeTestImpl(*rb, capacity, capacity, 0); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test05_ReadResetMid01() { - int capacity = 11; - std::vector<SharedType> source = createIntArray(capacity, 0); - std::shared_ptr<Ringbuffer<SharedType>> rb = createFull(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - rb->reset(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, 5, 0); - CHECKTM("Is empty "+rb->toString(), !rb->isEmpty()); - CHECKTM("Is Full "+rb->toString(), !rb->isFull()); - - rb->reset(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test06_ReadResetMid02() { - int capacity = 11; - std::vector<SharedType> source = createIntArray(capacity, 0); - std::shared_ptr<Ringbuffer<SharedType>> rb = createFull(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - rb->reset(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - moveGetPutImpl(*rb, 5); - readTestImpl(*rb, false, capacity, 5, 5); - CHECKTM("Is empty "+rb->toString(), !rb->isEmpty()); - CHECKTM("Is Full "+rb->toString(), !rb->isFull()); - - rb->reset(source); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - readTestImpl(*rb, false, capacity, capacity, 0); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - } - - void test_GrowFullImpl(int initialCapacity, int pos) { - int growAmount = 5; - int grownCapacity = initialCapacity+growAmount; - std::vector<SharedType> source = createIntArray(initialCapacity, 0); - std::shared_ptr<Ringbuffer<SharedType>> rb = createFull(source); - - for(int i=0; i<initialCapacity; i++) { - SharedType svI = rb->get(); - CHECKTM("Empty at read #"+std::to_string(i+1)+": "+rb->toString(), svI!=nullptr); - CHECKM("Wrong value at read #"+std::to_string(i+1)+": "+rb->toString(), (0+i)%initialCapacity, svI->intValue()); - } - CHECKM("Not zero size "+rb->toString(), 0, rb->getSize()); - - rb->reset(source); - CHECKM("Not orig size "+rb->toString(), initialCapacity, rb->getSize()); - - moveGetPutImpl(*rb, pos); - // PRINTM("X02 "+rb->toString()); - // rb->dump(stderr, "X02"); - - rb->recapacity(grownCapacity); - CHECKM("Wrong capacity "+rb->toString(), grownCapacity, rb->capacity()); - CHECKM("Not orig size "+rb->toString(), initialCapacity, rb->getSize()); - CHECKTM("Is full "+rb->toString(), !rb->isFull()); - CHECKTM("Is empty "+rb->toString(), !rb->isEmpty()); - // PRINTM("X03 "+rb->toString()); - // rb->dump(stderr, "X03"); - - for(int i=0; i<growAmount; i++) { - CHECKTM("Buffer is full at put #"+std::to_string(i)+": "+rb->toString(), rb->put( SharedType( new Integer(100+i) ) ) ); - } - CHECKM("Not new size "+rb->toString(), grownCapacity, rb->getSize()); - CHECKTM("Not full "+rb->toString(), rb->isFull()); - - for(int i=0; i<initialCapacity; i++) { - SharedType svI = rb->get(); - // PRINTM("X05["+std::to_string(i)+"]: "+rb->toString()+", svI-null: "+std::to_string(svI==nullptr)); - CHECKTM("Empty at read #"+std::to_string(i+1)+": "+rb->toString(), svI!=nullptr); - CHECKM("Wrong value at read #"+std::to_string(i+1)+": "+rb->toString(), (pos+i)%initialCapacity, svI->intValue()); - } - - for(int i=0; i<growAmount; i++) { - SharedType svI = rb->get(); - // PRINTM("X07["+std::to_string(i)+"]: "+rb->toString()+", svI-null: "+std::to_string(svI==nullptr)); - CHECKTM("Empty at read #"+std::to_string(i+1)+": "+rb->toString(), svI!=nullptr); - CHECKM("Wrong value at read #"+std::to_string(i+1)+": "+rb->toString(), 100+i, svI->intValue()); - } - - CHECKM("Not zero size "+rb->toString(), 0, rb->getSize()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - CHECKTM("Is full "+rb->toString(), !rb->isFull()); - } - - public: - - void test20_GrowFull01_Begin() { - test_GrowFullImpl(11, 0); - } - void test21_GrowFull02_Begin1() { - test_GrowFullImpl(11, 0+1); - } - void test22_GrowFull03_Begin2() { - test_GrowFullImpl(11, 0+2); - } - void test23_GrowFull04_Begin3() { - test_GrowFullImpl(11, 0+3); - } - void test24_GrowFull05_End() { - test_GrowFullImpl(11, 11-1); - } - void test25_GrowFull11_End1() { - test_GrowFullImpl(11, 11-1-1); - } - void test26_GrowFull12_End2() { - test_GrowFullImpl(11, 11-1-2); - } - void test27_GrowFull13_End3() { - test_GrowFullImpl(11, 11-1-3); - } - - void test_list() override { - test01_FullRead(); - test02_EmptyWrite(); - test03_FullReadReset(); - test04_EmptyWriteClear(); - test05_ReadResetMid01(); - test06_ReadResetMid02(); - - test20_GrowFull01_Begin(); - test21_GrowFull02_Begin1(); - test22_GrowFull03_Begin2(); - test23_GrowFull04_Begin3(); - test24_GrowFull05_End(); - test25_GrowFull11_End1(); - test26_GrowFull12_End2(); - test27_GrowFull13_End3(); - } -}; - -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - Cppunit_tests test1; - return test1.run(); -} - diff --git a/test/direct_bt/test_lfringbuffer11.cpp b/test/direct_bt/test_lfringbuffer11.cpp deleted file mode 100644 index dcbba69e..00000000 --- a/test/direct_bt/test_lfringbuffer11.cpp +++ /dev/null @@ -1,190 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> -#include <memory> -#include <thread> -#include <pthread.h> - -#include <cppunit.h> - -#include <direct_bt/UUID.hpp> -#include <direct_bt/Ringbuffer.hpp> -#include <direct_bt/LFRingbuffer.hpp> - -using namespace direct_bt; - -class Integer { - public: - int value; - - Integer(int v) : value(v) {} - - Integer(const Integer &o) noexcept = default; - Integer(Integer &&o) noexcept = default; - Integer& operator=(const Integer &o) noexcept = default; - Integer& operator=(Integer &&o) noexcept = default; - - operator int() const { - return value; - } - int intValue() const { return value; } - static Integer valueOf(const int i) { return Integer(i); } -}; - -std::shared_ptr<Integer> NullInteger = nullptr; - -typedef std::shared_ptr<Integer> SharedType; -typedef Ringbuffer<SharedType> SharedTypeRingbuffer; -typedef LFRingbuffer<SharedType, nullptr> SharedTypeLFRingbuffer; - -// Test examples. -class Cppunit_tests : public Cppunit { - private: - - std::shared_ptr<SharedTypeRingbuffer> createEmpty(int initialCapacity) { - return std::shared_ptr<SharedTypeRingbuffer>(new SharedTypeLFRingbuffer(initialCapacity)); - } - std::shared_ptr<SharedTypeRingbuffer> createFull(const std::vector<std::shared_ptr<Integer>> & source) { - return std::shared_ptr<SharedTypeRingbuffer>(new SharedTypeLFRingbuffer(source)); - } - - std::vector<SharedType> createIntArray(const int capacity, const int startValue) { - std::vector<SharedType> array(capacity); - for(int i=0; i<capacity; i++) { - array[i] = SharedType(new Integer(startValue+i)); - } - return array; - } - - void getThreadType01(const std::string msg, std::shared_ptr<Ringbuffer<SharedType>> rb, int len, int startValue) { - // std::thread::id this_id = std::this_thread::get_id(); - // pthread_t this_id = pthread_self(); - - fprintf(stderr, "%s: Created / %s\n", msg.c_str(), rb->toString().c_str()); - for(int i=0; i<len; i++) { - SharedType svI = rb->getBlocking(); - CHECKTM(msg+": Empty at read #"+std::to_string(i+1)+": "+rb->toString(), svI!=nullptr); - fprintf(stderr, "%s: Got %d / %s\n", - msg.c_str(), svI->intValue(), rb->toString().c_str()); - if( 0 <= startValue ) { - CHECKM(msg+": %s: Wrong value at read #"+std::to_string(i+1)+": "+rb->toString(), startValue+i, svI->intValue()); - } - } - fprintf(stderr, "%s: Dies / %s\n", msg.c_str(), rb->toString().c_str()); - } - - void putThreadType01(const std::string msg, std::shared_ptr<Ringbuffer<SharedType>> rb, int len, int startValue) { - // std::thread::id this_id = std::this_thread::get_id(); - // pthread_t this_id = pthread_self(); - - fprintf(stderr, "%s: Created / %s\n", msg.c_str(), rb->toString().c_str()); - int preSize = rb->getSize(); - (void)preSize; - - for(int i=0; i<len; i++) { - Integer * vI = new Integer(startValue+i); - fprintf(stderr, "%s: Putting %d ... / %s\n", - msg.c_str(), vI->intValue(), rb->toString().c_str()); - rb->putBlocking( SharedType( vI ) ); - } - fprintf(stderr, "%s: Dies / %s\n", msg.c_str(), rb->toString().c_str()); - } - - public: - - void test01_Read1Write1() { - fprintf(stderr, "\n\ntest01_Read1Write1\n"); - int capacity = 100; - std::shared_ptr<SharedTypeRingbuffer> rb = createEmpty(capacity); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", rb, capacity, 0); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, "test01.put01", rb, capacity, 0); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - } - - void test02_Read4Write1() { - fprintf(stderr, "\n\ntest02_Read4Write1\n"); - int capacity = 400; - std::shared_ptr<SharedTypeRingbuffer> rb = createEmpty(capacity); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test02.get01", rb, capacity/4, -1); // @suppress("Invalid arguments") - std::thread getThread02(&Cppunit_tests::getThreadType01, this, "test02.get02", rb, capacity/4, -1); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, "test02.put01", rb, capacity, 0); // @suppress("Invalid arguments") - std::thread getThread03(&Cppunit_tests::getThreadType01, this, "test02.get03", rb, capacity/4, -1); // @suppress("Invalid arguments") - std::thread getThread04(&Cppunit_tests::getThreadType01, this, "test02.get04", rb, capacity/4, -1); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - getThread02.join(); - getThread03.join(); - getThread04.join(); - - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - } - - void test03_Read8Write2() { - fprintf(stderr, "\n\ntest03_Read8Write2\n"); - int capacity = 800; - std::shared_ptr<SharedTypeRingbuffer> rb = createEmpty(capacity); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test03.get01", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread getThread02(&Cppunit_tests::getThreadType01, this, "test03.get02", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, "test03.put01", rb, capacity/2, 0); // @suppress("Invalid arguments") - std::thread getThread03(&Cppunit_tests::getThreadType01, this, "test03.get03", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread getThread04(&Cppunit_tests::getThreadType01, this, "test03.get04", rb, capacity/8, -1); // @suppress("Invalid arguments") - - std::thread getThread05(&Cppunit_tests::getThreadType01, this, "test03.get05", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread getThread06(&Cppunit_tests::getThreadType01, this, "test03.get06", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread putThread02(&Cppunit_tests::putThreadType01, this, "test03.put02", rb, capacity/2, 400); // @suppress("Invalid arguments") - std::thread getThread07(&Cppunit_tests::getThreadType01, this, "test03.get07", rb, capacity/8, -1); // @suppress("Invalid arguments") - std::thread getThread08(&Cppunit_tests::getThreadType01, this, "test03.get08", rb, capacity/8, -1); // @suppress("Invalid arguments") - - putThread01.join(); - putThread02.join(); - getThread01.join(); - getThread02.join(); - getThread03.join(); - getThread04.join(); - getThread05.join(); - getThread06.join(); - getThread07.join(); - getThread08.join(); - - CHECKTM("Not empty "+rb->toString(), rb->isEmpty()); - CHECKM("Not empty size "+rb->toString(), 0, rb->getSize()); - } - - void test_list() override { - test01_Read1Write1(); - test02_Read4Write1(); - test03_Read8Write2(); - - test01_Read1Write1(); - test02_Read4Write1(); - test03_Read8Write2(); - - test03_Read8Write2(); - test03_Read8Write2(); - test03_Read8Write2(); - } -}; - -int main(int argc, char *argv[]) { - (void)argc; - (void)argv; - - Cppunit_tests test1; - return test1.run(); -} - diff --git a/test/direct_bt/test_mm_sc_drf_00.cpp b/test/direct_bt/test_mm_sc_drf_00.cpp deleted file mode 100644 index 505cbcc1..00000000 --- a/test/direct_bt/test_mm_sc_drf_00.cpp +++ /dev/null @@ -1,243 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> - -#include <atomic> -#include <memory> - -#include <thread> -#include <pthread.h> - -#include <cppunit.h> - -#include <direct_bt/OrderedAtomic.hpp> - -using namespace direct_bt; - -static int loops = 10; - -/** - * test_mm_sc_drf_00: Testing SC-DRF non-atomic global read and write within an atomic acquire/release critical block. - * <p> - * Modified non-atomic memory within the atomic acquire (load) and release (store) block, - * must be visible for all threads according to memory model (MM) Sequentially Consistent (SC) being data-race-free (DRF). - * <br> - * See Herb Sutter's 2013-12-23 slides p19, first box "It must be impossible for the assertion to fail – wouldn’t be SC.". - * </p> - * <p> - * This test's threads utilize a spin-lock, waiting for their turn. - * Such busy cycles were chosen to simplify the test and are not recommended - * as they expose poor performance on a high thread-count and hence long 'working thread pipe'. - * </p> - * See 'test_mm_sc_drf_01' implementing same test using mutex-lock and condition wait. - */ -class Cppunit_tests : public Cppunit { - private: - enum Defaults : int { - array_size = 10 - }; - constexpr int number(const Defaults rhs) noexcept { - return static_cast<int>(rhs); - } - - int value1 = 0; - int array[array_size] = { 0 }; - sc_atomic_int sync_value; - - void reset(int v1, int array_value) { - int _sync_value = sync_value; // SC-DRF acquire atomic - (void) _sync_value; - value1 = v1; - for(int i=0; i<array_size; i++) { - array[i] = array_value; - } - sync_value = v1; // SC-DRF release atomic - } - - void putThreadType01(int _len, int startValue) { - const int len = std::min(number(array_size), _len); - { - int _sync_value = sync_value; // SC-DRF acquire atomic - _sync_value = startValue; - for(int i=0; i<len; i++) { - array[i] = _sync_value+i; - } - value1 = startValue; - sync_value = _sync_value; // SC-DRF release atomic - } - } - void getThreadType01(const std::string msg, int _len, int startValue) { - const int len = std::min(number(array_size), _len); - - int _sync_value; - while( startValue != ( _sync_value = sync_value ) ) ; // SC-DRF acquire atomic with spin-lock waiting for startValue - CHECKM(msg+": %s: Wrong value at read value1 (sync)", _sync_value, value1); - CHECKM(msg+": %s: Wrong value at read value1 (start)", startValue, value1); - - for(int i=0; i<len; i++) { - int v = array[i]; - CHECKM(msg+": %s: Wrong sync value at read array #"+std::to_string(i), (_sync_value+i), v); - CHECKM(msg+": %s: Wrong start value at read array #"+std::to_string(i), (startValue+i), v); - } - sync_value = _sync_value; // SC-DRF release atomic - } - - void putThreadType11(int indexAndValue) { - const int idx = std::min(number(array_size)-1, indexAndValue); - { - // idx is encoded on sync_value (v) as follows - // v > 0: get @ idx = v -1 - // v < 0: put @ idx = abs(v) -1 - int _sync_value; - // SC-DRF acquire atomic with spin-lock waiting for encoded idx - do { - _sync_value = sync_value; - } while( idx != (_sync_value * -1) - 1 ); - // fprintf(stderr, "putThreadType11.done @ %d (has %d, exp %d)\n", idx, _sync_value, (idx+1)*-1); - _sync_value = idx; - value1 = idx; - array[idx] = idx; // last written checked first, SC-DRF should handle... - sync_value = _sync_value; // SC-DRF release atomic - } - } - void getThreadType11(const std::string msg, int _idx) { - const int idx = std::min(number(array_size)-1, _idx); - - // idx is encoded on sync_value (v) as follows - // v > 0: get @ idx = v -1 - // v < 0: put @ idx = abs(v) -1 - int _sync_value; - // SC-DRF acquire atomic with spin-lock waiting for idx - do { - _sync_value = sync_value; - } while( idx != _sync_value ); - CHECKM(msg+": %s: Wrong value at read array (a), idx "+std::to_string(idx), idx, array[idx]); // check last-written first - CHECKM(msg+": %s: Wrong value at read value1, idx "+std::to_string(idx), idx, value1); - CHECKM(msg+": %s: Wrong value at read sync, idx "+std::to_string(idx), idx, _sync_value); - // next write encoded idx - _sync_value = (idx+1)%array_size; - _sync_value = ( _sync_value + 1 ) * -1; - // fprintf(stderr, "getThreadType11.done for %d, next %d (v %d)\n", idx, (idx+1)%array_size, _sync_value); - value1 = _sync_value; - sync_value = _sync_value; // SC-DRF release atomic - } - - - public: - - Cppunit_tests() - : value1(0), sync_value(0) {} - - void test01_Read1Write1() { - fprintf(stderr, "\n\ntest01_Read1Write1.a\n"); - reset(0, 1010); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 3); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 3); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - } - - void test02_Read2Write1() { - fprintf(stderr, "\n\ntest01_Read2Write1.a\n"); - reset(0, 1021); - { - std::thread getThread00(&Cppunit_tests::getThreadType01, this, "test01.get00", array_size, 4); // @suppress("Invalid arguments") - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 4); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 4); // @suppress("Invalid arguments") - putThread01.join(); - getThread00.join(); - getThread01.join(); - } - - fprintf(stderr, "\n\ntest01_Read2Write1.b\n"); - reset(0, 1022); - { - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 5); // @suppress("Invalid arguments") - std::thread getThread00(&Cppunit_tests::getThreadType01, this, "test01.get00", array_size, 5); // @suppress("Invalid arguments") - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 5); // @suppress("Invalid arguments") - putThread01.join(); - getThread00.join(); - getThread01.join(); - } - } - - void test03_Read4Write1() { - fprintf(stderr, "\n\ntest02_Read4Write1\n"); - reset(0, 1030); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test02.get01", array_size, 6); // @suppress("Invalid arguments") - std::thread getThread02(&Cppunit_tests::getThreadType01, this, "test02.get02", array_size, 6); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 6); // @suppress("Invalid arguments") - std::thread getThread03(&Cppunit_tests::getThreadType01, this, "test02.get03", array_size, 6); // @suppress("Invalid arguments") - std::thread getThread04(&Cppunit_tests::getThreadType01, this, "test02.get04", array_size, 6); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - getThread02.join(); - getThread03.join(); - getThread04.join(); - } - - void test11_Read10Write10() { - fprintf(stderr, "\n\ntest11_Read10Write10\n"); - reset(-1, 1110); // start put idx 0 - - std::thread reader[array_size]; - std::thread writer[array_size]; - for(int i=0; i<number(array_size); i++) { - reader[i] = std::thread(&Cppunit_tests::getThreadType11, this, "test11.get11", i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i] = std::thread(&Cppunit_tests::putThreadType11, this, i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i].join(); - } - for(int i=0; i<number(array_size); i++) { - reader[i].join(); - } - } - - void test12_Read10Write10() { - fprintf(stderr, "\n\ntest12_Read10Write10\n"); - reset(-1, 1120); // start put idx 0 - - std::thread reader[array_size]; - std::thread writer[array_size]; - for(int i=0; i<number(array_size); i++) { - writer[i] = std::thread(&Cppunit_tests::putThreadType11, this, i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - reader[i] = std::thread(&Cppunit_tests::getThreadType11, this, "test12.get11", i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i].join(); - } - for(int i=0; i<number(array_size); i++) { - reader[i].join(); - } - } - - void test_list() override { - for(int i=loops; i>0; i--) { test01_Read1Write1(); } - for(int i=loops; i>0; i--) { test02_Read2Write1(); } - for(int i=loops; i>0; i--) { test03_Read4Write1(); } - for(int i=loops; i>0; i--) { test11_Read10Write10(); } - for(int i=loops; i>0; i--) { test12_Read10Write10(); } - } -}; - -int main(int argc, char *argv[]) { - for(int i=1; i<argc; i++) { - std::string arg(argv[i]); - if( "-loops" == arg && argc > i+1 ) { - loops = atoi(argv[i+1]); - } - } - fprintf(stderr, "Loops %d\n", loops); - - Cppunit_tests test1; - return test1.run(); -} - diff --git a/test/direct_bt/test_mm_sc_drf_01.cpp b/test/direct_bt/test_mm_sc_drf_01.cpp deleted file mode 100644 index faddc96d..00000000 --- a/test/direct_bt/test_mm_sc_drf_01.cpp +++ /dev/null @@ -1,237 +0,0 @@ -#include <iostream> -#include <cassert> -#include <cinttypes> -#include <cstring> - -#include <atomic> -#include <mutex> -#include <condition_variable> -#include <memory> - -#include <thread> -#include <pthread.h> - -#include <cppunit.h> - -#include <direct_bt/OrderedAtomic.hpp> - -using namespace direct_bt; - -static int loops = 10; - -/** - * test_mm_sc_drf_01: Testing SC-DRF non-atomic global read and write within a locked mutex critical block. - * <p> - * Modified non-atomic memory within the locked mutex acquire and release block, - * must be visible for all threads according to memory model (MM) Sequentially Consistent (SC) being data-race-free (DRF). - * <br> - * See Herb Sutter's 2013-12-23 slides p19, first box "It must be impossible for the assertion to fail – wouldn’t be SC.". - * </p> - * See 'test_mm_sc_drf_00' implementing same test using an atomic acquire/release critical block with spin-lock. - */ -class Cppunit_tests : public Cppunit { - private: - enum Defaults : int { - array_size = 10 - }; - constexpr int number(const Defaults rhs) noexcept { - return static_cast<int>(rhs); - } - - int value1 = 0; - int array[array_size] = { 0 }; - std::mutex mtx_value; - std::condition_variable cvRead; - std::condition_variable cvWrite; - - void reset(int v1, int array_value) { - std::unique_lock<std::mutex> lock(mtx_value); // SC-DRF acquire and release @ scope exit - value1 = v1; - for(int i=0; i<array_size; i++) { - array[i] = array_value; - } - } - - void putThreadType01(int _len, int startValue) { - const int len = std::min(number(array_size), _len); - { - std::unique_lock<std::mutex> lock(mtx_value); // SC-DRF acquire and release @ scope exit - for(int i=0; i<len; i++) { - array[i] = startValue+i; - } - value1 = startValue; - cvRead.notify_all(); // notify waiting getter - } - } - void getThreadType01(const std::string msg, int _len, int startValue) { - const int len = std::min(number(array_size), _len); - - std::unique_lock<std::mutex> lock(mtx_value); // SC-DRF acquire and release @ scope exit - while( startValue != value1 ) { - cvRead.wait(lock); - } - CHECKM(msg+": %s: Wrong value at read value1 (start)", startValue, value1); - - for(int i=0; i<len; i++) { - int v = array[i]; - CHECKM(msg+": %s: Wrong start value at read array #"+std::to_string(i), (startValue+i), v); - } - } - - void putThreadType11(int indexAndValue) { - const int idx = std::min(number(array_size)-1, indexAndValue); - { - // idx is encoded on sync_value (v) as follows - // v > 0: get @ idx = v -1 - // v < 0: put @ idx = abs(v) -1 - std::unique_lock<std::mutex> lock(mtx_value); // SC-DRF acquire and release @ scope exit - // SC-DRF acquire atomic with spin-lock waiting for encoded idx - while( idx != (value1 * -1) - 1 ) { - cvWrite.wait(lock); - } - // fprintf(stderr, "putThreadType11.done @ %d (has %d, exp %d)\n", idx, value1, (idx+1)*-1); - value1 = idx; - array[idx] = idx; // last written checked first, SC-DRF should handle... - cvRead.notify_all(); - } - } - void getThreadType11(const std::string msg, int _idx) { - const int idx = std::min(number(array_size)-1, _idx); - - // idx is encoded on sync_value (v) as follows - // v > 0: get @ idx = v -1 - // v < 0: put @ idx = abs(v) -1 - // SC-DRF acquire atomic with spin-lock waiting for idx - std::unique_lock<std::mutex> lock(mtx_value); - while( idx != value1 ) { - // fprintf(stderr, "getThreadType11.wait for has %d == exp %d\n", value1, idx); - cvRead.wait(lock); - } - CHECKM(msg+": %s: Wrong value at read array (idx), idx "+std::to_string(idx), idx, array[idx]); // check last-written first - CHECKM(msg+": %s: Wrong value at read value1 (idx), idx "+std::to_string(idx), idx, value1); - // next write encoded idx - int next_idx = (idx+1)%array_size; - next_idx = ( next_idx + 1 ) * -1; - // fprintf(stderr, "getThreadType11.done for %d, next %d (v %d)\n", idx, (idx+1)%array_size, next_idx); - value1 = next_idx; - cvWrite.notify_all(); - } - - - public: - - Cppunit_tests() - : value1(0) {} - - void test01_Read1Write1() { - fprintf(stderr, "\n\ntest01_Read1Write1.a\n"); - reset(0, 1010); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 3); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 3); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - } - - void test02_Read2Write1() { - fprintf(stderr, "\n\ntest01_Read2Write1.a\n"); - reset(0, 1021); - { - std::thread getThread00(&Cppunit_tests::getThreadType01, this, "test01.get00", array_size, 4); // @suppress("Invalid arguments") - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 4); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 4); // @suppress("Invalid arguments") - putThread01.join(); - getThread00.join(); - getThread01.join(); - } - - fprintf(stderr, "\n\ntest01_Read2Write1.b\n"); - reset(0, 1022); - { - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 5); // @suppress("Invalid arguments") - std::thread getThread00(&Cppunit_tests::getThreadType01, this, "test01.get00", array_size, 5); // @suppress("Invalid arguments") - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test01.get01", array_size, 5); // @suppress("Invalid arguments") - putThread01.join(); - getThread00.join(); - getThread01.join(); - } - } - - void test03_Read4Write1() { - fprintf(stderr, "\n\ntest02_Read4Write1\n"); - reset(0, 1030); - - std::thread getThread01(&Cppunit_tests::getThreadType01, this, "test02.get01", array_size, 6); // @suppress("Invalid arguments") - std::thread getThread02(&Cppunit_tests::getThreadType01, this, "test02.get02", array_size, 6); // @suppress("Invalid arguments") - std::thread putThread01(&Cppunit_tests::putThreadType01, this, array_size, 6); // @suppress("Invalid arguments") - std::thread getThread03(&Cppunit_tests::getThreadType01, this, "test02.get03", array_size, 6); // @suppress("Invalid arguments") - std::thread getThread04(&Cppunit_tests::getThreadType01, this, "test02.get04", array_size, 6); // @suppress("Invalid arguments") - putThread01.join(); - getThread01.join(); - getThread02.join(); - getThread03.join(); - getThread04.join(); - } - - void test11_Read10Write10() { - fprintf(stderr, "\n\ntest11_Read10Write10\n"); - reset(-1, 1110); - - std::thread reader[array_size]; - std::thread writer[array_size]; - for(int i=0; i<number(array_size); i++) { - reader[i] = std::thread(&Cppunit_tests::getThreadType11, this, "test11.get11", i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i] = std::thread(&Cppunit_tests::putThreadType11, this, i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i].join(); - } - for(int i=0; i<number(array_size); i++) { - reader[i].join(); - } - } - - void test12_Read10Write10() { - fprintf(stderr, "\n\ntest12_Read10Write10\n"); - reset(-1, 1120); - - std::thread reader[array_size]; - std::thread writer[array_size]; - for(int i=0; i<number(array_size); i++) { - writer[i] = std::thread(&Cppunit_tests::putThreadType11, this, i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - reader[i] = std::thread(&Cppunit_tests::getThreadType11, this, "test12.get11", i); // @suppress("Invalid arguments") // @suppress("Symbol is not resolved") - } - for(int i=0; i<number(array_size); i++) { - writer[i].join(); - } - for(int i=0; i<number(array_size); i++) { - reader[i].join(); - } - } - - void test_list() override { - for(int i=loops; i>0; i--) { test01_Read1Write1(); } - for(int i=loops; i>0; i--) { test02_Read2Write1(); } - for(int i=loops; i>0; i--) { test03_Read4Write1(); } - for(int i=loops; i>0; i--) { test11_Read10Write10(); } - for(int i=loops; i>0; i--) { test12_Read10Write10(); } - } -}; - -int main(int argc, char *argv[]) { - for(int i=1; i<argc; i++) { - std::string arg(argv[i]); - if( "-loops" == arg && argc > i+1 ) { - loops = atoi(argv[i+1]); - } - } - fprintf(stderr, "Loops %d\n", loops); - - Cppunit_tests test1; - return test1.run(); -} - |