|
| 1 | +#include "unit_test_split.h" |
| 2 | +#include <vector> |
| 3 | +#include <wsjcpp_core.h> |
| 4 | + |
| 5 | +REGISTRY_UNIT_TEST(UnitTestSplit) |
| 6 | + |
| 7 | +UnitTestSplit::UnitTestSplit() |
| 8 | + : WSJCppUnitTestBase("UnitTestSplit") { |
| 9 | +} |
| 10 | + |
| 11 | +// --------------------------------------------------------------------- |
| 12 | + |
| 13 | +void UnitTestSplit::init() { |
| 14 | + // nothing |
| 15 | +} |
| 16 | + |
| 17 | +// --------------------------------------------------------------------- |
| 18 | + |
| 19 | +bool UnitTestSplit::run() { |
| 20 | +bool bTestSuccess = true; |
| 21 | + |
| 22 | + struct LTest { |
| 23 | + LTest( |
| 24 | + const std::string &sStr, |
| 25 | + const std::string &sDelim, |
| 26 | + const std::vector<std::string> &vExpectedVector |
| 27 | + ) { |
| 28 | + this->sStr = sStr; |
| 29 | + this->sDelim = sDelim; |
| 30 | + this->vExpectedVector = vExpectedVector; |
| 31 | + }; |
| 32 | + std::string sStr; |
| 33 | + std::string sDelim; |
| 34 | + std::vector<std::string> vExpectedVector; |
| 35 | + }; |
| 36 | + std::vector<LTest> tests; |
| 37 | + tests.push_back(LTest("1 2 3 4 5", " ", {"1", "2", "3", "4", "5"})); |
| 38 | + tests.push_back(LTest("|1f|2п|3%^|44354|5kdasjfdre|2", "|", {"", "1f", "2п", "3%^", "44354", "5kdasjfdre", "2"})); |
| 39 | + tests.push_back(LTest("|1f|2п|3%^|44354|5kdasjfdre|", "|", {"", "1f", "2п", "3%^", "44354", "5kdasjfdre", ""})); |
| 40 | + tests.push_back(LTest("some1 => some2 => some3", "=>", {"some1 ", " some2 ", " some3"})); |
| 41 | + tests.push_back(LTest("some1 => some2 => some3 =>", "=>", {"some1 ", " some2 ", " some3 ", ""})); |
| 42 | + |
| 43 | + for (int i = 0; i < tests.size(); i++) { |
| 44 | + LTest test = tests[i]; |
| 45 | + std::string sPrefix = "test" + std::to_string(i) + "(\"" + test.sStr + "\")"; |
| 46 | + std::vector<std::string> vSplitted = WSJCppCore::split(test.sStr, test.sDelim); |
| 47 | + compareN(bTestSuccess, sPrefix + ": size", vSplitted.size(), test.vExpectedVector.size()); |
| 48 | + int nMin = std::min(vSplitted.size(), test.vExpectedVector.size()); |
| 49 | + for (int n = 0; n < nMin; n++) { |
| 50 | + compareS(bTestSuccess, sPrefix + ", element: " + std::to_string(n), vSplitted[n], test.vExpectedVector[n]); |
| 51 | + } |
| 52 | + } |
| 53 | + |
| 54 | + return bTestSuccess; |
| 55 | +} |
| 56 | + |
0 commit comments