diff --git a/CMakeLists.txt b/CMakeLists.txt index 75c64770a..3a4d0c20f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -46,7 +46,9 @@ set(FAKER_SOURCES src/modules/sport/Sport.cpp src/modules/videoGame/VideoGame.cpp src/modules/medicine/Medicine.cpp - ) + src/modules/weather/Weather.cpp + src/common/WeatherHelper.cpp +) set(FAKER_UT_SOURCES src/modules/animal/AnimalTest.cpp @@ -78,7 +80,9 @@ set(FAKER_UT_SOURCES src/modules/sport/SportTest.cpp src/modules/videoGame/VideoGameTest.cpp src/modules/medicine/MedicineTest.cpp - ) + src/modules/weather/WeatherTest.cpp + src/common/WeatherHelperTest.cpp +) add_library(${LIBRARY_NAME} ${FAKER_SOURCES}) diff --git a/include/faker-cxx/Weather.h b/include/faker-cxx/Weather.h new file mode 100644 index 000000000..483ea7531 --- /dev/null +++ b/include/faker-cxx/Weather.h @@ -0,0 +1,118 @@ +#pragma once + +#include + +namespace faker +{ +class Weather +{ +public: + struct Temperature { + double metric, imperial; + }; + + /** + * @brief Generates a random temperature + * + * @return Temperature object with metric and imperial temperatures + * + * @code + * Weather::temperature(); // Temperature.metric = 10, Temperature.imperial = 50 + * @endcode + */ + + static Temperature temperature(); + + struct Pressure { + double metric; + double imperial; + }; + + /** + * @brief Generated a random pressure + * + * @return Pressure object with metric and imperial pressures + * + * @code + * Weather::pressure(); // Pressure.metric = 1000, Pressure.imperial = 14.5 + * @endcode + */ + static Pressure pressure(); + + struct Visibility { + double metric; + double imperial; + }; + + /** + * @brief Generated a random visibility + * + * @return Visibility object with metric and imperial visibility + * + * @code + * Weather::visibility(); // Visibility.metric = 10.0, Visibility.imperial = 6.2 + * @endcode + */ + static Visibility visibility(); + + struct WindSpeed { + double metric; + double imperial; + }; + + /** + * @brief Generated a random wind speed + * + * @return WindSpeed object with metric and imperial wind speed + * + * @code + * Weather::windSpeed(); // WindSpeed.metric = 10.0, WindSpeed.imperial = 6.2 + * @endcode + */ + static WindSpeed windSpeed(); + + /** + * @brief Generated a random uvIndex + * + * @return Int A random uvIndex + * + * @code + * Weather::uvIndex(); // 10 + * @endcode + */ + static int uvIndex(); + + /** + * @brief Generated a random humidity percentage + * + * @return Int A random humidity percentage + * + * @code + * Weather::humidity(); // 10 + * @endcode + */ + static int humidity(); + + /** + * @brief Generated a random weather description + * + * @return String A random weather description + * + * @code + * Weather::description(); // "Sunny" + * @endcode + */ + static std::string weatherDescription(); + + /** + * @brief Generated a random cloud cover percentage + * + * @return Int A random cloud cover percentage + * + * @code + * Weather::cloudCover(); // 10 + * @endcode + */ + static int cloudCover(); +}; +} \ No newline at end of file diff --git a/src/common/WeatherHelper.cpp b/src/common/WeatherHelper.cpp new file mode 100644 index 000000000..a140fe7bc --- /dev/null +++ b/src/common/WeatherHelper.cpp @@ -0,0 +1,19 @@ +#include "WeatherHelper.h" + +namespace faker +{ +double WeatherHelper::celsiusToFahrenheit(double celsius) +{ + return (celsius * 9.0 / 5.0) + 32; +} + +double WeatherHelper::kPaToPsi(double kPa) +{ + return kPa * 0.14503773773375; +} + +double WeatherHelper::milesToKilometers(double miles) +{ + return miles * 1.609344; +} +} diff --git a/src/common/WeatherHelper.h b/src/common/WeatherHelper.h new file mode 100644 index 000000000..9d0ef84e9 --- /dev/null +++ b/src/common/WeatherHelper.h @@ -0,0 +1,47 @@ +#pragma once + +namespace faker +{ +class WeatherHelper +{ +public: + /** + * @brief Converts celsius to fahrenheit + * + * @param celsius + * + * @return double + * + * @code + * WeatherHelper::celsiusToFahrenheit(10); // 50 + * @endcode + */ + static double celsiusToFahrenheit(double celsius); + + /** + * @brief Converts kPa to PSI + * + * @param kPa + * + * @return double + * + * @code + * WeatherHelper::kPaToPsi(1000); // 145.037 + * @endcode + */ + static double kPaToPsi(double kPa); + + /** + * @brief Converts miles to kilometers + * + * @param miles + * + * @return double + * + * @code + * WeatherHelper::milesToKilometers(10); // 16.0934 + * @endcode + */ + static double milesToKilometers(double miles); +}; +} \ No newline at end of file diff --git a/src/common/WeatherHelperTest.cpp b/src/common/WeatherHelperTest.cpp new file mode 100644 index 000000000..29c35e8e8 --- /dev/null +++ b/src/common/WeatherHelperTest.cpp @@ -0,0 +1,30 @@ +#include "WeatherHelper.h" +#include "gtest/gtest.h" + +using namespace faker; +using namespace ::testing; + +class WeatherHelperTest : public Test { +public: +}; + +TEST_F(WeatherHelperTest, shouldConvertCelsiusToFahrenheit) { + double celsius = 10.0; + double fahrenheit = WeatherHelper::celsiusToFahrenheit(celsius); + + ASSERT_EQ(fahrenheit, 50.0); +} + +TEST_F(WeatherHelperTest, shouldConvertKPaToPsi) { + double kPa = 100.0; + double psi = WeatherHelper::kPaToPsi(kPa); + + ASSERT_EQ(psi, 14.503773773375); +} + +TEST_F(WeatherHelperTest, shouldConvertMilesToKilometers) { + double miles = 10.0; + double kilometers = WeatherHelper::milesToKilometers(miles); + + ASSERT_EQ(kilometers, 16.09344); +} diff --git a/src/modules/weather/Weather.cpp b/src/modules/weather/Weather.cpp new file mode 100644 index 000000000..a7a200b07 --- /dev/null +++ b/src/modules/weather/Weather.cpp @@ -0,0 +1,59 @@ +#include "faker-cxx/Weather.h" + +#include + +#include "data/WeatherDescription.h" +#include "faker-cxx/Helper.h" +#include "faker-cxx/Number.h" +#include "../../common/WeatherHelper.h" + +namespace faker +{ +Weather::Temperature Weather::temperature() +{ + double metric = Number::decimal(10.0, 30.0); + double imperial = WeatherHelper::celsiusToFahrenheit(metric); + + return {metric, imperial}; +} + +Weather::Pressure Weather::pressure() +{ + double metric = Number::decimal(100.0, 102.0); + double imperial = WeatherHelper::kPaToPsi(metric); + + return {metric, imperial}; +} + +Weather::Visibility Weather::visibility() { + double metric = Number::decimal(1.0, 10.0); + double imperial = WeatherHelper::milesToKilometers(metric); + + return {metric, imperial}; +} + +Weather::WindSpeed Weather::windSpeed() +{ + return {Number::decimal(0.1, 24.2), Number::decimal(0.1, 15.0)}; +} + +int Weather::uvIndex() +{ + return Number::integer(0, 11); +} + +int Weather::humidity() +{ + return Number::integer(0, 100); +} + +std::string Weather::weatherDescription() +{ + return Helper::arrayElement(weatherDescriptions); +} + +int Weather::cloudCover() +{ + return Number::integer(0, 100); +} +} diff --git a/src/modules/weather/WeatherTest.cpp b/src/modules/weather/WeatherTest.cpp new file mode 100644 index 000000000..7f2c17385 --- /dev/null +++ b/src/modules/weather/WeatherTest.cpp @@ -0,0 +1,75 @@ +#include "faker-cxx/Weather.h" + +#include +#include + +#include "gtest/gtest.h" +#include "data/WeatherDescription.h" + +using namespace ::testing; +using namespace faker; + +class WeatherTest : public Test +{ +public: +}; + +TEST_F(WeatherTest, shouldGenerateTemperature) +{ + Weather::Temperature generatedTemperature = Weather::temperature(); + + ASSERT_TRUE(generatedTemperature.metric >= 10 && generatedTemperature.metric <= 30); + ASSERT_TRUE(generatedTemperature.imperial >= 50 && generatedTemperature.imperial <= 90); +} + +TEST_F(WeatherTest, shouldGeneratePressure) +{ + Weather::Pressure generatedPressure = Weather::pressure(); + + ASSERT_TRUE(generatedPressure.metric >= 100.0 && generatedPressure.metric <= 102.0); + ASSERT_TRUE(generatedPressure.imperial >= 14.5 && generatedPressure.imperial <= 14.8); +} + +TEST_F(WeatherTest, shouldGenerateVisibility) +{ + Weather::Visibility generatedVisibility = Weather::visibility(); + + ASSERT_TRUE(generatedVisibility.metric >= 1 && generatedVisibility.metric <= 10.0); + ASSERT_TRUE(generatedVisibility.imperial >= 1.6 && generatedVisibility.imperial <= 16.1); +} + +TEST_F(WeatherTest, shouldGenerateWindSpeed) +{ + Weather::WindSpeed generatedWindSpeed = Weather::windSpeed(); + + ASSERT_TRUE(generatedWindSpeed.metric >= 0.1 && generatedWindSpeed.metric <= 24.2); + ASSERT_TRUE(generatedWindSpeed.imperial >= 0.1 && generatedWindSpeed.imperial <= 15.0); +} + +TEST_F(WeatherTest, shouldGenerateUvIndex) +{ + int generatedUvIndex = Weather::uvIndex(); + + ASSERT_TRUE(generatedUvIndex >= 0 && generatedUvIndex <= 11); +} + +TEST_F(WeatherTest, shouldGenerateHumidity) +{ + int generatedHumidity = Weather::humidity(); + + ASSERT_TRUE(generatedHumidity >= 0 && generatedHumidity <= 100); +} + +TEST_F(WeatherTest, shouldGenerateWeatherDescription) +{ + std::string generatedWeatherDescription = Weather::weatherDescription(); + + ASSERT_TRUE(std::ranges::any_of(weatherDescriptions, [generatedWeatherDescription](const std::string& weatherDescription) + { return weatherDescription == generatedWeatherDescription; })); +} + +TEST_F(WeatherTest, shouldGenerateWeatherCloudCover) { + int generatedCloudCover = Weather::cloudCover(); + + ASSERT_TRUE(generatedCloudCover >= 0 && generatedCloudCover <= 100); +} diff --git a/src/modules/weather/data/WeatherDescription.h b/src/modules/weather/data/WeatherDescription.h new file mode 100644 index 000000000..0d65c1c42 --- /dev/null +++ b/src/modules/weather/data/WeatherDescription.h @@ -0,0 +1,16 @@ +#pragma once + +#include +#include + +namespace faker +{ +const std::vector weatherDescriptions = { + "clear sky", "few clouds", "scattered clouds", + "broken clouds", "shower rain", "rainy", + "thunderstorm", "snowy", "misty", + "smoky", "haze", "sunny", + "cloudy", "windy", "dark", + "foggy", +}; +}