feladat5 v2 done
This commit is contained in:
parent
a5b58c2d8a
commit
459e111f2f
6 changed files with 92 additions and 41 deletions
|
|
@ -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 "pch.h"
|
||||||
#include "CppUnitTest.h"
|
#include "CppUnitTest.h"
|
||||||
#include <CMyString.h>
|
#include <CMyString.h>
|
||||||
|
|
@ -107,6 +107,9 @@ namespace UnitTest1
|
||||||
Assert::AreEqual(repeattextres[i], repeatctor.c_str());
|
Assert::AreEqual(repeattextres[i], repeatctor.c_str());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
catch (const CMyStringException& e) {
|
||||||
|
Assert::AreEqual(CMyStringException::ErrInvalidChar, e.what());
|
||||||
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
Assert::IsTrue(false);
|
Assert::IsTrue(false);
|
||||||
|
|
@ -191,18 +194,21 @@ namespace UnitTest1
|
||||||
CMyString("testtesttest"),
|
CMyString("testtesttest"),
|
||||||
CMyString("testtesttest"),
|
CMyString("testtesttest"),
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
|
CMyString("dio"),
|
||||||
};
|
};
|
||||||
CMyString strs[]{
|
CMyString strs[]{
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
CMyString("testtesttesttest"),
|
CMyString("testtesttesttest"),
|
||||||
CMyString("testtesttesttest"),
|
CMyString("testtesttesttest"),
|
||||||
|
CMyString("narancs"),
|
||||||
};
|
};
|
||||||
int expected[][2] = {
|
int expected[][2] = {
|
||||||
{ 4, 10 },
|
{ 4, 5 },
|
||||||
{ 4, 13 },
|
{ 4, 13 },
|
||||||
{ 16, 34 },
|
{ 16, 17 },
|
||||||
{ 16, 34 },
|
{ 16, 17 },
|
||||||
|
{ 7, 8 },
|
||||||
};
|
};
|
||||||
int testcount = sizeof(expected) / (sizeof(int) * 2);
|
int testcount = sizeof(expected) / (sizeof(int) * 2);
|
||||||
for (size_t i = 0; i < testcount; i++)
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
|
@ -322,25 +328,36 @@ namespace UnitTest1
|
||||||
}
|
}
|
||||||
|
|
||||||
TEST_METHOD(ReserveTests) {
|
TEST_METHOD(ReserveTests) {
|
||||||
|
CMyString str = CMyString("test");
|
||||||
int allocs[] = {
|
int allocs[] = {
|
||||||
1,
|
1,
|
||||||
5,
|
5,
|
||||||
10,
|
10,
|
||||||
33,
|
33,
|
||||||
};
|
};
|
||||||
|
int expected[] = {
|
||||||
|
5,
|
||||||
|
5,
|
||||||
|
10,
|
||||||
|
33,
|
||||||
|
};
|
||||||
|
|
||||||
int testcount = sizeof(allocs) / sizeof(int);
|
int testcount = sizeof(allocs) / sizeof(int);
|
||||||
for (size_t i = 0; i < testcount; i++)
|
for (size_t i = 0; i < testcount; i++)
|
||||||
{
|
{
|
||||||
try
|
try
|
||||||
{
|
{
|
||||||
CMyString str = CMyString("test");
|
const char* ptr = str.c_str();
|
||||||
str.reserve(allocs[i]);
|
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("test"),
|
CMyString("test"),
|
||||||
CMyString('a', 10),
|
CMyString('a', 10),
|
||||||
|
CMyString("alma"),
|
||||||
|
CMyString("test"),
|
||||||
};
|
};
|
||||||
int pos[] = {
|
int pos[] = {
|
||||||
0,
|
0,
|
||||||
|
|
@ -361,6 +380,8 @@ namespace UnitTest1
|
||||||
3,
|
3,
|
||||||
3,
|
3,
|
||||||
3,
|
3,
|
||||||
|
4,
|
||||||
|
0,
|
||||||
};
|
};
|
||||||
int cnt[] = {
|
int cnt[] = {
|
||||||
1,
|
1,
|
||||||
|
|
@ -369,14 +390,18 @@ namespace UnitTest1
|
||||||
0,
|
0,
|
||||||
1,
|
1,
|
||||||
1,
|
1,
|
||||||
|
100,
|
||||||
|
0,
|
||||||
};
|
};
|
||||||
char* text[] = {
|
char* text[] = {
|
||||||
"",
|
"",
|
||||||
NULL,
|
"test",
|
||||||
NULL,
|
NULL,
|
||||||
"test",
|
"test",
|
||||||
"tes",
|
"tes",
|
||||||
"aaaaaaaaa",
|
"aaaaaaaaa",
|
||||||
|
"alma",
|
||||||
|
"test",
|
||||||
};
|
};
|
||||||
int testcount = sizeof(pos) / sizeof(int);
|
int testcount = sizeof(pos) / sizeof(int);
|
||||||
for (size_t i = 0; i < testcount; i++)
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
|
@ -388,14 +413,7 @@ namespace UnitTest1
|
||||||
}
|
}
|
||||||
catch (const CMyStringException& e)
|
catch (const CMyStringException& e)
|
||||||
{
|
{
|
||||||
if (i < 3)
|
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
|
||||||
{
|
|
||||||
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
|
|
||||||
}
|
|
||||||
else
|
|
||||||
{
|
|
||||||
Assert::IsTrue(false);
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
catch (...)
|
catch (...)
|
||||||
{
|
{
|
||||||
|
|
@ -472,6 +490,11 @@ namespace UnitTest1
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
CMyString("test"),
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("test"),
|
||||||
|
CMyString("banan"),
|
||||||
|
CMyString("banan"),
|
||||||
};
|
};
|
||||||
const char* text[] = {
|
const char* text[] = {
|
||||||
"",
|
"",
|
||||||
|
|
@ -481,24 +504,39 @@ namespace UnitTest1
|
||||||
"test",
|
"test",
|
||||||
"test",
|
"test",
|
||||||
"test",
|
"test",
|
||||||
|
"",
|
||||||
|
"test",
|
||||||
|
"a",
|
||||||
|
"narancs",
|
||||||
|
"narancs",
|
||||||
};
|
};
|
||||||
size_t extraargs[][2] = {
|
size_t extraargs[][2] = {
|
||||||
{ 0, ~0 },
|
{ 0, ~0 },
|
||||||
{ 0, ~0 },
|
{ 0, ~0 },
|
||||||
{ 0, ~0 },
|
{ 0, ~0 },
|
||||||
{ 0, ~0 },
|
{ 0, ~0 },
|
||||||
{ 4, ~0 },
|
{ 5, ~0 },
|
||||||
{ 0, 5 },
|
{ 0, 5 },
|
||||||
{ 2, 3 },
|
{ 2, 3 },
|
||||||
|
{ 1, 0 },
|
||||||
|
{ 4, ~0 },
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 0, ~0 },
|
||||||
|
{ 2, 2 },
|
||||||
};
|
};
|
||||||
int sizes[][2] = {
|
int sizes[][2] = {
|
||||||
{ 0, 1 },
|
{ 0, 1 },
|
||||||
{ 0, 1 },
|
{ 0, 1 },
|
||||||
{ 4, 5 },
|
{ 4, 5 },
|
||||||
{ 8, 16 },
|
{ 8, 18 },
|
||||||
{ 4, 5 },
|
{ 4, 5 },
|
||||||
{ 4, 5 },
|
{ 4, 5 },
|
||||||
{ 4, 5 },
|
{ 4, 5 },
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 4, 5 },
|
||||||
|
{ 5, 12 },
|
||||||
|
{ 12, 26 },
|
||||||
|
{ 7, 16 },
|
||||||
};
|
};
|
||||||
const char* expected[] = {
|
const char* expected[] = {
|
||||||
"",
|
"",
|
||||||
|
|
@ -508,6 +546,11 @@ namespace UnitTest1
|
||||||
CMyStringException::ErrOutOfRange,
|
CMyStringException::ErrOutOfRange,
|
||||||
CMyStringException::ErrOutOfRange,
|
CMyStringException::ErrOutOfRange,
|
||||||
CMyStringException::ErrOutOfRange,
|
CMyStringException::ErrOutOfRange,
|
||||||
|
CMyStringException::ErrCount,
|
||||||
|
"test",
|
||||||
|
"testa",
|
||||||
|
"banannarancs",
|
||||||
|
"bananra",
|
||||||
};
|
};
|
||||||
int testcount = sizeof(text) / sizeof(char*);
|
int testcount = sizeof(text) / sizeof(char*);
|
||||||
for (size_t i = 0; i < testcount; i++)
|
for (size_t i = 0; i < testcount; i++)
|
||||||
|
|
|
||||||
|
|
@ -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 "CMyString.h"
|
||||||
#include "CMyStringException.h"
|
#include "CMyStringException.h"
|
||||||
#include <iostream>
|
#include <iostream>
|
||||||
|
|
@ -57,7 +57,11 @@ CMyString::CMyString(const char* psz) {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
CMyString::CMyString(char ch, size_t repeat) {
|
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);
|
MakeEmptyStr(&m_nDataLength, &m_nAllocLength, &m_pchData);
|
||||||
}
|
}
|
||||||
|
|
@ -128,9 +132,9 @@ void CMyString::setat(size_t index, char ch) {
|
||||||
m_pchData[index] = ch;
|
m_pchData[index] = ch;
|
||||||
}
|
}
|
||||||
void CMyString::reserve(size_t newcapacity) {
|
void CMyString::reserve(size_t newcapacity) {
|
||||||
if (newcapacity < m_nAllocLength)
|
if (newcapacity <= m_nAllocLength)
|
||||||
{
|
{
|
||||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
return;
|
||||||
}
|
}
|
||||||
char* tmp = m_pchData;
|
char* tmp = m_pchData;
|
||||||
m_nAllocLength = newcapacity;
|
m_nAllocLength = newcapacity;
|
||||||
|
|
@ -143,7 +147,7 @@ void CMyString::erase(size_t pos, size_t count) {
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
if (pos >= m_nDataLength)
|
if (pos > m_nDataLength)
|
||||||
{
|
{
|
||||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
||||||
}
|
}
|
||||||
|
|
@ -193,27 +197,31 @@ void CMyString::reverse() {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
|
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
|
||||||
if (psz == nullptr || psz == m_pchData || psz == "")
|
if (psz == nullptr || psz == m_pchData)
|
||||||
{
|
{
|
||||||
return;
|
return;
|
||||||
}
|
}
|
||||||
|
if (count == 0)
|
||||||
|
{
|
||||||
|
throw CMyStringException(CMyStringException::ErrCount);
|
||||||
|
}
|
||||||
size_t len = strLength(psz);
|
size_t len = strLength(psz);
|
||||||
if (count == ~0)
|
if (count == ~0)
|
||||||
{
|
{
|
||||||
count = (int)len - offset;
|
count = len - offset;
|
||||||
}
|
}
|
||||||
int range = (count + offset);
|
int range = count + offset;
|
||||||
if (len < range || offset == len)
|
if (len < count || len < offset || len < range)
|
||||||
{
|
{
|
||||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
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 i = m_nDataLength;
|
||||||
size_t j = offset;
|
size_t j = offset;
|
||||||
while (j < count)
|
while (j < range)
|
||||||
{
|
{
|
||||||
m_pchData[i++] = psz[j++];
|
m_pchData[i++] = psz[j++];
|
||||||
}
|
}
|
||||||
|
|
@ -227,7 +235,7 @@ CMyString& CMyString::operator=(const CMyString& str) {
|
||||||
{
|
{
|
||||||
if (str.size() >= m_nAllocLength)
|
if (str.size() >= m_nAllocLength)
|
||||||
{
|
{
|
||||||
this->reserve((str.size() + 1) * 2);
|
this->reserve((str.size() + 1));
|
||||||
}
|
}
|
||||||
m_nDataLength = str.size();
|
m_nDataLength = str.size();
|
||||||
CopyStr(m_pchData, str.c_str(), m_nDataLength);
|
CopyStr(m_pchData, str.c_str(), m_nDataLength);
|
||||||
|
|
|
||||||
|
|
@ -1,4 +1,4 @@
|
||||||
// Tóth Dániel Mátyás | IFPQYY | v1
|
// Tóth Dániel Mátyás | IFPQYY | v2
|
||||||
#pragma once
|
#pragma once
|
||||||
class CMyString {
|
class CMyString {
|
||||||
public:
|
public:
|
||||||
|
|
|
||||||
|
|
@ -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 "CMyStringException.h"
|
||||||
|
#include "CMyString.h"
|
||||||
|
|
||||||
static int strLength(const char* str) {
|
static int strLength(const char* str) {
|
||||||
if (str == nullptr)
|
if (str == nullptr)
|
||||||
|
|
@ -27,10 +28,8 @@ static void CopyStr(char* dest, const char* src, size_t length) {
|
||||||
}
|
}
|
||||||
|
|
||||||
CMyStringException::CMyStringException(const char* input) {
|
CMyStringException::CMyStringException(const char* input) {
|
||||||
int len = strLength(input);
|
msg = CMyString(input);
|
||||||
msg = unique_ptr<char[]>(new char[len+1]);
|
|
||||||
CopyStr(msg.get(), input, len);
|
|
||||||
}
|
}
|
||||||
const char* CMyStringException::what() const {
|
const char* CMyStringException::what() const {
|
||||||
return msg.get();
|
return msg.c_str();
|
||||||
}
|
}
|
||||||
|
|
@ -1,6 +1,7 @@
|
||||||
// Tóth Dániel Mátyás | IFPQYY | v1
|
// Tóth Dániel Mátyás | IFPQYY | v2
|
||||||
#pragma once
|
#pragma once
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include "CMyString.h"
|
||||||
using namespace std;
|
using namespace std;
|
||||||
class CMyStringException {
|
class CMyStringException {
|
||||||
public:
|
public:
|
||||||
|
|
@ -11,5 +12,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:
|
||||||
unique_ptr<char[]> msg;
|
CMyString msg;
|
||||||
};
|
};
|
||||||
|
|
@ -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 "CMyString.h"
|
||||||
|
|
||||||
int main() {
|
int main() {
|
||||||
|
|
|
||||||
Loading…
Add table
Add a link
Reference in a new issue