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.

216 lines
7.2 KiB

  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "addition.h"
  4. num carry[1];
  5. union {
  6. float floating;
  7. unsigned int integer;
  8. } number[5];
  9. void setUp(void)
  10. {
  11. }
  12. void tearDown(void)
  13. {
  14. }
  15. void test_addition_full_adder_nullplusnullgleichnull(void)
  16. {
  17. num result[1];
  18. num expected = 0;
  19. full_adder(result, carry, 0, 0, 0);
  20. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  21. }
  22. void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void)
  23. {
  24. num result[1];
  25. num expected = 1;
  26. full_adder(result, carry, 0, 0, 1);
  27. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  28. }
  29. void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void)
  30. {
  31. num result[5];
  32. num expected[5] = { 0, 1, 1, 1, 1};
  33. full_adder((result+0), carry, 1, 0, 1);
  34. full_adder((result+1), carry, 0, 1, 0);
  35. full_adder((result+2), carry, 1, 0, 0);
  36. full_adder((result+3), carry, 0, 0, 1);
  37. full_adder((result+4), carry, 1, 1, 1);
  38. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  39. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  40. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  41. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  42. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  43. }
  44. void test_addition_full_adder_completesumwithcarry(void)
  45. {
  46. num result[5], carryresult[5];
  47. num expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0};
  48. full_adder(result+0, carryresult+0, 0, 1, 1);
  49. full_adder(result+1, carryresult+1, 1, 1, 0);
  50. full_adder(result+2, carryresult+2, 1, 1, 1);
  51. full_adder(result+3, carryresult+3, 0, 1, 0);
  52. full_adder(result+4, carryresult+4, 0, 0, 0);
  53. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  54. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  55. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  56. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  57. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  58. TEST_ASSERT_EQUAL_UINT(expectedcarry[0], carryresult[0]);
  59. TEST_ASSERT_EQUAL_UINT(expectedcarry[1], carryresult[1]);
  60. TEST_ASSERT_EQUAL_UINT(expectedcarry[2], carryresult[2]);
  61. TEST_ASSERT_EQUAL_UINT(expectedcarry[3], carryresult[3]);
  62. TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]);
  63. }
  64. void test_addition_addition_basecasezeropluszeroequalzero(void)
  65. {
  66. unsigned int result;
  67. unsigned int expected = 0;
  68. result = addition(0, 0);
  69. TEST_ASSERT_EQUAL_UINT(expected, result);
  70. }
  71. void test_addition_addition_basecaseonescolumns(void)
  72. {
  73. unsigned int result[2];
  74. unsigned int expected[2] = { 1, 1 };
  75. result[0] = addition(1, 0);
  76. result[1] = addition(0, 1);
  77. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  78. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  79. }
  80. void test_addition_addition_basecasetotenscolumns(void)
  81. {
  82. unsigned int result[5];
  83. unsigned int expected[5] = { 3, 3, 2, 3, 3 };
  84. result[0] = addition(0, 3);
  85. result[1] = addition(1, 2);
  86. result[2] = addition(1, 1);
  87. result[3] = addition(2, 1);
  88. result[4] = addition(3, 0);
  89. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  90. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  91. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  92. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  93. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  94. }
  95. void test_addition_addition_allunsignedinteger(void)
  96. {
  97. unsigned int result[5];
  98. unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 };
  99. result[0] = addition(13456275, 75423461);
  100. result[1] = addition(4294967294, 1);
  101. result[2] = addition(1023, 65535);
  102. result[3] = addition(0, 4294967295);
  103. result[4] = addition(54321567,423584312);
  104. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  105. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  106. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  107. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  108. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  109. }
  110. void test_addition_signfloatingnumber(void) {
  111. unsigned int result[2];
  112. unsigned int expected[2] = { 0, 1 };
  113. number[0].floating = 1, number[1].floating = -1;
  114. result[0] = sign(number[0].integer);
  115. result[1] = sign(number[1].integer);
  116. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  117. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  118. }
  119. void test_addition_precisionfloatingnumber(void) {
  120. unsigned int result[5];
  121. number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
  122. 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};
  123. result[0] = precision( number[0].integer );
  124. result[1] = precision( number[1].integer );
  125. result[2] = precision( number[2].integer );
  126. result[3] = precision( number[3].integer );
  127. result[4] = precision( number[4].integer );
  128. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  129. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  130. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  131. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  132. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  133. }
  134. void test_addition_exponentfloatingnumber(void) {
  135. unsigned int result[5];
  136. number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
  137. 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};
  138. result[0] = e( number[0].integer );
  139. result[1] = e( number[1].integer );
  140. result[2] = e( number[2].integer );
  141. result[3] = e( number[3].integer );
  142. result[4] = e( number[4].integer );
  143. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  144. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  145. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  146. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  147. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  148. }
  149. void test_addition_addition_float_0plus0gleich0(void) {
  150. float result;
  151. float expected = (float) 0.0;
  152. result = addition_float( (float) 0.0, (float) 0.0);
  153. TEST_ASSERT_EQUAL_FLOAT(expected, result);
  154. }
  155. void test_addition_addition_float_0plusnumbergleichnumber(void) {
  156. float result[5];
  157. number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
  158. float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };
  159. result[0] = addition_float(number[0].floating, (float) 0.0);
  160. result[1] = addition_float((float) 0.0, number[1].floating);
  161. result[2] = addition_float(number[2].floating, (float) 0.0);
  162. result[3] = addition_float((float) 0.0, number[3].floating);
  163. result[4] = addition_float((float) 0.0, number[4].floating);
  164. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  165. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  166. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  167. TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]);
  168. TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]);
  169. }
  170. #endif // TEST