moar tests

This commit is contained in:
Damimani 2026-11-06 21:57:42 +01:00
parent 938e557caa
commit d5974bb824
2 changed files with 104 additions and 3 deletions

View file

@ -185,6 +185,38 @@ namespace UnitTest1
}
}
}
TEST_METHOD(EqualOperatorTests) {
CMyString preps[]{
CMyString(),
CMyString("testtesttest"),
};
CMyString strs[]{
CMyString("test"),
CMyString("test"),
};
int expected[][2] = {
{ 4, 10 },
{ 4, 13 },
};
int testcount = sizeof(expected) / (sizeof(int) * 2);
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyString res = preps[i];
res = strs[i];
Assert::AreEqual(expected[i][0], (int)res.size());
Assert::AreEqual(expected[i][1], (int)res.capacity());
Assert::AreEqual(strs[i].c_str(), res.c_str());
}
catch (...)
{
Assert::IsTrue(false);
}
}
CMyString str1 = CMyString("test");
CMyString str2 = str1;
}
TEST_METHOD(GetAtTests) {
CMyString str[]{
@ -308,7 +340,65 @@ namespace UnitTest1
}
TEST_METHOD(EraseTests) {
//TODO
CMyString str[]{
CMyString(),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString('a', 10),
};
int pos[] = {
0,
4,
-1,
3,
3,
3,
};
int cnt[] = {
1,
1,
1,
0,
1,
1,
};
char* text[] = {
'\0',
'\0',
'\0',
"test",
"tes",
"aaaaaaaaa",
};
int testcount = sizeof(pos) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].erase(pos[i], cnt[i]);
Assert::AreEqual(text[i], str[i].c_str());
}
catch (const CMyStringException& e)
{
if (i < 3)
{
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
}
else
{
Assert::IsTrue(false);
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(ShrinkToFitTests) {
}
};
}

View file

@ -147,9 +147,20 @@ void CMyString::erase(size_t pos, size_t count) {
{
count = m_nDataLength - pos;
}
for (size_t i = 0; i < count; i++)
size_t i;
for (i = 0; i < count; i++)
{
m_pchData[pos + i] = '\0';
m_pchData[pos + i] = ' ';
}
for (size_t j = pos; j < m_nDataLength; j++)
{
if (!m_pchData[j + i])
{
m_pchData[j] = '\0';
m_nDataLength = j;
break;
}
m_pchData[j] = m_pchData[j + i];
}
}
bool CMyString::empty() const {