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.

282 lines
10 KiB

  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "addition.h"
  4. num carry[1];
  5. unsigned int e = 8388608;
  6. union {
  7. float floating;
  8. unsigned int integer;
  9. } number[6];
  10. void setUp(void)
  11. {
  12. }
  13. void tearDown(void)
  14. {
  15. }
  16. void test_addition_full_adder_nullplusnullgleichnull(void)
  17. {
  18. num result[1];
  19. num expected = 0;
  20. full_adder(result, carry, 0, 0, 0);
  21. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  22. }
  23. void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void)
  24. {
  25. num result[1];
  26. num expected = 1;
  27. full_adder(result, carry, 0, 0, 1);
  28. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  29. }
  30. void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void)
  31. {
  32. num result[5];
  33. num expected[5] = { 0, 1, 1, 1, 1};
  34. full_adder((result+0), carry, 1, 0, 1);
  35. full_adder((result+1), carry, 0, 1, 0);
  36. full_adder((result+2), carry, 1, 0, 0);
  37. full_adder((result+3), carry, 0, 0, 1);
  38. full_adder((result+4), carry, 1, 1, 1);
  39. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  40. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  41. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  42. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  43. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  44. }
  45. void test_addition_full_adder_completesumwithcarry(void)
  46. {
  47. num result[5], carryresult[5];
  48. num expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0};
  49. full_adder(result+0, carryresult+0, 0, 1, 1);
  50. full_adder(result+1, carryresult+1, 1, 1, 0);
  51. full_adder(result+2, carryresult+2, 1, 1, 1);
  52. full_adder(result+3, carryresult+3, 0, 1, 0);
  53. full_adder(result+4, carryresult+4, 0, 0, 0);
  54. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  55. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  56. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  57. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  58. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  59. TEST_ASSERT_EQUAL_UINT(expectedcarry[0], carryresult[0]);
  60. TEST_ASSERT_EQUAL_UINT(expectedcarry[1], carryresult[1]);
  61. TEST_ASSERT_EQUAL_UINT(expectedcarry[2], carryresult[2]);
  62. TEST_ASSERT_EQUAL_UINT(expectedcarry[3], carryresult[3]);
  63. TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]);
  64. }
  65. void test_addition_addition_basecasezeropluszeroequalzero(void)
  66. {
  67. unsigned int result;
  68. unsigned int expected = 0;
  69. result = addition(0, 0);
  70. TEST_ASSERT_EQUAL_UINT(expected, result);
  71. }
  72. void test_addition_addition_basecaseonescolumns(void)
  73. {
  74. unsigned int result[2];
  75. unsigned int expected[2] = { 1, 1 };
  76. result[0] = addition(1, 0);
  77. result[1] = addition(0, 1);
  78. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  79. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  80. }
  81. void test_addition_addition_basecasetotenscolumns(void)
  82. {
  83. unsigned int result[5];
  84. unsigned int expected[5] = { 3, 3, 2, 3, 3 };
  85. result[0] = addition(0, 3);
  86. result[1] = addition(1, 2);
  87. result[2] = addition(1, 1);
  88. result[3] = addition(2, 1);
  89. result[4] = addition(3, 0);
  90. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  91. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  92. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  93. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  94. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  95. }
  96. void test_addition_addition_allunsignedinteger(void)
  97. {
  98. unsigned int result[5];
  99. unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 };
  100. result[0] = addition(13456275, 75423461);
  101. result[1] = addition(4294967294, 1);
  102. result[2] = addition(1023, 65535);
  103. result[3] = addition(0, 4294967295);
  104. result[4] = addition(54321567,423584312);
  105. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  106. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  107. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  108. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  109. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  110. }
  111. void test_addition_signfloatingnumber(void)
  112. {
  113. unsigned int result[2];
  114. unsigned int expected[2] = { 0, 1 };
  115. number[0].floating = 1, number[1].floating = -1;
  116. result[0] = sign(number[0].integer);
  117. result[1] = sign(number[1].integer);
  118. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  119. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  120. }
  121. void test_addition_precisionfloatingnumber(void)
  122. {
  123. unsigned int result[5];
  124. number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
  125. 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};
  126. result[0] = precision( number[0].integer );
  127. result[1] = precision( number[1].integer );
  128. result[2] = precision( number[2].integer );
  129. result[3] = precision( number[3].integer );
  130. result[4] = precision( number[4].integer );
  131. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  132. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  133. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  134. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  135. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  136. }
  137. void test_addition_exponentfloatingnumber(void)
  138. {
  139. unsigned int result[5];
  140. number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
  141. 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};
  142. result[0] = exponent( number[0].integer );
  143. result[1] = exponent( number[1].integer );
  144. result[2] = exponent( number[2].integer );
  145. result[3] = exponent( number[3].integer );
  146. result[4] = exponent( number[4].integer );
  147. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  148. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  149. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  150. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  151. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  152. }
  153. void test_addition_addition_precision_positivnumberlowerthan2tothepowerof24(void)
  154. {
  155. unsigned int result[2];
  156. number[0].floating = 652468761.2348, number[1].floating = 2.675312546943, number[2].floating = 321647956212.2146, number[3].floating = 0.00000324567861, number[4].floating = number[0].floating + number[1].floating; number[5].floating = number[2].floating + number[3].floating;
  157. unsigned int expected[2] = { precision(number[4].integer), precision(number[5].integer) };
  158. unsigned int e0, e1, e2, e3, p0, p1, p2, p3;
  159. e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer);
  160. p0 = precision(number[0].integer) | e, p1 = precision(number[1].integer) | e, p2 = precision(number[2].integer) | e, p3 = precision(number[3].integer) | e;
  161. p1 >>= e0 - e1, p3 >>= e2 - e3;
  162. result[0] = addition_precision( p0, p1 ) ^ e;
  163. result[1] = addition_precision( p2, p3 ) ^ e;
  164. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  165. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  166. }
  167. void test_addition_output_numberwithsignexponentprecision(void)
  168. {
  169. unsigned int result[5];
  170. number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14;
  171. unsigned int expected[5] = { number[0].integer, number[1].integer, number[2].integer, number[3].integer, number[4].integer };
  172. unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4;
  173. 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);
  174. e0 = exponent(number[0].integer), e1 = exponent(number[1].integer), e2 = exponent(number[2].integer), e3 = exponent(number[3].integer), e4 = exponent(number[4].integer);
  175. 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);
  176. result[0] = output(s0, e0, p0);
  177. result[1] = output(s1, e1, p1);
  178. result[2] = output(s2, e2, p2);
  179. result[3] = output(s3, e3, p3);
  180. result[4] = output(s4, e4, p4);
  181. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  182. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  183. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  184. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  185. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  186. }
  187. void test_addition_addition_float_0plus0gleich0(void)
  188. {
  189. float result;
  190. float expected = (float) 0.0;
  191. result = addition_float( (float) 0.0, (float) 0.0);
  192. TEST_ASSERT_EQUAL_FLOAT(expected, result);
  193. }
  194. void test_addition_addition_float_0plusnumbergleichnumber(void)
  195. {
  196. float result[5];
  197. number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
  198. float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };
  199. result[0] = addition_float(number[0].floating, (float) 0.0);
  200. result[1] = addition_float((float) 0.0, number[1].floating);
  201. result[2] = addition_float(number[2].floating, (float) 0.0);
  202. result[3] = addition_float((float) 0.0, number[3].floating);
  203. result[4] = addition_float((float) 0.0, number[4].floating);
  204. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  205. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  206. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  207. TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]);
  208. TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]);
  209. }
  210. void test_addition_addition_float_sumofpositivnumbers(void)
  211. {
  212. float result[3];
  213. number[0].floating = 31972543.42176, number[1].floating = 423576.006432054, number[2].floating = 1346.4546124016, number[3].floating = 75620056402.5431, number[4].floating = 0.5351401453104, number[5].floating = 0.469178612;
  214. float expected[3] = { number[0].floating + number[1].floating, number[2].floating + number[3].floating, number[4].floating + number[5].floating };
  215. result[0] = addition_float(number[0].floating, number[1].floating);
  216. result[1] = addition_float(number[2].floating, number[3].floating);
  217. result[2] = addition_float(number[4].floating, number[5].floating);
  218. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  219. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  220. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  221. }
  222. #endif // TEST