Advertisement
Guest User

Untitled

a guest
Jan 23rd, 2017
67
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 7.53 KB | None | 0 0
  1. #include "bigint.h"
  2. #include <iostream>
  3. #include <string>
  4. #include <stdexcept>
  5.  
  6. using std::cout;
  7. using std::endl;
  8. using std::string;
  9. using std::invalid_argument;
  10. using std::overflow_error;
  11.  
  12. //This helps with testing, do not modify.
  13. bool checkTest(string testName, string whatItShouldBe, string whatItIs) {
  14.  
  15. if (whatItShouldBe == whatItIs) {
  16. cout << "Passed " << testName << " last digit was: " << whatItIs.at(whatItIs.length() - 1) << endl;
  17. return true;
  18. }
  19. else {
  20. if (whatItShouldBe == "") {
  21. cout << "**Failed test " << testName << " ** " << endl << " Output was " << whatItIs << endl << " Output should have been blank. " << endl;
  22. }
  23. else {
  24. cout << "**Failed test " << testName << " ** " << endl << " Output was " << whatItIs << endl << " Output should have been " << whatItShouldBe << endl;
  25. }
  26. return false;
  27. }
  28. }
  29.  
  30. void expectedException(string testName, string exception) {
  31.  
  32. cout << "**Failed test " << testName << " ** " << endl;
  33. cout << " Expected an Invalid Argument Error" << endl;
  34. }
  35.  
  36. int main()
  37. {
  38. BigInt result;
  39.  
  40. BigInt num1("999");
  41. BigInt num2("4873");
  42. BigInt num3("-739");
  43. BigInt num4("-9871");
  44. BigInt num5("123456789012345678901234567890");
  45. BigInt num6("5555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555556");
  46. BigInt num7("6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666667");
  47. BigInt num8("1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112");
  48. BigInt num9("-1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000");
  49. BigInt num10("-30");
  50. BigInt num11("4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444443");
  51. BigInt num12("-8999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999");
  52. BigInt num13("-4444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444443");
  53. BigInt num14("-1");
  54. BigInt num15("1");
  55.  
  56. // Each test is worth one point
  57. //Tests to see if you loaded in an BigInt object correctly
  58. // Test 1
  59. checkTest("Test 1", "999", num1.convertToString());
  60. // Test 2
  61. checkTest("Test 2", "-739", num3.convertToString());
  62.  
  63. //Test some addition problems
  64. // Test 3, negative + negative
  65. result = num3.add(num4);
  66. checkTest("Test 3", "-10610", result.convertToString());
  67. // Test 4, negative + positive
  68. result = num4.add(num1);
  69. checkTest("Test 4", "-8872", result.convertToString());
  70. // Test 5, positive + positive
  71. result = num8 + num8;
  72. checkTest("Test 5", "2222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222222224", result.convertToString());
  73. // Test 6 positive + negative
  74. result = num8 + num9;
  75. checkTest("Test 6", "111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111112", result.convertToString());
  76. // Test 7, negative + positive
  77. result = num10 + num7;
  78. checkTest("Test 7", "6666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666666637", result.convertToString());
  79. // Test 8, negative + negative
  80. result = num10 + num9;
  81. checkTest("Test 8", "-1000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000030", result.convertToString());
  82. // Test 9, negative + positive
  83. result = num13 + num6;
  84. checkTest("Test 9", "1111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111111113", result.convertToString());
  85. // Test 10, negative + negative
  86. result = num13 + num9;
  87. checkTest("Test 10", "-5444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444444443", result.convertToString());
  88.  
  89. //Some subtraction problems
  90. // Test 11, positive - positive, negative answer
  91. result = num1.subtract(num2);
  92. checkTest("Test 11", "-3874", result.convertToString());
  93. // Test 12, positive - positive, positive answer
  94. result = num2.subtract(num1);
  95. checkTest("Test 12", "3874", result.convertToString());
  96. // Test 13, negative - negative, negative answer
  97. result = num4.subtract(num3);
  98. checkTest("Test 13", "-9132", result.convertToString());
  99. // Test 14, negative - negative, positive answer
  100. result = num3.subtract(num4);
  101. checkTest("Test 14", "9132", result.convertToString());
  102. // Test 15, positive - positive, negative answer
  103. result = num5.subtract(num6);
  104. checkTest("Test 15", "-5555555555555555555555555555555555555555555555555555555555555555555555432098766543209876654320987666", result.convertToString());
  105. // Test 16 positive - positive, positive answer
  106. result = num6 - num5;
  107. checkTest("Test 16", "5555555555555555555555555555555555555555555555555555555555555555555555432098766543209876654320987666", result.convertToString());
  108. // Test 17, negative - negative, negative answer
  109. result = num9 - num10;
  110. checkTest("Test 17", "-999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999970", result.convertToString());
  111. // Test 18 positive - negative, positive answer
  112. result = num6 - num9;
  113. checkTest("Test 18", "6555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555555556", result.convertToString());
  114. // Test 19, negative - negative, positive answer
  115. result = num9 - num12;
  116. checkTest("Test 19", "7999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", result.convertToString());
  117. // Test 20, positive - negative, positive answer
  118. result = num6 - num13;
  119. checkTest("Test 20", "9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", result.convertToString());
  120. // Test 21, negative - positive, negative answer
  121. result = num13 - num6;
  122. checkTest("Test 21", "-9999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999999", result.convertToString());
  123. // Test 22, zero result
  124. result = num14 + num15;
  125. checkTest("Test 22", "0", result.convertToString());
  126. // Test 23, negative - negative, negative answer
  127. result = num14 - num15;
  128. checkTest("Test 23", "-2", result.convertToString());
  129. // Test 24, positive - negative, positive answer
  130. result = num15 - num14;
  131. checkTest("Test 24", "2", result.convertToString());
  132. // Test 25, negative + negative, negative answer
  133. result = num14 + num14;
  134. checkTest("Test 25", "-2", result.convertToString());
  135.  
  136.  
  137. // Test 26, Default constructor
  138. BigInt anInt;
  139. checkTest("Test 26", "0", anInt.convertToString());
  140.  
  141.  
  142. //Bonus Points
  143. cout << "***Bonus Points***" << endl;
  144. try {
  145. BigInt invalid("123Q");
  146. expectedException("Bonus Test 1", "Invalid Argument Error");
  147. }
  148. catch (const invalid_argument& ia) {
  149. checkTest("Bonus Test 1", "0", "0");
  150. }
  151. catch (...) {
  152. checkTest("Bonus Test 1", "Invalid Argument Error", "Unexpected Error");
  153. }
  154.  
  155. try {
  156. BigInt invalid("-");
  157. expectedException("Bonus Test 2", "Invalid Argument Error");
  158. }
  159. catch (const invalid_argument& ia) {
  160. checkTest("Bonus Test 2", "0", "0");
  161. }
  162. catch (...) {
  163. checkTest("Bonus Test 2", "Invalid Argument Error", "Unexpected Error");
  164. }
  165.  
  166. try {
  167. result = num7 + num7;
  168. expectedException("Bonus Test 3", "Overflow Error");
  169. }
  170. catch (const overflow_error& oe) {
  171. checkTest("Bonus Test 3", "0", "0");
  172. }
  173. catch (...) {
  174. checkTest("Bonus Test 3", "Overflow Error", "Unexpected Error");
  175. }
  176.  
  177. try {
  178. result = num12 - num7;
  179. expectedException("Bonus Test 4", "Overflow Error");
  180. }
  181. catch (const overflow_error& oe) {
  182. checkTest("Bonus Test 4", "0", "0");
  183. }
  184. catch (...) {
  185. checkTest("Bonus Test 4", "Overflow Error", "Unexpected Error");
  186. }
  187. return 0;
  188. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement