feladat6 done

This commit is contained in:
Damimani 2026-11-20 18:54:08 +01:00
parent 08a18e69d4
commit dfa03b9177
8 changed files with 549 additions and 20 deletions

View file

@ -1,4 +1,4 @@
// Tóth Dániel Mátyás | IFPQYY | v1
// Tóth Dániel Mátyás | IFPQYY | v2
#define _MYDEBUG
#include "pch.h"
#include "CppUnitTest.h"
@ -515,6 +515,7 @@ namespace UnitTest1
CMyString("banan"),
CMyString("banan"),
CMyString("alma"),
CMyString("ABCDE"),
};
const char* text[] = {
"",
@ -530,6 +531,7 @@ namespace UnitTest1
"narancs",
"narancs",
"dio",
"BCDE",
};
size_t extraargs[][2] = {
{ 0, ~0 },
@ -545,6 +547,7 @@ namespace UnitTest1
{ 0, ~0 },
{ 2, 2 },
{ 3, 1 },
{ 0, 2 },
};
int sizes[][2] = {
{ 0, 1 },
@ -560,6 +563,7 @@ namespace UnitTest1
{ 12, 26 },
{ 7, 16 },
{ 4, 5 },
{ 7, 16 },
};
const char* expected[] = {
"",
@ -575,6 +579,7 @@ namespace UnitTest1
"banannarancs",
"bananra",
"alma",
"ABCDEBC",
};
int testcount = sizeof(text) / sizeof(char*);
for (size_t i = 0; i < testcount; i++)
@ -599,11 +604,14 @@ namespace UnitTest1
TEST_METHOD(EqualsOperatorTests) {
CMyString a = CMyString("kutya");
CMyString* b = &a;
Assert::IsTrue(a == *b);
CMyString c = CMyString("kutya");
CMyString b = CMyString("kutya");
CMyString c = CMyString("cica");
CMyString d;
CMyString e = CMyString("cicajuice");
Assert::IsTrue(a == b);
Assert::IsFalse(a == c);
Assert::IsFalse(a == d);
Assert::IsFalse(a == e);
}
TEST_METHOD(IndexCheckTests) {
@ -826,6 +834,9 @@ namespace UnitTest1
CMyString("test"),
CMyString("test"),
CMyString("test"),
CMyString("KORTEALMAEPER"),
CMyString("KORTEALMAEPER"),
CMyString("KORTEALMAEPER"),
};
char* find[] = {
"",
@ -836,6 +847,9 @@ namespace UnitTest1
"test",
"testt",
"est",
"kORTE",
"ALMa",
"EPEr",
};
int expected[] = {
-1,
@ -846,6 +860,9 @@ namespace UnitTest1
0,
-1,
1,
-1,
-1,
-1,
};
size_t testcount = sizeof(expected) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
@ -1016,5 +1033,34 @@ namespace UnitTest1
}
}
}
TEST_METHOD(SpecificTests) {
CMyString s = CMyString("ABCDE");
const char* const belsoForras = s.c_str() + 1;
s.append(belsoForras, 0, 2);
Assert::AreEqual("ABCDEBC", s.c_str());
Assert::AreEqual((size_t)7, s.size());
Assert::AreEqual((size_t)16, s.capacity());
CMyString g = CMyString("ALMA");
g.reserve(12);
Assert::AreEqual((size_t)4, g.size());
Assert::AreEqual((size_t)12, g.capacity());
const char* p = g.c_str();
g.insert(2, "XY");
Assert::AreEqual("ALXYMA", g.c_str());
Assert::AreEqual((size_t)6, g.size());
Assert::AreEqual((size_t)12, g.capacity());
Assert::IsTrue(p == g.c_str());
CMyString h = CMyString("ABCDEFG");
const char* oldaddr = h.c_str();
h.replace(2, 3, "X");
Assert::AreEqual("ABXFG", h.c_str());
Assert::AreEqual((size_t)5, h.size());
Assert::AreEqual((size_t)8, h.capacity());
Assert::IsTrue(oldaddr == h.c_str());
Assert::AreEqual('\0', h.c_str()[h.size()]);
}
};
}

View file

@ -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 <iostream>
@ -57,8 +57,12 @@ static void insertStrAtIndex(const char* src, int pos, int delcnt, size_t* alloc
CopyStr(cpy + pos, src, sourceLength);
CopyStr(cpy + pos + sourceLength, *data + pos + delcnt, oldSize - pos - delcnt);
delete[] *data;
*data = cpy;
if (newSize+1 > *alloc)
{
reserveNoCheck(alloc, size, data, newCapacity);
}
CopyStr(*data, cpy, newSize);
delete[] cpy;
*size = newSize;
*alloc = newCapacity;
}
@ -251,6 +255,8 @@ void CMyString::append(const char* psz, unsigned offset, unsigned count) {
throw CMyStringException(CMyStringException::ErrCount);
}
size_t len = strLength(psz);
char* cpy = new char[len];
CopyStr(cpy, psz, len);
if (offset == len)
{
return;
@ -272,7 +278,7 @@ void CMyString::append(const char* psz, unsigned offset, unsigned count) {
size_t j = offset;
while (j < range)
{
m_pchData[i++] = psz[j++];
m_pchData[i++] = cpy[j++];
}
m_nDataLength = i;
m_pchData[i] = '\0';
@ -333,10 +339,6 @@ char& CMyString::operator[](size_t nIndex){
return m_pchData[nIndex];
}
bool CMyString::operator==(const CMyString& s1){
if (s1.c_str() != c_str())
{
return false;
}
if (s1.capacity() != capacity())
{
return false;
@ -345,6 +347,16 @@ bool CMyString::operator==(const CMyString& s1){
{
return false;
}
int i = 0;
const char* s1chararr = s1.c_str();
while (i < size())
{
if (m_pchData[i] != s1chararr[i])
{
return false;
}
i++;
}
return true;
}
CMyString& CMyString::operator+=(const CMyString& str){
@ -413,7 +425,7 @@ int CMyString::find(const char* psz) const{
size_t i = 0;
size_t j = 0;
size_t ret = -1;
while (pszlen + i <= m_nDataLength && j < pszlen)
while (pszlen + i - j <= m_nDataLength && j < pszlen)
{
if (getat(i) == psz[j])
{

View file

@ -1,4 +1,4 @@
// Tóth Dániel Mátyás | IFPQYY | v1
// Tóth Dániel Mátyás | IFPQYY | v2
#pragma once
#include <ostream>
class CMyString {

View file

@ -1,4 +1,4 @@
// Tóth Dániel Mátyás | IFPQYY | v1
// Tóth Dániel Mátyás | IFPQYY | v2
#include "CMyStringException.h"
static int strLength(const char* str) {

View file

@ -1,4 +1,4 @@
// Tóth Dániel Mátyás | IFPQYY | v1
// Tóth Dániel Mátyás | IFPQYY | v2
#pragma once
#include <memory>
using namespace std;

Binary file not shown.

View file

@ -1,4 +1,5 @@
// Tóth Dániel Mátyás | IFPQYY | v3
// Tóth Dániel Mátyás | IFPQYY | v2
#define _MYDEBUG
#include "pch.h"
#include "CppUnitTest.h"
#include <CMyString.h>
@ -385,6 +386,7 @@ namespace UnitTest1
CMyString('a', 10),
CMyString("alma"),
CMyString("test"),
CMyString("alma"),
};
int pos[] = {
0,
@ -395,6 +397,7 @@ namespace UnitTest1
3,
4,
0,
1,
};
int cnt[] = {
1,
@ -405,6 +408,7 @@ namespace UnitTest1
1,
100,
0,
-1,
};
char* text[] = {
"",
@ -415,6 +419,7 @@ namespace UnitTest1
"aaaaaaaaa",
"alma",
"test",
"a"
};
int testcount = sizeof(pos) / sizeof(int);
for (size_t i = 0; i < testcount; i++)
@ -510,6 +515,7 @@ namespace UnitTest1
CMyString("banan"),
CMyString("banan"),
CMyString("alma"),
CMyString("ABCDE"),
};
const char* text[] = {
"",
@ -525,6 +531,7 @@ namespace UnitTest1
"narancs",
"narancs",
"dio",
"BCDE",
};
size_t extraargs[][2] = {
{ 0, ~0 },
@ -540,6 +547,7 @@ namespace UnitTest1
{ 0, ~0 },
{ 2, 2 },
{ 3, 1 },
{ 0, 2 },
};
int sizes[][2] = {
{ 0, 1 },
@ -555,6 +563,7 @@ namespace UnitTest1
{ 12, 26 },
{ 7, 16 },
{ 4, 5 },
{ 7, 16 },
};
const char* expected[] = {
"",
@ -570,6 +579,7 @@ namespace UnitTest1
"banannarancs",
"bananra",
"alma",
"ABCDEBC",
};
int testcount = sizeof(text) / sizeof(char*);
for (size_t i = 0; i < testcount; i++)
@ -591,5 +601,466 @@ namespace UnitTest1
}
}
}
TEST_METHOD(EqualsOperatorTests) {
CMyString a = CMyString("kutya");
CMyString b = CMyString("kutya");
CMyString c = CMyString("cica");
CMyString d;
CMyString e = CMyString("cicajuice");
Assert::IsTrue(a == b);
Assert::IsFalse(a == c);
Assert::IsFalse(a == d);
Assert::IsFalse(a == e);
}
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"),
CMyString("KORTEALMAEPER"),
CMyString("KORTEALMAEPER"),
CMyString("KORTEALMAEPER"),
};
char* find[] = {
"",
"",
nullptr,
"h",
"e",
"test",
"testt",
"est",
"kORTE",
"ALMa",
"EPEr",
};
int expected[] = {
-1,
-1,
-1,
-1,
1,
0,
-1,
1,
-1,
-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);
}
}
}
TEST_METHOD(SpecificTests) {
CMyString s = CMyString("ABCDE");
const char* const belsoForras = s.c_str() + 1;
s.append(belsoForras, 0, 2);
Assert::AreEqual("ABCDEBC", s.c_str());
Assert::AreEqual((size_t)7, s.size());
Assert::AreEqual((size_t)16, s.capacity());
CMyString g = CMyString("ALMA");
g.reserve(12);
Assert::AreEqual((size_t)4, g.size());
Assert::AreEqual((size_t)12, g.capacity());
const char* p = g.c_str();
g.insert(2, "XY");
Assert::AreEqual("ALXYMA", g.c_str());
Assert::AreEqual((size_t)6, g.size());
Assert::AreEqual((size_t)12, g.capacity());
Assert::IsTrue(p == g.c_str());
CMyString h = CMyString("ABCDEFG");
const char* oldaddr = h.c_str();
h.replace(2, 3, "X");
Assert::AreEqual("ABXFG", h.c_str());
Assert::AreEqual((size_t)5, h.size());
Assert::AreEqual((size_t)8, h.capacity());
Assert::IsTrue(oldaddr == h.c_str());
Assert::AreEqual('\0', h.c_str()[h.size()]);
}
};
}

View file

@ -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() {