From 49d964d8f14ef470e0d311b1f6f744fc7712c79c Mon Sep 17 00:00:00 2001 From: Damimani <41192030+Damimani@users.noreply.github.com> Date: Sun, 15 Nov 2026 14:31:05 +0100 Subject: [PATCH 1/3] feladat6 v1 (no test) --- .github/copilot-instructions.md | 4 + feladat5/UnitTest1/UnitTest1.cpp | 6 +- feladat5/feladat5/CMyString.cpp | 267 +++++++++++++++++++++-- feladat5/feladat5/CMyString.h | 27 ++- feladat5/feladat5/CMyStringException.cpp | 2 +- feladat5/feladat5/CMyStringException.h | 2 +- feladat5/feladat5/main.cpp | 2 +- 7 files changed, 291 insertions(+), 19 deletions(-) create mode 100644 .github/copilot-instructions.md diff --git a/.github/copilot-instructions.md b/.github/copilot-instructions.md new file mode 100644 index 0000000..1ca2674 --- /dev/null +++ b/.github/copilot-instructions.md @@ -0,0 +1,4 @@ +# Copilot Instructions + +## Project Guidelines +- The user requires CMyString.h to remain unchanged when fixing the implementation. \ No newline at end of file diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index 4ce6dcc..16646c7 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "pch.h" #include "CppUnitTest.h" #include @@ -385,6 +385,7 @@ namespace UnitTest1 CMyString('a', 10), CMyString("alma"), CMyString("test"), + CMyString("alma"), }; int pos[] = { 0, @@ -395,6 +396,7 @@ namespace UnitTest1 3, 4, 0, + 1, }; int cnt[] = { 1, @@ -405,6 +407,7 @@ namespace UnitTest1 1, 100, 0, + -1, }; char* text[] = { "", @@ -415,6 +418,7 @@ namespace UnitTest1 "aaaaaaaaa", "alma", "test", + "a" }; int testcount = sizeof(pos) / sizeof(int); for (size_t i = 0; i < testcount; i++) diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 92aa8e0..372134b 100644 --- a/feladat5/feladat5/CMyString.cpp +++ b/feladat5/feladat5/CMyString.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyString.h" #include "CMyStringException.h" #include @@ -32,7 +32,16 @@ static void CopyStr(char* dest, const char* src, size_t length) { static void MakeEmptyStr(size_t* datalngth, size_t* alloclngth, char** ptr) { *datalngth = 0; *alloclngth = 1; + char* cpy = nullptr; + if (ptr != nullptr) + { + cpy = *ptr; + } *ptr = new char[1]; + if (cpy != nullptr) + { + delete[] cpy; + } (*ptr)[0] = '\0'; } @@ -41,11 +50,53 @@ static void reserveNoCheck(size_t* alloc, size_t* size, char** data, size_t news *alloc = newsize; *data = new char[*alloc]; CopyStr(*data, tmp, *size); - delete tmp; + delete[] tmp; +} +static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc, size_t* size, char** data) { + size_t dlngth = *size; + size_t alngth = *alloc; + char* old = new char[*alloc]; + size_t srclen = 0; + if (src != "") + { + srclen = strLength(src); + } + CopyStr(old, *data, *size); + try + { + char* cpy = new char[int(*size + srclen + 1)]; + CopyStr(cpy, *data, pos); + if (srclen != 0) + { + CopyStr(cpy + pos, src, srclen); + } + CopyStr(cpy + pos + srclen, *data + pos + delcnt, *size - pos); + *size = strLength(cpy); + if (*alloc <= *size) + { + reserveNoCheck(alloc, size, data, ((*size + 1) * 2)); + } + *data = cpy; + delete[] old; + } + catch (const bad_alloc e) + { + *size = dlngth; + *alloc = alngth; + *data = old; + throw e; + } + catch (...) { + throw CMyStringException(CMyStringException::ErrUnexpected); + } } CMyString::CMyString() { MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG + } CMyString::CMyString(const char* psz) { if (psz == nullptr) @@ -63,6 +114,9 @@ CMyString::CMyString(const char* psz) { m_pchData = new char[m_nAllocLength]; CopyStr(m_pchData, psz, m_nDataLength); } +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::CMyString(char ch, size_t repeat) { if (ch == '\0') @@ -73,17 +127,17 @@ CMyString::CMyString(char ch, size_t repeat) { { throw CMyStringException(CMyStringException::ErrCount); } - else + m_nDataLength = 0; + m_nAllocLength = repeat + 1; + m_pchData = new char[m_nAllocLength]; + while (m_nDataLength < repeat) { - m_nDataLength = 0; - m_nAllocLength = repeat + 1; - m_pchData = new char[m_nAllocLength]; - while (m_nDataLength < repeat) - { - m_pchData[m_nDataLength++] = ch; - } - m_pchData[m_nAllocLength-1] = '\0'; + m_pchData[m_nDataLength++] = ch; } + m_pchData[m_nAllocLength-1] = '\0'; +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::CMyString(const CMyString& str) { if (str.empty()) @@ -104,9 +158,15 @@ CMyString::CMyString(const CMyString& str) { } m_pchData[m_nDataLength] = '\0'; } +#ifdef _MYDEBUG + m_iCounter++; +#endif // _MYDEBUG } CMyString::~CMyString() { - delete m_pchData; + delete[] m_pchData; +#ifdef _MYDEBUG + m_iCounter--; +#endif // _MYDEBUG } size_t CMyString::size() const { return m_nDataLength; @@ -155,6 +215,10 @@ void CMyString::erase(size_t pos, size_t count) { { return; } + if (count > m_nDataLength) + { + count = m_nDataLength; + } if (pos + count > m_nDataLength) { count = m_nDataLength - pos; @@ -253,4 +317,181 @@ CMyString& CMyString::operator=(const CMyString& str) { } } return *this; -} \ No newline at end of file +} + +CMyString::CMyString(CMyString&& str){ + m_nDataLength = str.m_nDataLength; + m_nAllocLength = str.m_nAllocLength; + m_pchData = str.m_pchData; + str = CMyString(); +} +CMyString& CMyString::operator=(CMyString&& str){ + m_nDataLength = str.m_nDataLength; + m_nAllocLength = str.m_nAllocLength; + char* old = m_pchData; + m_pchData = str.m_pchData; + delete[] old; + str = CMyString(); + return *this; +} +char& CMyString::operator[](size_t nIndex){ + if (nIndex >= m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + return m_pchData[nIndex]; +} +bool CMyString::operator==(const CMyString& s1){ + if (s1.c_str() != c_str()) + { + return false; + } + if (s1.capacity() != capacity()) + { + return false; + } + if (s1.size() != size()) + { + return false; + } + return true; +} +CMyString& CMyString::operator+=(const CMyString& str){ + append(str.c_str()); + return *this; +} +CMyString& CMyString::operator+=(const char* p){ + append(p); + return *this; +} +CMyString& CMyString::operator+(const CMyString& str1){ + static CMyString res = *this; + res.append(str1.c_str()); + return res; +} +CMyString::operator const char* (){ + return m_pchData; +} +CMyString CMyString::substring(size_t pos, size_t len){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (pos == m_nDataLength || len == 0) + { + return ""; + } + if (len > m_nDataLength) + { + len = m_nDataLength; + } + if ((len + pos) > m_nDataLength) + { + len = m_nDataLength - pos; + } + char* cpy = new char[len + 1]; + CopyStr(cpy, c_str() + pos, len); + CMyString substr = CMyString(cpy); + delete[] cpy; + return substr; +} +void CMyString::swap(CMyString& str1){ + char* tempData = m_pchData; + size_t tempLength = m_nDataLength; + size_t tempAllocLength = m_nAllocLength; + + m_pchData = str1.m_pchData; + m_nDataLength = str1.m_nDataLength; + m_nAllocLength = str1.m_nAllocLength; + + str1.m_pchData = tempData; + str1.m_nDataLength = tempLength; + str1.m_nAllocLength = tempAllocLength; +} +int CMyString::find(const char* psz) const{ + if (psz == nullptr || psz == "") + { + return -1; + } + int pszlen = strLength(psz); + if (pszlen > m_nDataLength) + { + return -1; + } + size_t i = 0; + size_t j = 0; + size_t ret = -1; + while (pszlen + i <= m_nDataLength && j < pszlen) + { + if (getat(i) == psz[j]) + { + ret = ret == -1 ? i : ret; + j++; + } + else + { + ret = -1; + j = 0; + } + i++; + } + return ret; +} +void CMyString::insert(size_t pos, const char* psz){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (psz == nullptr || psz == "") + { + return; + } + if (pos == m_nDataLength) + { + append(psz); + } + else + { + insertStrAtIndex(psz, pos, 0, &m_nAllocLength, &m_nDataLength, &m_pchData); + } +} +void CMyString::replace(size_t pos, size_t count, const char* psz){ + if (pos > m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + if (count > m_nDataLength) + { + count = m_nDataLength; + } + if ((count + pos) > m_nDataLength) + { + count = m_nDataLength - pos; + } + if (psz == nullptr) + { + psz = ""; + } + if (pos == m_nDataLength) + { + append(psz); + } + else + { + insertStrAtIndex(psz, pos, count, &m_nAllocLength, &m_nDataLength, &m_pchData); + } +} + +std::ostream& operator<<(std::ostream& os, CMyString& s) { + os << s.c_str(); + return os; +} + +#ifdef _MYDEBUG +void CMyString::clearobjcount() { + m_iCounter = 0; +} +unsigned CMyString::objcount() { + return m_iCounter; +} +#endif // _MYDEBUG diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h index 11ceac5..a9e0d57 100644 --- a/feladat5/feladat5/CMyString.h +++ b/feladat5/feladat5/CMyString.h @@ -1,5 +1,6 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #pragma once +#include class CMyString { public: CMyString(); @@ -21,8 +22,30 @@ public: void reverse(); void append(const char* psz, unsigned offset = 0, unsigned count = ~0); CMyString& operator=(const CMyString& str); + CMyString(CMyString&& str); + CMyString& operator=(CMyString&& str); + char& operator[](size_t nIndex); + bool operator==(const CMyString& s1); + CMyString& operator+=(const CMyString& str); + CMyString& operator+=(const char* p); + CMyString& operator+(const CMyString& str1); + operator const char* (); + CMyString substring(size_t pos, size_t len = (~0)); + void swap(CMyString& str1); + int find(const char* psz) const; + void insert(size_t pos, const char* psz); + void replace(size_t pos, size_t count, const char* psz); private: char* m_pchData; size_t m_nDataLength; size_t m_nAllocLength; -}; \ No newline at end of file +#ifdef _MYDEBUG +public: + static void clearobjcount(); + static unsigned objcount(); +private: + static unsigned m_iCounter; +#endif // _MYDEBUG +}; + +std::ostream& operator<<(std::ostream& os, CMyString& s); \ No newline at end of file diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp index 222c3d2..7f0d4a2 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyStringException.h" #include "CMyString.h" diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h index fcb0761..77db8df 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #pragma once #include #include "CMyString.h" diff --git a/feladat5/feladat5/main.cpp b/feladat5/feladat5/main.cpp index f68ceaa..f93f701 100644 --- a/feladat5/feladat5/main.cpp +++ b/feladat5/feladat5/main.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyString.h" int main() { From 08a18e69d4cdf96a130954ca8f7e9f733972c980 Mon Sep 17 00:00:00 2001 From: Damimani <41192030+Damimani@users.noreply.github.com> Date: Sun, 15 Nov 2026 17:33:06 +0100 Subject: [PATCH 2/3] feladat6 v1 done --- feladat5/UnitTest1/UnitTest1.cpp | 423 ++++++++++++++++++++++- feladat5/feladat5/CMyString.cpp | 86 +++-- feladat5/feladat5/CMyStringException.cpp | 24 +- feladat5/feladat5/CMyStringException.h | 3 +- 4 files changed, 471 insertions(+), 65 deletions(-) diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index 16646c7..b38face 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -1,4 +1,5 @@ // Tóth Dániel Mátyás | IFPQYY | v1 +#define _MYDEBUG #include "pch.h" #include "CppUnitTest.h" #include @@ -509,7 +510,7 @@ namespace UnitTest1 CMyString("test"), CMyString("test"), CMyString("test"), - CMyString("test"), + CMyString("test"), CMyString("test"), CMyString("banan"), CMyString("banan"), @@ -595,5 +596,425 @@ namespace UnitTest1 } } } + + TEST_METHOD(EqualsOperatorTests) { + CMyString a = CMyString("kutya"); + CMyString* b = &a; + Assert::IsTrue(a == *b); + + CMyString c = CMyString("kutya"); + Assert::IsFalse(a == c); + } + + TEST_METHOD(IndexCheckTests) { + CMyString str[]{ + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + }; + size_t indexes[] = { + 0, + 0, + 4, + 3, + }; + char expected[] = { + '\0', //will mark not valid characters with null + 't', + '\0', + 't', + }; + size_t testcount = sizeof(expected) / sizeof(char); + for (size_t i = 0; i < testcount; i++) + { + try + { + Assert::AreEqual(expected[i], str[i][indexes[i]]); + } + catch (const CMyStringException& e) + { + if (expected[i] == '\0') + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(ConstCharCastingTest) { + CMyString str = CMyString("test"); + Assert::AreEqual(str.c_str(), (const char*)str); + } + + TEST_METHOD(MoveOperatorTests) { + CMyString a = CMyString(); + CMyString b = CMyString("test"); + a = move(b); + + Assert::AreEqual("test", (const char*)a); + Assert::IsTrue(b.empty()); + + CMyString c = CMyString(move(a)); + Assert::AreEqual("test", (const char*)c); + Assert::IsTrue(a.empty()); + } + + TEST_METHOD(AddStringOperatorTests) { + CMyString strbase[]{ + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("banan"), + }; + const char* text[] = { + "", + "test", + "a", + "narancs", + }; + size_t extraargs[][2] = { + { 0, ~0 }, + { 0, ~0 }, + { 0, ~0 }, + { 0, ~0 }, + }; + int sizes[][2] = { + { 0, 1 }, + { 8, 18 }, + { 5, 12 }, + { 12, 26 }, + }; + const char* expected[] = { + "", + "testtest", + "testa", + "banannarancs", + }; + int testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + CMyString res; + res = move(strbase[i] + text[i]); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i][0], (int)res.size()); + Assert::AreEqual(sizes[i][1], (int)res.capacity()); + + CMyString stradd = CMyString(text[i]); + res = CMyString(move(strbase[i] + stradd)); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i][0], (int)res.size()); + Assert::AreEqual(sizes[i][1], (int)res.capacity()); + + strbase[i] += stradd; + Assert::AreEqual(expected[i], strbase[i].c_str()); + Assert::AreEqual(sizes[i][0], (int)strbase[i].size()); + Assert::AreEqual(sizes[i][1], (int)strbase[i].capacity()); + } + catch (const CMyStringException& e) + { + Assert::AreEqual(expected[i], e.what()); + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + TEST_METHOD(SubstringTests) { + CMyString str[] = { + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("greek"), + }; + size_t args[][2] = { + { 0, ~0 }, + { 0, ~0 }, + { 2, ~0 }, + { 4, ~0 }, + { 5, ~0 }, + { 0, 1 }, + { 1, 1 }, + { 1, 999 }, + { 1, 0 }, + { 2, 2 }, + }; + char* expected[] = { + "", + "test", + "st", + "", + nullptr, + "t", + "e", + "est", + "", + "ee" + }; + int sizes[] = { + 0, + 4, + 2, + 0, + 0, + 1, + 1, + 3, + 0, + 2, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + CMyString res = str[i].substring(args[i][0], args[i][1]); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i], (int)res.size()); + Assert::AreEqual(sizes[i] +1, (int)res.capacity()); + } + catch (const CMyStringException& e) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(SwapTest) { + CMyString a = CMyString("contentA"); + CMyString b = CMyString("contentB"); + a.swap(b); + Assert::AreEqual("contentB", (const char*)a); + Assert::AreEqual("contentA", (const char*)b); + } + + TEST_METHOD(FindTests) { + CMyString str[] = { + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + }; + char* find[] = { + "", + "", + nullptr, + "h", + "e", + "test", + "testt", + "est", + }; + int expected[] = { + -1, + -1, + -1, + -1, + 1, + 0, + -1, + 1, + }; + size_t testcount = sizeof(expected) / sizeof(int); + for (size_t i = 0; i < testcount; i++) + { + try + { + Assert::AreEqual(expected[i], str[i].find(find[i])); + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + TEST_METHOD(InsertTests) { + CMyString str[] = { + CMyString(), + CMyString(), + CMyString("ALMA"), + CMyString("AB"), + CMyString("ALMA"), + CMyString("test"), + CMyString("test"), + }; + const char* inserttext[] = { + "", + "", + "XY", + str[3].c_str(), + "X", + "test", + nullptr, + }; + int insertindex[] = { + 0, + 1, + 2, + 1, + 5, + 4, + 4, + }; + char* expected[] = { + "", + nullptr, + "ALXYMA", + "AABB", + nullptr, + "testtest", + "test", + }; + int sizes[][2] = { + { 0, 1 }, + { 0, 1 }, + { 6, 14 }, + { 4, 10 }, + { 4, 5 }, + { 8, 18 }, + { 4, 5 }, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + str[i].insert(insertindex[i], inserttext[i]); + 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) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + Assert::AreEqual(sizes[i][0], (int)str[i].size()); + Assert::AreEqual(sizes[i][1], (int)str[i].capacity()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(ReplaceTests) { + CMyString str[] = { + CMyString(), + CMyString(), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + }; + const char* inserttext[] = { + "", + "", + "X", + "XYZ123", + nullptr, + "" + }; + int insertindex[] = { + 0, + 1, + 2, + 2, + 2, + 0, + }; + int count[] = { + 0, + 0, + 3, + 3, + 3, + 0, + }; + char* expected[] = { + "", + nullptr, + "ABXFG", + "ABXYZ123FG", + "ABFG", + "ABCDEFG", + }; + int sizes[][2] = { + { 0, 1 }, + { 0, 1 }, + { 5, 8 }, + { 10, 22 }, + { 4, 8 }, + { 7, 8 }, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + str[i].replace(insertindex[i], count[i], inserttext[i]); + 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) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + Assert::AreEqual(sizes[i][0], (int)str[i].size()); + Assert::AreEqual(sizes[i][1], (int)str[i].capacity()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } }; } diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 372134b..3897f5c 100644 --- a/feladat5/feladat5/CMyString.cpp +++ b/feladat5/feladat5/CMyString.cpp @@ -32,16 +32,7 @@ static void CopyStr(char* dest, const char* src, size_t length) { static void MakeEmptyStr(size_t* datalngth, size_t* alloclngth, char** ptr) { *datalngth = 0; *alloclngth = 1; - char* cpy = nullptr; - if (ptr != nullptr) - { - cpy = *ptr; - } *ptr = new char[1]; - if (cpy != nullptr) - { - delete[] cpy; - } (*ptr)[0] = '\0'; } @@ -53,41 +44,28 @@ static void reserveNoCheck(size_t* alloc, size_t* size, char** data, size_t news delete[] tmp; } static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc, size_t* size, char** data) { - size_t dlngth = *size; - size_t alngth = *alloc; - char* old = new char[*alloc]; - size_t srclen = 0; - if (src != "") - { - srclen = strLength(src); - } - CopyStr(old, *data, *size); + size_t sourceLength = strLength(src); + size_t oldSize = *size; + size_t newSize = oldSize - delcnt + sourceLength; + size_t newCapacity = (*alloc > newSize) ? *alloc : (newSize + 1) * 2; + char* cpy = nullptr; + try { - char* cpy = new char[int(*size + srclen + 1)]; + cpy = new char[newCapacity]; CopyStr(cpy, *data, pos); - if (srclen != 0) - { - CopyStr(cpy + pos, src, srclen); - } - CopyStr(cpy + pos + srclen, *data + pos + delcnt, *size - pos); - *size = strLength(cpy); - if (*alloc <= *size) - { - reserveNoCheck(alloc, size, data, ((*size + 1) * 2)); - } + CopyStr(cpy + pos, src, sourceLength); + CopyStr(cpy + pos + sourceLength, *data + pos + delcnt, oldSize - pos - delcnt); + + delete[] *data; *data = cpy; - delete[] old; + *size = newSize; + *alloc = newCapacity; } - catch (const bad_alloc e) + catch (...) { - *size = dlngth; - *alloc = alngth; - *data = old; - throw e; - } - catch (...) { - throw CMyStringException(CMyStringException::ErrUnexpected); + delete[] cpy; + throw; } } @@ -313,7 +291,10 @@ CMyString& CMyString::operator=(const CMyString& str) { } else { - MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); + if (!empty()) + { + MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); + } } } return *this; @@ -323,15 +304,25 @@ CMyString::CMyString(CMyString&& str){ m_nDataLength = str.m_nDataLength; m_nAllocLength = str.m_nAllocLength; m_pchData = str.m_pchData; - str = CMyString(); + str.m_nDataLength = 0; + str.m_nAllocLength = 1; + char* empty = new char[1]; + empty[0] = '\0'; + str.m_pchData = empty; } CMyString& CMyString::operator=(CMyString&& str){ - m_nDataLength = str.m_nDataLength; - m_nAllocLength = str.m_nAllocLength; - char* old = m_pchData; - m_pchData = str.m_pchData; - delete[] old; - str = CMyString(); + if (this != &str) + { + char* empty = new char[1]; + empty[0] = '\0'; + delete[] m_pchData; + m_nDataLength = str.m_nDataLength; + m_nAllocLength = str.m_nAllocLength; + m_pchData = str.m_pchData; + str.m_nDataLength = 0; + str.m_nAllocLength = 1; + str.m_pchData = empty; + } return *this; } char& CMyString::operator[](size_t nIndex){ @@ -365,7 +356,8 @@ CMyString& CMyString::operator+=(const char* p){ return *this; } CMyString& CMyString::operator+(const CMyString& str1){ - static CMyString res = *this; + static CMyString res; + res = *this; res.append(str1.c_str()); return res; } diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp index 7f0d4a2..e22b89f 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,6 +1,5 @@ // Tóth Dániel Mátyás | IFPQYY | v1 #include "CMyStringException.h" -#include "CMyString.h" static int strLength(const char* str) { if (str == nullptr) @@ -15,21 +14,16 @@ static int strLength(const char* str) { return cnt; } -static void CopyStr(char* dest, const char* src, size_t length) { - if (dest == nullptr || src == nullptr) { - return; - } - size_t i; - for (i = 0; i < length; i++) - { - dest[i] = src[i]; - } - dest[i] = '\0'; -} - CMyStringException::CMyStringException(const char* input) { - msg = CMyString(input); + size_t length = strLength(input); + + msg = shared_ptr(new char[length + 1]); + for (size_t i = 0; i < length; ++i) + { + msg[i] = input[i]; + } + msg[length] = '\0'; } const char* CMyStringException::what() const { - return msg.c_str(); + return msg.get(); } \ No newline at end of file diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h index 77db8df..e15e54c 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,7 +1,6 @@ // Tóth Dániel Mátyás | IFPQYY | v1 #pragma once #include -#include "CMyString.h" using namespace std; class CMyStringException { public: @@ -12,5 +11,5 @@ public: static constexpr const char* ErrCount = "Hiba: karakterek száma 0!"; static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!"; private: - CMyString msg; + shared_ptr msg; }; \ No newline at end of file From dfa03b917780b4a5536b7365fd24a50a6b3db5a7 Mon Sep 17 00:00:00 2001 From: Damimani <41192030+Damimani@users.noreply.github.com> Date: Fri, 20 Nov 2026 18:54:08 +0100 Subject: [PATCH 3/3] feladat6 done --- feladat5/UnitTest1/UnitTest1.cpp | 56 ++- feladat5/feladat5/CMyString.cpp | 30 +- feladat5/feladat5/CMyString.h | 2 +- feladat5/feladat5/CMyStringException.cpp | 2 +- feladat5/feladat5/CMyStringException.h | 2 +- feladat5/feladat5/IFPQYY_feladat6v2.zip | Bin 0 -> 7502 bytes feladat5/feladat5/UnitTest1.cpp | 475 ++++++++++++++++++++++- feladat5/feladat5/main.cpp | 2 +- 8 files changed, 549 insertions(+), 20 deletions(-) create mode 100644 feladat5/feladat5/IFPQYY_feladat6v2.zip diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index b38face..d611938 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #define _MYDEBUG #include "pch.h" #include "CppUnitTest.h" @@ -515,6 +515,7 @@ namespace UnitTest1 CMyString("banan"), CMyString("banan"), CMyString("alma"), + CMyString("ABCDE"), }; const char* text[] = { "", @@ -530,6 +531,7 @@ namespace UnitTest1 "narancs", "narancs", "dio", + "BCDE", }; size_t extraargs[][2] = { { 0, ~0 }, @@ -545,6 +547,7 @@ namespace UnitTest1 { 0, ~0 }, { 2, 2 }, { 3, 1 }, + { 0, 2 }, }; int sizes[][2] = { { 0, 1 }, @@ -560,6 +563,7 @@ namespace UnitTest1 { 12, 26 }, { 7, 16 }, { 4, 5 }, + { 7, 16 }, }; const char* expected[] = { "", @@ -575,6 +579,7 @@ namespace UnitTest1 "banannarancs", "bananra", "alma", + "ABCDEBC", }; int testcount = sizeof(text) / sizeof(char*); for (size_t i = 0; i < testcount; i++) @@ -599,11 +604,14 @@ namespace UnitTest1 TEST_METHOD(EqualsOperatorTests) { CMyString a = CMyString("kutya"); - CMyString* b = &a; - Assert::IsTrue(a == *b); - - CMyString c = CMyString("kutya"); + CMyString b = CMyString("kutya"); + CMyString c = CMyString("cica"); + CMyString d; + CMyString e = CMyString("cicajuice"); + Assert::IsTrue(a == b); Assert::IsFalse(a == c); + Assert::IsFalse(a == d); + Assert::IsFalse(a == e); } TEST_METHOD(IndexCheckTests) { @@ -826,6 +834,9 @@ namespace UnitTest1 CMyString("test"), CMyString("test"), CMyString("test"), + CMyString("KORTEALMAEPER"), + CMyString("KORTEALMAEPER"), + CMyString("KORTEALMAEPER"), }; char* find[] = { "", @@ -836,6 +847,9 @@ namespace UnitTest1 "test", "testt", "est", + "kORTE", + "ALMa", + "EPEr", }; int expected[] = { -1, @@ -846,6 +860,9 @@ namespace UnitTest1 0, -1, 1, + -1, + -1, + -1, }; size_t testcount = sizeof(expected) / sizeof(int); for (size_t i = 0; i < testcount; i++) @@ -1016,5 +1033,34 @@ namespace UnitTest1 } } } + + TEST_METHOD(SpecificTests) { + CMyString s = CMyString("ABCDE"); + const char* const belsoForras = s.c_str() + 1; + s.append(belsoForras, 0, 2); + Assert::AreEqual("ABCDEBC", s.c_str()); + Assert::AreEqual((size_t)7, s.size()); + Assert::AreEqual((size_t)16, s.capacity()); + + CMyString g = CMyString("ALMA"); + g.reserve(12); + Assert::AreEqual((size_t)4, g.size()); + Assert::AreEqual((size_t)12, g.capacity()); + const char* p = g.c_str(); + g.insert(2, "XY"); + Assert::AreEqual("ALXYMA", g.c_str()); + Assert::AreEqual((size_t)6, g.size()); + Assert::AreEqual((size_t)12, g.capacity()); + Assert::IsTrue(p == g.c_str()); + + CMyString h = CMyString("ABCDEFG"); + const char* oldaddr = h.c_str(); + h.replace(2, 3, "X"); + Assert::AreEqual("ABXFG", h.c_str()); + Assert::AreEqual((size_t)5, h.size()); + Assert::AreEqual((size_t)8, h.capacity()); + Assert::IsTrue(oldaddr == h.c_str()); + Assert::AreEqual('\0', h.c_str()[h.size()]); + } }; } diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 3897f5c..5938b66 100644 --- a/feladat5/feladat5/CMyString.cpp +++ b/feladat5/feladat5/CMyString.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #include "CMyString.h" #include "CMyStringException.h" #include @@ -57,8 +57,12 @@ static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc CopyStr(cpy + pos, src, sourceLength); CopyStr(cpy + pos + sourceLength, *data + pos + delcnt, oldSize - pos - delcnt); - delete[] *data; - *data = cpy; + if (newSize+1 > *alloc) + { + reserveNoCheck(alloc, size, data, newCapacity); + } + CopyStr(*data, cpy, newSize); + delete[] cpy; *size = newSize; *alloc = newCapacity; } @@ -251,6 +255,8 @@ void CMyString::append(const char* psz, unsigned offset, unsigned count) { throw CMyStringException(CMyStringException::ErrCount); } size_t len = strLength(psz); + char* cpy = new char[len]; + CopyStr(cpy, psz, len); if (offset == len) { return; @@ -272,7 +278,7 @@ void CMyString::append(const char* psz, unsigned offset, unsigned count) { size_t j = offset; while (j < range) { - m_pchData[i++] = psz[j++]; + m_pchData[i++] = cpy[j++]; } m_nDataLength = i; m_pchData[i] = '\0'; @@ -333,10 +339,6 @@ char& CMyString::operator[](size_t nIndex){ return m_pchData[nIndex]; } bool CMyString::operator==(const CMyString& s1){ - if (s1.c_str() != c_str()) - { - return false; - } if (s1.capacity() != capacity()) { return false; @@ -345,6 +347,16 @@ bool CMyString::operator==(const CMyString& s1){ { return false; } + int i = 0; + const char* s1chararr = s1.c_str(); + while (i < size()) + { + if (m_pchData[i] != s1chararr[i]) + { + return false; + } + i++; + } return true; } CMyString& CMyString::operator+=(const CMyString& str){ @@ -413,7 +425,7 @@ int CMyString::find(const char* psz) const{ size_t i = 0; size_t j = 0; size_t ret = -1; - while (pszlen + i <= m_nDataLength && j < pszlen) + while (pszlen + i - j <= m_nDataLength && j < pszlen) { if (getat(i) == psz[j]) { diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h index a9e0d57..940fb10 100644 --- a/feladat5/feladat5/CMyString.h +++ b/feladat5/feladat5/CMyString.h @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #pragma once #include class CMyString { diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp index e22b89f..546ea29 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #include "CMyStringException.h" static int strLength(const char* str) { diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h index e15e54c..5d26f1c 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #pragma once #include using namespace std; diff --git a/feladat5/feladat5/IFPQYY_feladat6v2.zip b/feladat5/feladat5/IFPQYY_feladat6v2.zip new file mode 100644 index 0000000000000000000000000000000000000000..45a304792a61b048e2fce9cd73b7fd130c62e3f8 GIT binary patch literal 7502 zcmai(byOTpy2b|$3>pFi3rz3?*TFToy9S5Aph1GWgb*ai;2sD*!QDN$y95~s1RY#+ z$?iShm%Y1tZuRLt)&0lwc0KjhIq$DZRRIN+5C8yR0A8j!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^eD8t@HQGtp&4mbxR@b#f4i_ib>ngu1iq-_e;>MnWk0@)&va)aTn0blcgj0 zLori_7~yd&_}CH4J}BURBl%QY)A>@?_UYs)rt;!^s?5_cNs+Q|vNyiCAn!^_^M0Q^ zDYHBX#}F)p-VFt?wXSnMXe+poz;R|cz56t2W+M=il*kE&=1~GM^S)eQ#E0)bl4BpB zZhZr7QpPUX%+*-A)4wc~*SK~Shpg$h^&oS|*4XN{VPREb)q}KEqx=FJJP5@ro<`F$ zB32(Ok-QH`dVbGP zbK0J|Y;1jr)z-*3a>HVsYzCMhwA%X&!nE>Y0exB zy~bl)L3kz6WNC7r;DO0`vP*twDQEwzawFOTXs&(J(90`Xu2xpanv2Lj?Inb8uqc4G zJXv-gIZNM#T+!gV`ErWKsJAhw1ZLROxbMUv@q;%VnG-YEiy3mfmM&ivSIr&6sUw=q zz)Fk^{vE?Z-d4qfKhFrT+a|AOGDQas4ASKW-`boRt>xN(HtyM$z^${ffIJY}da1k?hCg_yvkh9BJ~x zisE_LvwY*n{L9)`ePf@CU%4U8fCg@>F~iArvUsBM4l(AqcNtTq1P<9{w8AnCQ{PF7 z1>Ppy8-9r~_4Oog9RA?Mnb(Gfab%uBwK0X5_?eWz5Xv0uO+lsu@5c-%>yMoT=ns5y zC3-qwV*)lNoO6-uALKskc_->|kE*JGge>|GH2hD;UbGKT^Sgom@%#fHf5Qdve?KDu zNC0R^oyDwf%MYaaukBCyf8Ry=Qyw4lPkHM<#mQFfEB-(6f8SR4Q{3=j$LcS_`Wt=! z&mh#wYODXhIR6gSf5z#30I~lN=U+yX#Jy4Z-=q9DxcoB;E$-i_`KM2z5!he @@ -385,6 +386,7 @@ namespace UnitTest1 CMyString('a', 10), CMyString("alma"), CMyString("test"), + CMyString("alma"), }; int pos[] = { 0, @@ -395,6 +397,7 @@ namespace UnitTest1 3, 4, 0, + 1, }; int cnt[] = { 1, @@ -405,6 +408,7 @@ namespace UnitTest1 1, 100, 0, + -1, }; char* text[] = { "", @@ -415,6 +419,7 @@ namespace UnitTest1 "aaaaaaaaa", "alma", "test", + "a" }; int testcount = sizeof(pos) / sizeof(int); for (size_t i = 0; i < testcount; i++) @@ -505,11 +510,12 @@ namespace UnitTest1 CMyString("test"), CMyString("test"), CMyString("test"), - CMyString("test"), + CMyString("test"), CMyString("test"), CMyString("banan"), CMyString("banan"), CMyString("alma"), + CMyString("ABCDE"), }; const char* text[] = { "", @@ -525,6 +531,7 @@ namespace UnitTest1 "narancs", "narancs", "dio", + "BCDE", }; size_t extraargs[][2] = { { 0, ~0 }, @@ -540,6 +547,7 @@ namespace UnitTest1 { 0, ~0 }, { 2, 2 }, { 3, 1 }, + { 0, 2 }, }; int sizes[][2] = { { 0, 1 }, @@ -555,6 +563,7 @@ namespace UnitTest1 { 12, 26 }, { 7, 16 }, { 4, 5 }, + { 7, 16 }, }; const char* expected[] = { "", @@ -570,6 +579,7 @@ namespace UnitTest1 "banannarancs", "bananra", "alma", + "ABCDEBC", }; int testcount = sizeof(text) / sizeof(char*); for (size_t i = 0; i < testcount; i++) @@ -591,5 +601,466 @@ namespace UnitTest1 } } } + + TEST_METHOD(EqualsOperatorTests) { + CMyString a = CMyString("kutya"); + CMyString b = CMyString("kutya"); + CMyString c = CMyString("cica"); + CMyString d; + CMyString e = CMyString("cicajuice"); + Assert::IsTrue(a == b); + Assert::IsFalse(a == c); + Assert::IsFalse(a == d); + Assert::IsFalse(a == e); + } + + TEST_METHOD(IndexCheckTests) { + CMyString str[]{ + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + }; + size_t indexes[] = { + 0, + 0, + 4, + 3, + }; + char expected[] = { + '\0', //will mark not valid characters with null + 't', + '\0', + 't', + }; + size_t testcount = sizeof(expected) / sizeof(char); + for (size_t i = 0; i < testcount; i++) + { + try + { + Assert::AreEqual(expected[i], str[i][indexes[i]]); + } + catch (const CMyStringException& e) + { + if (expected[i] == '\0') + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(ConstCharCastingTest) { + CMyString str = CMyString("test"); + Assert::AreEqual(str.c_str(), (const char*)str); + } + + TEST_METHOD(MoveOperatorTests) { + CMyString a = CMyString(); + CMyString b = CMyString("test"); + a = move(b); + + Assert::AreEqual("test", (const char*)a); + Assert::IsTrue(b.empty()); + + CMyString c = CMyString(move(a)); + Assert::AreEqual("test", (const char*)c); + Assert::IsTrue(a.empty()); + } + + TEST_METHOD(AddStringOperatorTests) { + CMyString strbase[]{ + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("banan"), + }; + const char* text[] = { + "", + "test", + "a", + "narancs", + }; + size_t extraargs[][2] = { + { 0, ~0 }, + { 0, ~0 }, + { 0, ~0 }, + { 0, ~0 }, + }; + int sizes[][2] = { + { 0, 1 }, + { 8, 18 }, + { 5, 12 }, + { 12, 26 }, + }; + const char* expected[] = { + "", + "testtest", + "testa", + "banannarancs", + }; + int testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + CMyString res; + res = move(strbase[i] + text[i]); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i][0], (int)res.size()); + Assert::AreEqual(sizes[i][1], (int)res.capacity()); + + CMyString stradd = CMyString(text[i]); + res = CMyString(move(strbase[i] + stradd)); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i][0], (int)res.size()); + Assert::AreEqual(sizes[i][1], (int)res.capacity()); + + strbase[i] += stradd; + Assert::AreEqual(expected[i], strbase[i].c_str()); + Assert::AreEqual(sizes[i][0], (int)strbase[i].size()); + Assert::AreEqual(sizes[i][1], (int)strbase[i].capacity()); + } + catch (const CMyStringException& e) + { + Assert::AreEqual(expected[i], e.what()); + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + TEST_METHOD(SubstringTests) { + CMyString str[] = { + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("greek"), + }; + size_t args[][2] = { + { 0, ~0 }, + { 0, ~0 }, + { 2, ~0 }, + { 4, ~0 }, + { 5, ~0 }, + { 0, 1 }, + { 1, 1 }, + { 1, 999 }, + { 1, 0 }, + { 2, 2 }, + }; + char* expected[] = { + "", + "test", + "st", + "", + nullptr, + "t", + "e", + "est", + "", + "ee" + }; + int sizes[] = { + 0, + 4, + 2, + 0, + 0, + 1, + 1, + 3, + 0, + 2, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + CMyString res = str[i].substring(args[i][0], args[i][1]); + Assert::AreEqual(expected[i], res.c_str()); + Assert::AreEqual(sizes[i], (int)res.size()); + Assert::AreEqual(sizes[i] +1, (int)res.capacity()); + } + catch (const CMyStringException& e) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(SwapTest) { + CMyString a = CMyString("contentA"); + CMyString b = CMyString("contentB"); + a.swap(b); + Assert::AreEqual("contentB", (const char*)a); + Assert::AreEqual("contentA", (const char*)b); + } + + TEST_METHOD(FindTests) { + CMyString str[] = { + CMyString(), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("KORTEALMAEPER"), + CMyString("KORTEALMAEPER"), + CMyString("KORTEALMAEPER"), + }; + char* find[] = { + "", + "", + nullptr, + "h", + "e", + "test", + "testt", + "est", + "kORTE", + "ALMa", + "EPEr", + }; + int expected[] = { + -1, + -1, + -1, + -1, + 1, + 0, + -1, + 1, + -1, + -1, + -1, + }; + size_t testcount = sizeof(expected) / sizeof(int); + for (size_t i = 0; i < testcount; i++) + { + try + { + Assert::AreEqual(expected[i], str[i].find(find[i])); + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + TEST_METHOD(InsertTests) { + CMyString str[] = { + CMyString(), + CMyString(), + CMyString("ALMA"), + CMyString("AB"), + CMyString("ALMA"), + CMyString("test"), + CMyString("test"), + }; + const char* inserttext[] = { + "", + "", + "XY", + str[3].c_str(), + "X", + "test", + nullptr, + }; + int insertindex[] = { + 0, + 1, + 2, + 1, + 5, + 4, + 4, + }; + char* expected[] = { + "", + nullptr, + "ALXYMA", + "AABB", + nullptr, + "testtest", + "test", + }; + int sizes[][2] = { + { 0, 1 }, + { 0, 1 }, + { 6, 14 }, + { 4, 10 }, + { 4, 5 }, + { 8, 18 }, + { 4, 5 }, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + str[i].insert(insertindex[i], inserttext[i]); + 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) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + Assert::AreEqual(sizes[i][0], (int)str[i].size()); + Assert::AreEqual(sizes[i][1], (int)str[i].capacity()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(ReplaceTests) { + CMyString str[] = { + CMyString(), + CMyString(), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + CMyString("ABCDEFG"), + }; + const char* inserttext[] = { + "", + "", + "X", + "XYZ123", + nullptr, + "" + }; + int insertindex[] = { + 0, + 1, + 2, + 2, + 2, + 0, + }; + int count[] = { + 0, + 0, + 3, + 3, + 3, + 0, + }; + char* expected[] = { + "", + nullptr, + "ABXFG", + "ABXYZ123FG", + "ABFG", + "ABCDEFG", + }; + int sizes[][2] = { + { 0, 1 }, + { 0, 1 }, + { 5, 8 }, + { 10, 22 }, + { 4, 8 }, + { 7, 8 }, + }; + size_t testcount = sizeof(expected) / sizeof(char*); + for (size_t i = 0; i < testcount; i++) + { + try + { + str[i].replace(insertindex[i], count[i], inserttext[i]); + 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) + { + if (expected[i] == nullptr) + { + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + Assert::AreEqual(sizes[i][0], (int)str[i].size()); + Assert::AreEqual(sizes[i][1], (int)str[i].capacity()); + } + else + { + Assert::IsTrue(false); + } + } + catch (...) + { + Assert::IsTrue(false); + } + } + } + + TEST_METHOD(SpecificTests) { + CMyString s = CMyString("ABCDE"); + const char* const belsoForras = s.c_str() + 1; + s.append(belsoForras, 0, 2); + Assert::AreEqual("ABCDEBC", s.c_str()); + Assert::AreEqual((size_t)7, s.size()); + Assert::AreEqual((size_t)16, s.capacity()); + + CMyString g = CMyString("ALMA"); + g.reserve(12); + Assert::AreEqual((size_t)4, g.size()); + Assert::AreEqual((size_t)12, g.capacity()); + const char* p = g.c_str(); + g.insert(2, "XY"); + Assert::AreEqual("ALXYMA", g.c_str()); + Assert::AreEqual((size_t)6, g.size()); + Assert::AreEqual((size_t)12, g.capacity()); + Assert::IsTrue(p == g.c_str()); + + CMyString h = CMyString("ABCDEFG"); + const char* oldaddr = h.c_str(); + h.replace(2, 3, "X"); + Assert::AreEqual("ABXFG", h.c_str()); + Assert::AreEqual((size_t)5, h.size()); + Assert::AreEqual((size_t)8, h.capacity()); + Assert::IsTrue(oldaddr == h.c_str()); + Assert::AreEqual('\0', h.c_str()[h.size()]); + } }; } diff --git a/feladat5/feladat5/main.cpp b/feladat5/feladat5/main.cpp index f93f701..3399460 100644 --- a/feladat5/feladat5/main.cpp +++ b/feladat5/feladat5/main.cpp @@ -1,4 +1,4 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #include "CMyString.h" int main() {