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] 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