Loading tests/LabZero.cpp 0 → 100644 +62 −0 Original line number Diff line number Diff line #include <iostream> #include <string> #include <sstream> #include "../problems/LabZero.h" using namespace std; // // Created by Ari Trachtenberg on 9/3/24, with support from ChatGPT // // global variables streambuf* orig_cin_buf; // records the standard input buffer streambuf* orig_cout_buf; // records the standard output buffer ostringstream outStream; // captured output stream void labZero_pZero(); /** * Redirects input and ouput to strings * @param iStr The input string */ void redirIO(const string iStr) { // save the old buffers orig_cin_buf = cin.rdbuf(); orig_cout_buf = cout.rdbuf(); istringstream inStream(iStr); cin.rdbuf(inStream.rdbuf()); cout.rdbuf(outStream.rdbuf()); } /** * Restore input and output to STDIN/STDOUT, respectively. * @return The output string that was captured since the redirect */ string restoreIO() { cin.rdbuf(orig_cin_buf); cout.rdbuf(orig_cout_buf); return outStream.str(); } bool test_labZero_pZero() { redirIO("1\n2\n90\n"); // redirect input labZero_pZero(); float out = stof(restoreIO()); if (out*out>1.99 && out*out<2.01) { return true; } else { return false; } } int main() { cout << test_labZero_pZero() << endl; } No newline at end of file Loading
tests/LabZero.cpp 0 → 100644 +62 −0 Original line number Diff line number Diff line #include <iostream> #include <string> #include <sstream> #include "../problems/LabZero.h" using namespace std; // // Created by Ari Trachtenberg on 9/3/24, with support from ChatGPT // // global variables streambuf* orig_cin_buf; // records the standard input buffer streambuf* orig_cout_buf; // records the standard output buffer ostringstream outStream; // captured output stream void labZero_pZero(); /** * Redirects input and ouput to strings * @param iStr The input string */ void redirIO(const string iStr) { // save the old buffers orig_cin_buf = cin.rdbuf(); orig_cout_buf = cout.rdbuf(); istringstream inStream(iStr); cin.rdbuf(inStream.rdbuf()); cout.rdbuf(outStream.rdbuf()); } /** * Restore input and output to STDIN/STDOUT, respectively. * @return The output string that was captured since the redirect */ string restoreIO() { cin.rdbuf(orig_cin_buf); cout.rdbuf(orig_cout_buf); return outStream.str(); } bool test_labZero_pZero() { redirIO("1\n2\n90\n"); // redirect input labZero_pZero(); float out = stof(restoreIO()); if (out*out>1.99 && out*out<2.01) { return true; } else { return false; } } int main() { cout << test_labZero_pZero() << endl; } No newline at end of file