From a1f7c97c450d4f40bf9f6f77c9e2f7b2e59386b6 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 1 Feb 2024 08:35:09 +0100 Subject: [PATCH] refactoring: functions addition and full_adder data type change with typedef num --- src/addition.c | 8 ++++---- src/addition.h | 6 ++++-- test/test_addition.c | 18 +++++++++--------- 3 files changed, 17 insertions(+), 15 deletions(-) diff --git a/src/addition.c b/src/addition.c index d56f705..0bdb599 100644 --- a/src/addition.c +++ b/src/addition.c @@ -1,16 +1,16 @@ #include "addition.h" #include -void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number1, unsigned int number2, unsigned int carry) { +void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry) { sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2); nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2)); } -unsigned int addition(unsigned int number1, unsigned int number2) { +unsigned int addition(num number1, num number2) { unsigned int result = 0; - unsigned int sum[1] = {0}; - unsigned int nextcarry[1] = {0}; + num sum[1] = {0}; + num nextcarry[1] = {0}; for (int i = 0; i < 2; i++) { diff --git a/src/addition.h b/src/addition.h index ea892ff..a3b2b82 100644 --- a/src/addition.h +++ b/src/addition.h @@ -1,8 +1,10 @@ #ifndef ADDITION_H #define ADDITION_H -void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number1, unsigned int number2, unsigned int carry); +typedef unsigned int num; -unsigned int addition(unsigned int a, unsigned int b); +void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); + +unsigned int addition(num a, num b); #endif // ADDITION_H diff --git a/test/test_addition.c b/test/test_addition.c index 9b74fe9..36bfa7f 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -4,7 +4,7 @@ #include "addition.h" -int carry[1]; +num carry[1]; void setUp(void) { @@ -16,8 +16,8 @@ void tearDown(void) void test_addition_full_adder_nullplusnullgleichnull(void) { - unsigned int result[1]; - unsigned int expected = 0; + num result[1]; + num expected = 0; full_adder(result, carry, 0, 0, 0); @@ -26,8 +26,8 @@ void test_addition_full_adder_nullplusnullgleichnull(void) void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) { - unsigned int result[1]; - unsigned int expected = 1; + num result[1]; + num expected = 1; full_adder(result, carry, 0, 0, 1); @@ -36,8 +36,8 @@ void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) { - unsigned int result[5]; - unsigned int expected[5] = { 0, 1, 1, 1, 1}; + num result[5]; + num expected[5] = { 0, 1, 1, 1, 1}; full_adder((result+0), carry, 1, 0, 1); full_adder((result+1), carry, 0, 1, 0); @@ -54,8 +54,8 @@ void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) void test_addition_full_adder_completesumwithcarry(void) { - unsigned int result[5], carryresult[5]; - unsigned int expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0}; + num result[5], carryresult[5]; + num expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0}; full_adder(result+0, carryresult+0, 0, 1, 1); full_adder(result+1, carryresult+1, 1, 1, 0);