From abd46fe614c6e08b19c4d5905942e2a686ef204c Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 31 Jan 2024 08:54:41 +0100 Subject: [PATCH] refactoring: unify data types full adder and testing --- src/addition.c | 8 ++--- src/addition.h | 2 +- test/test_addition.c | 78 ++++++++++++++++++++++---------------------- 3 files changed, 44 insertions(+), 44 deletions(-) diff --git a/src/addition.c b/src/addition.c index 4d34e9c..597d458 100644 --- a/src/addition.c +++ b/src/addition.c @@ -1,17 +1,17 @@ #include "addition.h" #include -void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) { +void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number1, unsigned int number2, unsigned int carry) { sum[0] = number1 ^ number2 ^ carry; nextcarry[0] = (number1 & number2) | (number1 & carry) | (number2 & carry); } unsigned int addition(unsigned int a, unsigned int b) { - int s[1]; - int nc[1] = {0}; + unsigned int s[1]; + unsigned int nc[1] = {0}; full_adder(s, nc, a, b, 0); - return (unsigned int) s[0] ^ (nc[0] << 1); + return s[0] ^ (nc[0] << 1); } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 8a492c8..ea892ff 100644 --- a/src/addition.h +++ b/src/addition.h @@ -1,7 +1,7 @@ #ifndef ADDITION_H #define ADDITION_H -void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry); +void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number1, unsigned int number2, unsigned int carry); unsigned int addition(unsigned int a, unsigned int b); diff --git a/test/test_addition.c b/test/test_addition.c index 6177f55..9b74fe9 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -16,28 +16,28 @@ void tearDown(void) void test_addition_full_adder_nullplusnullgleichnull(void) { - int result[1]; - int expected = 0; + unsigned int result[1]; + unsigned int expected = 0; full_adder(result, carry, 0, 0, 0); - TEST_ASSERT_EQUAL_INT(expected, result[0]); + TEST_ASSERT_EQUAL_UINT(expected, result[0]); } void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) { - int result[1]; - int expected = 1; + unsigned int result[1]; + unsigned int expected = 1; full_adder(result, carry, 0, 0, 1); - TEST_ASSERT_EQUAL_INT(expected, result[0]); + TEST_ASSERT_EQUAL_UINT(expected, result[0]); } void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) { - int result[5]; - int expected[5] = { 0, 1, 1, 1, 1}; + unsigned int result[5]; + unsigned int expected[5] = { 0, 1, 1, 1, 1}; full_adder((result+0), carry, 1, 0, 1); full_adder((result+1), carry, 0, 1, 0); @@ -45,17 +45,17 @@ void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) full_adder((result+3), carry, 0, 0, 1); full_adder((result+4), carry, 1, 1, 1); - TEST_ASSERT_EQUAL_INT(expected[0], result[0]); - TEST_ASSERT_EQUAL_INT(expected[1], result[1]); - TEST_ASSERT_EQUAL_INT(expected[2], result[2]); - TEST_ASSERT_EQUAL_INT(expected[3], result[3]); - TEST_ASSERT_EQUAL_INT(expected[4], result[4]); + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } void test_addition_full_adder_completesumwithcarry(void) { - int result[5], carryresult[5]; - int expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0}; + unsigned int result[5], carryresult[5]; + unsigned int 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); @@ -63,44 +63,44 @@ void test_addition_full_adder_completesumwithcarry(void) full_adder(result+3, carryresult+3, 0, 1, 0); full_adder(result+4, carryresult+4, 0, 0, 0); - TEST_ASSERT_EQUAL_INT(expected[0], result[0]); - TEST_ASSERT_EQUAL_INT(expected[1], result[1]); - TEST_ASSERT_EQUAL_INT(expected[2], result[2]); - TEST_ASSERT_EQUAL_INT(expected[3], result[3]); - TEST_ASSERT_EQUAL_INT(expected[4], result[4]); - TEST_ASSERT_EQUAL_INT(expectedcarry[0], carryresult[0]); - TEST_ASSERT_EQUAL_INT(expectedcarry[1], carryresult[1]); - TEST_ASSERT_EQUAL_INT(expectedcarry[2], carryresult[2]); - TEST_ASSERT_EQUAL_INT(expectedcarry[3], carryresult[3]); - TEST_ASSERT_EQUAL_INT(expectedcarry[4], carryresult[4]); + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); + TEST_ASSERT_EQUAL_UINT(expectedcarry[0], carryresult[0]); + TEST_ASSERT_EQUAL_UINT(expectedcarry[1], carryresult[1]); + TEST_ASSERT_EQUAL_UINT(expectedcarry[2], carryresult[2]); + TEST_ASSERT_EQUAL_UINT(expectedcarry[3], carryresult[3]); + TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]); } void test_addition_addition_basecasezeropluszeroequalzero(void) { - int result; - int expected = 0; + unsigned int result; + unsigned int expected = 0; result = addition(0, 0); - TEST_ASSERT_EQUAL_INT(expected, result); + TEST_ASSERT_EQUAL_UINT(expected, result); } void test_addition_addition_basecaseonescolumns(void) { - int result[2]; - int expected[2] = { 1, 1 }; + unsigned int result[2]; + unsigned int expected[2] = { 1, 1 }; result[0] = addition(1, 0); result[1] = addition(0, 1); - TEST_ASSERT_EQUAL_INT(expected[0], result[0]); - TEST_ASSERT_EQUAL_INT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); } void test_addition_addition_basecasetotenscolumns(void) { - int result[5]; - int expected[5] = { 3, 3, 2, 3, 3 }; + unsigned int result[5]; + unsigned int expected[5] = { 3, 3, 2, 3, 3 }; result[0] = addition(0, 3); result[1] = addition(1, 2); @@ -108,11 +108,11 @@ void test_addition_addition_basecasetotenscolumns(void) result[3] = addition(2, 1); result[4] = addition(3, 0); - TEST_ASSERT_EQUAL_INT(expected[0], result[0]); - TEST_ASSERT_EQUAL_INT(expected[1], result[1]); - TEST_ASSERT_EQUAL_INT(expected[2], result[2]); - TEST_ASSERT_EQUAL_INT(expected[3], result[3]); - TEST_ASSERT_EQUAL_INT(expected[4], result[4]); + TEST_ASSERT_EQUAL_UINT(expected[0], result[0]); + TEST_ASSERT_EQUAL_UINT(expected[1], result[1]); + TEST_ASSERT_EQUAL_UINT(expected[2], result[2]); + TEST_ASSERT_EQUAL_UINT(expected[3], result[3]); + TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } #endif // TEST