Commit e307ecda authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

added tests

parent be0fb183
Loading
Loading
Loading
Loading
+120 −4
Original line number Diff line number Diff line
@@ -18,7 +18,17 @@ streambuf* orig_cout_buf; // records the standard output buffer
ostringstream outStream;    // captured output stream


void labZero_pZero();
// HELPERS

// Function to trim leading and trailing whitespace - from ChatGPT
string trim(const string& str) {
  size_t first = str.find_first_not_of(" \t\n\r\f\v");
  if (first == std::string::npos)
    return "";  // The string is all whitespace or empty

  size_t last = str.find_last_not_of(" \t\n\r\f\v");
  return str.substr(first, (last - first + 1));
}

/**
 * Redirects input and ouput to strings
@@ -36,15 +46,18 @@ void redirIO(const string iStr) {

/**
 * Restore input and output to STDIN/STDOUT, respectively.
 * @return The output string that was captured since the redirect
 * @return The output string that was captured since the redirect, trimmed of
 *         initial and final whitespace
 */
string restoreIO() {
    cin.rdbuf(orig_cin_buf);
    cout.rdbuf(orig_cout_buf);

    return outStream.str();
    return trim(outStream.str());
}


// TESTS
bool test_labZero_pZero() {
    redirIO("1\n2\n90\n"); // redirect input
    labZero_pZero();
@@ -55,9 +68,112 @@ bool test_labZero_pZero() {
    else {
        return false;
    }
}

bool test_labZero_pOne() {
  string out;

  // example 1
  redirIO("0900\n1730\n");
  labZero_pOne();
  out = restoreIO();
  if (out!="8 hours 30 minutes") {
    return false;
  }

  // example 2
  redirIO("1730\n0900\n");
  labZero_pOne();
  out = restoreIO();
  if (out!="15 hours 30 minutes") {
    return false;
  }

  // passed all tests
  return true;
}

bool test_labZero_pTwo() {
  redirIO("123456");
  labZero_pTwo();
  int out = stoi(restoreIO());
  if (out!=21)
    return false;

  return true;
}

bool test_labZero_pThree() {
  redirIO("1 2 3 4");
  labZero_pThree();
  string out = restoreIO();
  if (out!="in order")
    return false;

  return true;
}

bool test_labZero_pFour() {
  string out;

  // example 1
  redirIO("0 0\n0 1\n1 0\n1 1\n");
  labZero_pFour();
  out = restoreIO();
  if (out!="square")
    return false;

  // example 2
  redirIO("0 0\n0 1\n1 0\n1 1\n");
  labZero_pFour();
  out = restoreIO();
  if (out!="trapezoid")
    return false;

  return true;
}

bool test_labZero_pFive() {
  string out;

  // example 1
  redirIO("14\n");
  labZero_pFive();
  out = restoreIO();
  if (out!="XIV")
    return false;

  // example 2
  redirIO("1978\n");
  labZero_pFive();
  out = restoreIO();
  if (out!="MCMLXXVIII")
    return false;

  return true;
}



int main() {
    cout << test_labZero_pZero() << endl;
  bool t0 = test_labZero_pZero();
  bool t1 = test_labZero_pOne();
  bool t2 = test_labZero_pTwo();
  bool t3 = test_labZero_pThree();
  bool t4 = test_labZero_pFour();
  bool t5 = test_labZero_pFive();

  cout << "pZero: " << (t0?"passed":"failed") << endl;
  cout << "pOne: " << (t1?"passed":"failed") << endl;
  cout << "pTwo: " << (t2?"passed":"failed") << endl;
  cout << "pThree: " << (t3?"passed":"failed") << endl;
  cout << "pFour: " << (t4?"passed":"failed") << endl;
  cout << "pFive: " << (t5?"passed":"failed") << endl;

  if (t0 && t1 && t2 && t3 && t4 && t5) { // all tests passed
    exit(0);  // passed
  }
  else {
    exit(-1); // failed
  }
}
 No newline at end of file