Skip to content

Commit

Permalink
Merge pull request #4171 from Sonicadvance1/fix_ltrim
Browse files Browse the repository at this point in the history
Utils/StringUtil: Fixes ltrim and adds a unittest
  • Loading branch information
Sonicadvance1 authored Nov 26, 2024
2 parents 60c52e3 + 47369d0 commit 0463512
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 2 deletions.
2 changes: 1 addition & 1 deletion Source/Common/StringUtil.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -3,7 +3,7 @@

namespace FEX::StringUtil {
void ltrim(fextl::string& s) {
s.erase(std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
s.erase(s.begin(), std::find_if(s.begin(), s.end(), [](int ch) { return !std::isspace(ch); }));
}

void rtrim(fextl::string& s) {
Expand Down
3 changes: 2 additions & 1 deletion unittests/APITests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,10 @@ set (TESTS
ArgumentParser
InterruptableConditionVariable
Filesystem
StringUtils
)

list(APPEND LIBS FEXCore JemallocLibs)
list(APPEND LIBS Common FEXCore JemallocLibs)

foreach(API_TEST ${TESTS})
add_executable(${API_TEST} ${API_TEST}.cpp)
Expand Down
95 changes: 95 additions & 0 deletions unittests/APITests/StringUtils.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,95 @@
#include <catch2/catch_test_macros.hpp>
#include <Common/StringUtil.h>

static fextl::string ltrim_string(fextl::string in) {
FEX::StringUtil::ltrim(in);
return in;
}

static fextl::string rtrim_string(fextl::string in) {
FEX::StringUtil::rtrim(in);
return in;
}

static fextl::string trim_string(fextl::string in) {
FEX::StringUtil::trim(in);
return in;
}

TEST_CASE("ltrim") {
CHECK(ltrim_string("") == "");
CHECK(ltrim_string("FEXLoader") == "FEXLoader");

CHECK(ltrim_string("FEXLoader\n") == "FEXLoader\n");
CHECK(ltrim_string("FEXLoader\r") == "FEXLoader\r");
CHECK(ltrim_string("FEXLoader\f") == "FEXLoader\f");
CHECK(ltrim_string("FEXLoader\t") == "FEXLoader\t");
CHECK(ltrim_string("FEXLoader\v") == "FEXLoader\v");
CHECK(ltrim_string("FEXLoader ") == "FEXLoader ");

CHECK(ltrim_string("\nFEXLoader") == "FEXLoader");
CHECK(ltrim_string("\rFEXLoader") == "FEXLoader");
CHECK(ltrim_string("\fFEXLoader") == "FEXLoader");
CHECK(ltrim_string("\tFEXLoader") == "FEXLoader");
CHECK(ltrim_string("\vFEXLoader") == "FEXLoader");
CHECK(ltrim_string(" FEXLoader") == "FEXLoader");

CHECK(ltrim_string("\nFEXLoader\n") == "FEXLoader\n");
CHECK(ltrim_string("\rFEXLoader\r") == "FEXLoader\r");
CHECK(ltrim_string("\fFEXLoader\f") == "FEXLoader\f");
CHECK(ltrim_string("\tFEXLoader\t") == "FEXLoader\t");
CHECK(ltrim_string("\vFEXLoader\v") == "FEXLoader\v");
CHECK(ltrim_string(" FEXLoader ") == "FEXLoader ");
}

TEST_CASE("rtrim") {
CHECK(rtrim_string("") == "");
CHECK(rtrim_string("FEXLoader") == "FEXLoader");

CHECK(rtrim_string("FEXLoader\n") == "FEXLoader");
CHECK(rtrim_string("FEXLoader\r") == "FEXLoader");
CHECK(rtrim_string("FEXLoader\f") == "FEXLoader");
CHECK(rtrim_string("FEXLoader\t") == "FEXLoader");
CHECK(rtrim_string("FEXLoader\v") == "FEXLoader");
CHECK(rtrim_string("FEXLoader ") == "FEXLoader");

CHECK(rtrim_string("\nFEXLoader") == "\nFEXLoader");
CHECK(rtrim_string("\rFEXLoader") == "\rFEXLoader");
CHECK(rtrim_string("\fFEXLoader") == "\fFEXLoader");
CHECK(rtrim_string("\tFEXLoader") == "\tFEXLoader");
CHECK(rtrim_string("\vFEXLoader") == "\vFEXLoader");
CHECK(rtrim_string(" FEXLoader") == " FEXLoader");

CHECK(rtrim_string("\nFEXLoader\n") == "\nFEXLoader");
CHECK(rtrim_string("\rFEXLoader\r") == "\rFEXLoader");
CHECK(rtrim_string("\fFEXLoader\f") == "\fFEXLoader");
CHECK(rtrim_string("\tFEXLoader\t") == "\tFEXLoader");
CHECK(rtrim_string("\vFEXLoader\v") == "\vFEXLoader");
CHECK(rtrim_string(" FEXLoader ") == " FEXLoader");
}

TEST_CASE("trim") {
CHECK(trim_string("") == "");
CHECK(trim_string("FEXLoader") == "FEXLoader");

CHECK(trim_string("FEXLoader\n") == "FEXLoader");
CHECK(trim_string("FEXLoader\r") == "FEXLoader");
CHECK(trim_string("FEXLoader\f") == "FEXLoader");
CHECK(trim_string("FEXLoader\t") == "FEXLoader");
CHECK(trim_string("FEXLoader\v") == "FEXLoader");
CHECK(trim_string("FEXLoader ") == "FEXLoader");

CHECK(trim_string("\nFEXLoader") == "FEXLoader");
CHECK(trim_string("\rFEXLoader") == "FEXLoader");
CHECK(trim_string("\fFEXLoader") == "FEXLoader");
CHECK(trim_string("\tFEXLoader") == "FEXLoader");
CHECK(trim_string("\vFEXLoader") == "FEXLoader");
CHECK(trim_string(" FEXLoader") == "FEXLoader");

CHECK(trim_string("\nFEXLoader\n") == "FEXLoader");
CHECK(trim_string("\rFEXLoader\r") == "FEXLoader");
CHECK(trim_string("\fFEXLoader\f") == "FEXLoader");
CHECK(trim_string("\tFEXLoader\t") == "FEXLoader");
CHECK(trim_string("\vFEXLoader\v") == "FEXLoader");
CHECK(trim_string(" FEXLoader ") == "FEXLoader");
}

0 comments on commit 0463512

Please sign in to comment.