From 94ec6389f0a68c6a700980536b8ae5af2f09335a Mon Sep 17 00:00:00 2001 From: Enrico Schellenberger Date: Mon, 5 Feb 2024 20:41:16 +0100 Subject: [PATCH] Unittest and refractoring Moved ConX functions to Taschenrechner.h and main_Taschenrechner.c and added Unittests --- src/main/c/ConvertMode.c | 353 ------------------------------- src/main/c/main_taschenrechner.c | 353 +++++++++++++++++++++++++++++++ src/main/c/taschenrechner.h | 13 ++ src/test/c/test_taschenrechner.c | 114 ++++++---- 4 files changed, 438 insertions(+), 395 deletions(-) diff --git a/src/main/c/ConvertMode.c b/src/main/c/ConvertMode.c index 0ebb3ae..351a973 100644 --- a/src/main/c/ConvertMode.c +++ b/src/main/c/ConvertMode.c @@ -115,364 +115,11 @@ double getValue(int choice) { printf("\nThe convertet result is %dlf %d", result, Distance[endingUnit]); break; - - } } - } -double ConMeter(double meter, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //mm to x - switch (endingUnit) - { - case 0: //1mm - return meter; - break; - - case 1: //0.1cm - return meter/10; - break; - - case 2: //0.001m - return meter/1000; - break; - - case 3: //0.000001km - return meter/1000000; - break; - - default: - break; - } - - case 1: //cm to x - switch (endingUnit) - { - case 0: //10 - return meter*10; - break; - - case 1: //1 - return meter; - break; - - case 2: //0.01 - return meter/100; - break; - - case 3: //0.00001 - return meter/100000; - break; - - default: - break; - } - - case 2: //m to x - switch (endingUnit) - { - case 0: //1000 - return meter*1000; - break; - - case 1: //100 - return meter*100; - break; - - case 2: //1 - return meter; - break; - - case 3: //0.001 - return meter/1000; - break; - - default: - break; - } - - case 3:// km to x - switch (endingUnit) - { - case 0: // 1000000 - return meter*1000000; - break; - - case 1: // 100000 - return meter*100000; - break; - - case 2: // 1000 m - return meter*1000; - break; - - case 3: //1 km - return meter; - break; - - default: - break; - } - - default: - break; - } -} - -double ConMeterToFoot(double distance, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //feet/inch to x - switch (endingUnit) - { - case 0: //feet/inch to feet/inch - return distance; - break; - - case 1: //feet/inch to meter - return distance / 3,28084; - break; - - default: - break; - } - - case 1: //meter to x - switch (endingUnit) - { - case 0: //meter to feet/inch - return distance * 3, 28084; - break; - - case 1: //feet/inch to feet/inch - return distance; - break; - - default: - break; - } - - default: - break; - } -} - -double ConKilometerToMiles(double distance, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //miles to x - switch (endingUnit) - { - case 0: //miles to miles - return distance; - break; - - case 1: //miles to kilometer - return distance * 1,60934; - break; - - default: - break; - } - - case 1: //kilometer to x - switch (endingUnit) - { - case 0: //kilometer to miles - return distance / 1, 60934; - break; - - case 1: //kilometer to kilometer - return distance; - break; - - default: - break; - } - - default: - break; - } -} -double ConGram(double weight, int startingUnit, int endingUnit) { - switch (startingUnit) - {//'mg', 'g', 'kg', 't' - case 0: //mg to x - switch (endingUnit) - { - case 0: //1 - return weight; - break; - - case 1: //0.001 - return weight / 1000; - break; - - case 2: //0.000001 - return weight / 1000000; - break; - - case 3: //0.000000001 - return weight / 1000000000; - break; - - default: - break; - } - - case 1: //g to x - switch (endingUnit) - { - case 0: //1000 - return weight * 1000; - break; - - case 1: //1 - return weight; - break; - - case 2: //0.01 - return weight / 100; - break; - - case 3: //0.000001 - return weight / 100000; - break; - - default: - break; - } - - case 2: //kg to x - switch (endingUnit) - { - case 0: //1000 - return weight * 1000; - break; - - case 1: //100 - return weight * 100; - break; - - case 2: //1 - return weight; - break; - - case 3: //0.001 - return weight / 1000; - break; - - default: - break; - } - - case 3://t to x - switch (endingUnit) - { - case 0: // 1000000000 - return weight * 1000000; - break; - - case 1: // 1000000 - return weight * 100000; - break; - - case 2: // 1000 - return weight * 1000; - break; - - case 3: //1 - return weight; - break; - - default: - break; - } - - default: - break; - } -} - -double ConGramToPounds(double weight, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //kg to x - switch (endingUnit) - { - case 0: //kg to pounds - return weight *= 2.20462; - break; - - case 1: //kg to kg - return weight; - break; - - default: - break; - } - - case 1: //pounds to x - switch (endingUnit) - { - case 0: //pounds to kg - return weight *= 0,453592; - break; - - case 1: //pounds to pounds - return weight; - break; - - default: - break; - } - - default: - break; - } -} - -double ConTemp(double temp, int startingUnit, int endingUnit) { - switch (startingUnit) - { - case 0: //celsius to x - switch (endingUnit) - { - case 0: //celsius to fahrenheit - return temp = (temp - 32) * 0, 55555555; - break; - - case 1: //celsius to celsius - return temp; - break; - - default: - break; - } - - case 1: //fahrenheit to x - switch (endingUnit) - { - case 0: //fahrenheit to celsius - return temp = (temp * 1,8) + 32; - break; - - case 1: //fahrenheit to fahrenheit - return temp; - break; - - default: - break; - } - - default: - break; - } -} double ConSpeed() { diff --git a/src/main/c/main_taschenrechner.c b/src/main/c/main_taschenrechner.c index 30fa51c..1a3c5f2 100644 --- a/src/main/c/main_taschenrechner.c +++ b/src/main/c/main_taschenrechner.c @@ -88,6 +88,359 @@ int bitwiseXOR(int num1, int num2) { return num1 ^ num2; } +//Conversion Functions +double ConMeter(double meter, int startingUnit, int endingUnit) { + switch (startingUnit) + { + case 0: //mm to x + switch (endingUnit) + { + case 0: //1mm + return meter; + break; + + case 1: //0.1cm + return meter / 10; + break; + + case 2: //0.001m + return meter / 1000; + break; + + case 3: //0.000001km + return meter / 1000000; + break; + + default: + break; + } + + case 1: //cm to x + switch (endingUnit) + { + case 0: //10 + return meter * 10; + break; + + case 1: //1 + return meter; + break; + + case 2: //0.01 + return meter / 100; + break; + + case 3: //0.00001 + return meter / 100000; + break; + + default: + break; + } + + case 2: //m to x + switch (endingUnit) + { + case 0: //1000 + return meter * 1000; + break; + + case 1: //100 + return meter * 100; + break; + + case 2: //1 + return meter; + break; + + case 3: //0.001 + return meter / 1000; + break; + + default: + break; + } + + case 3:// km to x + switch (endingUnit) + { + case 0: // 1000000 + return meter * 1000000; + break; + + case 1: // 100000 + return meter * 100000; + break; + + case 2: // 1000 m + return meter * 1000; + break; + + case 3: //1 km + return meter; + break; + + default: + break; + } + + default: + break; + } +} + +double ConMeterToFoot(double distance, int startingUnit, int endingUnit) { + switch (startingUnit) + { + case 0: //feet/inch to x + switch (endingUnit) + { + case 0: //feet/inch to feet/inch + return distance; + break; + + case 1: //feet/inch to meter + return distance / 3. 28084; + break; + + default: + break; + } + + case 1: //meter to x + switch (endingUnit) + { + case 0: //meter to feet/inch + return distance * 3. 28084; + break; + + case 1: //feet/inch to feet/inch + return distance; + break; + + default: + break; + } + + default: + break; + } +} + +double ConKilometerToMiles(double distance, int startingUnit, int endingUnit) { + switch (startingUnit) + { + case 0: //miles to x + switch (endingUnit) + { + case 0: //miles to miles + return distance; + break; + + case 1: //miles to kilometer + return distance * 1. 60934; + break; + + default: + break; + } + + case 1: //kilometer to x + switch (endingUnit) + { + case 0: //kilometer to miles + return distance / 1. 60934; + break; + + case 1: //kilometer to kilometer + return distance; + break; + + default: + break; + } + + default: + break; + } +} + +double ConGram(double weight, int startingUnit, int endingUnit) { + switch (startingUnit) + {//'mg', 'g', 'kg', 't' + case 0: //mg to x + switch (endingUnit) + { + case 0: //1 + return weight; + break; + + case 1: //0.001 + return weight / 1000; + break; + + case 2: //0.000001 + return weight / 1000000; + break; + + case 3: //0.000000001 + return weight / 1000000000; + break; + + default: + break; + } + + case 1: //g to x + switch (endingUnit) + { + case 0: //1000 + return weight * 1000; + break; + + case 1: //1 + return weight; + break; + + case 2: //0.01 + return weight / 100; + break; + + case 3: //0.000001 + return weight / 100000; + break; + + default: + break; + } + + case 2: //kg to x + switch (endingUnit) + { + case 0: //1000 + return weight * 1000; + break; + + case 1: //100 + return weight * 100; + break; + + case 2: //1 + return weight; + break; + + case 3: //0.001 + return weight / 1000; + break; + + default: + break; + } + + case 3://t to x + switch (endingUnit) + { + case 0: // 1000000000 + return weight * 1000000; + break; + + case 1: // 1000000 + return weight * 100000; + break; + + case 2: // 1000 + return weight * 1000; + break; + + case 3: //1 + return weight; + break; + + default: + break; + } + + default: + break; + } +} + +double ConGramToPounds(double weight, int startingUnit, int endingUnit) { + switch (startingUnit) + { + case 0: //kg to x + switch (endingUnit) + { + case 0: //kg to pounds + return weight *= 2.20462; + break; + + case 1: //kg to kg + return weight; + break; + + default: + break; + } + + case 1: //pounds to x + switch (endingUnit) + { + case 0: //pounds to kg + return weight *= 0, 453592; + break; + + case 1: //pounds to pounds + return weight; + break; + + default: + break; + } + + default: + break; + } +} + +double ConTemp(double temp, int startingUnit, int endingUnit) { + switch (startingUnit) + { + case 0: //celsius to x + switch (endingUnit) + { + case 0: //celsius to fahrenheit + return temp = (temp - 32) * 0, 55555555; + break; + + case 1: //celsius to celsius + return temp; + break; + + default: + break; + } + + case 1: //fahrenheit to x + switch (endingUnit) + { + case 0: //fahrenheit to celsius + return temp = (temp * 1, 8) + 32; + break; + + case 1: //fahrenheit to fahrenheit + return temp; + break; + + default: + break; + } + + default: + break; + } +} + //.. // graphMode void graphMode() { diff --git a/src/main/c/taschenrechner.h b/src/main/c/taschenrechner.h index ffd85ad..69dd7ed 100644 --- a/src/main/c/taschenrechner.h +++ b/src/main/c/taschenrechner.h @@ -56,4 +56,17 @@ int mode(int userChoice); int displayMenu(); +//Conversion Functions +double ConMeter(double meter, int startingUnit, int endingUnit); + +double ConMeterToFoot(double distance, int startingUnit, int endingUnit); + +double ConKilometerToMiles(double distance, int startingUnit, int endingUnit); + +double ConGram(double weight, int startingUnit, int endingUnit); + +double ConGramToPounds(double weight, int startingUnit, int endingUnit); + +double ConTemp(double temp, int startingUnit, int endingUnit); + #endif // TASCHENRECHNER_H diff --git a/src/test/c/test_taschenrechner.c b/src/test/c/test_taschenrechner.c index 6ad4cc7..5e16459 100644 --- a/src/test/c/test_taschenrechner.c +++ b/src/test/c/test_taschenrechner.c @@ -1,42 +1,72 @@ -#ifdef TEST - -#include "unity.h" - -#include "taschenrechner.h" - -void setUp(void) -{ -} - -void tearDown(void) -{ -} - -void test_addition(void) -{ - doulbe result = add(1, 2); - TEST_ASSERT_EQUAL(3, result); -} - -void test_minus(void) -{ - doulbe result = minus(3, 1); - TEST_ASSERT_EQUAL(2, result); -} - -void test_multiply(void) -{ - doulbe result = multiply(1, 2); - TEST_ASSERT_EQUAL(2, result); -} - -void test_divide(void) -{ - doulbe result = divide(4, 2); - TEST_ASSERT_EQUAL(2, result); - - doulbe result1 = divide(4, 0); - TEST_ASSERT_EQUAL(0, result1); -} - -#endif // TEST +#ifdef TEST + +#include "unity.h" + +#include "taschenrechner.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_addition(void) +{ + doulbe result = add(1, 2); + TEST_ASSERT_EQUAL(3, result); +} + +void test_minus(void) +{ + doulbe result = minus(3, 1); + TEST_ASSERT_EQUAL(2, result); +} + +void test_multiply(void) +{ + doulbe result = multiply(1, 2); + TEST_ASSERT_EQUAL(2, result); +} + +void test_divide(void) +{ + doulbe result = divide(4, 2); + TEST_ASSERT_EQUAL(2, result); + + doulbe result1 = divide(4, 0); + TEST_ASSERT_EQUAL(0, result1); +} + +void test_ConMeter(void) {//5 mm to cm + double result = ConMeter(5, 0, 1); + TEST_ASSERT_EQUAL(50, result); +} + +void test_ConMeterToFoot(void) {//5 m to foot + double result = ConMeterToFoot(5, 0, 1); + TEST_ASSERT_EQUAL(16.4042, result); +} +void test_ConKilometerToMiles(void) {//5 miles to km + double result = ConKilometerToMiles(5, 0, 1); + TEST_ASSERT_EQUAL(3.10686, result); +} + +void test_ConGram(void) {//5 mg to g + double result = ConGram(5, 0, 1); + TEST_ASSERT_EQUAL(5000, result); +} + +void test_ConGramToPounds(void) {//5 kg to pounds + double result = ConGramToPounds(5, 0, 1); + TEST_ASSERT_EQUAL(11.0231, result); +} + +void test_ConTemp(void) {//5 celsius to fahrenheit + double result = ConTemp(5, 0, 1); + TEST_ASSERT_EQUAL(41, result); +} + + +#endif // TEST