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.");
}
};
}