diff --git a/feladat5/UnitTest1/UnitTest1.cpp b/feladat5/UnitTest1/UnitTest1.cpp index dc260dc..11d5b30 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 #include "pch.h" #include "CppUnitTest.h" #include @@ -107,6 +107,9 @@ namespace UnitTest1 Assert::AreEqual(repeattextres[i], repeatctor.c_str()); } } + catch (const CMyStringException& e) { + Assert::AreEqual(CMyStringException::ErrInvalidChar, e.what()); + } catch (...) { Assert::IsTrue(false); @@ -191,18 +194,21 @@ namespace UnitTest1 CMyString("testtesttest"), CMyString("testtesttest"), CMyString("test"), + CMyString("dio"), }; CMyString strs[]{ CMyString("test"), CMyString("test"), CMyString("testtesttesttest"), CMyString("testtesttesttest"), + CMyString("narancs"), }; int expected[][2] = { - { 4, 10 }, + { 4, 5 }, { 4, 13 }, - { 16, 34 }, - { 16, 34 }, + { 16, 17 }, + { 16, 17 }, + { 7, 8 }, }; int testcount = sizeof(expected) / (sizeof(int) * 2); for (size_t i = 0; i < testcount; i++) @@ -322,25 +328,36 @@ namespace UnitTest1 } TEST_METHOD(ReserveTests) { + CMyString str = CMyString("test"); int allocs[] = { 1, 5, 10, 33, }; + int expected[] = { + 5, + 5, + 10, + 33, + }; int testcount = sizeof(allocs) / sizeof(int); for (size_t i = 0; i < testcount; i++) { try { - CMyString str = CMyString("test"); + const char* ptr = str.c_str(); str.reserve(allocs[i]); - Assert::AreEqual(allocs[i], (int)str.capacity()); + Assert::AreEqual(expected[i], (int)str.capacity()); + if (i < 2) + { + Assert::AreEqual(ptr, str.c_str()); + } } - catch (const CMyStringException& e) + catch (...) { - Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); + Assert::IsTrue(false); } } } @@ -353,6 +370,8 @@ namespace UnitTest1 CMyString("test"), CMyString("test"), CMyString('a', 10), + CMyString("alma"), + CMyString("test"), }; int pos[] = { 0, @@ -361,6 +380,8 @@ namespace UnitTest1 3, 3, 3, + 4, + 0, }; int cnt[] = { 1, @@ -369,14 +390,18 @@ namespace UnitTest1 0, 1, 1, + 100, + 0, }; char* text[] = { "", - NULL, + "test", NULL, "test", "tes", "aaaaaaaaa", + "alma", + "test", }; int testcount = sizeof(pos) / sizeof(int); for (size_t i = 0; i < testcount; i++) @@ -388,14 +413,7 @@ namespace UnitTest1 } catch (const CMyStringException& e) { - if (i < 3) - { - Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); - } - else - { - Assert::IsTrue(false); - } + Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what()); } catch (...) { @@ -472,6 +490,11 @@ namespace UnitTest1 CMyString("test"), CMyString("test"), CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("test"), + CMyString("banan"), + CMyString("banan"), }; const char* text[] = { "", @@ -481,24 +504,39 @@ namespace UnitTest1 "test", "test", "test", + "", + "test", + "a", + "narancs", + "narancs", }; size_t extraargs[][2] = { { 0, ~0 }, { 0, ~0 }, { 0, ~0 }, { 0, ~0 }, - { 4, ~0 }, + { 5, ~0 }, { 0, 5 }, { 2, 3 }, + { 1, 0 }, + { 4, ~0 }, + { 0, ~0 }, + { 0, ~0 }, + { 2, 2 }, }; int sizes[][2] = { { 0, 1 }, { 0, 1 }, { 4, 5 }, - { 8, 16 }, + { 8, 18 }, { 4, 5 }, { 4, 5 }, { 4, 5 }, + { 4, 5 }, + { 4, 5 }, + { 5, 12 }, + { 12, 26 }, + { 7, 16 }, }; const char* expected[] = { "", @@ -508,6 +546,11 @@ namespace UnitTest1 CMyStringException::ErrOutOfRange, CMyStringException::ErrOutOfRange, CMyStringException::ErrOutOfRange, + CMyStringException::ErrCount, + "test", + "testa", + "banannarancs", + "bananra", }; int testcount = sizeof(text) / sizeof(char*); for (size_t i = 0; i < testcount; i++) diff --git a/feladat5/feladat5/CMyString.cpp b/feladat5/feladat5/CMyString.cpp index 2e36838..26ec905 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,7 +57,11 @@ CMyString::CMyString(const char* psz) { } } CMyString::CMyString(char ch, size_t repeat) { - if (ch == '\0' || repeat == 0) + if (ch == '\0') + { + throw CMyStringException(CMyStringException::ErrInvalidChar); + } + if (repeat == 0) { MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); } @@ -128,9 +132,9 @@ void CMyString::setat(size_t index, char ch) { m_pchData[index] = ch; } void CMyString::reserve(size_t newcapacity) { - if (newcapacity < m_nAllocLength) + if (newcapacity <= m_nAllocLength) { - throw CMyStringException(CMyStringException::ErrOutOfRange); + return; } char* tmp = m_pchData; m_nAllocLength = newcapacity; @@ -143,7 +147,7 @@ void CMyString::erase(size_t pos, size_t count) { { return; } - if (pos >= m_nDataLength) + if (pos > m_nDataLength) { throw CMyStringException(CMyStringException::ErrOutOfRange); } @@ -193,27 +197,31 @@ void CMyString::reverse() { } } void CMyString::append(const char* psz, unsigned offset, unsigned count) { - if (psz == nullptr || psz == m_pchData || psz == "") + if (psz == nullptr || psz == m_pchData) { return; } + if (count == 0) + { + throw CMyStringException(CMyStringException::ErrCount); + } size_t len = strLength(psz); if (count == ~0) { - count = (int)len - offset; + count = len - offset; } - int range = (count + offset); - if (len < range || offset == len) + int range = count + offset; + if (len < count || len < offset || len < range) { throw CMyStringException(CMyStringException::ErrOutOfRange); } - if (count + m_nDataLength > m_nAllocLength) + if (count + m_nDataLength >= m_nAllocLength) { - this->reserve((m_nDataLength + count)*2); + this->reserve((m_nDataLength + count + 1)*2); } size_t i = m_nDataLength; size_t j = offset; - while (j < count) + while (j < range) { m_pchData[i++] = psz[j++]; } @@ -227,7 +235,7 @@ CMyString& CMyString::operator=(const CMyString& str) { { if (str.size() >= m_nAllocLength) { - this->reserve((str.size() + 1) * 2); + this->reserve((str.size() + 1)); } m_nDataLength = str.size(); CopyStr(m_pchData, str.c_str(), m_nDataLength); diff --git a/feladat5/feladat5/CMyString.h b/feladat5/feladat5/CMyString.h index 4801e2f..4a793ea 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 class CMyString { public: diff --git a/feladat5/feladat5/CMyStringException.cpp b/feladat5/feladat5/CMyStringException.cpp index 8c3bfdb..78630ff 100644 --- a/feladat5/feladat5/CMyStringException.cpp +++ b/feladat5/feladat5/CMyStringException.cpp @@ -1,5 +1,6 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #include "CMyStringException.h" +#include "CMyString.h" static int strLength(const char* str) { if (str == nullptr) @@ -27,10 +28,8 @@ static void CopyStr(char* dest, const char* src, size_t length) { } CMyStringException::CMyStringException(const char* input) { - int len = strLength(input); - msg = unique_ptr(new char[len+1]); - CopyStr(msg.get(), input, len); + msg = CMyString(input); } const char* CMyStringException::what() const { - return msg.get(); + return msg.c_str(); } \ No newline at end of file diff --git a/feladat5/feladat5/CMyStringException.h b/feladat5/feladat5/CMyStringException.h index 362bba2..a3bd759 100644 --- a/feladat5/feladat5/CMyStringException.h +++ b/feladat5/feladat5/CMyStringException.h @@ -1,6 +1,7 @@ -// Tóth Dániel Mátyás | IFPQYY | v1 +// Tóth Dániel Mátyás | IFPQYY | v2 #pragma once #include +#include "CMyString.h" using namespace std; class CMyStringException { public: @@ -11,5 +12,5 @@ public: static constexpr const char* ErrCount = "Hiba: karakterek száma 0!"; static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!"; private: - unique_ptr msg; + CMyString msg; }; \ No newline at end of file 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() {