From 944e3a15446f334563f26ff008fc9c9ccf3f4d61 Mon Sep 17 00:00:00 2001 From: Enrico Schellenberger Date: Sat, 3 Feb 2024 11:56:50 +0100 Subject: [PATCH] unit tests added and adjusted calculations with double --- src/main/c/main_taschenrechner.c | 11 +++++++---- src/main/c/taschenrechner.h | 8 ++++---- src/test/c/test_taschenrechner.c | 27 +++++++++++++++++++++------ 3 files changed, 32 insertions(+), 14 deletions(-) diff --git a/src/main/c/main_taschenrechner.c b/src/main/c/main_taschenrechner.c index af494d1..f1fccac 100644 --- a/src/main/c/main_taschenrechner.c +++ b/src/main/c/main_taschenrechner.c @@ -6,19 +6,22 @@ #include "taschenrechner.h" -int add(int a, int b) { +doulbe add(doulbe a, doulbe b) { return a + b; } -int minus (int a, int b){ +doulbe minus (doulbe a, doulbe b){ return a - b; } -int multiply(int a, int b) { +doulbe multiply(doulbe a, doulbe b) { return a * b; } -int divide(int a, int b) { +doulbe divide(doulbe a, doulbe b) { + if (b == 0) { + return 0; + } return a / b; } diff --git a/src/main/c/taschenrechner.h b/src/main/c/taschenrechner.h index 1053da9..7b548f9 100644 --- a/src/main/c/taschenrechner.h +++ b/src/main/c/taschenrechner.h @@ -1,13 +1,13 @@ #ifndef TASCHENRECHNER_H #define TASCHENRECHNER_H -int add(int a, int b); +doulbe add(doulbe a, doulbe b); -int minus(int a, int b); +doulbe minus(doulbe a, doulbe b); -int multiply(int a, int b); +doulbe multiply(doulbe a, doulbe b); -int divide(int a, int b); +doulbe divide(doulbe a, doulbe b); // Square root function double squareRootFunction(double x); diff --git a/src/test/c/test_taschenrechner.c b/src/test/c/test_taschenrechner.c index d3fc8c9..6ad4cc7 100644 --- a/src/test/c/test_taschenrechner.c +++ b/src/test/c/test_taschenrechner.c @@ -14,14 +14,29 @@ void tearDown(void) void test_addition(void) { - /* arrange */ - char* result; - char expected[] = "2\n"; + doulbe result = add(1, 2); + TEST_ASSERT_EQUAL(3, result); +} + +void test_minus(void) +{ + doulbe result = minus(3, 1); + TEST_ASSERT_EQUAL(2, result); +} - /* act */ - result = convert(2); +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); - /* assert */ + doulbe result1 = divide(4, 0); + TEST_ASSERT_EQUAL(0, result1); } #endif // TEST