Home | News | Projects | Releases
Bugs | RFE | Repositories | Help
Added code and unit tests for the PreviousMonthNumberofDays function
authorSteve Brokenshire <sbrokenshire@xestia.co.uk>
Sun, 19 Jun 2016 09:55:47 +0000 (10:55 +0100)
committerSteve Brokenshire <sbrokenshire@xestia.co.uk>
Sun, 19 Jun 2016 09:55:47 +0000 (10:55 +0100)
source/common/monthviewgen.cpp [new file with mode: 0644]
source/common/monthviewgen.h [new file with mode: 0644]
source/tests/xestiacalendar_commonfunctions.h

diff --git a/source/common/monthviewgen.cpp b/source/common/monthviewgen.cpp
new file mode 100644 (file)
index 0000000..a80adb4
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>
+
+#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 (file)
index 0000000..e711167
--- /dev/null
@@ -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 <http://www.gnu.org/licenses/>
+
+#ifndef __COMMON_MONTHVIEWGEN_H__
+#define __COMMON_MONTHVIEWGEN_H__
+
+#include <vector>
+#include <ctime>
+#include <iostream>
+
+int PreviousMonthNumberofDays(int Month, int Year);
+
+#endif
\ No newline at end of file
index 8da4adc..225d0a4 100644 (file)
@@ -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
Xestia Software Development
Yn Maystri
© 2006 - 2019 Xestia Software Development
Software

Xestia Address Book
Xestia Calendar
Development

Xestia Gelforn
Everything else

About
News
Privacy Policy