From fa8bd24551d0e99a0efd44195417bf494497dd10 Mon Sep 17 00:00:00 2001 From: Damimani <41192030+Damimani@users.noreply.github.com> Date: Sat, 31 Oct 2026 10:05:41 +0100 Subject: [PATCH] unittest start --- .../14.51.36231/include/__msvc_ostream.hpp | 0 .../include/__msvc_string_view.hpp | 0 feladat4/UnitTest1/UnitTest1.cpp | 83 ++++++++++++++- feladat4/UnitTest1/UnitTest1.vcxproj | 4 +- feladat4/feladat4/cmydate.cpp | 12 +-- feladat4/feladat4/cmydateexception.cpp | 3 +- feladat4/feladat4/cmydateexception.h | 2 +- feladat4/feladat4/main.cpp | 100 +++--------------- 8 files changed, 105 insertions(+), 99 deletions(-) create mode 100644 Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_ostream.hpp create mode 100644 Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_string_view.hpp diff --git a/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_ostream.hpp b/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_ostream.hpp new file mode 100644 index 0000000..e69de29 diff --git a/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_string_view.hpp b/Program Files/Microsoft Visual Studio/18/Community/VC/Tools/MSVC/14.51.36231/include/__msvc_string_view.hpp new file mode 100644 index 0000000..e69de29 diff --git a/feladat4/UnitTest1/UnitTest1.cpp b/feladat4/UnitTest1/UnitTest1.cpp index bb39223..5b2a5bc 100644 --- a/feladat4/UnitTest1/UnitTest1.cpp +++ b/feladat4/UnitTest1/UnitTest1.cpp @@ -1,6 +1,7 @@ -//Tóth Dániel Mátyás | IFPQYY | V1 #include "pch.h" #include "CppUnitTest.h" +#include +#include using namespace Microsoft::VisualStudio::CppUnitTestFramework; @@ -10,8 +11,86 @@ namespace UnitTest1 { 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()); + } } }; } diff --git a/feladat4/UnitTest1/UnitTest1.vcxproj b/feladat4/UnitTest1/UnitTest1.vcxproj index d448e6d..9e6bbe8 100644 --- a/feladat4/UnitTest1/UnitTest1.vcxproj +++ b/feladat4/UnitTest1/UnitTest1.vcxproj @@ -77,6 +77,7 @@ true + C:\Users\danit\Desktop\2026-27-cpp-homework\feladat4\feladat4;$(IncludePath) true @@ -142,7 +143,8 @@ Windows - $(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + C:\Users\danit\Desktop\2026-27-cpp-homework\feladat4\feladat4\x64\Debug;$(VCInstallDir)UnitTest\lib;%(AdditionalLibraryDirectories) + cmydate;cmydateexception;%(AdditionalDependencies) diff --git a/feladat4/feladat4/cmydate.cpp b/feladat4/feladat4/cmydate.cpp index f1da396..37c8727 100644 --- a/feladat4/feladat4/cmydate.cpp +++ b/feladat4/feladat4/cmydate.cpp @@ -156,25 +156,25 @@ static int GetMonthDayCount(int month, int year) { } break; default: - throw new CMyDateException(CMyDateException::ErrUnexpected); + throw CMyDateException(CMyDateException::ErrUnexpected); break; } } static bool IsValidYear(int year) { if (year < 1970) { - throw new CMyDateException(CMyDateException::ErrMinYear); + throw CMyDateException(CMyDateException::ErrMinYear); } return true; } static bool IsValidMonth(int month) { if (month < 1) { - throw new CMyDateException(CMyDateException::ErrMinMonth); + throw CMyDateException(CMyDateException::ErrMinMonth); } if (month > 12) { - throw new CMyDateException(CMyDateException::ErrMaxMonth); + throw CMyDateException(CMyDateException::ErrMaxMonth); } return true; } @@ -182,11 +182,11 @@ static bool IsValidDay(int year, int month, int day) { int monthday = GetMonthDayCount(month, year); if (day < 1) { - throw new CMyDateException(CMyDateException::ErrMinDay); + throw CMyDateException(CMyDateException::ErrMinDay); } if (day > monthday) { - throw new CMyDateException(CMyDateException::ErrMaxDay); + throw CMyDateException(CMyDateException::ErrMaxDay); } return true; } diff --git a/feladat4/feladat4/cmydateexception.cpp b/feladat4/feladat4/cmydateexception.cpp index 75b06ff..c9f8874 100644 --- a/feladat4/feladat4/cmydateexception.cpp +++ b/feladat4/feladat4/cmydateexception.cpp @@ -21,10 +21,11 @@ static int CopyStr(char* dest, const char* src, const char* concat = "\0") { CMyDateException::CMyDateException(const char* s) { + msg = new char[256]; CopyStr(msg, s); } -const char* CMyDateException::what() { +const char* CMyDateException::what() const { return msg; } diff --git a/feladat4/feladat4/cmydateexception.h b/feladat4/feladat4/cmydateexception.h index 3e834f2..c32e3c8 100644 --- a/feladat4/feladat4/cmydateexception.h +++ b/feladat4/feladat4/cmydateexception.h @@ -3,7 +3,7 @@ class CMyDateException { public: 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* ErrMaxDay = "Hiba: nap nagyobb, mint az adott honap napszama!"; static constexpr const char* ErrMinMonth = "Hiba: honap kisebb, mint 1!"; diff --git a/feladat4/feladat4/main.cpp b/feladat4/feladat4/main.cpp index 9caaa8b..aea7482 100644 --- a/feladat4/feladat4/main.cpp +++ b/feladat4/feladat4/main.cpp @@ -1,95 +1,19 @@ //Tóth Dániel Mátyás | IFPQYY | V1 +#include +#include +#include "cmydateexception.h" #include "cmydate.h" 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 t = DateToTimeStamp(1976, 10, 23); - cout << t << "\n"; - int y = 0, m = 0, d = 0; - TimeStampToDate(t, &y, &m, &d); - cout << y << ". " << m << ". " << d << ".";*/ + CMyDate date; + try + { + date.IsValid(1969, 0, 0); + } + catch (const CMyDateException& e) + { + cout << e.what(); + } return 0; } \ No newline at end of file