feladat6 v1 done

This commit is contained in:
Damimani 2026-11-15 17:33:06 +01:00
parent 49d964d8f1
commit 08a18e69d4
4 changed files with 471 additions and 65 deletions

View file

@ -1,4 +1,5 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#define _MYDEBUG
#include "pch.h" #include "pch.h"
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include <CMyString.h> #include <CMyString.h>
@ -509,7 +510,7 @@ namespace UnitTest1
CMyString("test"), CMyString("test"),
CMyString("test"), CMyString("test"),
CMyString("test"), CMyString("test"),
CMyString("test"), CMyString("test"),
CMyString("test"), CMyString("test"),
CMyString("banan"), CMyString("banan"),
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);
}
}
}
}; };
} }

View file

@ -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) { static void MakeEmptyStr(size_t* datalngth, size_t* alloclngth, char** ptr) {
*datalngth = 0; *datalngth = 0;
*alloclngth = 1; *alloclngth = 1;
char* cpy = nullptr;
if (ptr != nullptr)
{
cpy = *ptr;
}
*ptr = new char[1]; *ptr = new char[1];
if (cpy != nullptr)
{
delete[] cpy;
}
(*ptr)[0] = '\0'; (*ptr)[0] = '\0';
} }
@ -53,41 +44,28 @@ static void reserveNoCheck(size_t* alloc, size_t* size, char** data, size_t news
delete[] tmp; delete[] tmp;
} }
static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc, size_t* size, char** data) { static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc, size_t* size, char** data) {
size_t dlngth = *size; size_t sourceLength = strLength(src);
size_t alngth = *alloc; size_t oldSize = *size;
char* old = new char[*alloc]; size_t newSize = oldSize - delcnt + sourceLength;
size_t srclen = 0; size_t newCapacity = (*alloc > newSize) ? *alloc : (newSize + 1) * 2;
if (src != "") char* cpy = nullptr;
{
srclen = strLength(src);
}
CopyStr(old, *data, *size);
try try
{ {
char* cpy = new char[int(*size + srclen + 1)]; cpy = new char[newCapacity];
CopyStr(cpy, *data, pos); CopyStr(cpy, *data, pos);
if (srclen != 0) CopyStr(cpy + pos, src, sourceLength);
{ CopyStr(cpy + pos + sourceLength, *data + pos + delcnt, oldSize - pos - delcnt);
CopyStr(cpy + pos, src, srclen);
} delete[] *data;
CopyStr(cpy + pos + srclen, *data + pos + delcnt, *size - pos);
*size = strLength(cpy);
if (*alloc <= *size)
{
reserveNoCheck(alloc, size, data, ((*size + 1) * 2));
}
*data = cpy; *data = cpy;
delete[] old; *size = newSize;
*alloc = newCapacity;
} }
catch (const bad_alloc e) catch (...)
{ {
*size = dlngth; delete[] cpy;
*alloc = alngth; throw;
*data = old;
throw e;
}
catch (...) {
throw CMyStringException(CMyStringException::ErrUnexpected);
} }
} }
@ -313,7 +291,10 @@ CMyString& CMyString::operator=(const CMyString& str) {
} }
else else
{ {
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData); if (!empty())
{
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
}
} }
} }
return *this; return *this;
@ -323,15 +304,25 @@ CMyString::CMyString(CMyString&& str){
m_nDataLength = str.m_nDataLength; m_nDataLength = str.m_nDataLength;
m_nAllocLength = str.m_nAllocLength; m_nAllocLength = str.m_nAllocLength;
m_pchData = str.m_pchData; 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){ CMyString& CMyString::operator=(CMyString&& str){
m_nDataLength = str.m_nDataLength; if (this != &str)
m_nAllocLength = str.m_nAllocLength; {
char* old = m_pchData; char* empty = new char[1];
m_pchData = str.m_pchData; empty[0] = '\0';
delete[] old; delete[] m_pchData;
str = CMyString(); 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; return *this;
} }
char& CMyString::operator[](size_t nIndex){ char& CMyString::operator[](size_t nIndex){
@ -365,7 +356,8 @@ CMyString& CMyString::operator+=(const char* p){
return *this; return *this;
} }
CMyString& CMyString::operator+(const CMyString& str1){ CMyString& CMyString::operator+(const CMyString& str1){
static CMyString res = *this; static CMyString res;
res = *this;
res.append(str1.c_str()); res.append(str1.c_str());
return res; return res;
} }

View file

@ -1,6 +1,5 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyStringException.h" #include "CMyStringException.h"
#include "CMyString.h"
static int strLength(const char* str) { static int strLength(const char* str) {
if (str == nullptr) if (str == nullptr)
@ -15,21 +14,16 @@ static int strLength(const char* str) {
return cnt; 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) { CMyStringException::CMyStringException(const char* input) {
msg = CMyString(input); size_t length = strLength(input);
msg = shared_ptr<char[]>(new char[length + 1]);
for (size_t i = 0; i < length; ++i)
{
msg[i] = input[i];
}
msg[length] = '\0';
} }
const char* CMyStringException::what() const { const char* CMyStringException::what() const {
return msg.c_str(); return msg.get();
} }

View file

@ -1,7 +1,6 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#pragma once #pragma once
#include <memory> #include <memory>
#include "CMyString.h"
using namespace std; using namespace std;
class CMyStringException { class CMyStringException {
public: public:
@ -12,5 +11,5 @@ public:
static constexpr const char* ErrCount = "Hiba: karakterek száma 0!"; static constexpr const char* ErrCount = "Hiba: karakterek száma 0!";
static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!"; static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!";
private: private:
CMyString msg; shared_ptr<char[]> msg;
}; };