From 6bdf1e294b8a621ae8d70133e24fc4c22652c7f7 Mon Sep 17 00:00:00 2001 From: Youval Kashuv Date: Sun, 11 Feb 2024 10:51:45 -0500 Subject: [PATCH 1/3] adds Date module functions: year/month/dayOfMonth/dayOfWeek --- include/faker-cxx/Date.h | 56 +++++++++++++++++++++++++++++++++++++++ src/modules/date/Date.cpp | 31 ++++++++++++++++++++++ 2 files changed, 87 insertions(+) diff --git a/include/faker-cxx/Date.h b/include/faker-cxx/Date.h index 83d0fb5e3..bfeb5037b 100644 --- a/include/faker-cxx/Date.h +++ b/include/faker-cxx/Date.h @@ -154,5 +154,61 @@ class Date * @endcode */ static std::string monthAbbreviatedName(); + + static unsigned int year(int minYear=1920, int maxYear=2000); + + /** + * @brief Returns a random year. + * + * @param minYear The minimum year to generate a year. Defaults to `1920`. + * @param maxYear The maximum year to generate a year. Defaults to `2000`. + * + * @returns unsigned int. + * + * @code + * Date::year() // "1996" + * @endcode + */ + + static unsigned int month(); + + /** + * @brief Returns a month. + * + * + * @returns unsigned int 1-12. + * + * @code + * Date::month() // "1" + * @endcode + */ + + static unsigned int dayOfMonth(); + + /** + * @brief Returns a random day of month. + * + * + * @returns unsigned int 1-31. + * + * @code + * Date::dayOfMonth() // "1" + * @endcode + */ + + static unsigned int dayOfWeek(); + + /** + * @brief Returns a random day of the week. + * + * + * @returns unsigned int 1-7. + * + * @code + * Date::dayOfWeek() // "1" + * @endcode + */ + + }; } diff --git a/src/modules/date/Date.cpp b/src/modules/date/Date.cpp index 9ec5f4c9f..6103df797 100644 --- a/src/modules/date/Date.cpp +++ b/src/modules/date/Date.cpp @@ -2,6 +2,7 @@ #include #include +#include #include #include "../../common/FormatHelper.h" @@ -150,4 +151,34 @@ std::string Date::monthAbbreviatedName() return Helper::arrayElement(monthAbbreviatedNames); } +unsigned int Date::year(int minYear, int maxYear) +{ + std::random_device rd; + std::mt19937 eng(rd()); + std::uniform_int_distribution distr(minYear, maxYear); + + return distr(eng); +} + +unsigned int Date::month() +{ + std::random_device rd; + std::mt19937 eng(rd()); + std::uniform_int_distribution distr(1, 12); +} + +unsigned int Date::dayOfMonth() +{ + std::random_device rd; + std::mt19937 eng(rd()); + std::uniform_int_distribution distr(1, 31); +} + +unsigned int Date::dayOfWeek() +{ + std::random_device rd; + std::mt19937 eng(rd()); + std::uniform_int_distribution distr(1, 7); +} + } From d16cb020fa5dc63474848234c05c21d7e55c4b26 Mon Sep 17 00:00:00 2001 From: KashuvY <119360561+KashuvY@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:02:35 -0500 Subject: [PATCH 2/3] moved documentation above function decleration --- include/faker-cxx/Date.h | 19 +++++++------------ 1 file changed, 7 insertions(+), 12 deletions(-) diff --git a/include/faker-cxx/Date.h b/include/faker-cxx/Date.h index bfeb5037b..58bbb5714 100644 --- a/include/faker-cxx/Date.h +++ b/include/faker-cxx/Date.h @@ -154,8 +154,6 @@ class Date * @endcode */ static std::string monthAbbreviatedName(); - - static unsigned int year(int minYear=1920, int maxYear=2000); /** * @brief Returns a random year. @@ -169,8 +167,7 @@ class Date * Date::year() // "1996" * @endcode */ - - static unsigned int month(); + static unsigned int year(int minYear=1920, int maxYear=2000); /** * @brief Returns a month. @@ -182,8 +179,7 @@ class Date * Date::month() // "1" * @endcode */ - - static unsigned int dayOfMonth(); + static unsigned int month(); /** * @brief Returns a random day of month. @@ -194,11 +190,10 @@ class Date * @code * Date::dayOfMonth() // "1" * @endcode - */ - - static unsigned int dayOfWeek(); + */ + static unsigned int dayOfMonth(); - /** + /** * @brief Returns a random day of the week. * * @@ -207,8 +202,8 @@ class Date * @code * Date::dayOfWeek() // "1" * @endcode - */ - + */ + static unsigned int dayOfWeek(); }; } From e3814cf86b38f57299d69849c007630395e04f29 Mon Sep 17 00:00:00 2001 From: KashuvY <119360561+KashuvY@users.noreply.github.com> Date: Sun, 11 Feb 2024 18:07:55 -0500 Subject: [PATCH 3/3] added tests for new functions: year, month, dayOfMonth, dayOfWeek --- src/modules/date/DateTest.cpp | 27 +++++++++++++++++++++++++++ 1 file changed, 27 insertions(+) diff --git a/src/modules/date/DateTest.cpp b/src/modules/date/DateTest.cpp index 4aef97e87..b0653d04c 100644 --- a/src/modules/date/DateTest.cpp +++ b/src/modules/date/DateTest.cpp @@ -263,3 +263,30 @@ TEST_F(DateTest, shouldGenerateMonthAbbreviatedName) [generatedMonthAbbreviatedName](const std::string& monthAbbreviatedName) { return monthAbbreviatedName == generatedMonthAbbreviatedName; })); } + +TEST_F(DateTest, shouldGenerateYearWithinRange) { + int minYear = 1900; + int maxYear = 2100; + auto generatedYear = Date::year(minYear, maxYear); + + ASSERT_TRUE(generatedYear >= minYear && generatedYear <= maxYear); +} + +TEST_F(DateTest, shouldGenerateMonthWithinRange) { + auto generatedMonth = Date::month(); + + ASSERT_TRUE(generatedMonth >= 1 && generatedMonth <= 12); +} + +TEST_F(DateTest, shouldGenerateDayOfMonthWithinRange) { + auto generatedDay = Date::dayOfMonth(); + + ASSERT_TRUE(generatedDay >= 1 && generatedDay <= 31); +} + +TEST_F(DateTest, shouldGenerateDayOfWeekWithinRange) { + auto generatedDayOfWeek = Date::dayOfWeek(); + + ASSERT_TRUE(generatedDayOfWeek >= 1 && generatedDayOfWeek <= 7); +} +