From c774edec2f78c5c1cd28c305e7935d9f415d2ac9 Mon Sep 17 00:00:00 2001 From: Eric Floyd Date: Wed, 1 Nov 2023 21:34:46 -0400 Subject: [PATCH] Fixed some conversions --- CMakeLists.txt | 3 ++ include/faker-cxx/Weather.h | 4 +-- src/common/WeatherHelper.cpp | 19 ++++++++++++ src/common/WeatherHelper.h | 47 +++++++++++++++++++++++++++++ src/common/WeatherHelperTest.cpp | 30 ++++++++++++++++++ src/modules/weather/Weather.cpp | 19 +++++++++--- src/modules/weather/WeatherTest.cpp | 8 ++--- 7 files changed, 119 insertions(+), 11 deletions(-) create mode 100644 src/common/WeatherHelper.cpp create mode 100644 src/common/WeatherHelper.h create mode 100644 src/common/WeatherHelperTest.cpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 1efdf6b8c..7fd64ac1f 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -47,6 +47,8 @@ set(FAKER_SOURCES src/modules/videoGame/VideoGame.cpp src/modules/medicine/Medicine.cpp src/modules/weather/Weather.cpp + src/common/WeatherHelper.h + src/common/WeatherHelper.cpp ) set(FAKER_UT_SOURCES @@ -80,6 +82,7 @@ set(FAKER_UT_SOURCES 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 index 4d428210d..483ea7531 100644 --- a/include/faker-cxx/Weather.h +++ b/include/faker-cxx/Weather.h @@ -8,7 +8,7 @@ class Weather { public: struct Temperature { - int metric, imperial; + double metric, imperial; }; /** @@ -24,7 +24,7 @@ class Weather static Temperature temperature(); struct Pressure { - int metric; + double metric; double imperial; }; 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 index 62d812ff1..a7a200b07 100644 --- a/src/modules/weather/Weather.cpp +++ b/src/modules/weather/Weather.cpp @@ -5,22 +5,31 @@ #include "data/WeatherDescription.h" #include "faker-cxx/Helper.h" #include "faker-cxx/Number.h" +#include "../../common/WeatherHelper.h" namespace faker { Weather::Temperature Weather::temperature() { - return {Number::integer(10, 30), Number::integer(50, 90)}; + double metric = Number::decimal(10.0, 30.0); + double imperial = WeatherHelper::celsiusToFahrenheit(metric); + + return {metric, imperial}; } Weather::Pressure Weather::pressure() { - return {Number::integer(1000, 1100), Number::integer(14, 16) + Number::decimal(0.1, 0.9)}; + double metric = Number::decimal(100.0, 102.0); + double imperial = WeatherHelper::kPaToPsi(metric); + + return {metric, imperial}; } -Weather::Visibility Weather::visibility() -{ - return {Number::decimal(0.1, 10.0), Number::decimal(0.1, 6.2)}; +Weather::Visibility Weather::visibility() { + double metric = Number::decimal(1.0, 10.0); + double imperial = WeatherHelper::milesToKilometers(metric); + + return {metric, imperial}; } Weather::WindSpeed Weather::windSpeed() diff --git a/src/modules/weather/WeatherTest.cpp b/src/modules/weather/WeatherTest.cpp index 5ef23438c..7f2c17385 100644 --- a/src/modules/weather/WeatherTest.cpp +++ b/src/modules/weather/WeatherTest.cpp @@ -26,16 +26,16 @@ TEST_F(WeatherTest, shouldGeneratePressure) { Weather::Pressure generatedPressure = Weather::pressure(); - ASSERT_TRUE(generatedPressure.metric >= 1000 && generatedPressure.metric <= 1100); - ASSERT_TRUE(generatedPressure.imperial >= 14.0 && generatedPressure.imperial <= 16.0); + 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 >= 0.1 && generatedVisibility.metric <= 10.0); - ASSERT_TRUE(generatedVisibility.imperial >= 0.1 && generatedVisibility.imperial <= 6.2); + ASSERT_TRUE(generatedVisibility.metric >= 1 && generatedVisibility.metric <= 10.0); + ASSERT_TRUE(generatedVisibility.imperial >= 1.6 && generatedVisibility.imperial <= 16.1); } TEST_F(WeatherTest, shouldGenerateWindSpeed)