Website Summary Files Commits Branches @commit @diff @stats
diff options
context:
space:
mode:
Commit519235af82e58a8437e7c6185a0911721e021b42 (patch)
Author
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-09 08:53:03 +0300
Committer
 Syndamia <kamen.d.mladenov@protonmail.com>
2022-05-09 08:53:03 +0300
Files24c61a8e7923ea26e82df70b08ed1a2f204b33f2
FMI-OOP-P1_TicketSystem-519235af82e58a8437e7c6185a0911721e021b42.tar
FMI-OOP-P1_TicketSystem-519235af82e58a8437e7c6185a0911721e021b42.tar.gz
FMI-OOP-P1_TicketSystem-519235af82e58a8437e7c6185a0911721e021b42.zip
Parentd59fb3f15f907649c2ef0cb6a9e2ff31e33f58af (diff)
Updated Date tests and fixed get_year
3 files changed, 198 insertions, 102 deletions
-rw-r--r--src/Generic/Date/Date.cpp4
-rw-r--r--tests/Test_DateTime.cpp101
-rw-r--r--tests/Tests_Date.cpp195
diff --git a/src/Generic/Date/Date.cpp b/src/Generic/Date/Date.cpp
index 17228f9..7c8f4d9 100644
--- a/src/Generic/Date/Date.cpp
+++ b/src/Generic/Date/Date.cpp
@@ -90,9 +90,11 @@ unsigned short Date::get_year() const {
/*! \return Whether year was saved or not
*
- * Year is always saved.
+ * Year isn't set when newYear is zero
*/
bool Date::set_year(unsigned short newYear) {
+ if (newYear == 0)
+ return false;
year = newYear;
return true;
}
diff --git a/tests/Test_DateTime.cpp b/tests/Test_DateTime.cpp
deleted file mode 100644
index 0bbca14..0000000
--- a/tests/Test_DateTime.cpp
+++ /dev/null
@@ -1,101 +0,0 @@
-#include <stdexcept>
-#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
-#include "../doctest/doctest/doctest.h"
-#include "../src/DateTime/DateTime.h"
-
-TEST_SUITE_BEGIN("ClassDateTime");
-
-const unsigned short h = 5, m = 12, d = 19, M = 4, y = 2022; // valid values
-
-TEST_SUITE("Constructors") {
- TEST_CASE("Ctor_SavesValues_WhenGivenValidValues") {
- DateTime dt(h, m, d, M, y);
-
- CHECK_EQ(dt.get_hour(), h);
- CHECK_EQ(dt.get_minute(), m);
- CHECK_EQ(dt.get_day(), d);
- CHECK_EQ(dt.get_month(), M);
- CHECK_EQ(dt.get_year(), y);
- }
- TEST_CASE("Ctor_ThrowsInvalidArgumentException_WhenGivenInvalidValues") {
- unsigned short nh = h, nm = m, nd = d, nM = M, ny = y;
- SUBCASE("HourIsTooBig") { nh = 194; }
- SUBCASE("MinutesAreTooBig") { nm = 7032; }
- SUBCASE("DayIsZero") { nd = 0; }
- SUBCASE("DayIsTooBig") { nd = 5721; }
- SUBCASE("MonthIsZero") { nM = 0; }
- SUBCASE("MonthIsTooBig") { nM = 15661; }
-
- CHECK_THROWS_AS(DateTime dt(nh, nm, nd, nM, ny), std::invalid_argument);
- }
-}
-
-TEST_SUITE("GettersAndSetters") {
- TEST_CASE("HourGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
- DateTime dt(h, m, d, M, y);
-
- CHECK_EQ(dt.get_hour(), h);
- }
- TEST_CASE("HourSetter_ReturnTrue_WhenGivenCorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nh = h;
- SUBCASE("SingleDigits") { nh = 2; }
- SUBCASE("DoubleDigits") { nh = 10; }
- SUBCASE("24hour") { nh = 17; }
- SUBCASE("Zero") { nh = 0; }
-
- CHECK_UNARY(dt.set_hour(nh));
- }
- TEST_CASE("HourSetter_SetsValue_WhenGivenCorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nh = h;
- SUBCASE("SingleDigits") { nh = 2; }
- SUBCASE("DoubleDigits") { nh = 10; }
- SUBCASE("24hour") { nh = 17; }
- SUBCASE("Zero") { nh = 0; }
- dt.set_hour(nh);
-
- CHECK_EQ(dt.get_hour(), nh);
- }
- TEST_CASE("HourSetter_ReturnsFalse_WhenGivenIncorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nh = h;
- SUBCASE("TooBig") { nh = 891; }
-
- CHECK_UNARY_FALSE(dt.set_hour(nh));
- }
-
- TEST_CASE("MinuteGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
- DateTime dt(h, m, d, M, y);
-
- CHECK_EQ(dt.get_minute(), h);
- }
- TEST_CASE("MinuteSetter_ReturnTrue_WhenGivenCorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nm = m;
- SUBCASE("SingleDigits") { nm = 2; }
- SUBCASE("DoubleDigits") { nm = 52; }
- SUBCASE("Zero") { nm = 0; }
-
- CHECK_UNARY(dt.set_minute(nm));
- }
- TEST_CASE("MinuteSetter_SetsValue_WhenGivenCorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nm = m;
- SUBCASE("SingleDigits") { nm = 2; }
- SUBCASE("DoubleDigits") { nm = 42; }
- SUBCASE("Zero") { nm = 0; }
- dt.set_minute(nm);
-
- CHECK_EQ(dt.get_minute(), nm);
- }
- TEST_CASE("MinuteSetter_ReturnsFalse_WhenGivenIncorrectValue") {
- DateTime dt(h, m, d, M, y);
- unsigned nm = m;
- SUBCASE("TooBig") { nm = 847; }
-
- CHECK_UNARY_FALSE(dt.set_minute(nm));
- }
-}
-
-TEST_SUITE_END();
diff --git a/tests/Tests_Date.cpp b/tests/Tests_Date.cpp
new file mode 100644
index 0000000..77e381d
--- /dev/null
+++ b/tests/Tests_Date.cpp
@@ -0,0 +1,195 @@
+#include <cstring>
+#define DOCTEST_CONFIG_IMPLEMENT_WITH_MAIN
+#include "../doctest/doctest/doctest.h"
+#include "../src/Generic/Date/Date.h"
+
+TEST_SUITE_BEGIN("ClassDate");
+
+const unsigned short d = 19, m = 4, y = 2022; // valid values
+
+TEST_SUITE("Constructors") {
+ TEST_CASE("DefaultCtor_SetsDateAsFirstOfMay2022_WhenCalled") {
+ Date dt;
+
+ CHECK_EQ(dt.get_day(), 1);
+ CHECK_EQ(dt.get_month(), 5);
+ CHECK_EQ(dt.get_year(), 2022);
+ }
+ TEST_CASE("StrCtor_ParsesValues_WhenGivenCorrectString") {
+ char str[] = "9 12 2021";
+ Date dt(str);
+
+ CHECK_EQ(dt.get_day(), 9);
+ CHECK_EQ(dt.get_month(), 12);
+ CHECK_EQ(dt. get_year(), 2021);
+ }
+ TEST_CASE("StrCtor_StoresZeroes_WhenGivenBadString") {
+ char str[] = "45 500 ABCD";
+ Date dt(str);
+
+ CHECK_EQ(dt.get_day(), 0);
+ CHECK_EQ(dt.get_month(), 0);
+ CHECK_EQ(dt.get_year(), 0);
+ }
+ TEST_CASE("StrCtor_StoresZeroes_WhenGivenBlankString") {
+ char str[] = "";
+ Date dt(str);
+
+ CHECK_EQ(dt.get_day(), 0);
+ CHECK_EQ(dt.get_month(), 0);
+ CHECK_EQ(dt.get_year(), 0);
+ }
+ TEST_CASE("Ctor_SavesValues_WhenGivenValidValues") {
+ Date dt(d, m, y);
+
+ CHECK_EQ(dt.get_day(), d);
+ CHECK_EQ(dt.get_month(), m);
+ CHECK_EQ(dt.get_year(), y);
+ }
+ TEST_CASE("Ctor_StoresZeroes_WhenGivenInvalidValues") {
+ unsigned short nd = 0, nm = 0, ny = 0;
+ SUBCASE("DayIsZero") { nd = 0; }
+ SUBCASE("DayIsTooBig") { nd = 5721; }
+ SUBCASE("MonthIsZero") { nm = 0; }
+ SUBCASE("MonthIsTooBig") { nm = 15661; }
+ SUBCASE("YearIsZero") { ny = 0; }
+
+ Date dt(nd, nm, ny);
+ CHECK_EQ(dt.get_day(), 0);
+ CHECK_EQ(dt.get_month(), 0);
+ CHECK_EQ(dt.get_year(), 0);
+ }
+}
+
+TEST_SUITE("GettersAndSetters") {
+ TEST_CASE("DayGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
+ Date dt(d, m, y);
+
+ CHECK_EQ(dt.get_day(), d);
+ }
+ TEST_CASE("DaySetter_ReturnsTrue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned nd = d;
+ SUBCASE("SingleDigits") { nd = 2; }
+ SUBCASE("DoubleDigits") { nd = 26; }
+
+ CHECK_UNARY(dt.set_day(nd));
+ }
+ TEST_CASE("DaySetter_SetsValue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned nd = d;
+ SUBCASE("SingleDigits") { nd = 9; }
+ SUBCASE("DoubleDigits") { nd = 13; }
+ dt.set_day(nd);
+
+ CHECK_EQ(dt.get_day(), nd);
+ }
+ TEST_CASE("DaySetter_ReturnsFalse_WhenGivenIncorrectValue") {
+ Date dt(d, m, y);
+ unsigned nd = d;
+ SUBCASE("Zero") { nd = 0; }
+ SUBCASE("TooBig") { nd = 847; }
+
+ CHECK_UNARY_FALSE(dt.set_day(nd));
+ }
+
+ TEST_CASE("MonthGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
+ Date dt(d, m, y);
+
+ CHECK_EQ(dt.get_month(), m);
+ }
+ TEST_CASE("MonthSetter_ReturnsTrue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned nm = m;
+ SUBCASE("SingleDigits") { nm = 2; }
+ SUBCASE("DoubleDigits") { nm = 11; }
+
+ CHECK_UNARY(dt.set_month(nm));
+ }
+ TEST_CASE("MonthSetter_SetsValue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned nm = m;
+ SUBCASE("SingleDigits") { nm = 9; }
+ SUBCASE("DoubleDigits") { nm = 10; }
+ dt.set_month(nm);
+
+ CHECK_EQ(dt.get_month(), nm);
+ }
+ TEST_CASE("MonthSetter_ReturnsFalse_WhenGivenIncorrectValue") {
+ Date dt(d, m, y);
+ unsigned nm = m;
+ SUBCASE("Zero") { nm = 0; }
+ SUBCASE("TooBig") { nm = 13; }
+
+ CHECK_UNARY_FALSE(dt.set_month(nm));
+ }
+
+ TEST_CASE("YearGetter_ReturnsCorrectValue_WhenCreatedWithValidValues") {
+ Date dt(d, m, y);
+
+ CHECK_EQ(dt.get_year(), y);
+ }
+ TEST_CASE("YearSetter_ReturnsTrue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned ny = y;
+ SUBCASE("SingleDigits") { ny = 2; }
+ SUBCASE("DoubleDigits") { ny = 58; }
+ SUBCASE("TripleDigits") { ny = 681; }
+ SUBCASE("QuadrupleDigits") { ny = 1878; }
+
+ CHECK_UNARY(dt.set_year(ny));
+ }
+ TEST_CASE("YearSetter_SetsValue_WhenGivenCorrectValue") {
+ Date dt(d, m, y);
+ unsigned ny = y;
+ SUBCASE("SingleDigits") { ny = 2; }
+ SUBCASE("DoubleDigits") { ny = 58; }
+ SUBCASE("TripleDigits") { ny = 681; }
+ SUBCASE("QuadrupleDigits") { ny = 1878; }
+ dt.set_year(ny);
+
+ CHECK_EQ(dt.get_year(), ny);
+ }
+ TEST_CASE("YearSetter_ReturnsFalse_WhenGivenIncorrectValue") {
+ Date dt(d, m, y);
+ unsigned ny = y;
+ SUBCASE("Zero") { ny = 0; }
+
+ CHECK_UNARY_FALSE(dt.set_year(ny));
+ }
+}
+
+TEST_SUITE("createString()") {
+ TEST_CASE("createString_CreatesCorrectString_WhenCalled") {
+ Date dt(8, 10, 2019);
+ String output = dt.createString();
+
+ CHECK_EQ(strcmp(output.get_cstr(), "8.10.2019"), 0);
+ }
+}
+
+TEST_SUITE("compare()") {
+ TEST_CASE("compare_ReturnsLessThanZero_WhenFirstDateIsBeforeSecond") {
+ Date dt1(d, m, y), dt2(d, m, y);
+ SUBCASE("DifferenceByDay") { dt1.set_day(d - 1); }
+ SUBCASE("DifferenceByMonth") { dt1.set_month(m - 1); }
+ SUBCASE("DifferenceByYear") { dt1.set_year(y - 1); }
+
+ CHECK_LT(dt1.compare(dt2), 0);
+ }
+ TEST_CASE("compare_ReturnsMoreThanZero_WhenFirstDateIsAfterSecond") {
+ Date dt1(d, m, y), dt2(d, m, y);
+ SUBCASE("DifferenceByDay") { dt2.set_day(d - 1); }
+ SUBCASE("DifferenceByMonth") { dt2.set_month(m - 1); }
+ SUBCASE("DifferenceByYear") { dt2.set_year(y - 1); }
+
+ CHECK_GT(dt1.compare(dt2), 0);
+ }
+ TEST_CASE("compare_ReturnsZero_WhenDatesAreEqual") {
+ Date dt1(d, m, y), dt2(d, m, y);
+
+ CHECK_EQ(dt1.compare(dt2), 0);
+ }
+}
+
+TEST_SUITE_END();