You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

245 lines
8.5 KiB

#ifdef TEST
#include "unity.h"
#include "addition.h"
num carry[1];
union {
float floating;
unsigned int integer;
} number[5];
void setUp(void)
{
}
void tearDown(void)
{
}
void test_addition_full_adder_nullplusnullgleichnull(void)
{
num result[1];
num expected = 0;
full_adder(result, carry, 0, 0, 0);
TEST_ASSERT_EQUAL_UINT(expected, result[0]);
}
void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void)
{
num result[1];
num expected = 1;
full_adder(result, carry, 0, 0, 1);
TEST_ASSERT_EQUAL_UINT(expected, result[0]);
}
void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void)
{
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);
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_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)
{
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);
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_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)
{
unsigned int result;
unsigned int expected = 0;
result = addition(0, 0);
TEST_ASSERT_EQUAL_UINT(expected, result);
}
void test_addition_addition_basecaseonescolumns(void)
{
unsigned int result[2];
unsigned int expected[2] = { 1, 1 };
result[0] = addition(1, 0);
result[1] = addition(0, 1);
TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
}
void test_addition_addition_basecasetotenscolumns(void)
{
unsigned int result[5];
unsigned 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_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_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]);
}
void test_addition_signfloatingnumber(void)
{
unsigned int result[2];
unsigned int expected[2] = { 0, 1 };
number[0].floating = 1, number[1].floating = -1;
result[0] = sign(number[0].integer);
result[1] = sign(number[1].integer);
TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
}
void test_addition_precisionfloatingnumber(void)
{
unsigned int result[5];
number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
unsigned int expected[5] = { ( number[0].integer << 9) >> 9 , ( number[1].integer << 9) >> 9, ( number[2].integer << 9) >> 9, ( number[3].integer << 9) >> 9, ( number[4].integer << 9) >> 9};
result[0] = precision( number[0].integer );
result[1] = precision( number[1].integer );
result[2] = precision( number[2].integer );
result[3] = precision( number[3].integer );
result[4] = precision( number[4].integer );
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_exponentfloatingnumber(void)
{
unsigned int result[5];
number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
unsigned int expected[5] = { ( number[0].integer << 1) >> 24 , ( number[1].integer << 1) >> 24, ( number[2].integer << 1) >> 24, ( number[3].integer << 1) >> 24, ( number[4].integer << 1) >> 24};
result[0] = e( number[0].integer );
result[1] = e( number[1].integer );
result[2] = e( number[2].integer );
result[3] = e( number[3].integer );
result[4] = e( number[4].integer );
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_output_numberwithsignexponentprecision(void)
{
unsigned int result[5];
number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14;
unsigned int expected[5] = { number[0].integer, number[1].integer, number[2].integer, number[3].integer, number[4].integer };
unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4;
s0 = sign(number[0].integer), s1 = sign(number[1].integer), s2 = sign(number[2].integer), s3 = sign(number[3].integer), s4 = sign(number[4].integer);
e0 = e(number[0].integer), e1 = e(number[1].integer), e2 = e(number[2].integer), e3 = e(number[3].integer), e4 = e(number[4].integer);
p0 = precision(number[0].integer), p1 = precision(number[1].integer), p2 = precision(number[2].integer), p3 = precision(number[3].integer), p4 = precision(number[4].integer);
result[0] = o(s0, e0, p0);
result[1] = o(s1, e1, p1);
result[2] = o(s2, e2, p2);
result[3] = o(s3, e3, p3);
result[4] = o(s4, e4, p4);
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_addition_float_0plus0gleich0(void)
{
float result;
float expected = (float) 0.0;
result = addition_float( (float) 0.0, (float) 0.0);
TEST_ASSERT_EQUAL_FLOAT(expected, result);
}
void test_addition_addition_float_0plusnumbergleichnumber(void)
{
float result[5];
number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };
result[0] = addition_float(number[0].floating, (float) 0.0);
result[1] = addition_float((float) 0.0, number[1].floating);
result[2] = addition_float(number[2].floating, (float) 0.0);
result[3] = addition_float((float) 0.0, number[3].floating);
result[4] = addition_float((float) 0.0, number[4].floating);
TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]);
TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]);
}
#endif // TEST