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

  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. {
  112. unsigned int result[2];
  113. unsigned int expected[2] = { 0, 1 };
  114. number[0].floating = 1, number[1].floating = -1;
  115. result[0] = sign(number[0].integer);
  116. result[1] = sign(number[1].integer);
  117. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  118. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  119. }
  120. void test_addition_precisionfloatingnumber(void)
  121. {
  122. unsigned int result[5];
  123. number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
  124. 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};
  125. result[0] = precision( number[0].integer );
  126. result[1] = precision( number[1].integer );
  127. result[2] = precision( number[2].integer );
  128. result[3] = precision( number[3].integer );
  129. result[4] = precision( number[4].integer );
  130. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  131. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  132. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  133. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  134. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  135. }
  136. void test_addition_exponentfloatingnumber(void)
  137. {
  138. unsigned int result[5];
  139. number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
  140. 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};
  141. result[0] = e( number[0].integer );
  142. result[1] = e( number[1].integer );
  143. result[2] = e( number[2].integer );
  144. result[3] = e( number[3].integer );
  145. result[4] = e( number[4].integer );
  146. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  147. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  148. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  149. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  150. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  151. }
  152. void test_addition_output_numberwithsignexponentprecision(void)
  153. {
  154. unsigned int result[5];
  155. number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14;
  156. unsigned int expected[5] = { number[0].integer, number[1].integer, number[2].integer, number[3].integer, number[4].integer };
  157. unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4;
  158. 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);
  159. 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);
  160. 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);
  161. result[0] = o(s0, e0, p0);
  162. result[1] = o(s1, e1, p1);
  163. result[2] = o(s2, e2, p2);
  164. result[3] = o(s3, e3, p3);
  165. result[4] = o(s4, e4, p4);
  166. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  167. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  168. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  169. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  170. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  171. }
  172. void test_addition_addition_float_0plus0gleich0(void)
  173. {
  174. float result;
  175. float expected = (float) 0.0;
  176. result = addition_float( (float) 0.0, (float) 0.0);
  177. TEST_ASSERT_EQUAL_FLOAT(expected, result);
  178. }
  179. void test_addition_addition_float_0plusnumbergleichnumber(void)
  180. {
  181. float result[5];
  182. number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
  183. float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };
  184. result[0] = addition_float(number[0].floating, (float) 0.0);
  185. result[1] = addition_float((float) 0.0, number[1].floating);
  186. result[2] = addition_float(number[2].floating, (float) 0.0);
  187. result[3] = addition_float((float) 0.0, number[3].floating);
  188. result[4] = addition_float((float) 0.0, number[4].floating);
  189. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  190. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  191. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  192. TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]);
  193. TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]);
  194. }
  195. #endif // TEST