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..d611938 100644 --- a/feladat5/UnitTest1/UnitTest1.cpp +++ b/feladat5/UnitTest1/UnitTest1.cpp @@ -1,4 +1,5 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v2 +#define _MYDEBUG #include "pch.h" #include "CppUnitTest.h" #include @@ -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/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 92aa8e0..5938b66 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 | v2 #include "CMyString.h" #include "CMyStringException.h" #include @@ -41,11 +41,44 @@ 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 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 + { + cpy = new char[newCapacity]; + CopyStr(cpy, *data, pos); + CopyStr(cpy + pos, src, sourceLength); + CopyStr(cpy + pos + sourceLength, *data + pos + delcnt, oldSize - pos - delcnt); + + if (newSize+1 > *alloc) + { + reserveNoCheck(alloc, size, data, newCapacity); + } + CopyStr(*data, cpy, newSize); + delete[] cpy; + *size = newSize; + *alloc = newCapacity; + } + catch (...) + { + delete[] cpy; + throw; + } } 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 +96,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 +109,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 +140,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 +197,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; @@ -209,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; @@ -230,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'; @@ -249,8 +297,205 @@ 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; -} \ 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.m_nDataLength = 0; + str.m_nAllocLength = 1; + char* empty = new char[1]; + empty[0] = '\0'; + str.m_pchData = empty; +} +CMyString& CMyString::operator=(CMyString&& str){ + 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){ + if (nIndex >= m_nDataLength) + { + throw CMyStringException(CMyStringException::ErrOutOfRange); + } + return m_pchData[nIndex]; +} +bool CMyString::operator==(const CMyString& s1){ + if (s1.capacity() != capacity()) + { + return false; + } + if (s1.size() != size()) + { + 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){ + 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; + 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 - j <= 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..940fb10 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 | v2 #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..546ea29 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,6 +1,5 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v2 #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 fcb0761..5d26f1c 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,7 +1,6 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v2 #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 diff --git a/feladat5/feladat5/IFPQYY_feladat6v2.zip b/feladat5/feladat5/IFPQYY_feladat6v2.zip new file mode 100644 index 0000000..45a3047 Binary files /dev/null and b/feladat5/feladat5/IFPQYY_feladat6v2.zip differ diff --git a/feladat5/feladat5/UnitTest1.cpp b/feladat5/feladat5/UnitTest1.cpp index 4ce6dcc..d611938 100644 --- a/feladat5/feladat5/UnitTest1.cpp +++ b/feladat5/feladat5/UnitTest1.cpp @@ -1,4 +1,5 @@ -// Tóth Dániel Mátyás | IFPQYY | v3 +// Tóth Dániel Mátyás | IFPQYY | v2 +#define _MYDEBUG #include "pch.h" #include "CppUnitTest.h" #include @@ -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 f68ceaa..3399460 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 | v2 #include "CMyString.h" int main() {