diff --git a/src/division.h b/src/division.h new file mode 100644 index 0000000..054648b --- /dev/null +++ b/src/division.h @@ -0,0 +1,13 @@ +// +// Created by Justin Trausch on 06.02.2024. +// + +#ifndef LINKES_TWIX_DIVISION_H +#define LINKES_TWIX_DIVISION_H + +int divisionInt(int a, int b); +double divisionDouble(double a, double b); +int divisionByTwo(int a, int b); + + +#endif //LINKES_TWIX_DIVISION_H diff --git a/src/divsion.c b/src/divsion.c new file mode 100644 index 0000000..5f1dcbd --- /dev/null +++ b/src/divsion.c @@ -0,0 +1,26 @@ +#include "division.h" + +int divisionInt(int a, int b) { + if (b != 0) { + return a / b; + } + else { + return 0; + } +} + +double divisionDouble(double a, double b) { + if (b != 0) { + return a / b; + } + else { + return 0; + } +} + +int divisionByTwo(int a, int b) { + if (b == 0){ + return a; +} + return a >> b; +} diff --git a/test/test_division.c b/test/test_division.c new file mode 100644 index 0000000..ccf59c7 --- /dev/null +++ b/test/test_division.c @@ -0,0 +1,61 @@ +#ifdef TEST + +#include "unity.h" + +#include "division.h" +#include "divsion.c" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_division_1(void) +{ + int result, expected = 5; + + result = divisionInt(10,2); + + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_division_1_durch_0(void) +{ + int result, expected = 0; + + result = divisionInt(10,0); + + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_division_2(void) +{ + double result, expected = 5.0; + + result = divisionDouble(10.0,2.0); + + TEST_ASSERT_EQUAL_DOUBLE(expected, result); +} + +void test_bitshift_druch_0(void) +{ + int result, expected = 16; + + result = divisionByTwo(16,0); + + TEST_ASSERT_EQUAL_INT(expected, result); +} + +void test_division_3(void) +{ + int result, expected = 4; + + result = divisionByTwo(16,2); + + TEST_ASSERT_EQUAL_INT(expected, result); +} + +#endif // TEST \ No newline at end of file