From 84196e721e41f0cdd7a3066479205e8eff96361a Mon Sep 17 00:00:00 2001 From: Eric Bagus Date: Thu, 8 Feb 2024 17:25:40 +0100 Subject: [PATCH] Added arithmetic division including unittest --- src/arithmeticDivision.c | 17 +++++++++++++++++ src/arithmeticDivision.h | 6 ++++++ test/test_arithmeticDivision.c | 19 +++++++++++++++++++ 3 files changed, 42 insertions(+) create mode 100644 src/arithmeticDivision.c create mode 100644 src/arithmeticDivision.h create mode 100644 test/test_arithmeticDivision.c diff --git a/src/arithmeticDivision.c b/src/arithmeticDivision.c new file mode 100644 index 0000000..7cdfc7e --- /dev/null +++ b/src/arithmeticDivision.c @@ -0,0 +1,17 @@ +#include "arithmeticDivision.h" +#include +#include +#include + +int* division_integer(int dividend, int divisor) { + if(divisor == 0) { + return NULL; + } + // Overflow protection + if (dividend == INT_MIN && divisor == -1) { + return NULL; + } + int* result = malloc(sizeof(int)); + *result = dividend / divisor; + return result; +} \ No newline at end of file diff --git a/src/arithmeticDivision.h b/src/arithmeticDivision.h new file mode 100644 index 0000000..e4080b7 --- /dev/null +++ b/src/arithmeticDivision.h @@ -0,0 +1,6 @@ +#ifndef THEADMIRALS_ARITHMETICDIVISION_H +#define THEADMIRALS_ARITHMETICDIVISION_H + +int* division_integer(int, int); + +#endif //THEADMIRALS_ARITHMETICDIVISION_H diff --git a/test/test_arithmeticDivision.c b/test/test_arithmeticDivision.c new file mode 100644 index 0000000..6428ea0 --- /dev/null +++ b/test/test_arithmeticDivision.c @@ -0,0 +1,19 @@ +#include "../src/arithmeticDivision.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + + +void test_arithmeticDivision_numberdividedbynumberequalsnumber(void) { + int expectedResult = 2; + int* result; + result = division_integer(14, 7); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +}