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
#define _MYDEBUG
#include "pch.h"
#include "CppUnitTest.h"
#include <CMyString.h>
@ -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);
}
}
}
};
}