Browse Source

Unittest and refractoring Moved ConX functions to Taschenrechner.h and main_Taschenrechner.c and added Unittests

remotes/origin/feature
Enrico Schellenberger 11 months ago
parent
commit
94ec6389f0
  1. 353
      src/main/c/ConvertMode.c
  2. 353
      src/main/c/main_taschenrechner.c
  3. 13
      src/main/c/taschenrechner.h
  4. 30
      src/test/c/test_taschenrechner.c

353
src/main/c/ConvertMode.c

@ -115,364 +115,11 @@ double getValue(int choice) {
printf("\nThe convertet result is %dlf %d", result, Distance[endingUnit]); printf("\nThe convertet result is %dlf %d", result, Distance[endingUnit]);
break; 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() { double ConSpeed() {

353
src/main/c/main_taschenrechner.c

@ -88,6 +88,359 @@ int bitwiseXOR(int num1, int num2) {
return num1 ^ 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 // graphMode
void graphMode() { void graphMode() {

13
src/main/c/taschenrechner.h

@ -56,4 +56,17 @@ int mode(int userChoice);
int displayMenu(); 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 #endif // TASCHENRECHNER_H

30
src/test/c/test_taschenrechner.c

@ -39,4 +39,34 @@ void test_divide(void)
TEST_ASSERT_EQUAL(0, result1); 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 #endif // TEST
Loading…
Cancel
Save