feladat4v2

This commit is contained in:
Damimani 2026-11-01 18:30:58 +01:00
parent fa8bd24551
commit 41988f0df6
8 changed files with 507 additions and 113 deletions

View file

@ -1,9 +1,12 @@
//Tóth Dániel Mátyás | IFPQYY | V2
#include "pch.h"
#include "CppUnitTest.h"
#include <iostream>
#include <cmydate.h>
#include <cmydateexception.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
namespace UnitTest1
{
@ -11,86 +14,231 @@ namespace UnitTest1
{
public:
TEST_METHOD(ValidDate)
TEST_METHOD(ValidDates)
{
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));
int dates[][3] = {
{ 1980, 4, 23 },
{ 1970, 1, 31 },
{ 1970, 9, 30 },
{ 1970, 2, 28 },
{ 1972, 2, 29 },
{ 2000, 2, 29 },
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
Assert::AreEqual(true, date.IsValid(dates[i][0], dates[i][1], dates[i][2]));
}
}
TEST_METHOD(InvalidDates)
{
CMyDate date;
try
int dates[][3] = {
{ 1969, 1, 1 },
{ 1970, 0, 1 },
{ 1970, 13, 1 },
{ 1970, 1, 0 },
{ 1970, 1, 32 },
{ 1970, 9, 31 },
{ 1970, 2, 29 },
{ 2100, 2, 29 },
};
const char* errors[] = {
CMyDateException::ErrMinYear,
CMyDateException::ErrMinMonth,
CMyDateException::ErrMaxMonth,
CMyDateException::ErrMinDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
date.IsValid(1969, 1, 1);
Assert::IsTrue(false);
Assert::IsFalse(date.IsValid(dates[i][0], dates[i][1], dates[i][2]));
}
catch (const CMyDateException &e)
}
TEST_METHOD(DayOfTheYearTests)
{
CMyDate date;
int dates[][3] = {
{ 1970, 1, 1 },
{ 1970, 2, 3 },
{ 1970, 3, 1 }, //non-leap year
{ 1972, 3, 1 }, //leap year
};
int counts[] = {
1,
34,
60,
61,
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
Assert::AreEqual(CMyDateException::ErrMinYear, e.what());
date = CMyDate(dates[i][0], dates[i][1], dates[i][2]);
Assert::AreEqual(counts[i], date.DayOfTheYear());
}
try
}
TEST_METHOD(AddDayTests)
{
CMyDate date1;
CMyDate date2;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1970, 2, 1 },
{ 1972, 2, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 2 },
{ 1970, 1, 1 },
};
int dates2[][3] = { //expected
{ 1970, 1, 2 },
{ 1970, 2, 1 },
{ 1970, 3, 1 },
{ 1972, 2, 29 },
{ 1971, 1, 1 },
{ 1972, 12, 31 },
{ 1970, 1, 1 },
{ 1970, 1, 1 },
};
int counts[] = {
1,
31,
28,
28,
365,
365,
-1, //technically allowed
- 1 //since SubDays calls AddDays, this counts as its test
};
int testcount = sizeof(dates1) / 12;
for (size_t i = 0; i < testcount; i++)
{
date = CMyDate(1970, 0, 1);
try
{
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
date1.AddDays(counts[i]);
Assert::AreEqual(date2.GetYear(), date1.GetYear());
Assert::AreEqual(date2.GetMonth(), date1.GetMonth());
Assert::AreEqual(date2.GetDay(), date1.GetDay());
}
catch (const CMyDateException& e)
{
Assert::AreEqual(CMyDateException::ErrMinYear, e.what()); //should only happen in the last case
}
catch (...) {
Assert::IsTrue(false);
}
}
catch (CMyDateException e)
}
TEST_METHOD(DifferenceInDaysTests)
{
CMyDate date1;
CMyDate date2;
int diff = 0;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 1 },
};
int dates2[][3] = {
{ 1970, 1, 1 },
{ 1970, 3, 1 },
{ 1972, 3, 1 },
{ 1971, 1, 1 },
{ 1973, 1, 1 },
{ 1974, 1, 1 },
};
int counts[] = {
0,
-59,
-60,
-365,
-366,
-1461
};
int testcount = sizeof(dates1)/12;
for (size_t i = 0; i < testcount; i++)
{
Assert::AreEqual(CMyDateException::ErrMinMonth, e.what());
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
diff = date1.DifferenceInDays(date2);
Assert::AreEqual(counts[i], diff);
diff = date2.DifferenceInDays(date1); //should be different in reverse ig
Assert::AreEqual(counts[i] * (-1), diff);
}
try
}
TEST_METHOD(AddMonthTests)
{
CMyDate date1;
CMyDate date2;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1970, 1, 10 },
{ 1970, 12, 1 },
{ 1970, 1, 30 },
{ 1970, 1, 1 },
};
int dates2[][3] = {
{ 1970, 2, 1 },
{ 1970, 3, 1 },
{ 1970, 2, 10 },
{ 1971, 1, 1 },
{ 1970, 1, 30 },
{ 1971, 1, 1 },
};
int counts[] = {
1,
2,
1,
1,
1,
12,
};
int testcount = sizeof(dates1) / 12;
for (size_t i = 0; i < testcount; i++)
{
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());
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
try
{
date1.AddMonths(counts[i]);
}
catch (const CMyDateException& e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
Assert::AreEqual(date2.GetYear(), date1.GetYear());
Assert::AreEqual(date2.GetMonth(), date1.GetMonth());
Assert::AreEqual(date2.GetDay(), date1.GetDay());
}
}
TEST_METHOD(DisplayTests) {
CMyDate date;
stringstream buff;
streambuf* prevcoutbuff = cout.rdbuf(buff.rdbuf());
date = CMyDate(1970, 1, 1);
date.Display();
string d = buff.str();
Assert::IsTrue(d == "1970 januar 1.");
}
};
}

Binary file not shown.

View file

@ -0,0 +1,244 @@
//Tóth Dániel Mátyás | IFPQYY | V2
#include "pch.h"
#include "CppUnitTest.h"
#include <iostream>
#include <cmydate.h>
#include <cmydateexception.h>
using namespace Microsoft::VisualStudio::CppUnitTestFramework;
using namespace std;
namespace UnitTest1
{
TEST_CLASS(UnitTest1)
{
public:
TEST_METHOD(ValidDates)
{
CMyDate date;
int dates[][3] = {
{ 1980, 4, 23 },
{ 1970, 1, 31 },
{ 1970, 9, 30 },
{ 1970, 2, 28 },
{ 1972, 2, 29 },
{ 2000, 2, 29 },
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
Assert::AreEqual(true, date.IsValid(dates[i][0], dates[i][1], dates[i][2]));
}
}
TEST_METHOD(InvalidDates)
{
CMyDate date;
int dates[][3] = {
{ 1969, 1, 1 },
{ 1970, 0, 1 },
{ 1970, 13, 1 },
{ 1970, 1, 0 },
{ 1970, 1, 32 },
{ 1970, 9, 31 },
{ 1970, 2, 29 },
{ 2100, 2, 29 },
};
const char* errors[] = {
CMyDateException::ErrMinYear,
CMyDateException::ErrMinMonth,
CMyDateException::ErrMaxMonth,
CMyDateException::ErrMinDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
CMyDateException::ErrMaxDay,
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
Assert::IsFalse(date.IsValid(dates[i][0], dates[i][1], dates[i][2]));
}
}
TEST_METHOD(DayOfTheYearTests)
{
CMyDate date;
int dates[][3] = {
{ 1970, 1, 1 },
{ 1970, 2, 3 },
{ 1970, 3, 1 }, //non-leap year
{ 1972, 3, 1 }, //leap year
};
int counts[] = {
1,
34,
60,
61,
};
int testcount = sizeof(dates) / 12;
for (size_t i = 0; i < testcount; i++)
{
date = CMyDate(dates[i][0], dates[i][1], dates[i][2]);
Assert::AreEqual(counts[i], date.DayOfTheYear());
}
}
TEST_METHOD(AddDayTests)
{
CMyDate date1;
CMyDate date2;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1970, 2, 1 },
{ 1972, 2, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 2 },
{ 1970, 1, 1 },
};
int dates2[][3] = { //expected
{ 1970, 1, 2 },
{ 1970, 2, 1 },
{ 1970, 3, 1 },
{ 1972, 2, 29 },
{ 1971, 1, 1 },
{ 1972, 12, 31 },
{ 1970, 1, 1 },
{ 1970, 1, 1 },
};
int counts[] = {
1,
31,
28,
28,
365,
365,
-1, //technically allowed
- 1 //since SubDays calls AddDays, this counts as its test
};
int testcount = sizeof(dates1) / 12;
for (size_t i = 0; i < testcount; i++)
{
try
{
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
date1.AddDays(counts[i]);
Assert::AreEqual(date2.GetYear(), date1.GetYear());
Assert::AreEqual(date2.GetMonth(), date1.GetMonth());
Assert::AreEqual(date2.GetDay(), date1.GetDay());
}
catch (const CMyDateException& e)
{
Assert::AreEqual(CMyDateException::ErrMinYear, e.what()); //should only happen in the last case
}
catch (...) {
Assert::IsTrue(false);
}
}
}
TEST_METHOD(DifferenceInDaysTests)
{
CMyDate date1;
CMyDate date2;
int diff = 0;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 1 },
{ 1972, 1, 1 },
{ 1970, 1, 1 },
};
int dates2[][3] = {
{ 1970, 1, 1 },
{ 1970, 3, 1 },
{ 1972, 3, 1 },
{ 1971, 1, 1 },
{ 1973, 1, 1 },
{ 1974, 1, 1 },
};
int counts[] = {
0,
-59,
-60,
-365,
-366,
-1461
};
int testcount = sizeof(dates1)/12;
for (size_t i = 0; i < testcount; i++)
{
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
diff = date1.DifferenceInDays(date2);
Assert::AreEqual(counts[i], diff);
diff = date2.DifferenceInDays(date1); //should be different in reverse ig
Assert::AreEqual(counts[i] * (-1), diff);
}
}
TEST_METHOD(AddMonthTests)
{
CMyDate date1;
CMyDate date2;
int dates1[][3] = {
{ 1970, 1, 1 },
{ 1970, 1, 1 },
{ 1970, 1, 10 },
{ 1970, 12, 1 },
{ 1970, 1, 30 },
{ 1970, 1, 1 },
};
int dates2[][3] = {
{ 1970, 2, 1 },
{ 1970, 3, 1 },
{ 1970, 2, 10 },
{ 1971, 1, 1 },
{ 1970, 1, 30 },
{ 1971, 1, 1 },
};
int counts[] = {
1,
2,
1,
1,
1,
12,
};
int testcount = sizeof(dates1) / 12;
for (size_t i = 0; i < testcount; i++)
{
CMyDate date1 = CMyDate(dates1[i][0], dates1[i][1], dates1[i][2]);
CMyDate date2 = CMyDate(dates2[i][0], dates2[i][1], dates2[i][2]);
try
{
date1.AddMonths(counts[i]);
}
catch (const CMyDateException& e)
{
Assert::AreEqual(CMyDateException::ErrMaxDay, e.what());
}
Assert::AreEqual(date2.GetYear(), date1.GetYear());
Assert::AreEqual(date2.GetMonth(), date1.GetMonth());
Assert::AreEqual(date2.GetDay(), date1.GetDay());
}
}
TEST_METHOD(DisplayTests) {
CMyDate date;
stringstream buff;
streambuf* prevcoutbuff = cout.rdbuf(buff.rdbuf());
date = CMyDate(1970, 1, 1);
date.Display();
string d = buff.str();
Assert::IsTrue(d == "1970 januar 1.");
}
};
}

View file

@ -1,4 +1,4 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V2
#include <iostream>
#include "cmydateexception.h"
#include "cmydate.h"
@ -6,18 +6,18 @@
using namespace std;
static char monthnames[12][11] = {
"Január",
"Február",
"Március",
"Április",
"Május",
"Június",
"Július",
"Augusztus",
"Szeptember",
"Október",
"November",
"December"
"januar",
"februar",
"március",
"aprilis",
"majus",
"junius",
"julius",
"augusztus",
"szeptember",
"oktober",
"november",
"december"
};
static bool IsLeapYear(int year);
@ -30,14 +30,15 @@ static void TimeStampToDate(int timestamp, int* year, int* month, int* day);
static int DayOfTheYearArgs(int year, int month, int day);
CMyDate::CMyDate() {
SetYear(1970);
SetMonth(1);
SetDay(1);
m_iYear = 1970;
m_iMonth = 1;
m_iDay = 1;
}
CMyDate::CMyDate(int year, int month, int day) {
SetYear(year);
SetMonth(month);
SetDay(day);
IsValid(year, month, day);
m_iYear = year;
m_iMonth = month;
m_iDay = day;
}
CMyDate::~CMyDate(){
//hey jimmy, get me a destructor with nothing
@ -49,19 +50,32 @@ int CMyDate::DayOfTheYear() const {
return DayOfTheYearArgs(GetYear(), GetMonth(), GetDay());
}
bool CMyDate::IsValid(int year, int month, int day) const {
IsValidYear(year);
IsValidMonth(month);
IsValidDay(year, month, day);
try
{
IsValidYear(year);
IsValidMonth(month);
IsValidDay(year, month, day);
}
catch (const CMyDateException&)
{
return false;
}
return true;
}
void CMyDate::AddDays(int days) {
if (days == 0)
{
return;
}
int timestamp = DateToTimeStamp(GetYear(), GetMonth(), GetDay());
int y = 0, m = 0, d = 0;
TimeStampToDate(timestamp + days, &y, &m, &d);
IsValid(y, m, d);
m_iYear = y;
m_iMonth = m;
m_iDay = d;
if (IsValid(y, m, d))
{
m_iYear = y;
m_iMonth = m;
m_iDay = d;
}
}
void CMyDate::SubDays(int days) {
AddDays(days * (-1));
@ -70,26 +84,14 @@ int CMyDate::DifferenceInDays(const CMyDate& other) const {
int thistmstmp = DateToTimeStamp(GetYear(), GetMonth(), GetDay());
int othertmstmp = DateToTimeStamp(other.GetYear(), other.GetMonth(), other.GetDay());
int result = thistmstmp - othertmstmp;
return result < 0 ? result * (-1) : result;
return result;
}
void CMyDate::AddMonths(int months) {
m_iYear += (m_iMonth - 1 + months) / 12;
m_iMonth += ((m_iMonth - 1 + months) % 12) + 1;
try
{
IsValidDay(m_iYear, m_iMonth, m_iDay);
}
catch (CMyDateException e)
{
if (e.what() == CMyDateException::ErrMaxDay)
{
SetDay(GetMonthDayCount(m_iMonth, m_iYear));
}
else
{
throw e;
}
}
int y = m_iYear + (m_iMonth - 1 + months) / 12;
int m = ((m_iMonth - 1 + months) % 12) + 1;
IsValidDay(y, m, m_iDay);
m_iYear = y;
m_iMonth = m;
}
int CMyDate::GetDay() const {
return m_iDay;
@ -107,13 +109,13 @@ void CMyDate::SetDay(int day) {
}
}
void CMyDate::SetMonth(int month) {
if (IsValidMonth(month))
if (IsValidMonth(month) && IsValidDay(m_iYear, month, m_iDay))
{
m_iMonth = month;
}
}
void CMyDate::SetYear(int year) {
if (IsValidYear(year))
if (IsValidYear(year) && IsValidDay(year, m_iMonth, m_iDay))
{
m_iYear = year;
}

View file

@ -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 CMyDate {
public:

View file

@ -1,4 +1,4 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V2
#include "cmydateexception.h"
static int CopyStr(char* dest, const char* src, const char* concat = "\0") {

View file

@ -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 CMyDateException {
public:

View file

@ -1,4 +1,4 @@
//Tóth Dániel Mátyás | IFPQYY | V1
//Tóth Dániel Mátyás | IFPQYY | V2
#include <exception>
#include <iostream>
#include "cmydateexception.h"