From fccee6873ca32eb0be9864aaf7f71d3f16547d32 Mon Sep 17 00:00:00 2001 From: Eric Bagus Date: Thu, 8 Feb 2024 17:05:53 +0100 Subject: [PATCH] Added arithmetic addition including unittest --- src/arithmeticAddition.c | 13 +++++++++++++ src/arithmeticAddition.h | 6 ++++++ test/test_arithmeticAddition.c | 24 ++++++++++++++++++++++++ 3 files changed, 43 insertions(+) create mode 100644 src/arithmeticAddition.c create mode 100644 src/arithmeticAddition.h create mode 100644 test/test_arithmeticAddition.c diff --git a/src/arithmeticAddition.c b/src/arithmeticAddition.c new file mode 100644 index 0000000..d75b63f --- /dev/null +++ b/src/arithmeticAddition.c @@ -0,0 +1,13 @@ +#include "arithmeticAddition.h" +#include +#include +#include + +int* addition_integer(int num1, int num2) { + if ((num2 > 0 && num1 > INT_MAX - num2) || (num2 < 0 && num1 < INT_MIN - num2)) { + return NULL; + } + int* result = malloc(sizeof(int)); + *result = num1 + num2; + return result; +} \ No newline at end of file diff --git a/src/arithmeticAddition.h b/src/arithmeticAddition.h new file mode 100644 index 0000000..92907ff --- /dev/null +++ b/src/arithmeticAddition.h @@ -0,0 +1,6 @@ +#ifndef THEADMIRALS_ARITHMETICADDITION_H +#define THEADMIRALS_ARITHMETICADDITION_H + +int* addition_integer(int, int); + +#endif //THEADMIRALS_ARITHMETICADDITION_H diff --git a/test/test_arithmeticAddition.c b/test/test_arithmeticAddition.c new file mode 100644 index 0000000..a7089a6 --- /dev/null +++ b/test/test_arithmeticAddition.c @@ -0,0 +1,24 @@ +#include "../src/arithmeticAddition.h" +#include "unity.h" +#include "limits.h" + +void setUp(void) { + // set stuff up here +} + +void tearDown(void) { + // clean stuff up here +} + +void test_arithmeticAddition_numberplusnumberequalsnumber(void) { + int expectedResult = 8; + int* result; + result = addition_integer(5, 3); + TEST_ASSERT_EQUAL_INT(expectedResult, *result); +} + +void test_arithmeticAddition_numberplusmaxintegervalueequalsnull(void) { + int* result; + result = addition_integer(INT_MAX, 1); + TEST_ASSERT_NULL(result); +} \ No newline at end of file