-
-
Notifications
You must be signed in to change notification settings - Fork 151
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
* Added Weather Module * Fixed some conversions * Removed unnecessary line from CMake
- Loading branch information
1 parent
0afd685
commit 033637a
Showing
8 changed files
with
370 additions
and
2 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,118 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
|
||
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(); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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; | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,59 @@ | ||
#include "faker-cxx/Weather.h" | ||
|
||
#include <string> | ||
|
||
#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<std::string>(weatherDescriptions); | ||
} | ||
|
||
int Weather::cloudCover() | ||
{ | ||
return Number::integer(0, 100); | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,75 @@ | ||
#include "faker-cxx/Weather.h" | ||
|
||
#include <algorithm> | ||
#include <string> | ||
|
||
#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); | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
#pragma once | ||
|
||
#include <string> | ||
#include <vector> | ||
|
||
namespace faker | ||
{ | ||
const std::vector<std::string> weatherDescriptions = { | ||
"clear sky", "few clouds", "scattered clouds", | ||
"broken clouds", "shower rain", "rainy", | ||
"thunderstorm", "snowy", "misty", | ||
"smoky", "haze", "sunny", | ||
"cloudy", "windy", "dark", | ||
"foggy", | ||
}; | ||
} |