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.

364 lines
15 KiB

  1. #ifdef TEST
  2. #include "unity.h"
  3. #include "addition.h"
  4. num carry[1];
  5. unsigned int e = 0x00800000;
  6. unsigned long long ed = 0x0010000000000000;
  7. union {
  8. float floating;
  9. unsigned int integer;
  10. } number[6];
  11. union {
  12. double dnum;
  13. unsigned long long lnum;
  14. } numberd[6];
  15. void setUp(void)
  16. {
  17. }
  18. void tearDown(void)
  19. {
  20. }
  21. void test_addition_full_adder_nullplusnullgleichnull(void)
  22. {
  23. num result[1];
  24. num expected = 0;
  25. full_adder(result, carry, 0, 0, 0);
  26. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  27. }
  28. void test_addition_full_adder_nullplusnullgleichnullmituebertrag(void)
  29. {
  30. num result[1];
  31. num expected = 1;
  32. full_adder(result, carry, 0, 0, 1);
  33. TEST_ASSERT_EQUAL_UINT(expected, result[0]);
  34. }
  35. void test_addition_full_adder_zahlpluszahlgleichsummeohneuebertrag(void)
  36. {
  37. num result[5];
  38. num expected[5] = { 0, 1, 1, 1, 1};
  39. full_adder((result+0), carry, 1, 0, 1);
  40. full_adder((result+1), carry, 0, 1, 0);
  41. full_adder((result+2), carry, 1, 0, 0);
  42. full_adder((result+3), carry, 0, 0, 1);
  43. full_adder((result+4), carry, 1, 1, 1);
  44. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  45. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  46. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  47. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  48. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  49. }
  50. void test_addition_full_adder_completesumwithcarry(void)
  51. {
  52. num result[5], carryresult[5];
  53. num expected[5] = { 0, 0, 1, 1, 0}, expectedcarry[5] = { 1, 1, 1, 0, 0};
  54. full_adder(result+0, carryresult+0, 0, 1, 1);
  55. full_adder(result+1, carryresult+1, 1, 1, 0);
  56. full_adder(result+2, carryresult+2, 1, 1, 1);
  57. full_adder(result+3, carryresult+3, 0, 1, 0);
  58. full_adder(result+4, carryresult+4, 0, 0, 0);
  59. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  60. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  61. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  62. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  63. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  64. TEST_ASSERT_EQUAL_UINT(expectedcarry[0], carryresult[0]);
  65. TEST_ASSERT_EQUAL_UINT(expectedcarry[1], carryresult[1]);
  66. TEST_ASSERT_EQUAL_UINT(expectedcarry[2], carryresult[2]);
  67. TEST_ASSERT_EQUAL_UINT(expectedcarry[3], carryresult[3]);
  68. TEST_ASSERT_EQUAL_UINT(expectedcarry[4], carryresult[4]);
  69. }
  70. void test_addition_addition_uint_basecasezeropluszeroequalzero(void)
  71. {
  72. unsigned int result;
  73. unsigned int expected = 0;
  74. result = addition_uint(0, 0);
  75. TEST_ASSERT_EQUAL_UINT(expected, result);
  76. }
  77. void test_addition_addition_uint_basecaseonescolumns(void)
  78. {
  79. unsigned int result[2];
  80. unsigned int expected[2] = { 1, 1 };
  81. result[0] = addition_uint(1, 0);
  82. result[1] = addition_uint(0, 1);
  83. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  84. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  85. }
  86. void test_addition_addition_uint_basecasetotenscolumns(void)
  87. {
  88. unsigned int result[5];
  89. unsigned int expected[5] = { 3, 3, 2, 3, 3 };
  90. result[0] = addition_uint(0, 3);
  91. result[1] = addition_uint(1, 2);
  92. result[2] = addition_uint(1, 1);
  93. result[3] = addition_uint(2, 1);
  94. result[4] = addition_uint(3, 0);
  95. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  96. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  97. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  98. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  99. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  100. }
  101. void test_addition_addition_uint_allunsignedinteger(void)
  102. {
  103. unsigned int result[5];
  104. unsigned int expected[5] = { 88879736, __UINT32_MAX__, 66558, __UINT32_MAX__, 477905879 };
  105. result[0] = addition_uint(13456275, 75423461);
  106. result[1] = addition_uint(4294967294, 1);
  107. result[2] = addition_uint(1023, 65535);
  108. result[3] = addition_uint(0, 4294967295);
  109. result[4] = addition_uint(54321567,423584312);
  110. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  111. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  112. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  113. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  114. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  115. }
  116. void test_addition_sign_floatingnumber(void)
  117. {
  118. unsigned int result[2];
  119. unsigned int expected[2] = { 0, 1 };
  120. number[0].floating = 1, number[1].floating = -1;
  121. result[0] = sign_float(number[0].integer);
  122. result[1] = sign_float(number[1].integer);
  123. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  124. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  125. }
  126. void test_addition_precision_floatingnumber(void)
  127. {
  128. unsigned int result[5];
  129. number[0].floating = 0, number[1].floating = -34753168, number[2].floating = 75613594, number[3].floating = -0.00000044568721, number[4].floating = 0.0000004796123;
  130. 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};
  131. result[0] = precision_float( number[0].integer );
  132. result[1] = precision_float( number[1].integer );
  133. result[2] = precision_float( number[2].integer );
  134. result[3] = precision_float( number[3].integer );
  135. result[4] = precision_float( number[4].integer );
  136. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  137. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  138. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  139. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  140. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  141. }
  142. void test_addition_exponent_floatingnumber(void)
  143. {
  144. unsigned int result[5];
  145. number[0].floating = 0, number[1].floating = -762134982, number[2].floating = 47621349, number[3].floating = -0.0000078961354, number[4].floating = 0.00001568943;
  146. 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};
  147. result[0] = exponent_float( number[0].integer );
  148. result[1] = exponent_float( number[1].integer );
  149. result[2] = exponent_float( number[2].integer );
  150. result[3] = exponent_float( number[3].integer );
  151. result[4] = exponent_float( number[4].integer );
  152. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  153. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  154. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  155. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  156. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  157. }
  158. void test_addition_addition_precision_float_positivnumberlowerthan2tothepowerof24(void)
  159. {
  160. unsigned int result[2];
  161. 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;
  162. unsigned int expected[2] = { precision_float(number[4].integer), precision_float(number[5].integer) };
  163. unsigned int e0, e1, e2, e3, p0, p1, p2, p3;
  164. e0 = exponent_float(number[0].integer), e1 = exponent_float(number[1].integer), e2 = exponent_float(number[2].integer), e3 = exponent_float(number[3].integer);
  165. p0 = precision_float(number[0].integer) | e, p1 = precision_float(number[1].integer) | e, p2 = precision_float(number[2].integer) | e, p3 = precision_float(number[3].integer) | e;
  166. p1 >>= e0 - e1, p3 >>= e2 - e3;
  167. result[0] = addition_precision_float( p0, p1 ) ^ e;
  168. result[1] = addition_precision_float( p2, p3 ) ^ e;
  169. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  170. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  171. }
  172. void test_addition_output_float_numberwithsignexponentprecision(void)
  173. {
  174. unsigned int result[5];
  175. number[0].floating = 1468921, number[1].floating = -32468127698, number[2].floating = 0.000000795311468, number[3].floating = -0.00000843214521, number[4].floating = 14;
  176. unsigned int expected[5] = { number[0].integer, number[1].integer, number[2].integer, number[3].integer, number[4].integer };
  177. unsigned int s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4;
  178. s0 = sign_float(number[0].integer), s1 = sign_float(number[1].integer), s2 = sign_float(number[2].integer), s3 = sign_float(number[3].integer), s4 = sign_float(number[4].integer);
  179. e0 = exponent_float(number[0].integer), e1 = exponent_float(number[1].integer), e2 = exponent_float(number[2].integer), e3 = exponent_float(number[3].integer), e4 = exponent_float(number[4].integer);
  180. p0 = precision_float(number[0].integer), p1 = precision_float(number[1].integer), p2 = precision_float(number[2].integer), p3 = precision_float(number[3].integer), p4 = precision_float(number[4].integer);
  181. result[0] = output_float(s0, e0, p0);
  182. result[1] = output_float(s1, e1, p1);
  183. result[2] = output_float(s2, e2, p2);
  184. result[3] = output_float(s3, e3, p3);
  185. result[4] = output_float(s4, e4, p4);
  186. TEST_ASSERT_EQUAL_UINT(expected[0], result[0]);
  187. TEST_ASSERT_EQUAL_UINT(expected[1], result[1]);
  188. TEST_ASSERT_EQUAL_UINT(expected[2], result[2]);
  189. TEST_ASSERT_EQUAL_UINT(expected[3], result[3]);
  190. TEST_ASSERT_EQUAL_UINT(expected[4], result[4]);
  191. }
  192. void test_addition_addition_float_0plus0gleich0(void)
  193. {
  194. float result;
  195. float expected = (float) 0.0;
  196. result = addition_float( (float) 0.0, (float) 0.0);
  197. TEST_ASSERT_EQUAL_FLOAT(expected, result);
  198. }
  199. void test_addition_addition_float_0plusnumbergleichnumber(void)
  200. {
  201. float result[5];
  202. number[0].floating = 45672.56487, number[1].floating = -9531145672.467, number[2].floating = 91357634, number[3].floating = -0.0000000079533144, number[4].floating = 0.0756215698;
  203. float expected[5] = { number[0].floating, number[1].floating, number[2].floating, number[3].floating, number[4].floating };
  204. result[0] = addition_float(number[0].floating, (float) 0.0);
  205. result[1] = addition_float((float) 0.0, number[1].floating);
  206. result[2] = addition_float(number[2].floating, (float) 0.0);
  207. result[3] = addition_float((float) 0.0, number[3].floating);
  208. result[4] = addition_float((float) 0.0, number[4].floating);
  209. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  210. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  211. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  212. TEST_ASSERT_EQUAL_FLOAT(expected[3], result[3]);
  213. TEST_ASSERT_EQUAL_FLOAT(expected[4], result[4]);
  214. }
  215. void test_addition_addition_float_sumofpositivnumbers(void)
  216. {
  217. float result[3];
  218. 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;
  219. float expected[3] = { number[0].floating + number[1].floating, number[2].floating + number[3].floating, number[4].floating + number[5].floating };
  220. result[0] = addition_float(number[0].floating, number[1].floating);
  221. result[1] = addition_float(number[2].floating, number[3].floating);
  222. result[2] = addition_float(number[4].floating, number[5].floating);
  223. TEST_ASSERT_EQUAL_FLOAT(expected[0], result[0]);
  224. TEST_ASSERT_EQUAL_FLOAT(expected[1], result[1]);
  225. TEST_ASSERT_EQUAL_FLOAT(expected[2], result[2]);
  226. }
  227. void test_addition_sign_double_numbers(void)
  228. {
  229. unsigned long long result[2];
  230. unsigned long long expected[2] = { 1, 0 };
  231. numberd[0].dnum = -3004683105640520235, numberd[1].dnum = 0.05313546453135;
  232. result[0] = sign_double(numberd[0].lnum);
  233. result[1] = sign_double(numberd[1].lnum);
  234. TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]);
  235. TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]);
  236. }
  237. void test_addition_precision_double_numbers(void)
  238. {
  239. unsigned long long result[5];
  240. numberd[0].dnum = 3216752.2348648931012, numberd[1].dnum = -79865431546, numberd[2].dnum = 203, numberd[3].dnum = -0.00460023540056432002, numberd[4].dnum = 0.000000000791340265431213;
  241. unsigned long long expected[5] = { (numberd[0].lnum << 12) >> 12 , (numberd[1].lnum << 12) >> 12, (numberd[2].lnum << 12) >> 12, (numberd[3].lnum << 12) >> 12, (numberd[4].lnum << 12) >> 12};
  242. result[0] = precision_double( numberd[0].lnum );
  243. result[1] = precision_double( numberd[1].lnum );
  244. result[2] = precision_double( numberd[2].lnum );
  245. result[3] = precision_double( numberd[3].lnum );
  246. result[4] = precision_double( numberd[4].lnum );
  247. TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]);
  248. TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]);
  249. TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]);
  250. TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]);
  251. TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]);
  252. }
  253. void test_addition_exponent_double_numbers(void)
  254. {
  255. unsigned long long result[5];
  256. numberd[0].dnum = 830046503435461000, numberd[1].dnum = -906056810308432, numberd[2].dnum = 0.5315020335035034357, numberd[3].dnum = -34, numberd[4].dnum = 94315454312021310;
  257. unsigned long long expected[5] = { (numberd[0].lnum << 1) >> 53 , (numberd[1].lnum << 1) >> 53, (numberd[2].lnum << 1) >> 53, (numberd[3].lnum << 1) >> 53, (numberd[4].lnum << 1) >> 53};
  258. result[0] = exponent_double( numberd[0].lnum );
  259. result[1] = exponent_double( numberd[1].lnum );
  260. result[2] = exponent_double( numberd[2].lnum );
  261. result[3] = exponent_double( numberd[3].lnum );
  262. result[4] = exponent_double( numberd[4].lnum );
  263. TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]);
  264. TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]);
  265. TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]);
  266. TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]);
  267. TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]);
  268. }
  269. void test_addition_output_double_numberwithsignexponentprecision(void)
  270. {
  271. unsigned long long result[5];
  272. numberd[0].dnum = 7, numberd[1].dnum = -0.0000000006432154846123454, numberd[2].dnum = 731202035312050934, numberd[3].dnum = 0.0000009341213541974341, numberd[4].dnum = -9443345434194197313103.1136;
  273. unsigned long long expected[5] = { numberd[0].lnum, numberd[1].lnum, numberd[2].lnum, numberd[3].lnum, numberd[4].lnum };
  274. unsigned long long s0, s1, s2, s3, s4, e0, e1, e2, e3, e4, p0, p1, p2, p3, p4;
  275. s0 = sign_double(numberd[0].lnum), s1 = sign_double(numberd[1].lnum), s2 = sign_double(numberd[2].lnum), s3 = sign_double(numberd[3].lnum), s4 = sign_double(numberd[4].lnum);
  276. e0 = exponent_double(numberd[0].lnum), e1 = exponent_double(numberd[1].lnum), e2 = exponent_double(numberd[2].lnum), e3 = exponent_double(numberd[3].lnum), e4 = exponent_double(numberd[4].lnum);
  277. p0 = precision_double(numberd[0].lnum), p1 = precision_double(numberd[1].lnum), p2 = precision_double(numberd[2].lnum), p3 = precision_double(numberd[3].lnum), p4 = precision_double(numberd[4].lnum);
  278. result[0] = output_double(s0, e0, p0);
  279. result[1] = output_double(s1, e1, p1);
  280. result[2] = output_double(s2, e2, p2);
  281. result[3] = output_double(s3, e3, p3);
  282. result[4] = output_double(s4, e4, p4);
  283. TEST_ASSERT_EQUAL_UINT64(expected[0], result[0]);
  284. TEST_ASSERT_EQUAL_UINT64(expected[1], result[1]);
  285. TEST_ASSERT_EQUAL_UINT64(expected[2], result[2]);
  286. TEST_ASSERT_EQUAL_UINT64(expected[3], result[3]);
  287. TEST_ASSERT_EQUAL_UINT64(expected[4], result[4]);
  288. }
  289. #endif // TEST