Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- /* Class used for testing my own BigInt library
- * Stores tested numbers as string,
- * because some tests are on numbers bigger than int
- * result vector is empty when test is auto-checked
- * which happens when num1 and num2 aren't bigger than int,
- * else you need to input exactly the required number of
- * expected results into the vector result
- * The format of the text for the operator>> is:
- * { label num1 num2 autoChecked{ result+ result- result* } }
- * { label num1 num2 autoChecked{ } }
- * There have to be all operators' results given, or none
- * The order of tests:
- * num1 + num2
- * num2 + num1
- * num1 - num2
- * num2 - num1
- * num1 * num2
- * num2 * num1
- */
- struct TestBigInt
- {
- void doTests(); // apply the operators (with BigInts)
- void autoFillExR(); // fills exResult for autochecking (i.e. convert to int, do calculations, and than back to string)
- static constexpr int amountOfResults = 6; // exactly this meany results have to be read to exResult
- std::string label;
- std::string num1;
- std::string num2;
- bool autoChecked;
- std::vector<std::string> exResult; // expected results
- // Expected results of operations in this order: +, -, *
- std::vector<std::string> outResult; // results output by big int calculations
- };
- std::istream& operator>>(std::istream& is, TestBigInt& t);
- // Test the BigInt with the given test case
- std::ostream& performTest(std::ostream& os, TestBigInt& t);
- // Needed for auto-checking
- int stringToI(const std::string& s);
- std::string iToString(int a);
- void TestBigInt::doTests()
- {
- BigInt firstNum{ num1 };
- BigInt secNum{ num2 };
- // THESE FOUR LINES CAUSE THE BUG
- // outResult.push_back((firstNum + secNum).toString());
- // outResult.push_back((secNum + firstNum).toString());
- // outResult.push_back((firstNum - secNum).toString());
- // outResult.push_back((secNum - firstNum).toString());
- outResult.push_back((firstNum * secNum).toString());
- outResult.push_back((secNum * firstNum).toString());
- std::cout << "pushed_back outResults\n";
- }
- void TestBigInt::autoFillExR()
- {
- int firstNum = stringToI(num1);
- int secNum = stringToI(num2);
- exResult.push_back(iToString(firstNum + secNum));
- exResult.push_back(iToString(secNum + firstNum));
- exResult.push_back(iToString(firstNum - secNum));
- exResult.push_back(iToString(secNum - firstNum));
- exResult.push_back(iToString(firstNum * secNum));
- exResult.push_back(iToString(secNum * firstNum));
- std::cout << "pushed_back exResults\n";
- }
- std::istream& operator>>(std::istream& is, TestBigInt& t)
- /* Read input in the form:
- * { label num1 num2 autoChecked{ result+ result- result* } }
- * { label num1 num2 autoChecked{ } }
- */
- {
- char ch1;
- is >> ch1;
- if (ch1 != '{') { // no tests anymore
- is.unget();
- is.clear(std::ios_base::failbit);
- return is;
- }
- char ch2;
- std::string lab;
- std::string firstNum;
- std::string secNum;
- bool check;
- is >> lab >> firstNum >> secNum >> check >> ch2;
- if ((ch2 != '{'))
- throw std::runtime_error("invalid formatting brace1");
- t.label = lab;
- t.num1 = firstNum;
- t.num2 = secNum;
- t.autoChecked = check;
- if (!(check)) { // not auto-checking; need to input expected results
- for (int i = 0; i < t.amountOfResults; ++i) {
- std::string s;
- is >> s;
- if (s == "}")
- throw std::runtime_error("invalid number of expected results");
- t.exResult.push_back(s);
- }
- }
- char ch3, ch4;
- is >> ch3 >> ch4;
- if ((ch3 != '}') || (ch4 != '}'))
- throw std::runtime_error("invalid formatting brace2");
- return is;
- }
- std::ostream& performTest(std::ostream& os, TestBigInt& t)
- {
- if (t.autoChecked) // if the results are to be autochecked, than fill the exResult -- else it is already full
- t.autoFillExR();
- t.doTests();
- for (auto itE = t.exResult.cbegin(), itO = t.outResult.cbegin(); itE != t.exResult.cend() && itO != t.outResult.cend(); ++itE, ++itO)
- if (*itE != *itO)
- os << "Test not passed: " << t.label << ", " << distance(t.exResult.cbegin(), itE) << "\n\tExpected: " << *itE << ", got " << *itO << "\n";
- return os;
- }
- int stringToI(const std::string& s)
- {
- std::istringstream is{ s };
- int res;
- is >> res;
- return res;
- }
- std::string iToString(int a)
- {
- std::ostringstream os;
- os << a;
- return (os.str());
- }
- int main()
- {
- /* "BigIntTestCases.txt" - format { label num1 num2 autoChecked { } }, contents:
- * { 1 3 2 1 { } }
- * { 2 10 7 1 { } }
- * { 3 21 9 1 { } }
- * ...
- */
- ifstream ifs{ "BigIntTestCases.txt" };
- vector<TestBigInt> tests;
- for (TestBigInt t; ifs >> t; )
- tests.push_back(t);
- ofstream ofs{ "TestOutput.txt" };
- for (auto it = tests.begin(); it != tests.end(); ++it) {
- cout << "Read: " << it->label << ' ' << it->num1 << ' ' << it->num2 << ' ' << it->autoChecked << '\n';
- performTest(ofs, (*it));
- }
- std::cout << "Tests Complete! \n";
- return 0;
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement