2026-27-cpp-homework/feladat5/UnitTest1/UnitTest1.cpp
2026-11-15 17:33:06 +01:00

1020 lines
19 KiB
C++

// Tóth Dániel Mátyás | IFPQYY | v1
#define _MYDEBUG
#include "pch.h"
#include "CppUnitTest.h"
#include <CMyString.h>
#include <CMyStringException.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(EmptyTests)
{
CMyString empty = CMyString();
Assert::AreEqual(0, (int)empty.size());
Assert::AreEqual(1, (int)empty.capacity());
Assert::IsTrue(empty.empty());
}
TEST_METHOD(c_strTests)
{
try
{
char* tmp = "txt";
CMyString str = CMyString((const char*)tmp);
Assert::IsFalse(tmp == str.c_str()); //memory addr
Assert::AreEqual(tmp, str.c_str()); //value
}
catch (...)
{
Assert::IsTrue(false);
}
}
TEST_METHOD(StringConstructionTests)
{
//char* constructor
char* pszargs[] = {
{nullptr},
{""},
{"test"},
{"long text aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa"},
};
int pszres[] = {
0,
0,
4,
48,
};
int testcount = sizeof(pszres) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyString pszctor = CMyString(pszargs[i]);
Assert::AreEqual(pszres[i], (int)pszctor.size());
Assert::AreEqual((pszres[i]) + 1, (int)pszctor.capacity());
if (pszargs[i] != nullptr)
{
Assert::AreEqual(pszargs[i], pszctor.c_str());
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
//repeat constructor
char repeatchargs[] = {
'\0',
'\0',
'a',
'a',
};
int repeatcntargs[] = {
0,
5,
0,
5,
};
int repeatres[] = {
0,
0,
0,
5,
};
char* repeattextres[] = {
nullptr,
nullptr,
nullptr,
"aaaaa",
};
testcount = sizeof(repeatres) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyString repeatctor = CMyString(repeatchargs[i], repeatcntargs[i]);
Assert::AreEqual(repeatres[i], (int)repeatctor.size());
Assert::AreEqual((repeatres[i]) + 1, (int)repeatctor.capacity());
if (repeattextres[i] != nullptr)
{
Assert::AreEqual(repeattextres[i], repeatctor.c_str());
}
}
catch (const CMyStringException& e) {
if (repeatchargs[i] == '\0')
{
Assert::AreEqual(CMyStringException::ErrInvalidChar, e.what());
}
else
{
Assert::AreEqual(CMyStringException::ErrCount, e.what());
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
//copy constructor
CMyString strargs[] = {
CMyString(),
CMyString("test"),
CMyString('a', 10),
CMyString("really long text thats to be cleared"),
};
strargs[3].clear();
int strres[] = {
0,
4,
10,
0,
};
int allocLengths[] = {
1,
5,
11,
1,
};
char* cpytextres[] = {
"",
"test",
"aaaaaaaaaa",
"",
};
testcount = sizeof(strres) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyString cpy = CMyString(strargs[i]);
CMyString strctor = CMyString(cpy);
Assert::AreEqual(strres[i], (int)strctor.size());
Assert::AreEqual((allocLengths[i]), (int)strctor.capacity());
Assert::AreEqual(cpytextres[i], strctor.c_str());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(ClearTests) {
CMyString str[]{
CMyString(),
CMyString("test"),
CMyString('a', 10),
CMyString(CMyString("to be copied then cleared")),
};
int allocLengths[] = {
1,
5,
11,
26,
};
int testcount = sizeof(allocLengths) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].clear();
Assert::IsTrue(str[i].empty());
Assert::AreEqual(allocLengths[i], (int)str[i].capacity());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(EqualOperatorTests) {
CMyString preps[]{
CMyString(),
CMyString("testtesttest"),
CMyString("testtesttest"),
CMyString("test"),
CMyString("dio"),
};
CMyString strs[]{
CMyString("test"),
CMyString("test"),
CMyString("testtesttesttest"),
CMyString("testtesttesttest"),
CMyString("narancs"),
};
int expected[][2] = {
{ 4, 5 },
{ 4, 13 },
{ 16, 17 },
{ 16, 17 },
{ 7, 8 },
};
int testcount = sizeof(expected) / (sizeof(int) * 2);
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyString res = preps[i];
res = strs[i];
Assert::AreEqual(expected[i][0], (int)res.size());
Assert::AreEqual(expected[i][1], (int)res.capacity());
Assert::AreEqual(strs[i].c_str(), res.c_str());
}
catch (...)
{
Assert::IsTrue(false);
}
}
CMyString str1 = CMyString("narancsnarancsnarancs");
CMyString str2 = CMyString("eper");
const char* cptr = str1.c_str();
str1 = str2;
str1.shrink_to_fit();
Assert::AreEqual((size_t)4, str1.size());
Assert::AreEqual((size_t)5, str1.capacity());
Assert::AreNotEqual(cptr, str1.c_str());
}
TEST_METHOD(GetAtTests) {
CMyString str[]{
CMyString(),
CMyString(),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString('a', 10),
CMyString(CMyString("to be copied then cleared")),
};
int pos[] = {
1,
0,
4,
-1,
1,
9,
12,
};
char character[] = {
'\0',
'\0',
'\0',
'\0', //first four are invalid
'e',
'a',
' ',
};
int testcount = sizeof(pos) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
Assert::AreEqual(character[i], str[i].getat(pos[i]));
}
catch(const CMyStringException& e)
{
if (i < 4)
{
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
}
else
{
Assert::IsTrue(false);
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(SetAtTests) {
CMyString str[]{
CMyString(),
CMyString("test"),
CMyString("test"),
CMyString('a', 10),
};
int pos[] = {
0,
4,
-1,
3,
};
char* text[] = {
'\0',
'\0',
'\0', //first three are invalid
"aaataaaaaa",
};
int testcount = sizeof(pos) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].setat(pos[i], 't');
Assert::AreEqual(text[i], str[i].c_str());
}
catch (const CMyStringException& e)
{
if (i < 3)
{
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
}
else
{
Assert::IsTrue(false);
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
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
{
const char* ptr = str.c_str();
str.reserve(allocs[i]);
Assert::AreEqual(expected[i], (int)str.capacity());
if (i < 2)
{
Assert::AreEqual(ptr, str.c_str());
}
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(EraseTests) {
CMyString str[]{
CMyString(),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString('a', 10),
CMyString("alma"),
CMyString("test"),
CMyString("alma"),
};
int pos[] = {
0,
4,
-1,
3,
3,
3,
4,
0,
1,
};
int cnt[] = {
1,
1,
1,
0,
1,
1,
100,
0,
-1,
};
char* text[] = {
"",
"test",
NULL,
"test",
"tes",
"aaaaaaaaa",
"alma",
"test",
"a"
};
int testcount = sizeof(pos) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].erase(pos[i], cnt[i]);
Assert::AreEqual(text[i], str[i].c_str());
}
catch (const CMyStringException& e)
{
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(ShrinkToFitTests) {
CMyString str[]{
CMyString("test"),
CMyString("test"),
CMyString(),
};
int eraseargs[][2] = {
{ 0, 0 },
{ 0, 4 },
{ 0, 0 },
};
int res[][2] = {
{ 4, 5 },
{ 0, 1 },
{ 0, 1 },
};
int testcount = sizeof(res) / sizeof(int) / 2;
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].erase(eraseargs[i][0], eraseargs[i][1]);
str[i].shrink_to_fit();
Assert::AreEqual(res[i][0], (int)str[i].size());
Assert::AreEqual(res[i][1], (int)str[i].capacity());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(ReverseTests) {
CMyString str[]{
CMyString(),
CMyString("bal"),
CMyString("test"),
CMyString("disestablishmentarianism"),
};
char* text[] = {
"",
"lab",
"tset",
"msinairatnemhsilbatsesid"
};
int testcount = sizeof(text) / sizeof(char*);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].reverse();
Assert::AreEqual(text[i], str[i].c_str());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
TEST_METHOD(AppendTests) {
CMyString str[]{
CMyString(),
CMyString(),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("banan"),
CMyString("banan"),
CMyString("alma"),
};
const char* text[] = {
"",
nullptr,
str[2].c_str(),
"test",
"test",
"test",
"test",
"",
"test",
"a",
"narancs",
"narancs",
"dio",
};
size_t extraargs[][2] = {
{ 0, ~0 },
{ 0, ~0 },
{ 0, ~0 },
{ 0, ~0 },
{ 5, ~0 },
{ 0, 5 },
{ 2, 3 },
{ 1, 0 },
{ 4, ~0 },
{ 0, ~0 },
{ 0, ~0 },
{ 2, 2 },
{ 3, 1 },
};
int sizes[][2] = {
{ 0, 1 },
{ 0, 1 },
{ 4, 5 },
{ 8, 18 },
{ 4, 5 },
{ 4, 5 },
{ 4, 5 },
{ 4, 5 },
{ 4, 5 },
{ 5, 12 },
{ 12, 26 },
{ 7, 16 },
{ 4, 5 },
};
const char* expected[] = {
"",
"",
"test",
"testtest",
CMyStringException::ErrOutOfRange,
CMyStringException::ErrOutOfRange,
CMyStringException::ErrOutOfRange,
CMyStringException::ErrCount,
"test",
"testa",
"banannarancs",
"bananra",
"alma",
};
int testcount = sizeof(text) / sizeof(char*);
for (size_t i = 0; i < testcount; i++)
{
try
{
str[i].append(text[i], extraargs[i][0], extraargs[i][1]);
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)
{
Assert::AreEqual(expected[i], e.what());
}
catch (...)
{
Assert::IsTrue(false);
}
}
}
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);
}
}
}
};
}