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 "CppUnitTest.h"
|
||||
#include <CMyString.h>
|
||||
|
|
@ -107,6 +107,9 @@ namespace UnitTest1
|
|||
Assert::AreEqual(repeattextres[i], repeatctor.c_str());
|
||||
}
|
||||
}
|
||||
catch (const CMyStringException& e) {
|
||||
Assert::AreEqual(CMyStringException::ErrInvalidChar, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
Assert::IsTrue(false);
|
||||
|
|
@ -191,18 +194,21 @@ namespace UnitTest1
|
|||
CMyString("testtesttest"),
|
||||
CMyString("testtesttest"),
|
||||
CMyString("test"),
|
||||
CMyString("dio"),
|
||||
};
|
||||
CMyString strs[]{
|
||||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("testtesttesttest"),
|
||||
CMyString("testtesttesttest"),
|
||||
CMyString("narancs"),
|
||||
};
|
||||
int expected[][2] = {
|
||||
{ 4, 10 },
|
||||
{ 4, 5 },
|
||||
{ 4, 13 },
|
||||
{ 16, 34 },
|
||||
{ 16, 34 },
|
||||
{ 16, 17 },
|
||||
{ 16, 17 },
|
||||
{ 7, 8 },
|
||||
};
|
||||
int testcount = sizeof(expected) / (sizeof(int) * 2);
|
||||
for (size_t i = 0; i < testcount; i++)
|
||||
|
|
@ -322,25 +328,36 @@ namespace UnitTest1
|
|||
}
|
||||
|
||||
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
|
||||
{
|
||||
CMyString str = CMyString("test");
|
||||
const char* ptr = str.c_str();
|
||||
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('a', 10),
|
||||
CMyString("alma"),
|
||||
CMyString("test"),
|
||||
};
|
||||
int pos[] = {
|
||||
0,
|
||||
|
|
@ -361,6 +380,8 @@ namespace UnitTest1
|
|||
3,
|
||||
3,
|
||||
3,
|
||||
4,
|
||||
0,
|
||||
};
|
||||
int cnt[] = {
|
||||
1,
|
||||
|
|
@ -369,14 +390,18 @@ namespace UnitTest1
|
|||
0,
|
||||
1,
|
||||
1,
|
||||
100,
|
||||
0,
|
||||
};
|
||||
char* text[] = {
|
||||
"",
|
||||
NULL,
|
||||
"test",
|
||||
NULL,
|
||||
"test",
|
||||
"tes",
|
||||
"aaaaaaaaa",
|
||||
"alma",
|
||||
"test",
|
||||
};
|
||||
int testcount = sizeof(pos) / sizeof(int);
|
||||
for (size_t i = 0; i < testcount; i++)
|
||||
|
|
@ -388,14 +413,7 @@ namespace UnitTest1
|
|||
}
|
||||
catch (const CMyStringException& e)
|
||||
{
|
||||
if (i < 3)
|
||||
{
|
||||
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
|
||||
}
|
||||
else
|
||||
{
|
||||
Assert::IsTrue(false);
|
||||
}
|
||||
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
|
||||
}
|
||||
catch (...)
|
||||
{
|
||||
|
|
@ -472,6 +490,11 @@ namespace UnitTest1
|
|||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("test"),
|
||||
CMyString("banan"),
|
||||
CMyString("banan"),
|
||||
};
|
||||
const char* text[] = {
|
||||
"",
|
||||
|
|
@ -481,24 +504,39 @@ namespace UnitTest1
|
|||
"test",
|
||||
"test",
|
||||
"test",
|
||||
"",
|
||||
"test",
|
||||
"a",
|
||||
"narancs",
|
||||
"narancs",
|
||||
};
|
||||
size_t extraargs[][2] = {
|
||||
{ 0, ~0 },
|
||||
{ 0, ~0 },
|
||||
{ 0, ~0 },
|
||||
{ 0, ~0 },
|
||||
{ 4, ~0 },
|
||||
{ 5, ~0 },
|
||||
{ 0, 5 },
|
||||
{ 2, 3 },
|
||||
{ 1, 0 },
|
||||
{ 4, ~0 },
|
||||
{ 0, ~0 },
|
||||
{ 0, ~0 },
|
||||
{ 2, 2 },
|
||||
};
|
||||
int sizes[][2] = {
|
||||
{ 0, 1 },
|
||||
{ 0, 1 },
|
||||
{ 4, 5 },
|
||||
{ 8, 16 },
|
||||
{ 8, 18 },
|
||||
{ 4, 5 },
|
||||
{ 4, 5 },
|
||||
{ 4, 5 },
|
||||
{ 4, 5 },
|
||||
{ 4, 5 },
|
||||
{ 5, 12 },
|
||||
{ 12, 26 },
|
||||
{ 7, 16 },
|
||||
};
|
||||
const char* expected[] = {
|
||||
"",
|
||||
|
|
@ -508,6 +546,11 @@ namespace UnitTest1
|
|||
CMyStringException::ErrOutOfRange,
|
||||
CMyStringException::ErrOutOfRange,
|
||||
CMyStringException::ErrOutOfRange,
|
||||
CMyStringException::ErrCount,
|
||||
"test",
|
||||
"testa",
|
||||
"banannarancs",
|
||||
"bananra",
|
||||
};
|
||||
int testcount = sizeof(text) / sizeof(char*);
|
||||
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 "CMyStringException.h"
|
||||
#include <iostream>
|
||||
|
|
@ -57,7 +57,11 @@ CMyString::CMyString(const char* psz) {
|
|||
}
|
||||
}
|
||||
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);
|
||||
}
|
||||
|
|
@ -128,9 +132,9 @@ void CMyString::setat(size_t index, char ch) {
|
|||
m_pchData[index] = ch;
|
||||
}
|
||||
void CMyString::reserve(size_t newcapacity) {
|
||||
if (newcapacity < m_nAllocLength)
|
||||
if (newcapacity <= m_nAllocLength)
|
||||
{
|
||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
||||
return;
|
||||
}
|
||||
char* tmp = m_pchData;
|
||||
m_nAllocLength = newcapacity;
|
||||
|
|
@ -143,7 +147,7 @@ void CMyString::erase(size_t pos, size_t count) {
|
|||
{
|
||||
return;
|
||||
}
|
||||
if (pos >= m_nDataLength)
|
||||
if (pos > m_nDataLength)
|
||||
{
|
||||
throw CMyStringException(CMyStringException::ErrOutOfRange);
|
||||
}
|
||||
|
|
@ -193,27 +197,31 @@ void CMyString::reverse() {
|
|||
}
|
||||
}
|
||||
void CMyString::append(const char* psz, unsigned offset, unsigned count) {
|
||||
if (psz == nullptr || psz == m_pchData || psz == "")
|
||||
if (psz == nullptr || psz == m_pchData)
|
||||
{
|
||||
return;
|
||||
}
|
||||
if (count == 0)
|
||||
{
|
||||
throw CMyStringException(CMyStringException::ErrCount);
|
||||
}
|
||||
size_t len = strLength(psz);
|
||||
if (count == ~0)
|
||||
{
|
||||
count = (int)len - offset;
|
||||
count = len - offset;
|
||||
}
|
||||
int range = (count + offset);
|
||||
if (len < range || offset == len)
|
||||
int range = count + offset;
|
||||
if (len < count || len < offset || len < range)
|
||||
{
|
||||
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 j = offset;
|
||||
while (j < count)
|
||||
while (j < range)
|
||||
{
|
||||
m_pchData[i++] = psz[j++];
|
||||
}
|
||||
|
|
@ -227,7 +235,7 @@ CMyString& CMyString::operator=(const CMyString& str) {
|
|||
{
|
||||
if (str.size() >= m_nAllocLength)
|
||||
{
|
||||
this->reserve((str.size() + 1) * 2);
|
||||
this->reserve((str.size() + 1));
|
||||
}
|
||||
m_nDataLength = str.size();
|
||||
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
|
||||
class CMyString {
|
||||
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 "CMyString.h"
|
||||
|
||||
static int strLength(const char* str) {
|
||||
if (str == nullptr)
|
||||
|
|
@ -27,10 +28,8 @@ static void CopyStr(char* dest, const char* src, size_t length) {
|
|||
}
|
||||
|
||||
CMyStringException::CMyStringException(const char* input) {
|
||||
int len = strLength(input);
|
||||
msg = unique_ptr<char[]>(new char[len+1]);
|
||||
CopyStr(msg.get(), input, len);
|
||||
msg = CMyString(input);
|
||||
}
|
||||
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
|
||||
#include <memory>
|
||||
#include "CMyString.h"
|
||||
using namespace std;
|
||||
class CMyStringException {
|
||||
public:
|
||||
|
|
@ -11,5 +12,5 @@ public:
|
|||
static constexpr const char* ErrCount = "Hiba: karakterek száma 0!";
|
||||
static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!";
|
||||
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"
|
||||
|
||||
int main() {
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue