From a5b58c2d8a1092291b139bff2c5b4ab61e6a7aa9 Mon Sep 17 00:00:00 2001 From: Damimani Date: Sat, 7 Nov 2026 13:55:42 +0100 Subject: [PATCH] feladat5 v1 done --- feladat5/UnitTest1/UnitTest1.cpp | 135 ++++++++++++++++++++++++++++++- feladat5/feladat5/CMyString.cpp | 17 ++-- feladat5/feladat5/CMyString.h | 2 +- 3 files changed, 144 insertions(+), 10 deletions(-) diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index 28e7d7d..dc260dc 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -189,14 +189,20 @@ namespace UnitTest1 CMyString preps[]{ CMyString(), CMyString("testtesttest"), + CMyString("testtesttest"), + CMyString("test"), }; CMyString strs[]{ CMyString("test"), CMyString("test"), + CMyString("testtesttesttest"), + CMyString("testtesttesttest"), }; int expected[][2] = { { 4, 10 }, { 4, 13 }, + { 16, 34 }, + { 16, 34 }, }; int testcount = sizeof(expected) / (sizeof(int) * 2); for (size_t i = 0; i < testcount; i++) @@ -365,9 +371,9 @@ namespace UnitTest1 1, }; char* text[] = { - '\0', - '\0', - '\0', + "", + NULL, + NULL, "test", "tes", "aaaaaaaaa", @@ -398,7 +404,130 @@ namespace UnitTest1 } } 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); + } + } } }; } diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 95d6cb5..2e36838 100644 --- a/feladat5/feladat5/CMyString.cpp +++ b/feladat5/feladat5/CMyString.cpp @@ -139,6 +139,10 @@ void CMyString::reserve(size_t newcapacity) { delete tmp; } void CMyString::erase(size_t pos, size_t count) { + if (empty()) + { + return; + } if (pos >= m_nDataLength) { throw CMyStringException(CMyStringException::ErrOutOfRange); @@ -172,7 +176,7 @@ void CMyString::display() const { cout << m_pchData; } } -void CMyString::shrink_to_fill() { +void CMyString::shrink_to_fit() { m_nAllocLength = m_nDataLength + 1; this->reserve(m_nAllocLength); } @@ -189,22 +193,23 @@ void CMyString::reverse() { } } void CMyString::append(const char* psz, unsigned offset, unsigned count) { - if (psz == nullptr || psz == m_pchData) + if (psz == nullptr || psz == m_pchData || psz == "") { return; } size_t len = strLength(psz); 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); } 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 j = offset; @@ -222,7 +227,7 @@ CMyString& CMyString::operator=(const CMyString& str) { { if (str.size() >= m_nAllocLength) { - this->reserve(str.capacity() * 2); + this->reserve((str.size() + 1) * 2); } m_nDataLength = str.size(); CopyStr(m_pchData, str.c_str(), m_nDataLength); diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h index c081537..4801e2f 100644 --- a/feladat5/feladat5/CMyString.h +++ b/feladat5/feladat5/CMyString.h @@ -17,7 +17,7 @@ public: void erase(size_t pos, size_t count); bool empty() const; void display() const; - void shrink_to_fill(); + void shrink_to_fit(); void reverse(); void append(const char* psz, unsigned offset = 0, unsigned count = ~0); CMyString& operator=(const CMyString& str);