Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Feature/weather #214

Merged
merged 3 commits into from
Nov 2, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
8 changes: 6 additions & 2 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})

Expand Down
118 changes: 118 additions & 0 deletions include/faker-cxx/Weather.h
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();
};
}
19 changes: 19 additions & 0 deletions src/common/WeatherHelper.cpp
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;
}
}
47 changes: 47 additions & 0 deletions src/common/WeatherHelper.h
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);
};
}
30 changes: 30 additions & 0 deletions src/common/WeatherHelperTest.cpp
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);
}
59 changes: 59 additions & 0 deletions src/modules/weather/Weather.cpp
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);
}
}
75 changes: 75 additions & 0 deletions src/modules/weather/WeatherTest.cpp
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);
}
16 changes: 16 additions & 0 deletions src/modules/weather/data/WeatherDescription.h
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",
};
}