From d4957eff5d161e41f0e33d0e93fbed5eb4deb674 Mon Sep 17 00:00:00 2001 From: Steve Brokenshire Date: Sun, 19 Jun 2016 10:55:47 +0100 Subject: [PATCH] Added code and unit tests for the PreviousMonthNumberofDays function --- source/common/monthviewgen.cpp | 72 +++++++++++++++++++ source/common/monthviewgen.h | 28 ++++++++ source/tests/xestiacalendar_commonfunctions.h | 61 ++++++++++++++++ 3 files changed, 161 insertions(+) create mode 100644 source/common/monthviewgen.cpp create mode 100644 source/common/monthviewgen.h diff --git a/source/common/monthviewgen.cpp b/source/common/monthviewgen.cpp new file mode 100644 index 0000000..a80adb4 --- /dev/null +++ b/source/common/monthviewgen.cpp @@ -0,0 +1,72 @@ +// monthviewgen.cpp - Xestia Calendar month view generation functions. +// +// (c) 2016 Xestia Software Development. +// +// This file is part of Xestia Calendar. +// +// Xestia Address Book is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by the +// Free Software Foundation, version 3 of the license. +// +// Xestia Address Book is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with Xestia Calendar. If not, see + +#include "monthviewgen.h" + +using namespace std; + +int PreviousMonthNumberofDays(int Month, int Year){ + + // Work out what the previous month is. + + // Return 31 if month matches. + + if (Month == 1 || Month == 2 || Month == 4 || + Month == 6 || Month == 8 || Month == 9 || Month == 11){ + + return 31; + + } + + // Return 30 if month matches. + + if (Month == 5 || Month == 7 || Month == 10 || Month == 12){ + + return 30; + + } + + // Work out if year is a leap year and return 28 or 29 + // for february. + + if (Year % 4 == 0){ + + if (Year % 100 == 0){ + + if (Year % 400 == 0){ + + return 29; + + } else { + + return 28; + + } + + } + + return 29; + + } else { + + return 28; + + } + +} + diff --git a/source/common/monthviewgen.h b/source/common/monthviewgen.h new file mode 100644 index 0000000..e711167 --- /dev/null +++ b/source/common/monthviewgen.h @@ -0,0 +1,28 @@ +// monthviewgen.h - Xestia Calendar month view generation functions header. +// +// (c) 2016 Xestia Software Development. +// +// This file is part of Xestia Calendar. +// +// Xestia Address Book is free software: you can redistribute it and/or modify +// it under the terms of the GNU General Public License as published by the +// Free Software Foundation, version 3 of the license. +// +// Xestia Address Book is distributed in the hope that it will be useful, +// but WITHOUT ANY WARRANTY; without even the implied warranty of +// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the +// GNU General Public License for more details. +// +// You should have received a copy of the GNU General Public License along +// with Xestia Calendar. If not, see + +#ifndef __COMMON_MONTHVIEWGEN_H__ +#define __COMMON_MONTHVIEWGEN_H__ + +#include +#include +#include + +int PreviousMonthNumberofDays(int Month, int Year); + +#endif \ No newline at end of file diff --git a/source/tests/xestiacalendar_commonfunctions.h b/source/tests/xestiacalendar_commonfunctions.h index 8da4adc..225d0a4 100644 --- a/source/tests/xestiacalendar_commonfunctions.h +++ b/source/tests/xestiacalendar_commonfunctions.h @@ -19,6 +19,7 @@ #include "../common/file.h" #include "../common/text.h" #include "../common/colour.h" +#include "../common/monthviewgen.h" TEST(CommonFunctions, FileTests){ @@ -401,4 +402,64 @@ TEST(CommonFunctions, OutputText){ ASSERT_EQ(ResultText4, OutputText4); ASSERT_EQ(ResultText5, OutputText5); +} + +TEST(CommonFunctions, PreviousMonthNumberofDays){ + + // Test the year 2015 (does not have a leap year). + + ASSERT_EQ(30, PreviousMonthNumberofDays(12, 2015)); + + // Test the year 2016 (has a leap year). + + ASSERT_EQ(31, PreviousMonthNumberofDays(1, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(2, 2016)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(4, 2016)); + ASSERT_EQ(30, PreviousMonthNumberofDays(5, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(6, 2016)); + ASSERT_EQ(30, PreviousMonthNumberofDays(7, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(8, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(9, 2016)); + ASSERT_EQ(30, PreviousMonthNumberofDays(10, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(11, 2016)); + ASSERT_EQ(30, PreviousMonthNumberofDays(12, 2016)); + ASSERT_EQ(31, PreviousMonthNumberofDays(1, 2017)); + + // Test the leap year section. + + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2016)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2012)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2008)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2004)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2000)); + + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2015)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2014)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2013)); + + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2041)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2042)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2043)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2044)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2045)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2046)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2047)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2048)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2049)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2050)); + + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 1900)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2200)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2300)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2400)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2500)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2600)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2700)); + ASSERT_EQ(29, PreviousMonthNumberofDays(3, 2800)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 2900)); + ASSERT_EQ(28, PreviousMonthNumberofDays(3, 3000)); + +} + } \ No newline at end of file -- 2.39.2