feladat5 almost done, needs unit tests

This commit is contained in:
Damimani 2026-11-05 12:43:22 +01:00
parent 157bcf4af8
commit a72fa4c5a1
8 changed files with 256 additions and 60 deletions

View file

@ -77,6 +77,7 @@
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
<IncludePath>C:\Users\danit\Desktop\2026-27-cpp-homework\feladat5\feladat5;$(IncludePath)</IncludePath>
</PropertyGroup> </PropertyGroup>
<PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <PropertyGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<LinkIncremental>true</LinkIncremental> <LinkIncremental>true</LinkIncremental>
@ -142,7 +143,8 @@
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Windows</SubSystem> <SubSystem>Windows</SubSystem>
<AdditionalLibraryDirectories>$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories> <AdditionalLibraryDirectories>C:\Users\danit\Desktop\2026-27-cpp-homework\feladat5\feladat5\x64\Debug;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>CMyString;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -179,6 +181,11 @@
<ItemGroup> <ItemGroup>
<ClInclude Include="pch.h" /> <ClInclude Include="pch.h" />
</ItemGroup> </ItemGroup>
<ItemGroup>
<ProjectReference Include="..\feladat5\feladat5.vcxproj">
<Project>{30d9e2e3-47ad-44f5-b8ba-07a26ba06d5e}</Project>
</ProjectReference>
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>

View file

@ -3,5 +3,6 @@
<Platform Name="x64" /> <Platform Name="x64" />
<Platform Name="x86" /> <Platform Name="x86" />
</Configurations> </Configurations>
<Project Path="feladat5/feladat5.vcxproj" /> <Project Path="feladat5/feladat5.vcxproj" Id="30d9e2e3-47ad-44f5-b8ba-07a26ba06d5e" />
<Project Path="UnitTest1/UnitTest1.vcxproj" />
</Solution> </Solution>

View file

@ -1,6 +1,32 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyString.h" #include "CMyString.h"
#include "CMyStringException.h" #include "CMyStringException.h"
#include <iostream>
static int strLength(const char* str) {
if (str == nullptr)
{
return 0;
}
int cnt = 0;
while (str[cnt])
{
cnt++;
}
return cnt;
}
static void CopyStr(char* dest, const char* src, size_t length) {
if (dest == nullptr || src == nullptr) {
throw new bad_exception;
}
size_t i;
for (i = 0; i < length; i++)
{
dest[i] = src[i];
}
dest[i] = '\0';
}
CMyString::CMyString() { CMyString::CMyString() {
m_nDataLength = 0; m_nDataLength = 0;
@ -23,20 +49,22 @@ CMyString::CMyString(const char* psz) {
m_nAllocLength = m_nDataLength + 1; m_nAllocLength = m_nDataLength + 1;
} }
m_pchData = new char[m_nAllocLength]; m_pchData = new char[m_nAllocLength];
int i = 0; CopyStr(m_pchData, psz, m_nDataLength);
while (i < m_nAllocLength)
{
m_pchData[i] = psz[i];
i++;
}
m_pchData[i] = '\0';
} }
CMyString::CMyString(char ch, size_t repeat = 1) { CMyString::CMyString(char ch, size_t repeat) {
m_nDataLength = repeat; if (ch == '\0')
m_nAllocLength = repeat + 1; {
m_nDataLength = 0;
m_nAllocLength = 1;
}
else
{
m_nDataLength = repeat;
m_nAllocLength = repeat + 1;
}
m_pchData = new char[m_nAllocLength]; m_pchData = new char[m_nAllocLength];
int i = 0; int i = 0;
while (i < repeat) while (i < m_nDataLength)
{ {
m_pchData[i] = ch; m_pchData[i] = ch;
i++; i++;
@ -56,55 +84,153 @@ CMyString::CMyString(const CMyString& str) {
m_pchData = new char[m_nAllocLength]; m_pchData = new char[m_nAllocLength];
int i = 0; int i = 0;
const char* cont = str.c_str(); const char* cont = str.c_str();
while (cont[i]) while (cont[i]) // will never be a nullptr
{ {
m_pchData[i] = cont[i]; m_pchData[i] = cont[i];
i++; i++;
} }
m_pchData[i] = '\0'; m_pchData[i] = '\0'; //i will never go over nAllocLength
} }
CMyString::~CMyString() { CMyString::~CMyString() {
delete m_pchData; delete m_pchData;
} }
size_t CMyString::size() const { size_t CMyString::size() const {
return m_nDataLength;
} }
size_t CMyString::capacity() const { size_t CMyString::capacity() const {
return m_nDataLength;
} }
const char* CMyString::c_str() const { const char* CMyString::c_str() const {
return m_pchData;
} }
void CMyString::clear() { void CMyString::clear() {
m_nDataLength = 0;
m_pchData[0] = '\0';
} }
char CMyString::getat(size_t index) const { char CMyString::getat(size_t index) const {
if (index >= m_nDataLength)
{
throw new CMyStringException(CMyStringException::ErrOutOfRange);
}
return m_pchData[index];
} }
void CMyString::setat(size_t index, char ch) { void CMyString::setat(size_t index, char ch) {
if (index >= m_nDataLength)
{
throw new CMyStringException(CMyStringException::ErrOutOfRange);
}
if (ch == '\0')
{
throw new CMyStringException(CMyStringException::ErrInvalidChar);
}
m_pchData[index] = ch;
} }
void CMyString::reserve(size_t newcapacity) { void CMyString::reserve(size_t newcapacity) {
if (newcapacity < m_nAllocLength)
{
throw new CMyStringException(CMyStringException::ErrUnexpected);
}
char* tmp = m_pchData;
m_pchData = new char[newcapacity];
CopyStr(m_pchData, tmp, m_nDataLength);
delete tmp;
} }
void CMyString::erase(size_t pos, size_t count) { void CMyString::erase(size_t pos, size_t count) {
if (pos >= m_nDataLength)
{
throw new CMyStringException(CMyStringException::ErrOutOfRange);
}
if (pos + count > m_nDataLength)
{
count = m_nDataLength - pos;
}
for (size_t i = 0; i < count; i++)
{
m_pchData[pos + i] = '\0';
}
} }
bool CMyString::empty() const { bool CMyString::empty() const {
return m_nDataLength == 0; return m_nDataLength == 0;
} }
void CMyString::display() const { void CMyString::display() const {
if (!empty())
{
cout << m_pchData;
}
} }
void CMyString::shrink_to_fill() { void CMyString::shrink_to_fill() {
m_nAllocLength = m_nDataLength + 1;
char* tmp = m_pchData;
m_pchData = new char[m_nAllocLength];
CopyStr(m_pchData, tmp, m_nDataLength);
delete tmp;
} }
void CMyString::reverse() { void CMyString::reverse() {
if (!empty())
{
for (size_t i = 0; i < m_nDataLength / 2; i++)
{
int j = m_nDataLength - i - 1;
char tmp = m_pchData[i];
m_pchData[i] = m_pchData[j];
m_pchData[j] = tmp;
}
}
} }
void CMyString::append(const char* psz, unsigned offset = 0, unsigned count = -0) { void CMyString::append(const char* psz, unsigned offset, unsigned count) {
if (psz == nullptr || psz == m_pchData)
{
return;
}
int len = strLength(psz);
if (count == ~0)
{
count = len;
}
if (len < (count - offset))
{
throw new CMyStringException(CMyStringException::ErrOutOfRange);
}
if (count + m_nDataLength > m_nAllocLength)
{
char* tmp = m_pchData;
m_nAllocLength = (count + m_nDataLength) * 2;
m_pchData = new char[m_nAllocLength];
CopyStr(m_pchData, tmp, m_nDataLength);
delete tmp;
}
int i = m_nDataLength;
int j = offset;
while (j < count)
{
m_pchData[i++] = psz[j++];
}
m_nDataLength = i;
m_pchData[i] = '\0';
} }
CMyString& CMyString::operator=(const CMyString& str) { CMyString& CMyString::operator=(const CMyString& str) {
if (m_pchData != str.c_str())
{
if (!str.empty())
{
if (str.capacity() > m_nAllocLength)
{
m_nAllocLength = str.capacity();
char* cpy = m_pchData;
m_pchData = new char[m_nAllocLength];
delete cpy;
}
CopyStr(m_pchData, str.c_str(), m_nDataLength);
m_nDataLength = str.size();
}
else
{
m_nDataLength = 0;
m_nAllocLength = 1;
delete m_pchData;
m_pchData = new char[m_nAllocLength];
m_pchData[0] = '\0';
}
}
return *this;
} }

View file

@ -19,7 +19,7 @@ public:
void display() const; void display() const;
void shrink_to_fill(); void shrink_to_fill();
void reverse(); void reverse();
void append(const char* psz, unsigned offset = 0, unsigned count = -0); void append(const char* psz, unsigned offset = 0, unsigned count = ~0);
CMyString& operator=(const CMyString& str); CMyString& operator=(const CMyString& str);
private: private:
char* m_pchData; char* m_pchData;

View file

@ -1,2 +1,36 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyStringException.h" #include "CMyStringException.h"
static int strLength(const char* str) {
if (str == nullptr)
{
return 0;
}
int cnt = 0;
while (str[cnt])
{
cnt++;
}
return cnt;
}
static void CopyStr(char* dest, const char* src, size_t length) {
if (dest == nullptr || src == nullptr) {
return;
}
size_t i;
for (i = 0; i < length; i++)
{
dest[i] = src[i];
}
dest[i] = '\0';
}
CMyStringException::CMyStringException(const char* input) {
int len = strLength(input);
msg = unique_ptr<char[]>(new char[len+1]);
CopyStr(msg.get(), input, len);
}
const char* CMyStringException::what() {
return msg.get();
}

View file

@ -1,9 +1,15 @@
// Tóth Dániel Mátyás | IFPQYY | v1 // Tóth Dániel Mátyás | IFPQYY | v1
#pragma once #pragma once
#include <memory>
using namespace std;
class CMyStringException { class CMyStringException {
public: public:
CMyStringException(char* input); CMyStringException(const char* input);
const char* what(void); const char* what(void);
static constexpr const char* ErrOutOfRange = "Hiba: index hiba!";
static constexpr const char* ErrInvalidChar = "Hiba: érvénytelen karakter!";
static constexpr const char* ErrCount = "Hiba: karakterek száma 0!";
static constexpr const char* ErrUnexpected = "Hiba: Nem várt hiba!";
private: private:
char* msg; unique_ptr<char[]> msg;
}; };

View file

@ -17,7 +17,6 @@
<Configuration>Release</Configuration> <Configuration>Release</Configuration>
<Platform>x64</Platform> <Platform>x64</Platform>
</ProjectConfiguration> </ProjectConfiguration>
</ItemGroup> </ItemGroup>
<PropertyGroup Label="Globals"> <PropertyGroup Label="Globals">
<VCProjectVersion>18.0</VCProjectVersion> <VCProjectVersion>18.0</VCProjectVersion>
@ -53,27 +52,24 @@
<WholeProgramOptimization>true</WholeProgramOptimization> <WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet> <CharacterSet>Unicode</CharacterSet>
</PropertyGroup> </PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings"> <ImportGroup Label="ExtensionSettings">
</ImportGroup> </ImportGroup>
<ImportGroup Label="Shared" > <ImportGroup Label="Shared">
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup> </ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<ImportGroup Label="PropertySheets" Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
<Import Project="$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props" Condition="exists('$(UserRootDir)\Microsoft.Cpp.$(Platform).user.props')" Label="LocalAppDataPlatform" />
</ImportGroup>
<PropertyGroup Label="UserMacros" /> <PropertyGroup Label="UserMacros" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile> <ClCompile>
<WarningLevel>Level3</WarningLevel> <WarningLevel>Level3</WarningLevel>
@ -84,10 +80,10 @@
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<Manifest> <Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap> <EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest> </Manifest>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -102,10 +98,10 @@
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<Manifest> <Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap> <EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest> </Manifest>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -118,10 +114,10 @@
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<Manifest> <Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap> <EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest> </Manifest>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -136,15 +132,22 @@
</ClCompile> </ClCompile>
<Link> <Link>
<SubSystem>Console</SubSystem> <SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation> <GenerateDebugInformation>true</GenerateDebugInformation>
</Link> </Link>
<Manifest> <Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap> <EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest> </Manifest>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemGroup>
<ItemGroup></ItemGroup> <ClCompile Include="CMyString.cpp" />
<ClCompile Include="CMyStringException.cpp" />
<ClCompile Include="main.cpp" />
</ItemGroup>
<ItemGroup>
<ClInclude Include="CMyString.h" />
<ClInclude Include="CMyStringException.h" />
</ItemGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" /> <Import Project="$(VCTargetsPath)\Microsoft.Cpp.targets" />
<ImportGroup Label="ExtensionTargets"> <ImportGroup Label="ExtensionTargets">
</ImportGroup> </ImportGroup>
</Project> </Project>

View file

@ -14,4 +14,23 @@
<Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions> <Extensions>rc;ico;cur;bmp;dlg;rc2;rct;bin;rgs;gif;jpg;jpeg;jpe;resx;tiff;tif;png;wav;mfcribbon-ms</Extensions>
</Filter> </Filter>
</ItemGroup> </ItemGroup>
<ItemGroup>
<ClCompile Include="main.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CMyString.cpp">
<Filter>Source Files</Filter>
</ClCompile>
<ClCompile Include="CMyStringException.cpp">
<Filter>Source Files</Filter>
</ClCompile>
</ItemGroup>
<ItemGroup>
<ClInclude Include="CMyString.h">
<Filter>Header Files</Filter>
</ClInclude>
<ClInclude Include="CMyStringException.h">
<Filter>Header Files</Filter>
</ClInclude>
</ItemGroup>
</Project> </Project>