From db1aa8a8a00c9a436ba72624511b4daa1cd9c385 Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 13:46:15 +0100 Subject: [PATCH 1/6] Remove the BASE flag. --- src/FGJSBBase.cpp | 2 - src/input_output/FGXMLElement.cpp | 1 + src/input_output/FGfdmSocket.cpp | 2 + src/input_output/string_utilities.cpp | 94 ++++++++++++++++++- src/input_output/string_utilities.h | 121 +++---------------------- src/simgear/xml/easyxml.cxx | 1 + tests/unit_tests/CMakeLists.txt | 8 +- tests/unit_tests/StringUtilitiesTest.h | 90 +++++++++--------- 8 files changed, 163 insertions(+), 156 deletions(-) diff --git a/src/FGJSBBase.cpp b/src/FGJSBBase.cpp index f1c6213912..30b39688be 100644 --- a/src/FGJSBBase.cpp +++ b/src/FGJSBBase.cpp @@ -35,8 +35,6 @@ HISTORY INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ -#define BASE - #include "FGJSBBase.h" #include "models/FGAtmosphere.h" diff --git a/src/input_output/FGXMLElement.cpp b/src/input_output/FGXMLElement.cpp index c7154287b0..5ac3697e93 100644 --- a/src/input_output/FGXMLElement.cpp +++ b/src/input_output/FGXMLElement.cpp @@ -28,6 +28,7 @@ INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ +#include #include // for assembling the error messages / what of exceptions. #include // using domain_error, invalid_argument, and length_error. #include "FGXMLElement.h" diff --git a/src/input_output/FGfdmSocket.cpp b/src/input_output/FGfdmSocket.cpp index ced3a16c0b..5034068912 100644 --- a/src/input_output/FGfdmSocket.cpp +++ b/src/input_output/FGfdmSocket.cpp @@ -51,6 +51,8 @@ INCLUDES #include #endif #include +#include +#include #include #include "FGfdmSocket.h" diff --git a/src/input_output/string_utilities.cpp b/src/input_output/string_utilities.cpp index 1d4fbd0eca..2031e9dcbe 100644 --- a/src/input_output/string_utilities.cpp +++ b/src/input_output/string_utilities.cpp @@ -35,12 +35,14 @@ INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ #include +#include +#include +#include #ifdef __APPLE__ #include #else #include #endif -#include #include "FGJSBBase.h" #include "string_utilities.h" @@ -51,6 +53,7 @@ typedef _locale_t locale_t; #define strtod_l _strtod_l #endif +namespace JSBSim { struct CNumericLocale { CNumericLocale() @@ -100,3 +103,92 @@ double atof_locale_c(const std::string& input) std::cerr << s.str() << std::endl; throw JSBSim::BaseException(s.str()); } + + +std::string& trim_left(std::string& str) +{ + while (!str.empty() && isspace((unsigned char)str[0])) { + str = str.erase(0,1); + } + return str; +} + +std::string& trim_right(std::string& str) +{ + while (!str.empty() && isspace((unsigned char)str[str.size()-1])) { + str = str.erase(str.size()-1,1); + } + return str; +} + +std::string& trim(std::string& str) +{ + if (str.empty()) return str; + std::string temp_str = trim_right(str); + return str = trim_left(temp_str); +} + +std::string& trim_all_space(std::string& str) +{ + for (size_t i=0; i split(std::string str, char d) +{ + std::vector str_array; + size_t index=0; + std::string temp = ""; + + trim(str); + index = str.find(d); + while (index != std::string::npos) { + temp = str.substr(0,index); + trim(temp); + if (!temp.empty()) str_array.push_back(temp); + str = str.erase(0,index+1); + index = str.find(d); + } + if (!str.empty()) { + temp = trim(str); + if (!temp.empty()) str_array.push_back(temp); + } + + return str_array; +} + +std::string replace(std::string str, const std::string& oldstr, const std::string& newstr) +{ + std::string temp = str; + size_t old_idx = str.find(oldstr); + if (old_idx != std::string::npos) { + temp = str.replace(old_idx, 1, newstr); + } + return temp; +} +}; diff --git a/src/input_output/string_utilities.h b/src/input_output/string_utilities.h index 005dd2b496..71a3ce0431 100644 --- a/src/input_output/string_utilities.h +++ b/src/input_output/string_utilities.h @@ -39,121 +39,26 @@ INCLUDES %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ #include -#include -#include #include -#include - -#include "JSBSim_API.h" /*%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% CLASS DECLARATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ -JSBSIM_API bool is_number(const std::string& str); -JSBSIM_API double atof_locale_c(const std::string& input); - -#if !defined(BASE) - extern std::string& trim_left(std::string& str); - extern std::string& trim_right(std::string& str); - extern std::string& trim(std::string& str); - extern std::string& trim_all_space(std::string& str); - extern std::string& to_upper(std::string& str); - extern std::string& to_lower(std::string& str); - std::vector split(std::string str, char d); - - extern std::string replace(std::string str, const std::string& old, const std::string& newstr); -#else - #include - - std::string& trim_left(std::string& str) - { - while (!str.empty() && isspace((unsigned char)str[0])) { - str = str.erase(0,1); - } - return str; - } - - std::string& trim_right(std::string& str) - { - while (!str.empty() && isspace((unsigned char)str[str.size()-1])) { - str = str.erase(str.size()-1,1); - } - return str; - } - - std::string& trim(std::string& str) - { - if (str.empty()) return str; - std::string temp_str = trim_right(str); - return str = trim_left(temp_str); - } - - std::string& trim_all_space(std::string& str) - { - for (size_t i=0; i split(std::string str, char d) - { - std::vector str_array; - size_t index=0; - std::string temp = ""; - - trim(str); - index = str.find(d); - while (index != std::string::npos) { - temp = str.substr(0,index); - trim(temp); - if (!temp.empty()) str_array.push_back(temp); - str = str.erase(0,index+1); - index = str.find(d); - } - if (!str.empty()) { - temp = trim(str); - if (!temp.empty()) str_array.push_back(temp); - } - - return str_array; - } - - std::string replace(std::string str, const std::string& oldstr, const std::string& newstr) - { - std::string temp = str; - size_t old_idx = str.find(oldstr); - if (old_idx != std::string::npos) { - temp = str.replace(old_idx, 1, newstr); - } - return temp; - } +namespace JSBSim { +double atof_locale_c(const std::string& input); -#endif +std::string& trim_left(std::string& str); +std::string& trim_right(std::string& str); +std::string& trim(std::string& str); +std::string& trim_all_space(std::string& str); +std::string& to_upper(std::string& str); +std::string& to_lower(std::string& str); +bool is_number(const std::string& str); +std::vector split(std::string str, char d); + +std::string replace(std::string str, const std::string& old, const std::string& newstr); +}; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/src/simgear/xml/easyxml.cxx b/src/simgear/xml/easyxml.cxx index 1b5725ffd4..3700bf24fd 100644 --- a/src/simgear/xml/easyxml.cxx +++ b/src/simgear/xml/easyxml.cxx @@ -23,6 +23,7 @@ INCLUDES #include #include +#include #include "FGJSBBase.h" diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 8f83b31542..eae6e9e559 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -19,9 +19,15 @@ set(UNIT_TESTS FGColumnVector3Test FGConditionTest FGPropertyManagerTest) +add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) + foreach(test ${UNIT_TESTS}) cxxtest_add_test(${test}1 ${test}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${test}.h) - target_link_libraries(${test}1 libJSBSim) + if (test STREQUAL "StringUtilitiesTest") + target_link_libraries(${test}1 libJSBSim string_utilities) + else() + target_link_libraries(${test}1 libJSBSim) + endif() add_coverage(${test}1) endforeach() diff --git a/tests/unit_tests/StringUtilitiesTest.h b/tests/unit_tests/StringUtilitiesTest.h index c82817f8ac..fa09ee000a 100644 --- a/tests/unit_tests/StringUtilitiesTest.h +++ b/tests/unit_tests/StringUtilitiesTest.h @@ -1,8 +1,6 @@ #include #include -#define BASE #include -#undef BASE #include "FGJSBBase.h" class StringUtilitiesTest : public CxxTest::TestSuite @@ -12,60 +10,60 @@ class StringUtilitiesTest : public CxxTest::TestSuite const std::string s_ref(" \t xx\t\tyy zz \t "); const std::string all_spaces(" \t \t\t "); std::string s = s_ref; - TS_ASSERT_EQUALS(trim_left(s), std::string("xx\t\tyy zz \t ")); - TS_ASSERT_EQUALS(trim_left(empty), empty); + TS_ASSERT_EQUALS(JSBSim::trim_left(s), std::string("xx\t\tyy zz \t ")); + TS_ASSERT_EQUALS(JSBSim::trim_left(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(trim_left(s), empty); + TS_ASSERT_EQUALS(JSBSim::trim_left(s), empty); s = s_ref; - TS_ASSERT_EQUALS(trim_right(s), std::string(" \t xx\t\tyy zz")); - TS_ASSERT_EQUALS(trim_right(empty), empty); + TS_ASSERT_EQUALS(JSBSim::trim_right(s), std::string(" \t xx\t\tyy zz")); + TS_ASSERT_EQUALS(JSBSim::trim_right(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(trim_right(s), empty); + TS_ASSERT_EQUALS(JSBSim::trim_right(s), empty); s = s_ref; - TS_ASSERT_EQUALS(trim(s), std::string("xx\t\tyy zz")); - TS_ASSERT_EQUALS(trim(empty), empty); + TS_ASSERT_EQUALS(JSBSim::trim(s), std::string("xx\t\tyy zz")); + TS_ASSERT_EQUALS(JSBSim::trim(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(trim(s), empty); + TS_ASSERT_EQUALS(JSBSim::trim(s), empty); s = s_ref; - TS_ASSERT_EQUALS(trim_all_space(s), std::string("xxyyzz")); - TS_ASSERT_EQUALS(trim_all_space(empty), empty); + TS_ASSERT_EQUALS(JSBSim::trim_all_space(s), std::string("xxyyzz")); + TS_ASSERT_EQUALS(JSBSim::trim_all_space(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(trim_all_space(s), empty); + TS_ASSERT_EQUALS(JSBSim::trim_all_space(s), empty); } void testStringCase() { const std::string s_ref(" MiXed\tCaSE; "); std::string s = s_ref; - TS_ASSERT_EQUALS(to_upper(s), std::string(" MIXED\tCASE; ")); - TS_ASSERT_EQUALS(to_upper(empty), empty); + TS_ASSERT_EQUALS(JSBSim::to_upper(s), std::string(" MIXED\tCASE; ")); + TS_ASSERT_EQUALS(JSBSim::to_upper(empty), empty); s = s_ref; - TS_ASSERT_EQUALS(to_lower(s), std::string(" mixed\tcase; ")); - TS_ASSERT_EQUALS(to_lower(empty), empty); + TS_ASSERT_EQUALS(JSBSim::to_lower(s), std::string(" mixed\tcase; ")); + TS_ASSERT_EQUALS(JSBSim::to_lower(empty), empty); } void testNumberString() { - TS_ASSERT(is_number("1.0")); - TS_ASSERT(is_number("1526")); - TS_ASSERT(is_number(".01256")); - TS_ASSERT(is_number("-1.0e+1")); - TS_ASSERT(!is_number(empty)); - TS_ASSERT(!is_number("125x5#")); + TS_ASSERT(JSBSim::is_number("1.0")); + TS_ASSERT(JSBSim::is_number("1526")); + TS_ASSERT(JSBSim::is_number(".01256")); + TS_ASSERT(JSBSim::is_number("-1.0e+1")); + TS_ASSERT(!JSBSim::is_number(empty)); + TS_ASSERT(!JSBSim::is_number("125x5#")); } void testSplit() { - std::vector list = split(empty,','); + std::vector list = JSBSim::split(empty,','); TS_ASSERT_EQUALS(list.size(), 0); - list = split(std::string(",,,,,"),','); + list = JSBSim::split(std::string(",,,,,"),','); TS_ASSERT_EQUALS(list.size(), 0); - list = split(std::string(" xx yy zz "),','); + list = JSBSim::split(std::string(" xx yy zz "),','); TS_ASSERT_EQUALS(list.size(), 1); TS_ASSERT_EQUALS(list[0], std::string("xx yy zz")); - list = split(std::string(" xx yy zz "),' '); + list = JSBSim::split(std::string(" xx yy zz "),' '); TS_ASSERT_EQUALS(list.size(), 3); TS_ASSERT_EQUALS(list[0], std::string("xx")); TS_ASSERT_EQUALS(list[1], std::string("yy")); TS_ASSERT_EQUALS(list[2], std::string("zz")); - list = split(",xx,,yy,zz,",','); + list = JSBSim::split(",xx,,yy,zz,",','); TS_ASSERT_EQUALS(list.size(), 3); TS_ASSERT_EQUALS(list[0], std::string("xx")); TS_ASSERT_EQUALS(list[1], std::string("yy")); @@ -73,30 +71,34 @@ class StringUtilitiesTest : public CxxTest::TestSuite } void testReplace() { - TS_ASSERT_EQUALS(replace(empty, std::string("x"), std::string("a")), empty); - TS_ASSERT_EQUALS(replace(std::string(" xyzzu "), std::string("x"), + TS_ASSERT_EQUALS(JSBSim::replace(empty, std::string("x"), std::string("a")), empty); + TS_ASSERT_EQUALS(JSBSim::replace(std::string(" xyzzu "), std::string("x"), std::string("a")), std::string(" ayzzu ")); - TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("x"), + TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("x"), std::string("a")), std::string("ayzzu")); - TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("u"), + TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("u"), std::string("a")), std::string("xyzza")); - TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("z"), + TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("z"), std::string("y")), std::string("xyyzu")); - TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("yzz"), + TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("yzz"), std::string("ab")), std::string("xabzzu")); - TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("b"), + TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("b"), std::string("w")), std::string("xyzzu")); } void testAtofLocaleC() { - TS_ASSERT_EQUALS(atof_locale_c("+1 "), 1.0); - TS_ASSERT_EQUALS(atof_locale_c(" 123.4"), 123.4); - TS_ASSERT_EQUALS(atof_locale_c("-3.14e-2"), -0.0314); - TS_ASSERT_EQUALS(atof_locale_c("1E-999"), 0.0); - TS_ASSERT_EQUALS(atof_locale_c("-1E-999"), 0.0); - TS_ASSERT_EQUALS(atof_locale_c("0.0"), 0.0); - TS_ASSERT_THROWS(atof_locale_c("1E+999"), JSBSim::BaseException&); - TS_ASSERT_THROWS(atof_locale_c("-1E+999"), JSBSim::BaseException&); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("+1 "), 1.0); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c(" 123.4"), 123.4); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("-3.14e-2"), -0.0314); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("1E-999"), 0.0); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("-1E-999"), 0.0); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("0.0"), 0.0); + TS_ASSERT_THROWS(JSBSim::atof_locale_c("1E+999"), JSBSim::BaseException&); + TS_ASSERT_THROWS(JSBSim::atof_locale_c("-1E+999"), JSBSim::BaseException&); + setlocale(LC_NUMERIC, "fr_FR"); + TS_ASSERT_EQUALS(atof("1,2"), 1.2); + TS_ASSERT_EQUALS(atof("1.2"), 1.0); + TS_ASSERT_EQUALS(JSBSim::atof_locale_c("1.2"), 1.2); } private: From 86c2e698e7abb578c452f0ff557fb7f4077280f1 Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 14:05:35 +0100 Subject: [PATCH 2/6] Remove locale specific tests. --- tests/unit_tests/StringUtilitiesTest.h | 4 ---- 1 file changed, 4 deletions(-) diff --git a/tests/unit_tests/StringUtilitiesTest.h b/tests/unit_tests/StringUtilitiesTest.h index fa09ee000a..5eef3c1523 100644 --- a/tests/unit_tests/StringUtilitiesTest.h +++ b/tests/unit_tests/StringUtilitiesTest.h @@ -95,10 +95,6 @@ class StringUtilitiesTest : public CxxTest::TestSuite TS_ASSERT_EQUALS(JSBSim::atof_locale_c("0.0"), 0.0); TS_ASSERT_THROWS(JSBSim::atof_locale_c("1E+999"), JSBSim::BaseException&); TS_ASSERT_THROWS(JSBSim::atof_locale_c("-1E+999"), JSBSim::BaseException&); - setlocale(LC_NUMERIC, "fr_FR"); - TS_ASSERT_EQUALS(atof("1,2"), 1.2); - TS_ASSERT_EQUALS(atof("1.2"), 1.0); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("1.2"), 1.2); } private: From 83c587249a3ec3a2baac6b1b119a045c192a414b Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 14:25:34 +0100 Subject: [PATCH 3/6] Code clean up --- tests/unit_tests/CMakeLists.txt | 6 +- tests/unit_tests/StringUtilitiesTest.h | 88 +++++++++++++------------- 2 files changed, 49 insertions(+), 45 deletions(-) diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index eae6e9e559..21dd1493cc 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -19,11 +19,13 @@ set(UNIT_TESTS FGColumnVector3Test FGConditionTest FGPropertyManagerTest) -add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) +if(WIN32 AND BUILD_SHARED_LIBS) + add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) +endif() foreach(test ${UNIT_TESTS}) cxxtest_add_test(${test}1 ${test}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${test}.h) - if (test STREQUAL "StringUtilitiesTest") + if (WIN32 AND BUILD_SHARED_LIBS AND test STREQUAL "StringUtilitiesTest") target_link_libraries(${test}1 libJSBSim string_utilities) else() target_link_libraries(${test}1 libJSBSim) diff --git a/tests/unit_tests/StringUtilitiesTest.h b/tests/unit_tests/StringUtilitiesTest.h index 5eef3c1523..287cd167b4 100644 --- a/tests/unit_tests/StringUtilitiesTest.h +++ b/tests/unit_tests/StringUtilitiesTest.h @@ -1,7 +1,9 @@ #include #include -#include #include "FGJSBBase.h" +#include + +using namespace JSBSim; class StringUtilitiesTest : public CxxTest::TestSuite { @@ -10,60 +12,60 @@ class StringUtilitiesTest : public CxxTest::TestSuite const std::string s_ref(" \t xx\t\tyy zz \t "); const std::string all_spaces(" \t \t\t "); std::string s = s_ref; - TS_ASSERT_EQUALS(JSBSim::trim_left(s), std::string("xx\t\tyy zz \t ")); - TS_ASSERT_EQUALS(JSBSim::trim_left(empty), empty); + TS_ASSERT_EQUALS(trim_left(s), std::string("xx\t\tyy zz \t ")); + TS_ASSERT_EQUALS(trim_left(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(JSBSim::trim_left(s), empty); + TS_ASSERT_EQUALS(trim_left(s), empty); s = s_ref; - TS_ASSERT_EQUALS(JSBSim::trim_right(s), std::string(" \t xx\t\tyy zz")); - TS_ASSERT_EQUALS(JSBSim::trim_right(empty), empty); + TS_ASSERT_EQUALS(trim_right(s), std::string(" \t xx\t\tyy zz")); + TS_ASSERT_EQUALS(trim_right(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(JSBSim::trim_right(s), empty); + TS_ASSERT_EQUALS(trim_right(s), empty); s = s_ref; - TS_ASSERT_EQUALS(JSBSim::trim(s), std::string("xx\t\tyy zz")); - TS_ASSERT_EQUALS(JSBSim::trim(empty), empty); + TS_ASSERT_EQUALS(trim(s), std::string("xx\t\tyy zz")); + TS_ASSERT_EQUALS(trim(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(JSBSim::trim(s), empty); + TS_ASSERT_EQUALS(trim(s), empty); s = s_ref; - TS_ASSERT_EQUALS(JSBSim::trim_all_space(s), std::string("xxyyzz")); - TS_ASSERT_EQUALS(JSBSim::trim_all_space(empty), empty); + TS_ASSERT_EQUALS(trim_all_space(s), std::string("xxyyzz")); + TS_ASSERT_EQUALS(trim_all_space(empty), empty); s = all_spaces; - TS_ASSERT_EQUALS(JSBSim::trim_all_space(s), empty); + TS_ASSERT_EQUALS(trim_all_space(s), empty); } void testStringCase() { const std::string s_ref(" MiXed\tCaSE; "); std::string s = s_ref; - TS_ASSERT_EQUALS(JSBSim::to_upper(s), std::string(" MIXED\tCASE; ")); - TS_ASSERT_EQUALS(JSBSim::to_upper(empty), empty); + TS_ASSERT_EQUALS(to_upper(s), std::string(" MIXED\tCASE; ")); + TS_ASSERT_EQUALS(to_upper(empty), empty); s = s_ref; - TS_ASSERT_EQUALS(JSBSim::to_lower(s), std::string(" mixed\tcase; ")); - TS_ASSERT_EQUALS(JSBSim::to_lower(empty), empty); + TS_ASSERT_EQUALS(to_lower(s), std::string(" mixed\tcase; ")); + TS_ASSERT_EQUALS(to_lower(empty), empty); } void testNumberString() { - TS_ASSERT(JSBSim::is_number("1.0")); - TS_ASSERT(JSBSim::is_number("1526")); - TS_ASSERT(JSBSim::is_number(".01256")); - TS_ASSERT(JSBSim::is_number("-1.0e+1")); - TS_ASSERT(!JSBSim::is_number(empty)); - TS_ASSERT(!JSBSim::is_number("125x5#")); + TS_ASSERT(is_number("1.0")); + TS_ASSERT(is_number("1526")); + TS_ASSERT(is_number(".01256")); + TS_ASSERT(is_number("-1.0e+1")); + TS_ASSERT(!is_number(empty)); + TS_ASSERT(!is_number("125x5#")); } void testSplit() { - std::vector list = JSBSim::split(empty,','); + std::vector list = split(empty,','); TS_ASSERT_EQUALS(list.size(), 0); - list = JSBSim::split(std::string(",,,,,"),','); + list = split(std::string(",,,,,"),','); TS_ASSERT_EQUALS(list.size(), 0); - list = JSBSim::split(std::string(" xx yy zz "),','); + list = split(std::string(" xx yy zz "),','); TS_ASSERT_EQUALS(list.size(), 1); TS_ASSERT_EQUALS(list[0], std::string("xx yy zz")); - list = JSBSim::split(std::string(" xx yy zz "),' '); + list = split(std::string(" xx yy zz "),' '); TS_ASSERT_EQUALS(list.size(), 3); TS_ASSERT_EQUALS(list[0], std::string("xx")); TS_ASSERT_EQUALS(list[1], std::string("yy")); TS_ASSERT_EQUALS(list[2], std::string("zz")); - list = JSBSim::split(",xx,,yy,zz,",','); + list = split(",xx,,yy,zz,",','); TS_ASSERT_EQUALS(list.size(), 3); TS_ASSERT_EQUALS(list[0], std::string("xx")); TS_ASSERT_EQUALS(list[1], std::string("yy")); @@ -71,30 +73,30 @@ class StringUtilitiesTest : public CxxTest::TestSuite } void testReplace() { - TS_ASSERT_EQUALS(JSBSim::replace(empty, std::string("x"), std::string("a")), empty); - TS_ASSERT_EQUALS(JSBSim::replace(std::string(" xyzzu "), std::string("x"), + TS_ASSERT_EQUALS(replace(empty, std::string("x"), std::string("a")), empty); + TS_ASSERT_EQUALS(replace(std::string(" xyzzu "), std::string("x"), std::string("a")), std::string(" ayzzu ")); - TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("x"), + TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("x"), std::string("a")), std::string("ayzzu")); - TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("u"), + TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("u"), std::string("a")), std::string("xyzza")); - TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("z"), + TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("z"), std::string("y")), std::string("xyyzu")); - TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("yzz"), + TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("yzz"), std::string("ab")), std::string("xabzzu")); - TS_ASSERT_EQUALS(JSBSim::replace(std::string("xyzzu"), std::string("b"), + TS_ASSERT_EQUALS(replace(std::string("xyzzu"), std::string("b"), std::string("w")), std::string("xyzzu")); } void testAtofLocaleC() { - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("+1 "), 1.0); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c(" 123.4"), 123.4); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("-3.14e-2"), -0.0314); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("1E-999"), 0.0); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("-1E-999"), 0.0); - TS_ASSERT_EQUALS(JSBSim::atof_locale_c("0.0"), 0.0); - TS_ASSERT_THROWS(JSBSim::atof_locale_c("1E+999"), JSBSim::BaseException&); - TS_ASSERT_THROWS(JSBSim::atof_locale_c("-1E+999"), JSBSim::BaseException&); + TS_ASSERT_EQUALS(atof_locale_c("+1 "), 1.0); + TS_ASSERT_EQUALS(atof_locale_c(" 123.4"), 123.4); + TS_ASSERT_EQUALS(atof_locale_c("-3.14e-2"), -0.0314); + TS_ASSERT_EQUALS(atof_locale_c("1E-999"), 0.0); + TS_ASSERT_EQUALS(atof_locale_c("-1E-999"), 0.0); + TS_ASSERT_EQUALS(atof_locale_c("0.0"), 0.0); + TS_ASSERT_THROWS(atof_locale_c("1E+999"), BaseException&); + TS_ASSERT_THROWS(atof_locale_c("-1E+999"), BaseException&); } private: From e7bd9a4096d7f5fc670eb01a9c3a7c983dd7a2da Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 14:41:57 +0100 Subject: [PATCH 4/6] Fix DLL import for MinGW. --- tests/unit_tests/CMakeLists.txt | 1 + 1 file changed, 1 insertion(+) diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 21dd1493cc..a4261c1740 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -21,6 +21,7 @@ set(UNIT_TESTS FGColumnVector3Test if(WIN32 AND BUILD_SHARED_LIBS) add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) + target_compile_definitions(string_utilities PRIVATE JSBSIM_STATIC_LINK) endif() foreach(test ${UNIT_TESTS}) From 4cf786922ff3e95770fb49fcf4800876c433fe32 Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 15:03:19 +0100 Subject: [PATCH 5/6] Further fixes for MinGW. --- tests/unit_tests/CMakeLists.txt | 6 +++++- 1 file changed, 5 insertions(+), 1 deletion(-) diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index a4261c1740..86b8383795 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -19,6 +19,10 @@ set(UNIT_TESTS FGColumnVector3Test FGConditionTest FGPropertyManagerTest) +# Windows cannot access the functions in `string_utilities.h` when JSBSim is +# compiled as a DLL. Rather than adding `JSBSIM_API` to all of these functions +# just for the sake of using them in unit tests, we are compiling `string_utilities.h` +# as a static library and linking it to the unit tests. if(WIN32 AND BUILD_SHARED_LIBS) add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) target_compile_definitions(string_utilities PRIVATE JSBSIM_STATIC_LINK) @@ -26,7 +30,7 @@ endif() foreach(test ${UNIT_TESTS}) cxxtest_add_test(${test}1 ${test}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${test}.h) - if (WIN32 AND BUILD_SHARED_LIBS AND test STREQUAL "StringUtilitiesTest") + if (WIN32 AND BUILD_SHARED_LIBS) target_link_libraries(${test}1 libJSBSim string_utilities) else() target_link_libraries(${test}1 libJSBSim) From c875e393807524e2e3ddb5de0565d4f42432aea3 Mon Sep 17 00:00:00 2001 From: Bertrand Coconnier Date: Sun, 12 Feb 2023 16:02:10 +0100 Subject: [PATCH 6/6] Simplify the build of unit tests. --- src/input_output/string_utilities.h | 22 ++++++++++------------ tests/unit_tests/CMakeLists.txt | 15 +-------------- 2 files changed, 11 insertions(+), 26 deletions(-) diff --git a/src/input_output/string_utilities.h b/src/input_output/string_utilities.h index 71a3ce0431..8583ef6355 100644 --- a/src/input_output/string_utilities.h +++ b/src/input_output/string_utilities.h @@ -46,18 +46,16 @@ CLASS DECLARATION %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%*/ namespace JSBSim { -double atof_locale_c(const std::string& input); - -std::string& trim_left(std::string& str); -std::string& trim_right(std::string& str); -std::string& trim(std::string& str); -std::string& trim_all_space(std::string& str); -std::string& to_upper(std::string& str); -std::string& to_lower(std::string& str); -bool is_number(const std::string& str); -std::vector split(std::string str, char d); - -std::string replace(std::string str, const std::string& old, const std::string& newstr); +JSBSIM_API double atof_locale_c(const std::string& input); +JSBSIM_API std::string& trim_left(std::string& str); +JSBSIM_API std::string& trim_right(std::string& str); +JSBSIM_API std::string& trim(std::string& str); +JSBSIM_API std::string& trim_all_space(std::string& str); +JSBSIM_API std::string& to_upper(std::string& str); +JSBSIM_API std::string& to_lower(std::string& str); +JSBSIM_API bool is_number(const std::string& str); +JSBSIM_API std::vector split(std::string str, char d); +JSBSIM_API std::string replace(std::string str, const std::string& old, const std::string& newstr); }; //%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%% diff --git a/tests/unit_tests/CMakeLists.txt b/tests/unit_tests/CMakeLists.txt index 86b8383795..8f83b31542 100644 --- a/tests/unit_tests/CMakeLists.txt +++ b/tests/unit_tests/CMakeLists.txt @@ -19,22 +19,9 @@ set(UNIT_TESTS FGColumnVector3Test FGConditionTest FGPropertyManagerTest) -# Windows cannot access the functions in `string_utilities.h` when JSBSim is -# compiled as a DLL. Rather than adding `JSBSIM_API` to all of these functions -# just for the sake of using them in unit tests, we are compiling `string_utilities.h` -# as a static library and linking it to the unit tests. -if(WIN32 AND BUILD_SHARED_LIBS) - add_library(string_utilities STATIC ${PROJECT_SOURCE_DIR}/src/input_output/string_utilities.cpp) - target_compile_definitions(string_utilities PRIVATE JSBSIM_STATIC_LINK) -endif() - foreach(test ${UNIT_TESTS}) cxxtest_add_test(${test}1 ${test}.cpp ${CMAKE_CURRENT_SOURCE_DIR}/${test}.h) - if (WIN32 AND BUILD_SHARED_LIBS) - target_link_libraries(${test}1 libJSBSim string_utilities) - else() - target_link_libraries(${test}1 libJSBSim) - endif() + target_link_libraries(${test}1 libJSBSim) add_coverage(${test}1) endforeach()