unittest start

This commit is contained in:
Damimani 2026-10-31 10:05:41 +01:00
parent bc87732585
commit fa8bd24551
8 changed files with 105 additions and 99 deletions

View file

@ -1,6 +1,7 @@
//Tóth Dániel Mátyás | IFPQYY | V1
#include "pch.h" #include "pch.h"
#include "CppUnitTest.h" #include "CppUnitTest.h"
#include <cmydate.h>
#include <cmydateexception.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework; using namespace Microsoft::VisualStudio::CppUnitTestFramework;
@ -10,8 +11,86 @@ namespace UnitTest1
{ {
public: public:
TEST_METHOD(TestMethod1)
TEST_METHOD(ValidDate)
{ {
CMyDate date;
Assert::AreEqual(true, date.IsValid(1980, 4, 23));
Assert::AreEqual(true, date.IsValid(1970, 1, 31));
Assert::AreEqual(true, date.IsValid(1970, 2, 28));
Assert::AreEqual(true, date.IsValid(1970, 9, 30));
Assert::AreEqual(true, date.IsValid(1972, 2, 29));
}
TEST_METHOD(InvalidDates)
{
CMyDate date;
try
{
date.IsValid(1969, 1, 1);
Assert::IsTrue(false);
}
catch (const CMyDateException &e)
{
Assert::AreEqual(CMyDateException::ErrMinYear, e.what());
}
try
{
date = CMyDate(1970, 0, 1);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMinMonth, e.what());
}
try
{
date = CMyDate(1970, 13, 1);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMaxMonth, e.what());
}
try
{
date = CMyDate(1970, 1, 0);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMinDay, e.what());
}
try
{
date = CMyDate(1970, 1, 32);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
try
{
date = CMyDate(1970, 9, 31);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
try
{
date = CMyDate(1970, 4, 29);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
try
{
date = CMyDate(2000, 4, 29);
}
catch (CMyDateException e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
} }
}; };
} }

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\feladat4\feladat4;$(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\feladat4\feladat4\x64\Debug;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories)</AdditionalLibraryDirectories>
<AdditionalDependencies>cmydate;cmydateexception;%(AdditionalDependencies)</AdditionalDependencies>
</Link> </Link>
</ItemDefinitionGroup> </ItemDefinitionGroup>
<ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'"> <ItemDefinitionGroup Condition="'$(Configuration)|$(Platform)'=='Release|x64'">

View file

@ -156,25 +156,25 @@ static int GetMonthDayCount(int month, int year) {
} }
break; break;
default: default:
throw new CMyDateException(CMyDateException::ErrUnexpected); throw CMyDateException(CMyDateException::ErrUnexpected);
break; break;
} }
} }
static bool IsValidYear(int year) { static bool IsValidYear(int year) {
if (year < 1970) if (year < 1970)
{ {
throw new CMyDateException(CMyDateException::ErrMinYear); throw CMyDateException(CMyDateException::ErrMinYear);
} }
return true; return true;
} }
static bool IsValidMonth(int month) { static bool IsValidMonth(int month) {
if (month < 1) if (month < 1)
{ {
throw new CMyDateException(CMyDateException::ErrMinMonth); throw CMyDateException(CMyDateException::ErrMinMonth);
} }
if (month > 12) if (month > 12)
{ {
throw new CMyDateException(CMyDateException::ErrMaxMonth); throw CMyDateException(CMyDateException::ErrMaxMonth);
} }
return true; return true;
} }
@ -182,11 +182,11 @@ static bool IsValidDay(int year, int month, int day) {
int monthday = GetMonthDayCount(month, year); int monthday = GetMonthDayCount(month, year);
if (day < 1) if (day < 1)
{ {
throw new CMyDateException(CMyDateException::ErrMinDay); throw CMyDateException(CMyDateException::ErrMinDay);
} }
if (day > monthday) if (day > monthday)
{ {
throw new CMyDateException(CMyDateException::ErrMaxDay); throw CMyDateException(CMyDateException::ErrMaxDay);
} }
return true; return true;
} }

View file

@ -21,10 +21,11 @@ static int CopyStr(char* dest, const char* src, const char* concat = "\0") {
CMyDateException::CMyDateException(const char* s) CMyDateException::CMyDateException(const char* s)
{ {
msg = new char[256];
CopyStr(msg, s); CopyStr(msg, s);
} }
const char* CMyDateException::what() { const char* CMyDateException::what() const {
return msg; return msg;
} }

View file

@ -3,7 +3,7 @@
class CMyDateException { class CMyDateException {
public: public:
CMyDateException(const char* s); CMyDateException(const char* s);
const char* what(void); const char* what(void) const;
static constexpr const char* ErrMinDay = "Hiba: nap kisebb, mint 1!"; static constexpr const char* ErrMinDay = "Hiba: nap kisebb, mint 1!";
static constexpr const char* ErrMaxDay = "Hiba: nap nagyobb, mint az adott honap napszama!"; static constexpr const char* ErrMaxDay = "Hiba: nap nagyobb, mint az adott honap napszama!";
static constexpr const char* ErrMinMonth = "Hiba: honap kisebb, mint 1!"; static constexpr const char* ErrMinMonth = "Hiba: honap kisebb, mint 1!";

View file

@ -1,95 +1,19 @@
//Tóth Dániel Mátyás | IFPQYY | V1 //Tóth Dániel Mátyás | IFPQYY | V1
#include <exception>
#include <iostream>
#include "cmydateexception.h"
#include "cmydate.h" #include "cmydate.h"
using namespace std; using namespace std;
/*#define TIMESTAMP_YEAR 1970
static bool IsLeapYear(int year) {
if (year % 4 == 0 && (year % 100 != 0 || year % 400 == 0))
{
return true;
}
return false;
}
static int GetMonthDayCount(int month, int year) {
switch (month)
{
case 1:
case 3:
case 5:
case 7:
case 8:
case 10:
case 12:
return 31;
break;
case 4:
case 6:
case 9:
case 11:
return 30;
break;
case 2:
if (IsLeapYear(year))
{
return 29;
}
else
{
return 28;
}
break;
default:
throw new CMyDateException(CMyDateException::ErrUnexpected);
break;
}
}
static int DayOfTheYearArgs(int year, int month, int day) {
for (int i = 1; i < month; i++)
{
day += GetMonthDayCount(i, year);
}
return day;
}
static int DateToTimeStamp(int year, int month, int day) { //counting from 1970-01-01
int y = year - 1;
int e = TIMESTAMP_YEAR - 1;
int days = 365 * (year - TIMESTAMP_YEAR)
+ (y / 4 - e / 4)
- (y / 100 - e / 100)
+ (y / 400 - e / 400);
return days + DayOfTheYearArgs(year, month, day - 1);
}
static void TimeStampToDate(int timestamp, int* year, int* month, int* day) {
*year = TIMESTAMP_YEAR;
while (timestamp < 0)
{
(*year)--;
timestamp += IsLeapYear(*year) ? 366 : 365;
}
while (timestamp >= (IsLeapYear(*year) ? 366 : 365))
{
timestamp -= IsLeapYear(*year) ? 366 : 365;
(*year)++;
}
*month = 1;
int monthDay = GetMonthDayCount(*month, *year);
while (timestamp >= monthDay)
{
timestamp -= monthDay;
(*month)++;
monthDay = GetMonthDayCount(*month, *year);
}
*day = timestamp + 1;
}*/
int main() { int main() {
/*int t = DateToTimeStamp(1976, 10, 23); CMyDate date;
cout << t << "\n"; try
int y = 0, m = 0, d = 0; {
TimeStampToDate(t, &y, &m, &d); date.IsValid(1969, 0, 0);
cout << y << ". " << m << ". " << d << ".";*/ }
catch (const CMyDateException& e)
{
cout << e.what();
}
return 0; return 0;
} }