Skip to content
Snippets Groups Projects
Forked from Configs / EC327 / Labs / Current
193 commits behind the upstream repository.
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
LabZero.cpp 3.63 KiB

#include <iostream>
#include <string>
#include <sstream>

#include "../problems/LabZero_ChapterTwo.h"
#include "../problems/LabZero_ChapterThree.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
istringstream inStream;   // input stream to produce from a given string
ostringstream outStream;  // captured output stream


// 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
 * @param iStr The input string
 */
void redirIO(const string iStr) {
  // save the old buffers
  orig_cin_buf = cin.rdbuf();
  orig_cout_buf = cout.rdbuf();

  // set up streams
  inStream.str(iStr);
  outStream.str(""); outStream.clear();
  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, trimmed of
 *         initial and final whitespace
 */
string restoreIO() {
  cin.rdbuf(orig_cin_buf);
  cout.rdbuf(orig_cout_buf);

  return trim(outStream.str());
}


// TESTS
bool test_labZero_pZero() {
  redirIO("1 2 90"); // redirect input
  labZero_pZero();
  float out = stof(restoreIO());
  if (out*out>1.99 && out*out<2.01) {
    return true;
  }
  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() {
  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
  }
}