diff --git a/src/currencyExchange.c b/src/currencyExchange.c new file mode 100644 index 0000000..2a22334 --- /dev/null +++ b/src/currencyExchange.c @@ -0,0 +1,13 @@ +#include "currencyExchange.h" + +float convert(float euro, int newCurrencyCode) { + switch(newCurrencyCode) { + case CURRENCY_CODE_USD: + return ( euro * USD_RATE_OF_ONE_EURO ); + + case CURRENCY_CODE_GBP: + return ( euro * GBP_RATE_OF_ONE_EURO ); + } + + return -1; +} \ No newline at end of file diff --git a/src/currencyExchange.h b/src/currencyExchange.h new file mode 100644 index 0000000..6629fcc --- /dev/null +++ b/src/currencyExchange.h @@ -0,0 +1,9 @@ +#include + +#define USD_RATE_OF_ONE_EURO 1.07 +#define GBP_RATE_OF_ONE_EURO 0.89 + +#define CURRENCY_CODE_USD 1 +#define CURRENCY_CODE_GBP 2 + +float convert(float euro, int newCurrencyCode); \ No newline at end of file diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c new file mode 100644 index 0000000..821f56a --- /dev/null +++ b/tests/test_currencyExchange.c @@ -0,0 +1,48 @@ +#ifdef TEST + +#include "unity.h" +#include "currencyExchange.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_convert(void) { + + /* Arrange */ + + int length = 10; + float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899}; + + float expectedUSD[length]; + float expectedGBP[length]; + + float resultUSD[length]; + float resultGBP[length]; + + for (int i = 0; i < length; i++) { + expectedUSD[i] = euro[i] * USD_RATE_OF_ONE_EURO; + expectedGBP[i] = euro[i] * GBP_RATE_OF_ONE_EURO; + } + + /* Act */ + + for (int i = 0; i < length; i++) { + resultUSD[i] = convert(euro[i], CURRENCY_CODE_USD); + resultGBP[i] = convert(euro[i], CURRENCY_CODE_GBP); + } + + /* Assert*/ + + for (int i = 0; i < length; i++) { + TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); + TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]); + } +} + + +#endif // TEST \ No newline at end of file