Infinity Calculator implementation for Assignment 2 in Computer Organization.
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.

207 lines
4.1 KiB

  1. void test_add() {
  2. infint_t *ii = infint_new(2);
  3. infint_add(ii, 50);
  4. assert(ii->data[0] == 50);
  5. assert(ii->data[1] == 0);
  6. infint_add(ii, 90);
  7. assert(ii->data[0] == 40);
  8. assert(ii->data[1] == 1);
  9. ii->data[1] = 99;
  10. infint_add(ii, 80);
  11. assert(ii->data[0] == 20);
  12. assert(ii->data[1] == 0);
  13. assert(ii->data[2] == 1);
  14. assert(ii->size == 3);
  15. assert(ii->capacity == 4);
  16. infint_free(ii);
  17. ii = infint_new(2);
  18. ii->data[0] = 0;
  19. ii->data[1] = 1;
  20. ii->size = 2;
  21. ii->sign = 1;
  22. infint_add(ii, 50);
  23. assert(ii->data[0] == 50);
  24. assert(ii->data[1] == 0);
  25. assert(ii->sign == 1);
  26. infint_free(ii);
  27. }
  28. void test_sub() {
  29. infint_t *ii = infint_new(2);
  30. ii->data[0] = 0;
  31. ii->data[1] = 1;
  32. ii->size = 2;
  33. infint_sub(ii, 75);
  34. assert(ii->data[0] == 25);
  35. assert(ii->data[1] == 0);
  36. assert(ii->sign == 0);
  37. infint_sub(ii, 75);
  38. assert(ii->data[0] == 50);
  39. assert(ii->data[1] == 0);
  40. assert(ii->sign == 1);
  41. infint_sub(ii, 75);
  42. assert(ii->data[0] == 25);
  43. assert(ii->data[1] == 1);
  44. assert(ii->sign == 1);
  45. infint_free(ii);
  46. }
  47. void test_mult() {
  48. infint_t *ii = infint_new(2);
  49. ii->data[0] = 1;
  50. infint_mult(ii, 20);
  51. assert(ii->data[0] == 20);
  52. assert(ii->data[1] == 0);
  53. assert(ii->size == 1);
  54. infint_mult(ii, 20);
  55. assert(ii->data[0] == 0);
  56. assert(ii->data[1] == 4);
  57. assert(ii->size == 2);
  58. infint_mult(ii, 0);
  59. assert(ii->data[0] == 0);
  60. assert(ii->data[1] == 0);
  61. assert(ii->size == 1);
  62. infint_free(ii);
  63. }
  64. void test_div() {
  65. infint_t *ii = infint_new(2);
  66. ii->data[0] = 0;
  67. ii->data[1] = 10;
  68. ii->size = 2;
  69. assert(infint_div(ii, 2) == 0);
  70. assert(ii->data[0] == 0);
  71. assert(ii->data[1] == 5);
  72. assert(infint_div(ii, 10) == 0);
  73. assert(ii->data[0] == 50);
  74. assert(ii->data[1] == 0);
  75. assert(ii->size == 1);
  76. assert(infint_div(ii, 3) == 2);
  77. assert(ii->data[0] == 16);
  78. assert(ii->data[1] == 0);
  79. assert(ii->size == 1);
  80. infint_free(ii);
  81. }
  82. void test_add2() {
  83. infint_t *ii1, *ii2;
  84. ii1 = infint_new(4);
  85. ii2 = infint_new(2);
  86. ii2->data[0] = 75;
  87. ii2->data[1] = 2;
  88. ii2->size = 2;
  89. infint_add2(ii1, ii2);
  90. assert(ii1->data[0] == 75);
  91. assert(ii1->data[1] == 2);
  92. assert(ii1->size == 2);
  93. infint_add2(ii1, ii2);
  94. assert(ii1->data[0] == 50);
  95. assert(ii1->data[1] == 5);
  96. assert(ii1->size == 2);
  97. ii2->data[1] = 94;
  98. infint_add2(ii1, ii2);
  99. assert(ii1->data[0] == 25);
  100. assert(ii1->data[1] == 0);
  101. assert(ii1->data[2] == 1);
  102. assert(ii1->size == 3);
  103. infint_free(ii1);
  104. infint_free(ii2);
  105. }
  106. void test_sub2() {
  107. infint_t *ii1, *ii2;
  108. ii1 = infint_new(2);
  109. ii1->size = 2;
  110. ii1->data[0] = 0;
  111. ii1->data[1] = 1;
  112. ii2 = infint_new(1);
  113. ii2->data[0] = 75;
  114. infint_sub2(ii1, ii2);
  115. assert(ii1->data[0] == 25);
  116. assert(ii1->data[1] == 0);
  117. assert(ii1->size == 1);
  118. assert(ii1->sign == 0);
  119. infint_sub2(ii1, ii2);
  120. assert(ii1->data[0] == 50);
  121. assert(ii1->data[1] == 0);
  122. assert(ii1->size == 1);
  123. assert(ii1->sign == 1);
  124. infint_sub2(ii1, ii2);
  125. assert(ii1->data[0] == 25);
  126. assert(ii1->data[1] == 1);
  127. assert(ii1->size == 2);
  128. assert(ii1->sign == 1);
  129. ii2->sign = 1;
  130. infint_sub2(ii1, ii2);
  131. assert(ii1->data[0] == 50);
  132. assert(ii1->data[1] == 0);
  133. assert(ii1->size == 1);
  134. assert(ii1->sign == 1);
  135. infint_sub2(ii1, ii2);
  136. assert(ii1->data[0] == 25);
  137. assert(ii1->data[1] == 0);
  138. assert(ii1->size == 1);
  139. assert(ii1->sign == 0);
  140. infint_free(ii1);
  141. infint_free(ii2);
  142. }
  143. void test_mult2() {
  144. infint_t *ii1, *ii2;
  145. ii1 = infint_new(1);
  146. ii1->data[0] = 25;
  147. ii2 = infint_new(1);
  148. ii2->data[0] = 50;
  149. infint_mult2(ii1,ii2);
  150. assert(ii1->size == 2);
  151. assert(ii1->data[0] == 50);
  152. assert(ii1->data[1] == 12);
  153. infint_mult2(ii1,ii2);
  154. assert(ii1->size == 3);
  155. assert(ii1->data[0] == 0);
  156. assert(ii1->data[1] == 25);
  157. assert(ii1->data[2] == 6);
  158. infint_mult2(ii1,ii1);
  159. assert(ii1->size == 5);
  160. assert(ii1->data[0] == 0);
  161. assert(ii1->data[1] == 0);
  162. assert(ii1->data[2] == 25);
  163. assert(ii1->data[3] == 6);
  164. assert(ii1->data[4] == 39);
  165. infint_free(ii1);
  166. infint_free(ii2);
  167. }