Skip to content

Commit cbcaae5

Browse files
committed
#161 - Test CppStringT::rfind_n() with char and wchar_t
Completed.
1 parent 6ff8a0f commit cbcaae5

File tree

2 files changed

+122
-22
lines changed

2 files changed

+122
-22
lines changed

cpp-strings-tests/cpp-strings-tests.cpp

Lines changed: 116 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -344,26 +344,6 @@ namespace cppstringstests
344344
Assert::AreEqual(pcs::CppString::npos, test.find_n(".", 28, 1));
345345
Assert::AreEqual(size_t(13), test.find_n(".", 13, test.size() - 13));
346346

347-
348-
/*
349-
pcs::CppWString wtest{L"ABC0123456789.ABC0123456789."};
350-
for (int wc = 0; wc <= 0xffff; ++wc) {
351-
wchar_t wch{ wchar_t(wc) };
352-
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(wch), wtest.find_n(wch, 2, -1));
353-
Assert::AreEqual(wtest.substr(0, 2).MyBaseClass::find(wch), wtest.find_n(wch, 2));
354-
Assert::AreEqual(wtest.substr(2, 5).MyBaseClass::find(wch), wtest.find_n(wch, 2, 5));
355-
356-
CppWString ws(wch);
357-
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(ws), wtest.find_n(ws, 2, -1));
358-
Assert::AreEqual(wtest.substr(0, 2).MyBaseClass::find(ws), wtest.find_n(ws, 2));
359-
Assert::AreEqual(wtest.substr(3, 5).MyBaseClass::find(ws), wtest.find_n(ws, 3, 5));
360-
361-
wchar_t wstr[2]{ wch, 0 };
362-
Assert::AreEqual(wtest.substr(2).MyBaseClass::find(wstr), wtest.find_n(wstr, 2, -1));
363-
Assert::AreEqual(wtest.substr(0, 2).MyBaseClass::find(wstr), wtest.find_n(wstr, 2));
364-
Assert::AreEqual(wtest.substr(3, 5).MyBaseClass::find(wstr), wtest.find_n(wstr, 3, 5));
365-
}
366-
*/
367347
pcs::CppWString wtest{ L"ABC0123456789.ABC0123456789." };
368348
for (int wc = 0; wc <= 0xffff; ++wc) {
369349
wchar_t wch{ wchar_t(wc) };
@@ -1221,7 +1201,6 @@ namespace cppstringstests
12211201
else
12221202
Assert::AreEqual(found_pos, wtest.rfind(ws, 2, pcs::CppString::size_type(5 + 2 - 1)) - 2);
12231203

1224-
12251204
if (wc > 0) {
12261205
wchar_t wstr[2]{ wch, 0 };
12271206
Assert::AreEqual(wtest.MyBaseClass::rfind(wstr), wtest.rfind(wstr));
@@ -1244,5 +1223,121 @@ namespace cppstringstests
12441223
Assert::AreEqual(pcs::CppString::npos, wtest.rfind(L".", 14));
12451224
Assert::AreEqual(size_t(13), wtest.rfind(L".", 13));
12461225
}
1226+
1227+
TEST_METHOD(rfind_n)
1228+
{
1229+
size_t found_pos;
1230+
1231+
pcs::CppString test{ "ABC0123456789.ABC0123456789." };
1232+
for (int c = 0; c <= 255; ++c) {
1233+
char ch{ char(c) };
1234+
Assert::AreEqual(test.MyBaseClass::rfind(ch), test.rfind_n(ch, size_t(-1)));
1235+
1236+
found_pos = test.substr(2).MyBaseClass::rfind(ch);
1237+
if (found_pos == pcs::CppString::npos)
1238+
Assert::AreEqual(found_pos, test.rfind_n(ch, 2));
1239+
else
1240+
Assert::AreEqual(found_pos, test.substr(2).rfind_n(ch, test.size() - 2));
1241+
1242+
found_pos = test.substr(2, 5).MyBaseClass::rfind(ch);
1243+
if (found_pos == pcs::CppString::npos)
1244+
Assert::AreEqual(found_pos, test.rfind_n(ch, 2, pcs::CppString::size_type(5)));
1245+
else
1246+
Assert::AreEqual(found_pos, test.rfind_n(ch, 2, pcs::CppString::size_type(5)) - 2);
1247+
1248+
CppString s(ch);
1249+
Assert::AreEqual(test.MyBaseClass::rfind(s), test.rfind_n(s, size_t(-1)));
1250+
found_pos = test.substr(2).MyBaseClass::rfind(s);
1251+
if (found_pos == pcs::CppString::npos)
1252+
Assert::AreEqual(found_pos, test.rfind_n(s, 2));
1253+
else
1254+
Assert::AreEqual(found_pos, test.substr(2).rfind_n(s, test.size() - 2));
1255+
1256+
found_pos = test.substr(2, 5).MyBaseClass::rfind(s);
1257+
if (found_pos == pcs::CppString::npos)
1258+
Assert::AreEqual(found_pos, test.rfind_n(s, 2, pcs::CppString::size_type(5)));
1259+
else
1260+
Assert::AreEqual(found_pos, test.rfind_n(s, 2, pcs::CppString::size_type(5)) - 2);
1261+
1262+
if (c > 0) {
1263+
char str[2]{ ch, 0 };
1264+
Assert::AreEqual(test.MyBaseClass::rfind(str), test.rfind_n(str, size_t(-1)));
1265+
1266+
found_pos = test.substr(2).MyBaseClass::rfind(str);
1267+
if (found_pos == pcs::CppString::npos)
1268+
Assert::AreEqual(found_pos, test.rfind_n(str, test.size() - 2));
1269+
else
1270+
Assert::AreEqual(found_pos, test.substr(2).rfind_n(str, test.size() - 2));
1271+
1272+
found_pos = test.substr(2, 5).MyBaseClass::rfind(str);
1273+
if (found_pos == pcs::CppString::npos)
1274+
Assert::AreEqual(found_pos, test.rfind_n(str, 2, pcs::CppString::size_type(5)));
1275+
else
1276+
Assert::AreEqual(found_pos, test.rfind_n(str, 2, pcs::CppString::size_type(5)) - 2);
1277+
}
1278+
}
1279+
Assert::AreEqual(size_t(14), test.rfind_n("A", 1, test.size() - 1));
1280+
Assert::AreEqual(pcs::CppString::npos, test.rfind_n("A", 15, 1));
1281+
Assert::AreEqual(size_t(0), test.rfind_n("", size_t(-1)));
1282+
Assert::AreEqual(size_t(27), test.rfind_n(".", 14, test.size() - 14));
1283+
Assert::AreEqual(pcs::CppString::npos, test.rfind_n(".", 28, 1));
1284+
Assert::AreEqual(size_t(27), test.rfind_n(".", 13, test.size() - 13));
1285+
1286+
pcs::CppWString wtest{ L"ABC0123456789.ABC0123456789." };
1287+
for (int wc = 0; wc <= 0xffff; ++wc) {
1288+
wchar_t wch{ wchar_t(wc) };
1289+
Assert::AreEqual(wtest.MyBaseClass::rfind(wch), wtest.rfind_n(wch, size_t(-1)));
1290+
1291+
found_pos = wtest.substr(2).MyBaseClass::rfind(wch);
1292+
if (found_pos == pcs::CppString::npos)
1293+
Assert::AreEqual(found_pos, wtest.rfind_n(wch, 2));
1294+
else
1295+
Assert::AreEqual(found_pos, wtest.substr(2).rfind_n(wch, wtest.size() - 2));
1296+
1297+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(wch);
1298+
if (found_pos == pcs::CppString::npos)
1299+
Assert::AreEqual(found_pos, wtest.rfind_n(wch, 2, pcs::CppString::size_type(5)));
1300+
else
1301+
Assert::AreEqual(found_pos, wtest.rfind_n(wch, 2, pcs::CppString::size_type(5)) - 2);
1302+
1303+
CppWString ws(wch);
1304+
Assert::AreEqual(wtest.MyBaseClass::rfind(ws), wtest.rfind_n(ws, size_t(-1)));
1305+
found_pos = wtest.substr(2).MyBaseClass::rfind(ws);
1306+
if (found_pos == pcs::CppString::npos)
1307+
Assert::AreEqual(found_pos, wtest.rfind_n(ws, 2));
1308+
else
1309+
Assert::AreEqual(found_pos, wtest.substr(2).rfind_n(ws, wtest.size() - 2));
1310+
1311+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(ws);
1312+
if (found_pos == pcs::CppString::npos)
1313+
Assert::AreEqual(found_pos, wtest.rfind_n(ws, 2, pcs::CppString::size_type(5)));
1314+
else
1315+
Assert::AreEqual(found_pos, wtest.rfind_n(ws, 2, pcs::CppString::size_type(5)) - 2);
1316+
1317+
if (wc > 0) {
1318+
wchar_t wstr[2]{ wch, 0 };
1319+
Assert::AreEqual(wtest.MyBaseClass::rfind(wstr), wtest.rfind_n(wstr, size_t(-1)));
1320+
1321+
found_pos = wtest.substr(2).MyBaseClass::rfind(wstr);
1322+
if (found_pos == pcs::CppString::npos)
1323+
Assert::AreEqual(found_pos, wtest.rfind_n(wstr, wtest.size() - 2));
1324+
else
1325+
Assert::AreEqual(found_pos, wtest.substr(2).rfind_n(wstr, wtest.size() - 2));
1326+
1327+
found_pos = wtest.substr(2, 5).MyBaseClass::rfind(wstr);
1328+
if (found_pos == pcs::CppString::npos)
1329+
Assert::AreEqual(found_pos, wtest.rfind_n(wstr, 2, pcs::CppString::size_type(5)));
1330+
else
1331+
Assert::AreEqual(found_pos, wtest.rfind_n(wstr, 2, pcs::CppString::size_type(5)) - 2);
1332+
}
1333+
}
1334+
Assert::AreEqual(size_t(14), wtest.rfind_n(L"A", 1, wtest.size() - 1));
1335+
Assert::AreEqual(pcs::CppString::npos, wtest.rfind_n(L"A", 15, 1));
1336+
Assert::AreEqual(size_t(0), wtest.rfind_n(L"", size_t(-1)));
1337+
Assert::AreEqual(size_t(27), wtest.rfind_n(L".", 14, wtest.size() - 14));
1338+
Assert::AreEqual(pcs::CppString::npos, wtest.rfind_n(L".", 28, 1));
1339+
Assert::AreEqual(size_t(27), wtest.rfind_n(L".", 13, wtest.size() - 13));
1340+
}
1341+
12471342
};
12481343
}

cpp-strings/cppstrings.h

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1229,6 +1229,8 @@ namespace pcs // i.e. "pythonic c++ strings"
12291229
{
12301230
if (start > end)
12311231
return CppStringT::npos;
1232+
else if (sub.empty())
1233+
return 0;
12321234
else {
12331235
const size_type found_pos{ this->substr(start, end - start + 1).rfind(sub) };
12341236
return (found_pos == CppStringT::npos) ? CppStringT::npos : found_pos + start;
@@ -1299,7 +1301,10 @@ namespace pcs // i.e. "pythonic c++ strings"
12991301
*/
13001302
inline constexpr size_type rfind_n(const CppStringT& sub, const size_type count) const noexcept
13011303
{
1302-
return rfind(sub, 0, count);
1304+
if (count == 0)
1305+
return CppStringT::npos;
1306+
else
1307+
return rfind(sub, 0, count - 1);
13031308
}
13041309

13051310

0 commit comments

Comments
 (0)