Browse Source

refactoring: functions addition and full_adder data type change with typedef num

master
Dennis Sperzel 11 months ago
parent
commit
a1f7c97c45
  1. 8
      src/addition.c
  2. 6
      src/addition.h
  3. 18
      test/test_addition.c

8
src/addition.c

@ -1,16 +1,16 @@
#include "addition.h" #include "addition.h"
#include <stdlib.h> #include <stdlib.h>
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); sum[0] = (number1 % 2) ^ (number2 % 2) ^ (carry % 2);
nextcarry[0] = ((number1 % 2) & (number2 % 2)) | ((number1 % 2) & (carry % 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 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++) for (int i = 0; i < 2; i++)
{ {

6
src/addition.h

@ -1,8 +1,10 @@
#ifndef ADDITION_H #ifndef ADDITION_H
#define 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 #endif // ADDITION_H

18
test/test_addition.c

@ -4,7 +4,7 @@
#include "addition.h" #include "addition.h"
int carry[1];
num carry[1];
void setUp(void) void setUp(void)
{ {
@ -16,8 +16,8 @@ void tearDown(void)
void test_addition_full_adder_nullplusnullgleichnull(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); 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) 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); 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) 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+0), carry, 1, 0, 1);
full_adder((result+1), carry, 0, 1, 0); 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) 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+0, carryresult+0, 0, 1, 1);
full_adder(result+1, carryresult+1, 1, 1, 0); full_adder(result+1, carryresult+1, 1, 1, 0);

Loading…
Cancel
Save