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] 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!zBWpHO$5gP0LmBv0Gx-f zl1iQ$uFf_NZ&=NpoO;OSqbJ#qNNMjo0#J8qDd2Ax(cWTxxbYA}%lXvEolmP3=iJqH zgyVxO+8fz-ekt3SZ;02F7P*~y?9n^#vfaHwwVKvYf4*T5JD6X8vSDzt)>o(RLtN*u zMaNoK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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() {