Loading tests/Support/Common.h +67 −32 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ #include <string> #include <sstream> #include <regex> #include <cmath> using namespace std; /** Loading Loading @@ -68,19 +69,24 @@ public: * @return A int that was found * Based on ChatGPT output */ static float getInt(string str) { std::regex floatRe(R"(-?\d+)"); std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe); static int getInt(string str) { std::regex intRe(R"(-?\d+)"); std::sregex_iterator currentMatch(str.begin(), str.end(), intRe); std::sregex_iterator lastMatch; // end iterator std::string lastFloat; std::string lastInt; while (currentMatch != lastMatch) { lastFloat = currentMatch->str(); // Update with the current match lastInt = currentMatch->str(); // Update with the current match ++currentMatch; // Move to the next match } return stoi(lastFloat); // Return the last matched float try { return stoi(lastInt); // Return the last matched float } catch (exception& ignored) { cerr << "I could not parse " << lastInt << " into an integer." << endl; return NAN; } } /** Loading @@ -94,7 +100,8 @@ public: // set up streams inStream.str(iStr); outStream.str(""); outStream.clear(); outStream.str(""); outStream.clear(); cin.rdbuf(inStream.rdbuf()); cout.rdbuf(outStream.rdbuf()); } Loading @@ -111,5 +118,33 @@ public: return trim(outStream.str()); } // Testing functions /** * Test a given function and returns a float result * @param func A reference to the function. to be evaluated * @param input The input to be provided, through stdin, to the function. * @return The contents of what func directs to standard output */ static string testFunc(void (*func)(void), string input) { redirIO(input); func(); return restoreIO(); } /** * Same as testFuncFloat, but with a float output */ static float testFuncFloat(void (*func)(void), string input) { return getFloat(testFunc(func, input)); } /** * Same as testFuncFloat, but with an int output */ static int testFuncInt(void (*func)(void), string input) { return getInt(testFunc(func, input)); } }; #endif //PUBLIC_COMMON_H Loading
tests/Support/Common.h +67 −32 Original line number Diff line number Diff line Loading @@ -9,6 +9,7 @@ #include <string> #include <sstream> #include <regex> #include <cmath> using namespace std; /** Loading Loading @@ -68,19 +69,24 @@ public: * @return A int that was found * Based on ChatGPT output */ static float getInt(string str) { std::regex floatRe(R"(-?\d+)"); std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe); static int getInt(string str) { std::regex intRe(R"(-?\d+)"); std::sregex_iterator currentMatch(str.begin(), str.end(), intRe); std::sregex_iterator lastMatch; // end iterator std::string lastFloat; std::string lastInt; while (currentMatch != lastMatch) { lastFloat = currentMatch->str(); // Update with the current match lastInt = currentMatch->str(); // Update with the current match ++currentMatch; // Move to the next match } return stoi(lastFloat); // Return the last matched float try { return stoi(lastInt); // Return the last matched float } catch (exception& ignored) { cerr << "I could not parse " << lastInt << " into an integer." << endl; return NAN; } } /** Loading @@ -94,7 +100,8 @@ public: // set up streams inStream.str(iStr); outStream.str(""); outStream.clear(); outStream.str(""); outStream.clear(); cin.rdbuf(inStream.rdbuf()); cout.rdbuf(outStream.rdbuf()); } Loading @@ -111,5 +118,33 @@ public: return trim(outStream.str()); } // Testing functions /** * Test a given function and returns a float result * @param func A reference to the function. to be evaluated * @param input The input to be provided, through stdin, to the function. * @return The contents of what func directs to standard output */ static string testFunc(void (*func)(void), string input) { redirIO(input); func(); return restoreIO(); } /** * Same as testFuncFloat, but with a float output */ static float testFuncFloat(void (*func)(void), string input) { return getFloat(testFunc(func, input)); } /** * Same as testFuncFloat, but with an int output */ static int testFuncInt(void (*func)(void), string input) { return getInt(testFunc(func, input)); } }; #endif //PUBLIC_COMMON_H