Skip to content

Commit

Permalink
Fixed some conversions
Browse files Browse the repository at this point in the history
  • Loading branch information
eric-bodhi committed Nov 2, 2023
1 parent 8c75cf2 commit c774ede
Show file tree
Hide file tree
Showing 7 changed files with 119 additions and 11 deletions.
3 changes: 3 additions & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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})
Expand Down
4 changes: 2 additions & 2 deletions include/faker-cxx/Weather.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ class Weather
{
public:
struct Temperature {
int metric, imperial;
double metric, imperial;
};

/**
Expand All @@ -24,7 +24,7 @@ class Weather
static Temperature temperature();

struct Pressure {
int metric;
double metric;
double imperial;
};

Expand Down
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);
}
19 changes: 14 additions & 5 deletions src/modules/weather/Weather.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down
8 changes: 4 additions & 4 deletions src/modules/weather/WeatherTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand Down

0 comments on commit c774ede

Please sign in to comment.