Skip to content

Commit 866069a

Browse files
committed
#162-Test CppStringT::rindex() with char and wchar_t
Completed. Validated.
1 parent db2e408 commit 866069a

File tree

2 files changed

+250
-1
lines changed

2 files changed

+250
-1
lines changed

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

Lines changed: 248 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1995,5 +1995,253 @@ namespace cppstringstests
19951995
Assert::AreEqual(size_t(27), wtest.rfind_n(L".", 13, wtest.size() - 13));
19961996
}
19971997

1998+
TEST_METHOD(rindex_char)
1999+
{
2000+
using string_type = pcs::CppString;
2001+
2002+
string_type test_str{ "ABC0123456789." };
2003+
char ch{ '3' };
2004+
Assert::AreEqual(test_str.MyBaseClass::rfind(ch), test_str.rindex(ch));
2005+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(ch), test_str.rindex(ch, 2) - 2);
2006+
Assert::AreEqual(test_str.substr(2, 5).MyBaseClass::rfind(ch), test_str.rindex(ch, 2, string_type::size_type(5 + 2 - 1)) - 2);
2007+
2008+
try {
2009+
const string_type::size_type pos = test_str.rindex('z');
2010+
Assert::IsTrue(pos != pcs::CppString::npos);
2011+
}
2012+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2013+
2014+
try {
2015+
const string_type::size_type pos = test_str.rindex('z', 2);
2016+
Assert::IsTrue(pos != string_type::npos);
2017+
}
2018+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2019+
2020+
try {
2021+
const string_type::size_type pos = test_str.rindex('z', 2, string_type::size_type(5 + 2 - 1));
2022+
Assert::IsTrue(pos != string_type::npos);
2023+
}
2024+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2025+
2026+
string_type s(ch);
2027+
Assert::AreEqual(test_str.MyBaseClass::rfind(s), test_str.rindex(s));
2028+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(s), test_str.rindex(s, 2) - 2);
2029+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(s), test_str.rindex(s, 3, string_type::size_type(5 + 3 - 1)) - 3);
2030+
s = 'z';
2031+
try {
2032+
const string_type::size_type pos = test_str.rindex(s);
2033+
Assert::IsTrue(pos != string_type::npos);
2034+
}
2035+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2036+
2037+
try {
2038+
const string_type::size_type pos = test_str.rindex(s, 2);
2039+
Assert::IsTrue(pos != string_type::npos);
2040+
}
2041+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2042+
2043+
try {
2044+
const string_type::size_type pos = test_str.rindex(s, 2, string_type::size_type(5 + 2 - 1));
2045+
Assert::IsTrue(pos != string_type::npos);
2046+
}
2047+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2048+
2049+
char str[2]{ ch, 0 };
2050+
Assert::AreEqual(test_str.MyBaseClass::rfind(str), test_str.rindex(str));
2051+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(str), test_str.rindex(str, 2) - 2);
2052+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(str), test_str.rindex(str, 3, string_type::size_type(5 + 3 - 1)) - 3);
2053+
str[0] = 'z';
2054+
try {
2055+
const string_type::size_type pos = test_str.rindex(s);
2056+
Assert::IsTrue(pos != string_type::npos);
2057+
}
2058+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2059+
2060+
try {
2061+
const string_type::size_type pos = test_str.rindex(s, 2);
2062+
Assert::IsTrue(pos != string_type::npos);
2063+
}
2064+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2065+
2066+
try {
2067+
const string_type::size_type pos = test_str.rindex(s, 2, string_type::size_type(5 + 2 - 1));
2068+
Assert::IsTrue(pos != string_type::npos);
2069+
}
2070+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2071+
}
2072+
2073+
TEST_METHOD(rindex_wchart)
2074+
{
2075+
using string_type = pcs::CppWString;
2076+
2077+
string_type test_str(L"ABC0123456789.");
2078+
wchar_t ch{ L'3' };
2079+
Assert::AreEqual(test_str.MyBaseClass::rfind(ch), test_str.rindex(ch));
2080+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(ch), test_str.rindex(ch, 2) - 2);
2081+
Assert::AreEqual(test_str.substr(2, 5).MyBaseClass::rfind(ch), test_str.rindex(ch, 2, string_type::size_type(5 + 2 - 1)) - 2);
2082+
try {
2083+
const string_type::size_type pos = test_str.rindex('z');
2084+
Assert::IsTrue(pos != pcs::CppString::npos);
2085+
}
2086+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2087+
2088+
try {
2089+
const string_type::size_type pos = test_str.rindex('z', 2);
2090+
Assert::IsTrue(pos != string_type::npos);
2091+
}
2092+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2093+
2094+
try {
2095+
const string_type::size_type pos = test_str.rindex('z', 2, string_type::size_type(5 + 2 - 1));
2096+
Assert::IsTrue(pos != string_type::npos);
2097+
}
2098+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2099+
2100+
string_type s(ch);
2101+
Assert::AreEqual(test_str.MyBaseClass::rfind(s), test_str.rindex(s));
2102+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(s), test_str.rindex(s, 2) - 2);
2103+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(s), test_str.rindex(s, 3, string_type::size_type(5 + 3 - 1)) - 3);
2104+
s = 'z';
2105+
try {
2106+
const string_type::size_type pos = test_str.rindex(s);
2107+
Assert::IsTrue(pos != string_type::npos);
2108+
}
2109+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2110+
2111+
try {
2112+
const string_type::size_type pos = test_str.rindex(s, 2);
2113+
Assert::IsTrue(pos != string_type::npos);
2114+
}
2115+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2116+
2117+
try {
2118+
const string_type::size_type pos = test_str.rindex(s, 2, string_type::size_type(5 + 2 - 1));
2119+
Assert::IsTrue(pos != string_type::npos);
2120+
}
2121+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2122+
2123+
wchar_t str[2]{ ch, 0 };
2124+
Assert::AreEqual(test_str.MyBaseClass::rfind(str), test_str.rindex(str));
2125+
Assert::AreEqual(test_str.substr(2).MyBaseClass::rfind(str), test_str.rindex(str, 2) - 2);
2126+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(str), test_str.rindex(str, 3, string_type::size_type(5 + 3 - 1)) - 3);
2127+
str[0] = 'z';
2128+
try {
2129+
const string_type::size_type pos = test_str.rindex(s);
2130+
Assert::IsTrue(pos != string_type::npos);
2131+
}
2132+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2133+
2134+
try {
2135+
const string_type::size_type pos = test_str.rindex(s, 2);
2136+
Assert::IsTrue(pos != string_type::npos);
2137+
}
2138+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2139+
2140+
try {
2141+
const string_type::size_type pos = test_str.rindex(s, 2, string_type::size_type(5 + 2 - 1));
2142+
Assert::IsTrue(pos != string_type::npos);
2143+
}
2144+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2145+
}
2146+
2147+
TEST_METHOD(rindex_n_char)
2148+
{
2149+
using string_type = pcs::CppString;
2150+
2151+
pcs::CppString test_str{ "ABC0123456789." };
2152+
char ch{ '3' };
2153+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(ch), test_str.rindex_n(ch, 20));
2154+
Assert::AreEqual(test_str.substr(2, 5).MyBaseClass::rfind(ch), test_str.rindex_n(ch, 2, 5) - 2);
2155+
try {
2156+
const string_type::size_type pos = test_str.rindex_n('z', 20);
2157+
Assert::IsTrue(pos != string_type::npos);
2158+
}
2159+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2160+
try {
2161+
const string_type::size_type pos = test_str.rindex_n('z', 2, 5);
2162+
Assert::IsTrue(pos != string_type::npos);
2163+
}
2164+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2165+
2166+
CppString s(ch);
2167+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(s), test_str.rindex_n(s, 20));
2168+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(s), test_str.rindex_n(s, 3, 5) - 3);
2169+
s = 'z';
2170+
try {
2171+
const string_type::size_type pos = test_str.rindex_n(s, 20);
2172+
Assert::IsTrue(pos != string_type::npos);
2173+
}
2174+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2175+
try {
2176+
const string_type::size_type pos = test_str.rindex_n(s, 2, 5);
2177+
Assert::IsTrue(pos != string_type::npos);
2178+
}
2179+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2180+
2181+
char str[2]{ ch, 0 };
2182+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(str), test_str.rindex_n(str, 20));
2183+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(str), test_str.rindex_n(str, 3, 5) - 3);
2184+
str[0] = 'z';
2185+
try {
2186+
const string_type::size_type pos = test_str.rindex_n(s, 20);
2187+
Assert::IsTrue(pos != string_type::npos);
2188+
}
2189+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2190+
try {
2191+
const string_type::size_type pos = test_str.rindex_n(s, 2, 5);
2192+
Assert::IsTrue(pos != string_type::npos);
2193+
}
2194+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2195+
}
2196+
2197+
TEST_METHOD(rindex_n_wchar_t)
2198+
{
2199+
using string_type = pcs::CppWString;
2200+
2201+
string_type test_str{ L"ABC0123456789." };
2202+
wchar_t ch{ L'3' };
2203+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(ch), test_str.rindex_n(ch, 20));
2204+
Assert::AreEqual(test_str.substr(2, 5).MyBaseClass::rfind(ch), test_str.rindex_n(ch, 2, 5) - 2);
2205+
try {
2206+
const string_type::size_type pos = test_str.rindex_n('z', 20);
2207+
Assert::IsTrue(pos != string_type::npos);
2208+
}
2209+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2210+
try {
2211+
const string_type::size_type pos = test_str.rindex_n('z', 2, 5);
2212+
Assert::IsTrue(pos != string_type::npos);
2213+
}
2214+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2215+
2216+
string_type s(ch);
2217+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(s), test_str.rindex_n(s, 20));
2218+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(s), test_str.rindex_n(s, 3, 5) - 3);
2219+
try {
2220+
const string_type::size_type pos = test_str.rindex_n(s, 20);
2221+
Assert::IsTrue(pos != string_type::npos);
2222+
}
2223+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2224+
try {
2225+
const string_type::size_type pos = test_str.rindex_n(s, 2, 5);
2226+
Assert::IsTrue(pos != string_type::npos);
2227+
}
2228+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2229+
2230+
wchar_t str[2]{ ch, 0 };
2231+
Assert::AreEqual(test_str.substr(0, 20).MyBaseClass::rfind(str), test_str.rindex_n(str, 20));
2232+
Assert::AreEqual(test_str.substr(3, 5).MyBaseClass::rfind(str), test_str.rindex_n(str, 3, 5) - 3);
2233+
str[0] = L'z';
2234+
try {
2235+
const string_type::size_type pos = test_str.rindex_n(s, 20);
2236+
Assert::IsTrue(pos != string_type::npos);
2237+
}
2238+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2239+
try {
2240+
const string_type::size_type pos = test_str.rindex_n(s, 2, 5);
2241+
Assert::IsTrue(pos != string_type::npos);
2242+
}
2243+
catch (const string_type::NotFoundException e) { /* ok case! */ }
2244+
}
2245+
19982246
};
19992247
}

cpp-strings/cppstrings.h

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1318,7 +1318,8 @@ namespace pcs // i.e. "pythonic c++ strings"
13181318
{
13191319
const size_type ret_value = rfind(sub, start, end);
13201320
if (ret_value == CppStringT::npos)
1321-
throw NotFoundException(std::format("substring \"{}\" not found in string \"{}\"", sub, this->c_str()));
1321+
throw NotFoundException("substring not found in string");
1322+
//throw NotFoundException(std::format("substring \"{}\" not found in string \"{}\"", sub, this->c_str()));
13221323
else
13231324
return ret_value;
13241325
}

0 commit comments

Comments
 (0)