Advertisement
Guest User

BigInt Problem

a guest
May 25th, 2015
402
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 4.78 KB | None | 0 0
  1. /* Class used for testing my own BigInt library
  2. * Stores tested numbers as string,
  3. * because some tests are on numbers bigger than int
  4. * result vector is empty when test is auto-checked
  5. * which happens when num1 and num2 aren't bigger than int,
  6. * else you need to input exactly the required number of
  7. * expected results into the vector result
  8. * The format of the text for the operator>> is:
  9. *   { label num1 num2 autoChecked{ result+ result- result* } }
  10. *   { label num1 num2 autoChecked{ } }
  11. * There have to be all operators' results given, or none
  12. * The order of tests:
  13. *   num1 + num2
  14. *   num2 + num1
  15. *   num1 - num2
  16. *   num2 - num1
  17. *   num1 * num2
  18. *   num2 * num1
  19. */
  20. struct TestBigInt
  21. {
  22.     void doTests(); // apply the operators (with BigInts)
  23.     void autoFillExR(); // fills exResult for autochecking (i.e. convert to int, do calculations, and than back to string)
  24.  
  25.     static constexpr int amountOfResults = 6; // exactly this meany results have to be read to exResult
  26.     std::string label;
  27.     std::string num1;
  28.     std::string num2;
  29.     bool autoChecked;
  30.     std::vector<std::string> exResult; // expected results
  31.     // Expected results of operations in this order: +, -, *
  32.     std::vector<std::string> outResult; // results output by big int calculations
  33. };
  34.  
  35. std::istream& operator>>(std::istream& is, TestBigInt& t);
  36.  
  37. // Test the BigInt with the given test case
  38. std::ostream& performTest(std::ostream& os, TestBigInt& t);
  39.  
  40. // Needed for auto-checking
  41. int stringToI(const std::string& s);
  42. std::string iToString(int a);
  43.  
  44. void TestBigInt::doTests()
  45. {
  46.     BigInt firstNum{ num1 };
  47.     BigInt secNum{ num2 };
  48.     // THESE FOUR LINES CAUSE THE BUG
  49. //  outResult.push_back((firstNum + secNum).toString());
  50. //  outResult.push_back((secNum + firstNum).toString());
  51. //  outResult.push_back((firstNum - secNum).toString());
  52. //  outResult.push_back((secNum - firstNum).toString());
  53.     outResult.push_back((firstNum * secNum).toString());
  54.     outResult.push_back((secNum * firstNum).toString());
  55.     std::cout << "pushed_back outResults\n";
  56. }
  57.  
  58. void TestBigInt::autoFillExR()
  59. {
  60.     int firstNum = stringToI(num1);
  61.     int secNum = stringToI(num2);
  62.  
  63.     exResult.push_back(iToString(firstNum + secNum));
  64.     exResult.push_back(iToString(secNum + firstNum));
  65.     exResult.push_back(iToString(firstNum - secNum));
  66.     exResult.push_back(iToString(secNum - firstNum));
  67.     exResult.push_back(iToString(firstNum * secNum));
  68.     exResult.push_back(iToString(secNum * firstNum));
  69.     std::cout << "pushed_back exResults\n";
  70. }
  71.  
  72. std::istream& operator>>(std::istream& is, TestBigInt& t)
  73. /* Read input in the form:
  74. *   { label num1 num2 autoChecked{ result+ result- result* } }
  75. *   { label num1 num2 autoChecked{ } }
  76. */
  77. {
  78.     char ch1;
  79.     is >> ch1;
  80.     if (ch1 != '{') { // no tests anymore
  81.         is.unget();
  82.         is.clear(std::ios_base::failbit);
  83.         return is;
  84.     }
  85.  
  86.     char ch2;
  87.     std::string lab;
  88.     std::string firstNum;
  89.     std::string secNum;
  90.     bool check;
  91.     is >> lab >> firstNum >> secNum >> check >> ch2;
  92.     if ((ch2 != '{'))
  93.         throw std::runtime_error("invalid formatting brace1");
  94.  
  95.     t.label = lab;
  96.     t.num1 = firstNum;
  97.     t.num2 = secNum;
  98.     t.autoChecked = check;
  99.  
  100.     if (!(check)) { // not auto-checking; need to input expected results
  101.         for (int i = 0; i < t.amountOfResults; ++i) {
  102.             std::string s;
  103.             is >> s;
  104.             if (s == "}")
  105.                 throw std::runtime_error("invalid number of expected results");
  106.             t.exResult.push_back(s);
  107.         }
  108.     }
  109.  
  110.     char ch3, ch4;
  111.     is >> ch3 >> ch4;
  112.     if ((ch3 != '}') || (ch4 != '}'))
  113.         throw std::runtime_error("invalid formatting brace2");
  114.    
  115.     return is;
  116. }
  117.  
  118. std::ostream& performTest(std::ostream& os, TestBigInt& t)
  119. {  
  120.     if (t.autoChecked) // if the results are to be autochecked, than fill the exResult -- else it is already full
  121.         t.autoFillExR();
  122.  
  123.     t.doTests();
  124.  
  125.     for (auto itE = t.exResult.cbegin(), itO = t.outResult.cbegin(); itE != t.exResult.cend() && itO != t.outResult.cend(); ++itE, ++itO)
  126.         if (*itE != *itO)
  127.             os << "Test not passed: " << t.label  << ", " << distance(t.exResult.cbegin(), itE) << "\n\tExpected: " << *itE << ", got " << *itO << "\n";
  128.    
  129.     return os;
  130. }
  131.  
  132. int stringToI(const std::string& s)
  133. {
  134.     std::istringstream is{ s };
  135.     int res;
  136.     is >> res;
  137.     return res;
  138. }
  139.  
  140. std::string iToString(int a)
  141. {
  142.     std::ostringstream os;
  143.     os << a;
  144.     return (os.str());
  145. }
  146.  
  147. int main()
  148. {
  149.     /* "BigIntTestCases.txt" - format { label num1 num2 autoChecked { } }, contents:
  150.     * { 1 3 2 1 { } }
  151.     * { 2 10 7 1 { } }
  152.     * { 3 21 9 1 { } }
  153.     * ...
  154.     */
  155.     ifstream ifs{ "BigIntTestCases.txt" };
  156.     vector<TestBigInt> tests;
  157.     for (TestBigInt t; ifs >> t; )
  158.         tests.push_back(t);
  159.     ofstream ofs{ "TestOutput.txt" };
  160.     for (auto it = tests.begin(); it != tests.end(); ++it) {
  161.         cout << "Read: " << it->label << ' ' << it->num1 << ' ' << it->num2 << ' ' << it->autoChecked << '\n';
  162.         performTest(ofs, (*it));
  163.     }
  164.     std::cout << "Tests Complete! \n";
  165.  
  166.     return 0;
  167. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement