From 9c9ca9a1f191f24b5fcf6a403f4bdf554f9e8494 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 16:34:27 +0100 Subject: [PATCH 01/23] addition full adder base case 0+0=0 --- src/addition.c | 6 ++++++ src/addition.h | 6 ++++++ test/test_addition.c | 20 ++++++++++++++++++++ 3 files changed, 32 insertions(+) create mode 100644 src/addition.c create mode 100644 src/addition.h create mode 100644 test/test_addition.c diff --git a/src/addition.c b/src/addition.c new file mode 100644 index 0000000..9cdc24d --- /dev/null +++ b/src/addition.c @@ -0,0 +1,6 @@ +#include "addition.h" +#include + +void volladdierer (int* s, int* nc, int a, int b, int c) { + s[0] = 0; +} \ No newline at end of file diff --git a/src/addition.h b/src/addition.h new file mode 100644 index 0000000..0f01e7e --- /dev/null +++ b/src/addition.h @@ -0,0 +1,6 @@ +#ifndef ADDITION_H +#define ADDITION_H + +void volladdierer (int* s, int* nc, int a, int b, int c); + +#endif // ADDITION_H diff --git a/test/test_addition.c b/test/test_addition.c new file mode 100644 index 0000000..1d9823c --- /dev/null +++ b/test/test_addition.c @@ -0,0 +1,20 @@ +#ifdef TEST + +#include "unity.h" + +#include "addition.h" + +void setUp(void) +{ +} + +void tearDown(void) +{ +} + +void test_addition_NeedToImplement(void) +{ + TEST_IGNORE_MESSAGE("Need to Implement addition"); +} + +#endif // TEST From 9bb254442eaa742740ae803a9afa938cdac428a9 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 17:37:03 +0100 Subject: [PATCH 02/23] testing: addition full adder base case 0+0=0 --- test/test_addition.c | 11 +++++++++-- 1 file changed, 9 insertions(+), 2 deletions(-) diff --git a/test/test_addition.c b/test/test_addition.c index 1d9823c..e18db0c 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -4,6 +4,8 @@ #include "addition.h" +int* nullcarry = NULL; + void setUp(void) { } @@ -12,9 +14,14 @@ void tearDown(void) { } -void test_addition_NeedToImplement(void) +void test_addition_volladdierer_nullplusnullgleichnull(void) { - TEST_IGNORE_MESSAGE("Need to Implement addition"); + int result[1]; + int expected = 0; + + volladdierer(result, nullcarry, 0, 0, 0); + + TEST_ASSERT_EQUAL_INT(expected, result[0]); } #endif // TEST From b664da82e1520912e11d600b2859e8509675d805 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 17:40:39 +0100 Subject: [PATCH 03/23] addition full adder base case 0+0=0 with carryover --- src/addition.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 9cdc24d..fbf355a 100644 --- a/src/addition.c +++ b/src/addition.c @@ -2,5 +2,5 @@ #include void volladdierer (int* s, int* nc, int a, int b, int c) { - s[0] = 0; + s[0] = c; } \ No newline at end of file From 60596dba57fee83023f775dcb6c7de2158ea611d Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 17:46:10 +0100 Subject: [PATCH 04/23] testing: addition full adder base case 0+0=0 with carryover --- test/test_addition.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index e18db0c..02d69c2 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -24,4 +24,14 @@ void test_addition_volladdierer_nullplusnullgleichnull(void) TEST_ASSERT_EQUAL_INT(expected, result[0]); } +void test_addition_volladdierer_nullplusnullgleichnullmituebertrag(void) +{ + int result[1]; + int expected = 1; + + volladdierer(result, nullcarry, 0, 0, 1); + + TEST_ASSERT_EQUAL_INT(expected, result[0]); +} + #endif // TEST From 1ed9c0bf69cc4cc6cf45b86ecbc7a5a660e3a2b7 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 18:14:36 +0100 Subject: [PATCH 05/23] refactoring: function full_adder and variables --- src/addition.c | 4 ++-- src/addition.h | 2 +- test/test_addition.c | 8 ++++---- 3 files changed, 7 insertions(+), 7 deletions(-) diff --git a/src/addition.c b/src/addition.c index fbf355a..192513a 100644 --- a/src/addition.c +++ b/src/addition.c @@ -1,6 +1,6 @@ #include "addition.h" #include -void volladdierer (int* s, int* nc, int a, int b, int c) { - s[0] = c; +void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) { + sum[0] = carry; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index 0f01e7e..abb1cd4 100644 --- a/src/addition.h +++ b/src/addition.h @@ -1,6 +1,6 @@ #ifndef ADDITION_H #define ADDITION_H -void volladdierer (int* s, int* nc, int a, int b, int c); +void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry); #endif // ADDITION_H diff --git a/test/test_addition.c b/test/test_addition.c index 02d69c2..de01d4f 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -14,22 +14,22 @@ void tearDown(void) { } -void test_addition_volladdierer_nullplusnullgleichnull(void) +void test_addition_full_adder_nullplusnullgleichnull(void) { int result[1]; int expected = 0; - volladdierer(result, nullcarry, 0, 0, 0); + full_adder(result, nullcarry, 0, 0, 0); TEST_ASSERT_EQUAL_INT(expected, result[0]); } -void test_addition_volladdierer_nullplusnullgleichnullmituebertrag(void) +void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) { int result[1]; int expected = 1; - volladdierer(result, nullcarry, 0, 0, 1); + full_adder(result, nullcarry, 0, 0, 1); TEST_ASSERT_EQUAL_INT(expected, result[0]); } From 7db5125bda9a89408ac765e6a9340eed5342cc62 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 20:00:12 +0100 Subject: [PATCH 06/23] addition full adder complete sum without carry --- src/addition.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 192513a..753d77d 100644 --- a/src/addition.c +++ b/src/addition.c @@ -2,5 +2,5 @@ #include void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) { - sum[0] = carry; + sum[0] = number1 ^ number2 ^ carry; } \ No newline at end of file From e8e328744af6a37b7f2faebdbc1653b8b653433c Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 21:17:43 +0100 Subject: [PATCH 07/23] testing: addition full adder complete sum without carry --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index de01d4f..51a9c46 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -34,4 +34,22 @@ void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) TEST_ASSERT_EQUAL_INT(expected, result[0]); } +void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) +{ + int result[5]; + int expected[5] = { 0, 1, 1, 1, 1}; + + full_adder((result+0), nullcarry, 1, 0, 1); + full_adder((result+1), nullcarry, 0, 1, 0); + full_adder((result+2), nullcarry, 1, 0, 0); + full_adder((result+3), nullcarry, 0, 0, 1); + full_adder((result+4), nullcarry, 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]); +} + #endif // TEST From 6ba44193daf7c6b6ee7e59218117e7ef575c53e1 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 21:26:48 +0100 Subject: [PATCH 08/23] refactoring: full adder preparation for using nextcarry --- test/test_addition.c | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/test/test_addition.c b/test/test_addition.c index 51a9c46..3678b7f 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -4,7 +4,7 @@ #include "addition.h" -int* nullcarry = NULL; +int carry[1]; void setUp(void) { @@ -19,7 +19,7 @@ void test_addition_full_adder_nullplusnullgleichnull(void) int result[1]; int expected = 0; - full_adder(result, nullcarry, 0, 0, 0); + full_adder(result, carry, 0, 0, 0); TEST_ASSERT_EQUAL_INT(expected, result[0]); } @@ -29,7 +29,7 @@ void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void) int result[1]; int expected = 1; - full_adder(result, nullcarry, 0, 0, 1); + full_adder(result, carry, 0, 0, 1); TEST_ASSERT_EQUAL_INT(expected, result[0]); } @@ -39,11 +39,11 @@ void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) int result[5]; int expected[5] = { 0, 1, 1, 1, 1}; - full_adder((result+0), nullcarry, 1, 0, 1); - full_adder((result+1), nullcarry, 0, 1, 0); - full_adder((result+2), nullcarry, 1, 0, 0); - full_adder((result+3), nullcarry, 0, 0, 1); - full_adder((result+4), nullcarry, 1, 1, 1); + full_adder((result+0), carry, 1, 0, 1); + full_adder((result+1), carry, 0, 1, 0); + full_adder((result+2), carry, 1, 0, 0); + 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]); From 4d7ac5d3522e632c95c58f5739c80c46f018ef2e Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 29 Jan 2024 21:29:24 +0100 Subject: [PATCH 09/23] addition full adder complete sum with carry --- src/addition.c | 2 ++ 1 file changed, 2 insertions(+) diff --git a/src/addition.c b/src/addition.c index 753d77d..6d7ca86 100644 --- a/src/addition.c +++ b/src/addition.c @@ -3,4 +3,6 @@ void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) { sum[0] = number1 ^ number2 ^ carry; + + nextcarry[0] = (number1 & number2) | (number1 & carry) | (number2 & carry); } \ No newline at end of file From 505b48c0476e99bb880c2d296a91920455a47afa Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 30 Jan 2024 08:50:33 +0100 Subject: [PATCH 10/23] testing: addition full adder complete sum with carry --- test/test_addition.c | 23 +++++++++++++++++++++++ 1 file changed, 23 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 3678b7f..9596038 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -52,4 +52,27 @@ void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void) TEST_ASSERT_EQUAL_INT(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}; + + full_adder(result+0, carryresult+0, 0, 1, 1); + full_adder(result+1, carryresult+1, 1, 1, 0); + full_adder(result+2, carryresult+2, 1, 1, 1); + 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]); +} + #endif // TEST From a75b5ab0cfdf7bd67288463ce72d41bf21b24f5c Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 30 Jan 2024 18:40:12 +0100 Subject: [PATCH 11/23] addition base case 0+0=0 --- src/addition.c | 4 ++++ src/addition.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/addition.c b/src/addition.c index 6d7ca86..aaf9b6a 100644 --- a/src/addition.c +++ b/src/addition.c @@ -5,4 +5,8 @@ void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) sum[0] = number1 ^ number2 ^ carry; nextcarry[0] = (number1 & number2) | (number1 & carry) | (number2 & carry); +} + +unsigned int addition(unsigned int a, unsigned int b) { + return 0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index abb1cd4..8a492c8 100644 --- a/src/addition.h +++ b/src/addition.h @@ -3,4 +3,6 @@ void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry); +unsigned int addition(unsigned int a, unsigned int b); + #endif // ADDITION_H From 320e7fd6f41e636cecec2d537e4e31b59d03e048 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 30 Jan 2024 18:45:15 +0100 Subject: [PATCH 12/23] testing: addition base case 0+0=0 --- test/test_addition.c | 10 ++++++++++ 1 file changed, 10 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 9596038..8af2427 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -75,4 +75,14 @@ void test_addition_full_adder_completesumwithcarry(void) TEST_ASSERT_EQUAL_INT(expectedcarry[4], carryresult[4]); } +void test_addition_addition_basecasezeropluszeroequalzero(void) +{ + int result; + int expected = 0; + + result = addition(0, 0); + + TEST_ASSERT_EQUAL_INT(expected, result); +} + #endif // TEST From 627200af59bae70b0f008627c35c3e617d44b98e Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 30 Jan 2024 19:03:14 +0100 Subject: [PATCH 13/23] addition base case onecolumns --- src/addition.c | 7 ++++++- 1 file changed, 6 insertions(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index aaf9b6a..8b42dc7 100644 --- a/src/addition.c +++ b/src/addition.c @@ -8,5 +8,10 @@ void full_adder (int* sum, int* nextcarry, int number1, int number2, int carry) } unsigned int addition(unsigned int a, unsigned int b) { - return 0; + int s[1]; + int nc[1] = {0}; + + full_adder(s, nc, a, b, 0); + + return (unsigned int) s[0]; } \ No newline at end of file From 721c04c3b4bd231f125c31d929f19f41d4414acf Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Tue, 30 Jan 2024 19:05:00 +0100 Subject: [PATCH 14/23] testing: addition base case onecolumns --- test/test_addition.c | 12 ++++++++++++ 1 file changed, 12 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 8af2427..02db089 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -85,4 +85,16 @@ void test_addition_addition_basecasezeropluszeroequalzero(void) TEST_ASSERT_EQUAL_INT(expected, result); } +void test_addition_addition_basecaseonescolumns(void) +{ + int result[2]; + 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]); +} + #endif // TEST From faade3302988a9d5598b392dc9e08c3410c014d0 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 31 Jan 2024 08:36:22 +0100 Subject: [PATCH 15/23] addition base case to tenscolumns --- src/addition.c | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/src/addition.c b/src/addition.c index 8b42dc7..4d34e9c 100644 --- a/src/addition.c +++ b/src/addition.c @@ -12,6 +12,6 @@ unsigned int addition(unsigned int a, unsigned int b) { int nc[1] = {0}; full_adder(s, nc, a, b, 0); - - return (unsigned int) s[0]; + + return (unsigned int) s[0] ^ (nc[0] << 1); } \ No newline at end of file From fe1129bef6be2bfc1cd46be193ec428e6c4d9742 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 31 Jan 2024 08:37:42 +0100 Subject: [PATCH 16/23] testing: addition base case to tenscolumns --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 02db089..6177f55 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -97,4 +97,22 @@ void test_addition_addition_basecaseonescolumns(void) TEST_ASSERT_EQUAL_INT(expected[1], result[1]); } +void test_addition_addition_basecasetotenscolumns(void) +{ + int result[5]; + int expected[5] = { 3, 3, 2, 3, 3 }; + + result[0] = addition(0, 3); + result[1] = addition(1, 2); + result[2] = addition(1, 1); + 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]); +} + #endif // TEST From abd46fe614c6e08b19c4d5905942e2a686ef204c Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 31 Jan 2024 08:54:41 +0100 Subject: [PATCH 17/23] 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 From a237f7651f55d2534ecd4617e2b51f120c79dbd7 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Wed, 31 Jan 2024 09:54:01 +0100 Subject: [PATCH 18/23] addition preperation for greater numbers --- src/addition.c | 18 +++++++++++++----- 1 file changed, 13 insertions(+), 5 deletions(-) diff --git a/src/addition.c b/src/addition.c index 597d458..d165ae4 100644 --- a/src/addition.c +++ b/src/addition.c @@ -2,16 +2,24 @@ #include void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number1, unsigned int number2, unsigned int carry) { - sum[0] = number1 ^ number2 ^ carry; + sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2); - nextcarry[0] = (number1 & number2) | (number1 & carry) | (number2 & carry); + nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2)); } unsigned int addition(unsigned int a, unsigned int b) { - unsigned int s[1]; + unsigned int erg = 0; + unsigned int s[1] = {0}; unsigned int nc[1] = {0}; - full_adder(s, nc, a, b, 0); + for (int i = 0; i < 2; i++) + { + full_adder(s, nc, a, b, nc[0]); + erg ^= (s[0] << i); - return s[0] ^ (nc[0] << 1); + a >>= 1; + b >>= 1; + } + + return erg; } \ No newline at end of file From 86319a95029d1aae3f13ef94799e887a0c0aab1e Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 1 Feb 2024 08:09:59 +0100 Subject: [PATCH 19/23] refactoring: function addition rename variable --- src/addition.c | 18 +++++++++--------- 1 file changed, 9 insertions(+), 9 deletions(-) diff --git a/src/addition.c b/src/addition.c index d165ae4..d56f705 100644 --- a/src/addition.c +++ b/src/addition.c @@ -7,19 +7,19 @@ void full_adder (unsigned int* sum, unsigned int* nextcarry, unsigned int number nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 2)) | ((number2 % 2) & (carry % 2)); } -unsigned int addition(unsigned int a, unsigned int b) { - unsigned int erg = 0; - unsigned int s[1] = {0}; - unsigned int nc[1] = {0}; +unsigned int addition(unsigned int number1, unsigned int number2) { + unsigned int result = 0; + unsigned int sum[1] = {0}; + unsigned int nextcarry[1] = {0}; for (int i = 0; i < 2; i++) { - full_adder(s, nc, a, b, nc[0]); - erg ^= (s[0] << i); + full_adder(sum, nextcarry, number1, number2, nextcarry[0]); + result ^= (sum[0] << i); - a >>= 1; - b >>= 1; + number1 >>= 1; + number2 >>= 1; } - return erg; + return result; } \ No newline at end of file From a1f7c97c450d4f40bf9f6f77c9e2f7b2e59386b6 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 1 Feb 2024 08:35:09 +0100 Subject: [PATCH 20/23] 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); From faee79b2bd2b9659f5279cb1b4e5672920493b61 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 1 Feb 2024 08:43:15 +0100 Subject: [PATCH 21/23] addition for all unsigned integer (modulo UNIT_MAX) --- src/addition.c | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/src/addition.c b/src/addition.c index 0bdb599..adfc3fe 100644 --- a/src/addition.c +++ b/src/addition.c @@ -12,7 +12,7 @@ unsigned int addition(num number1, num number2) { num sum[1] = {0}; num nextcarry[1] = {0}; - for (int i = 0; i < 2; i++) + for (int i = 0; i < 8 * sizeof(num); i++) { full_adder(sum, nextcarry, number1, number2, nextcarry[0]); result ^= (sum[0] << i); From bd819ee5221a1058a0809c4ef0fefbe772ce5055 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Thu, 1 Feb 2024 09:09:42 +0100 Subject: [PATCH 22/23] testing: addition for all unsigned integer --- test/test_addition.c | 18 ++++++++++++++++++ 1 file changed, 18 insertions(+) diff --git a/test/test_addition.c b/test/test_addition.c index 36bfa7f..40af2ff 100644 --- a/test/test_addition.c +++ b/test/test_addition.c @@ -115,4 +115,22 @@ void test_addition_addition_basecasetotenscolumns(void) TEST_ASSERT_EQUAL_UINT(expected[4], result[4]); } +void test_addition_addition_allunsignedinteger(void) +{ + unsigned int result[5]; + unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 }; + + result[0] = addition(13456275, 75423461); + result[1] = addition(4294967294, 1); + result[2] = addition(1023, 65535); + result[3] = addition(0, 4294967295); + result[4] = addition(54321567,423584312); + + 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 From 42e3afe750565ed6d91e9c1c4263c0d041a92a17 Mon Sep 17 00:00:00 2001 From: Dennis Sperzel Date: Mon, 5 Feb 2024 09:55:56 +0100 Subject: [PATCH 23/23] addition floating numbers base case 0+0=0 --- src/addition.c | 4 ++++ src/addition.h | 2 ++ 2 files changed, 6 insertions(+) diff --git a/src/addition.c b/src/addition.c index adfc3fe..0298801 100644 --- a/src/addition.c +++ b/src/addition.c @@ -22,4 +22,8 @@ unsigned int addition(num number1, num number2) { } return result; +} + +float addition_float(float a, float b) { + return 0.0; } \ No newline at end of file diff --git a/src/addition.h b/src/addition.h index a3b2b82..0898765 100644 --- a/src/addition.h +++ b/src/addition.h @@ -7,4 +7,6 @@ void full_adder (num* sum, num* nextcarry, num number1, num number2, num carry); unsigned int addition(num a, num b); +float addition_float(float a, float b); + #endif // ADDITION_H