feladat5 v1 done
This commit is contained in:
parent
d5974bb824
commit
a5b58c2d8a
3 changed files with 144 additions and 10 deletions
|
|
@ -189,14 +189,20 @@ namespace UnitTest1
|
||||||
CMyString preps[]{
|
CMyString preps[]{
|
||||||
CMyString(),
|
CMyString(),
|
||||||
CMyString("testtesttest"),
|
CMyString("testtesttest"),
|
||||||
|
CMyString("testtesttest"),
|
||||||
|
CMyString("test"),
|
||||||
};
|
};
|
||||||
CMyString strs[]{
|
CMyString strs[]{
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
|
CMyString("testtesttesttest"),
|
||||||
|
CMyString("testtesttesttest"),
|
||||||
};
|
};
|
||||||
int expected[][2] = {
|
int expected[][2] = {
|
||||||
{ 4, 10 },
|
{ 4, 10 },
|
||||||
{ 4, 13 },
|
{ 4, 13 },
|
||||||
|
{ 16, 34 },
|
||||||
|
{ 16, 34 },
|
||||||
};
|
};
|
||||||
int testcount = sizeof(expected) / (sizeof(int) * 2);
|
int testcount = sizeof(expected) / (sizeof(int) * 2);
|
||||||
for (size_t i = 0; i < testcount; i++)
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
|
@ -365,9 +371,9 @@ namespace UnitTest1
|
||||||
1,
|
1,
|
||||||
};
|
};
|
||||||
char* text[] = {
|
char* text[] = {
|
||||||
'\0',
|
"",
|
||||||
'\0',
|
NULL,
|
||||||
'\0',
|
NULL,
|
||||||
"test",
|
"test",
|
||||||
"tes",
|
"tes",
|
||||||
"aaaaaaaaa",
|
"aaaaaaaaa",
|
||||||
|
|
@ -398,7 +404,130 @@ namespace UnitTest1
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
TEST_METHOD(ShrinkToFitTests) {
|
TEST_METHOD(ShrinkToFitTests) {
|
||||||
|
CMyString str[]{
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString(),
|
||||||
|
};
|
||||||
|
int eraseargs[][2] = {
|
||||||
|
{ 0, 0 },
|
||||||
|
{ 0, 4 },
|
||||||
|
{ 0, 0 },
|
||||||
|
};
|
||||||
|
int res[][2] = {
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 0, 1 },
|
||||||
|
{ 0, 1 },
|
||||||
|
};
|
||||||
|
int testcount = sizeof(res) / sizeof(int) / 2;
|
||||||
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
str[i].erase(eraseargs[i][0], eraseargs[i][1]);
|
||||||
|
str[i].shrink_to_fit();
|
||||||
|
Assert::AreEqual(res[i][0], (int)str[i].size());
|
||||||
|
Assert::AreEqual(res[i][1], (int)str[i].capacity());
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
Assert::IsTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
TEST_METHOD(ReverseTests) {
|
||||||
|
CMyString str[]{
|
||||||
|
CMyString(),
|
||||||
|
CMyString("bal"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("disestablishmentarianism"),
|
||||||
|
};
|
||||||
|
char* text[] = {
|
||||||
|
"",
|
||||||
|
"lab",
|
||||||
|
"tset",
|
||||||
|
"msinairatnemhsilbatsesid"
|
||||||
|
};
|
||||||
|
int testcount = sizeof(text) / sizeof(char*);
|
||||||
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
str[i].reverse();
|
||||||
|
Assert::AreEqual(text[i], str[i].c_str());
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
Assert::IsTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
TEST_METHOD(AppendTests) {
|
||||||
|
CMyString str[]{
|
||||||
|
CMyString(),
|
||||||
|
CMyString(),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
};
|
||||||
|
const char* text[] = {
|
||||||
|
"",
|
||||||
|
nullptr,
|
||||||
|
str[2].c_str(),
|
||||||
|
"test",
|
||||||
|
"test",
|
||||||
|
"test",
|
||||||
|
"test",
|
||||||
|
};
|
||||||
|
size_t extraargs[][2] = {
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 4, ~0 },
|
||||||
|
{ 0, 5 },
|
||||||
|
{ 2, 3 },
|
||||||
|
};
|
||||||
|
int sizes[][2] = {
|
||||||
|
{ 0, 1 },
|
||||||
|
{ 0, 1 },
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 8, 16 },
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 4, 5 },
|
||||||
|
};
|
||||||
|
const char* expected[] = {
|
||||||
|
"",
|
||||||
|
"",
|
||||||
|
"test",
|
||||||
|
"testtest",
|
||||||
|
CMyStringException::ErrOutOfRange,
|
||||||
|
CMyStringException::ErrOutOfRange,
|
||||||
|
CMyStringException::ErrOutOfRange,
|
||||||
|
};
|
||||||
|
int testcount = sizeof(text) / sizeof(char*);
|
||||||
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
{
|
||||||
|
try
|
||||||
|
{
|
||||||
|
str[i].append(text[i], extraargs[i][0], extraargs[i][1]);
|
||||||
|
Assert::AreEqual(expected[i], str[i].c_str());
|
||||||
|
Assert::AreEqual(sizes[i][0], (int)str[i].size());
|
||||||
|
Assert::AreEqual(sizes[i][1], (int)str[i].capacity());
|
||||||
|
}
|
||||||
|
catch (const CMyStringException& e)
|
||||||
|
{
|
||||||
|
Assert::AreEqual(expected[i], e.what());
|
||||||
|
}
|
||||||
|
catch (...)
|
||||||
|
{
|
||||||
|
Assert::IsTrue(false);
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
}
|
}
|
||||||
|
|
|
||||||
|
|
@ -139,6 +139,10 @@ void CMyString::reserve(size_t newcapacity) {
|
||||||
delete tmp;
|
delete tmp;
|
||||||
}
|
}
|
||||||
void CMyString::erase(size_t pos, size_t count) {
|
void CMyString::erase(size_t pos, size_t count) {
|
||||||
|
if (empty())
|
||||||
|
{
|
||||||
|
return;
|
||||||
|
}
|
||||||
if (pos >= m_nDataLength)
|
if (pos >= m_nDataLength)
|
||||||
{
|
{
|
||||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
||||||
|
|
@ -172,7 +176,7 @@ void CMyString::display() const {
|
||||||
cout << m_pchData;
|
cout << m_pchData;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CMyString::shrink_to_fill() {
|
void CMyString::shrink_to_fit() {
|
||||||
m_nAllocLength = m_nDataLength + 1;
|
m_nAllocLength = m_nDataLength + 1;
|
||||||
this->reserve(m_nAllocLength);
|
this->reserve(m_nAllocLength);
|
||||||
}
|
}
|
||||||
|
|
@ -189,22 +193,23 @@ void CMyString::reverse() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
|
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
|
||||||
if (psz == nullptr || psz == m_pchData)
|
if (psz == nullptr || psz == m_pchData || psz == "")
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
size_t len = strLength(psz);
|
size_t len = strLength(psz);
|
||||||
if (count == ~0)
|
if (count == ~0)
|
||||||
{
|
{
|
||||||
count = (int)len;
|
count = (int)len - offset;
|
||||||
}
|
}
|
||||||
if (len < (int)(count - offset))
|
int range = (count + offset);
|
||||||
|
if (len < range || offset == len)
|
||||||
{
|
{
|
||||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
||||||
}
|
}
|
||||||
if (count + m_nDataLength > m_nAllocLength)
|
if (count + m_nDataLength > m_nAllocLength)
|
||||||
{
|
{
|
||||||
this->reserve((m_nAllocLength + count)*2);
|
this->reserve((m_nDataLength + count)*2);
|
||||||
}
|
}
|
||||||
size_t i = m_nDataLength;
|
size_t i = m_nDataLength;
|
||||||
size_t j = offset;
|
size_t j = offset;
|
||||||
|
|
@ -222,7 +227,7 @@ CMyString& CMyString::operator=(const CMyString& str) {
|
||||||
{
|
{
|
||||||
if (str.size() >= m_nAllocLength)
|
if (str.size() >= m_nAllocLength)
|
||||||
{
|
{
|
||||||
this->reserve(str.capacity() * 2);
|
this->reserve((str.size() + 1) * 2);
|
||||||
}
|
}
|
||||||
m_nDataLength = str.size();
|
m_nDataLength = str.size();
|
||||||
CopyStr(m_pchData, str.c_str(), m_nDataLength);
|
CopyStr(m_pchData, str.c_str(), m_nDataLength);
|
||||||
|
|
|
||||||
|
|
@ -17,7 +17,7 @@ public:
|
||||||
void erase(size_t pos, size_t count);
|
void erase(size_t pos, size_t count);
|
||||||
bool empty() const;
|
bool empty() const;
|
||||||
void display() const;
|
void display() const;
|
||||||
void shrink_to_fill();
|
void shrink_to_fit();
|
||||||
void reverse();
|
void reverse();
|
||||||
void append(const char* psz, unsigned offset = 0, unsigned count = ~0);
|
void append(const char* psz, unsigned offset = 0, unsigned count = ~0);
|
||||||
CMyString& operator=(const CMyString& str);
|
CMyString& operator=(const CMyString& str);
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue