From 336d01499aced1c24816f28a48896e58c3f94b87 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Tue, 7 Feb 2023 16:12:25 +0100 Subject: [PATCH 01/12] Add feature to change currency to USD --- src/currencyExchange.c | 8 ++++++++ 1 file changed, 8 insertions(+) create mode 100644 src/currencyExchange.c diff --git a/src/currencyExchange.c b/src/currencyExchange.c new file mode 100644 index 0000000..b8cf582 --- /dev/null +++ b/src/currencyExchange.c @@ -0,0 +1,8 @@ +#include "currencyExchange.h" + +float toUSD(float euro) { + + float USD = euro * CURRENT_USD_RATE_OF_ONE_EURO; + + return USD; +} \ No newline at end of file From 4c112e72e606ad10178f429d3ab6203d565bae62 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Tue, 7 Feb 2023 16:12:53 +0100 Subject: [PATCH 02/12] Add needed headers for currency change toUSD --- src/currencyExchange.h | 5 +++++ 1 file changed, 5 insertions(+) create mode 100644 src/currencyExchange.h diff --git a/src/currencyExchange.h b/src/currencyExchange.h new file mode 100644 index 0000000..406b0ed --- /dev/null +++ b/src/currencyExchange.h @@ -0,0 +1,5 @@ +#include + +#define CURRENT_USD_RATE_OF_ONE_EURO 1.07 + +float toUSD(float euro); \ No newline at end of file From ee62a56d1a58b273fce4c7e19c8862b30843dd1d Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Tue, 7 Feb 2023 16:25:56 +0100 Subject: [PATCH 03/12] Add test for toUSD function --- tests/test_currencyExchange.c | 35 +++++++++++++++++++++++++++++++++++ 1 file changed, 35 insertions(+) create mode 100644 tests/test_currencyExchange.c diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c new file mode 100644 index 0000000..168f0c2 --- /dev/null +++ b/tests/test_currencyExchange.c @@ -0,0 +1,35 @@ +#ifdef TEST + +#include "unity.h" +#include "currencyExchange.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_toUSD(void) { + int length = 5; + float euro[] = {34, 233, 400, 100, 45}; + float expectedUSD[length]; + float resultUSD[length]; + + for (int i = 0; i < length; i++) { + resultUSD[i] = toUSD(euro[i]); + } + + for (int i = 0; i < length; i++) { + expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; + } + + for (int i = 0; i < length; i++) { + TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); + } + +} + + +#endif // TEST From 984c860e0da4ceb3592c1a149e1618a0f00f3520 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Tue, 7 Feb 2023 19:57:56 +0100 Subject: [PATCH 04/12] Refactoring: organise code --- tests/test_currencyExchange.c | 10 +++++++++- 1 file changed, 9 insertions(+), 1 deletion(-) diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c index 168f0c2..942436f 100644 --- a/tests/test_currencyExchange.c +++ b/tests/test_currencyExchange.c @@ -12,8 +12,12 @@ void tearDown(void) } void test_toUSD(void) { - int length = 5; + + /* Arrange */ + + int length = 5; float euro[] = {34, 233, 400, 100, 45}; + float expectedUSD[length]; float resultUSD[length]; @@ -21,10 +25,14 @@ void test_toUSD(void) { resultUSD[i] = toUSD(euro[i]); } + /* Act */ + for (int i = 0; i < length; i++) { expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; } + /* Assert */ + for (int i = 0; i < length; i++) { TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); } From 7f7ab6195bba1258403ac474310e09384cc02c7f Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 10:50:19 +0100 Subject: [PATCH 05/12] Add feature to change currency to GBP --- src/currencyExchange.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/currencyExchange.c b/src/currencyExchange.c index b8cf582..ccf75d3 100644 --- a/src/currencyExchange.c +++ b/src/currencyExchange.c @@ -5,4 +5,11 @@ float toUSD(float euro) { float USD = euro * CURRENT_USD_RATE_OF_ONE_EURO; return USD; +} + +float toGBP(float euro) { + + float GBP = euro * CURRENT_GBP_RATE_OF_ONE_EURO; + + return GBP; } \ No newline at end of file From ee420f6fb29e5d9b53c76e690ab2bf6792d9bd81 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 10:50:47 +0100 Subject: [PATCH 06/12] Add test for currency change to GBP --- tests/test_currencyExchange.c | 29 +++++++++++++++++++++++++++++ 1 file changed, 29 insertions(+) diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c index 942436f..b27cd94 100644 --- a/tests/test_currencyExchange.c +++ b/tests/test_currencyExchange.c @@ -39,5 +39,34 @@ void test_toUSD(void) { } +void test_toGBP(void) { + + /* Arrange */ + + int length = 5; + float euro[] = {34, 233, 400, 100, 45}; + + float expectedGBP[length]; + float resultGBP[length]; + + for (int i = 0; i < length; i++) { + resultGBP[i] = toGBP(euro[i]); + } + + /* Act */ + + for (int i = 0; i < length; i++) { + expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; + } + + /* Assert */ + + for (int i = 0; i < length; i++) { + TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]); + } + +} + + #endif // TEST From 3f52b9b11a1ca77132d09cc3db2684215f5ac694 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 10:51:15 +0100 Subject: [PATCH 07/12] Add information about GBP in header --- src/currencyExchange.h | 4 +++- 1 file changed, 3 insertions(+), 1 deletion(-) diff --git a/src/currencyExchange.h b/src/currencyExchange.h index 406b0ed..fe80c35 100644 --- a/src/currencyExchange.h +++ b/src/currencyExchange.h @@ -1,5 +1,7 @@ #include #define CURRENT_USD_RATE_OF_ONE_EURO 1.07 +#define CURRENT_GBP_RATE_OF_ONE_EURO 0.89 -float toUSD(float euro); \ No newline at end of file +float toUSD(float euro); +float toGBP(float euro); \ No newline at end of file From 8da7e42336863f0ef25ec18041502bf711ad1df2 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 15:13:23 +0100 Subject: [PATCH 08/12] Add generalised function for currency exchange --- src/currencyExchange.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/src/currencyExchange.c b/src/currencyExchange.c index ccf75d3..b3dc39a 100644 --- a/src/currencyExchange.c +++ b/src/currencyExchange.c @@ -12,4 +12,14 @@ float toGBP(float euro) { float GBP = euro * CURRENT_GBP_RATE_OF_ONE_EURO; return GBP; +} + +float convert(float euro, int newCurrencyCode) { + switch(newCurrencyCode) { + case 1: + return ( euro * CURRENT_USD_RATE_OF_ONE_EURO ); + case 2: + return ( euro * CURRENT_GBP_RATE_OF_ONE_EURO ); + } + return -1; } \ No newline at end of file From 33e3f3eb6fd728e1a3b3f34dfcf554f8c3251ccb Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 15:42:51 +0100 Subject: [PATCH 09/12] Add test for function 'convert' --- tests/test_currencyExchange.c | 104 +++++++++++++++++++++------------- 1 file changed, 64 insertions(+), 40 deletions(-) diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c index b27cd94..13aef17 100644 --- a/tests/test_currencyExchange.c +++ b/tests/test_currencyExchange.c @@ -11,62 +11,86 @@ void tearDown(void) { } -void test_toUSD(void) { - - /* Arrange */ - +//void test_toUSD(void) { +// +// /* Arrange */ +// +// int length = 5; +// float euro[] = {34, 233, 400, 100, 45}; +// +// float expectedUSD[length]; +// float resultUSD[length]; +// +// for (int i = 0; i < length; i++) { +// resultUSD[i] = toUSD(euro[i]); +// } +// +// /* Act */ +// +// for (int i = 0; i < length; i++) { +// expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; +// } +// +// /* Assert */ +// +// for (int i = 0; i < length; i++) { +// TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); +// } +// +//} +// +//void test_toGBP(void) { +// +// /* Arrange */ +// +// int length = 5; +// float euro[] = {34, 233, 400, 100, 45}; +// +// float expectedGBP[length]; +// float resultGBP[length]; +// +// for (int i = 0; i < length; i++) { +// resultGBP[i] = toGBP(euro[i]); +// } +// +// /* Act */ +// +// for (int i = 0; i < length; i++) { +// expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; +// } +// +// /* Assert */ +// +// for (int i = 0; i < length; i++) { +// TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]); +// } +// +//} +void test_convert(void) { int length = 5; float euro[] = {34, 233, 400, 100, 45}; float expectedUSD[length]; - float resultUSD[length]; - - for (int i = 0; i < length; i++) { - resultUSD[i] = toUSD(euro[i]); - } - - /* Act */ - - for (int i = 0; i < length; i++) { - expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; - } - - /* Assert */ - - for (int i = 0; i < length; i++) { - TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); - } - -} - -void test_toGBP(void) { - - /* Arrange */ - - int length = 5; - float euro[] = {34, 233, 400, 100, 45}; - float expectedGBP[length]; + + float resultUSD[length]; float resultGBP[length]; for (int i = 0; i < length; i++) { - resultGBP[i] = toGBP(euro[i]); + expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; + expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; } - /* Act */ - for (int i = 0; i < length; i++) { - expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; + resultUSD[i] = convert(euro[i], 1); + resultGBP[i] = convert(euro[i], 2); } - /* 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 +#endif // TEST \ No newline at end of file From 2aee8c516b916bb46569762a1399dff1b5ce7a0a Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 15:48:56 +0100 Subject: [PATCH 10/12] Refactoring: Remove useless code and update constant names --- src/currencyExchange.c | 24 ++++++------------------ 1 file changed, 6 insertions(+), 18 deletions(-) diff --git a/src/currencyExchange.c b/src/currencyExchange.c index b3dc39a..2a22334 100644 --- a/src/currencyExchange.c +++ b/src/currencyExchange.c @@ -1,25 +1,13 @@ #include "currencyExchange.h" -float toUSD(float euro) { - - float USD = euro * CURRENT_USD_RATE_OF_ONE_EURO; - - return USD; -} - -float toGBP(float euro) { - - float GBP = euro * CURRENT_GBP_RATE_OF_ONE_EURO; - - return GBP; -} - float convert(float euro, int newCurrencyCode) { switch(newCurrencyCode) { - case 1: - return ( euro * CURRENT_USD_RATE_OF_ONE_EURO ); - case 2: - return ( euro * CURRENT_GBP_RATE_OF_ONE_EURO ); + 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 From 7309447c03aa84d4e0b6caba24c5d3fffc8d4624 Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 15:49:22 +0100 Subject: [PATCH 11/12] Add relevant constants for USD and GBP --- src/currencyExchange.h | 10 ++++++---- 1 file changed, 6 insertions(+), 4 deletions(-) diff --git a/src/currencyExchange.h b/src/currencyExchange.h index fe80c35..6629fcc 100644 --- a/src/currencyExchange.h +++ b/src/currencyExchange.h @@ -1,7 +1,9 @@ #include -#define CURRENT_USD_RATE_OF_ONE_EURO 1.07 -#define CURRENT_GBP_RATE_OF_ONE_EURO 0.89 +#define USD_RATE_OF_ONE_EURO 1.07 +#define GBP_RATE_OF_ONE_EURO 0.89 -float toUSD(float euro); -float toGBP(float euro); \ No newline at end of file +#define CURRENCY_CODE_USD 1 +#define CURRENCY_CODE_GBP 2 + +float convert(float euro, int newCurrencyCode); \ No newline at end of file From 00b7c7622cabb87f0b60c018e7377659da695afc Mon Sep 17 00:00:00 2001 From: Shivam Chaudhary Date: Thu, 9 Feb 2023 15:50:05 +0100 Subject: [PATCH 12/12] Refactoring: organise test code with some more values --- tests/test_currencyExchange.c | 74 ++++++----------------------------- 1 file changed, 13 insertions(+), 61 deletions(-) diff --git a/tests/test_currencyExchange.c b/tests/test_currencyExchange.c index 13aef17..821f56a 100644 --- a/tests/test_currencyExchange.c +++ b/tests/test_currencyExchange.c @@ -11,64 +11,12 @@ void tearDown(void) { } -//void test_toUSD(void) { -// -// /* Arrange */ -// -// int length = 5; -// float euro[] = {34, 233, 400, 100, 45}; -// -// float expectedUSD[length]; -// float resultUSD[length]; -// -// for (int i = 0; i < length; i++) { -// resultUSD[i] = toUSD(euro[i]); -// } -// -// /* Act */ -// -// for (int i = 0; i < length; i++) { -// expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; -// } -// -// /* Assert */ -// -// for (int i = 0; i < length; i++) { -// TEST_ASSERT_EQUAL_FLOAT(expectedUSD[i], resultUSD[i]); -// } -// -//} -// -//void test_toGBP(void) { -// -// /* Arrange */ -// -// int length = 5; -// float euro[] = {34, 233, 400, 100, 45}; -// -// float expectedGBP[length]; -// float resultGBP[length]; -// -// for (int i = 0; i < length; i++) { -// resultGBP[i] = toGBP(euro[i]); -// } -// -// /* Act */ -// -// for (int i = 0; i < length; i++) { -// expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; -// } -// -// /* Assert */ -// -// for (int i = 0; i < length; i++) { -// TEST_ASSERT_EQUAL_FLOAT(expectedGBP[i], resultGBP[i]); -// } -// -//} void test_convert(void) { - int length = 5; - float euro[] = {34, 233, 400, 100, 45}; + + /* Arrange */ + + int length = 10; + float euro[] = {34, 233, 400, 100, 45, 344, 767.32, 122, 435, 899}; float expectedUSD[length]; float expectedGBP[length]; @@ -77,15 +25,19 @@ void test_convert(void) { float resultGBP[length]; for (int i = 0; i < length; i++) { - expectedUSD[i] = euro[i] * CURRENT_USD_RATE_OF_ONE_EURO; - expectedGBP[i] = euro[i] * CURRENT_GBP_RATE_OF_ONE_EURO; + 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], 1); - resultGBP[i] = convert(euro[i], 2); + 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]);