most tests are done
This commit is contained in:
parent
a72fa4c5a1
commit
938e557caa
6 changed files with 360 additions and 74 deletions
|
|
@ -1,6 +1,8 @@
|
|||
// Tóth Dániel Mátyás | IFPQYY | v1
|
||||
#include "pch.h"
|
||||
#include "CppUnitTest.h"
|
||||
#include <CMyString.h>
|
||||
#include <CMyStringException.h>
|
||||
|
||||
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
|
||||
|
||||
|
|
@ -9,9 +11,304 @@ namespace UnitTest1
|
|||
TEST_CLASS(UnitTest1)
|
||||
{
|
||||
public:
|
||||
|
||||
TEST_METHOD(TestMethod1)
|
||||
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 (...)
|
||||
{
|
||||
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(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) {
|
||||
int allocs[] = {
|
||||
1,
|
||||
5,
|
||||
10,
|
||||
33,
|
||||
};
|
||||
|
||||
int testcount = sizeof(allocs) / sizeof(int);
|
||||
for (size_t i = 0; i < testcount; i++)
|
||||
{
|
||||
try
|
||||
{
|
||||
CMyString str = CMyString("test");
|
||||
str.reserve(allocs[i]);
|
||||
Assert::AreEqual(allocs[i], (int)str.capacity());
|
||||
}
|
||||
catch (const CMyStringException& e)
|
||||
{
|
||||
Assert::AreEqual(CMyStringException::ErrOutOfRange, e.what());
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
TEST_METHOD(EraseTests) {
|
||||
//TODO
|
||||
}
|
||||
};
|
||||
}
|
||||
|
|
|
|||
|
|
@ -144,7 +144,7 @@
|
|||
<Link>
|
||||
<SubSystem>Windows</SubSystem>
|
||||
<AdditionalLibraryDirectories>C:\Users\danit\Desktop\2026-27-cpp-homework\feladat5\feladat5\x64\Debug;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
|
||||
<AdditionalDependencies>CMyString;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
<AdditionalDependencies>CMyString;CMyStringException;%(AdditionalDependencies)</AdditionalDependencies>
|
||||
</Link>
|
||||
</ItemDefinitionGroup>
|
||||
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue