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

View file

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

View file

@ -1,6 +1,32 @@
// Tóth Dániel Mátyás | IFPQYY | v1
#include "CMyString.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() {
m_nDataLength = 0;
@ -23,20 +49,22 @@ CMyString::CMyString(const char* psz) {
m_nAllocLength = m_nDataLength + 1;
}
m_pchData = new char[m_nAllocLength];
int i = 0;
while (i < m_nAllocLength)
{
m_pchData[i] = psz[i];
i++;
}
m_pchData[i] = '\0';
CopyStr(m_pchData, psz, m_nDataLength);
}
CMyString::CMyString(char ch, size_t repeat = 1) {
m_nDataLength = repeat;
m_nAllocLength = repeat + 1;
CMyString::CMyString(char ch, size_t repeat) {
if (ch == '\0')
{
m_nDataLength = 0;
m_nAllocLength = 1;
}
else
{
m_nDataLength = repeat;
m_nAllocLength = repeat + 1;
}
m_pchData = new char[m_nAllocLength];
int i = 0;
while (i < repeat)
while (i < m_nDataLength)
{
m_pchData[i] = ch;
i++;
@ -56,55 +84,153 @@ CMyString::CMyString(const CMyString& str) {
m_pchData = new char[m_nAllocLength];
int i = 0;
const char* cont = str.c_str();
while (cont[i])
while (cont[i]) // will never be a nullptr
{
m_pchData[i] = cont[i];
i++;
}
m_pchData[i] = '\0';
m_pchData[i] = '\0'; //i will never go over nAllocLength
}
CMyString::~CMyString() {
delete m_pchData;
}
size_t CMyString::size() const {
return m_nDataLength;
}
size_t CMyString::capacity() const {
return m_nDataLength;
}
const char* CMyString::c_str() const {
return m_pchData;
}
void CMyString::clear() {
m_nDataLength = 0;
m_pchData[0] = '\0';
}
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) {
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) {
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) {
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 {
return m_nDataLength == 0;
}
void CMyString::display() const {
if (!empty())
{
cout << m_pchData;
}
}
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() {
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) {
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 shrink_to_fill();
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);
private:
char* m_pchData;

View file

@ -1,2 +1,36 @@
// Tóth Dániel Mátyás | IFPQYY | v1
#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
#pragma once
#include <memory>
using namespace std;
class CMyStringException {
public:
CMyStringException(char* input);
CMyStringException(const char* input);
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:
char* msg;
unique_ptr<char[]> msg;
};

View file

@ -17,7 +17,6 @@
<Configuration>Release</Configuration>
<Platform>x64</Platform>
</ProjectConfiguration>
</ItemGroup>
<PropertyGroup Label="Globals">
<VCProjectVersion>18.0</VCProjectVersion>
@ -53,27 +52,24 @@
<WholeProgramOptimization>true</WholeProgramOptimization>
<CharacterSet>Unicode</CharacterSet>
</PropertyGroup>
<Import Project="$(VCTargetsPath)\Microsoft.Cpp.props" />
<ImportGroup Label="ExtensionSettings">
</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 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" />
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|Win32'">
<ClCompile>
<WarningLevel>Level3</WarningLevel>
@ -84,10 +80,10 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|Win32'">
@ -102,10 +98,10 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Debug|x64'">
@ -118,10 +114,10 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">
@ -136,15 +132,22 @@
</ClCompile>
<Link>
<SubSystem>Console</SubSystem>
<GenerateDebugInformation>true</GenerateDebugInformation>
<GenerateDebugInformation>true</GenerateDebugInformation>
</Link>
<Manifest>
<EnableSegmentHeap>true</EnableSegmentHeap>
<EnableSegmentHeap>true</EnableSegmentHeap>
</Manifest>
</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" />
<ImportGroup Label="ExtensionTargets">
</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>
</Filter>
</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>