Commit 37299e96 authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Refactord labs to have a common functions in their own class

parent a0bfeb61
Loading
Loading
Loading
Loading

tests/.gitkeep

deleted100644 → 0
+0 −0

Empty file deleted.

tests/LabOne.cpp

0 → 100644
+5 −0
Original line number Diff line number Diff line
//
// Created by Ari Trachtenberg on 9/10/24.
//

#include "LabOne.h"
+94 −148
Original line number Diff line number Diff line

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

#include "../problems/LabZero_ChapterTwo.h"
#include "../problems/LabZero_ChapterThree.h"
#include "Support/Common.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
    Common::redirIO("1 2 90"); // redirect input
    labZero_pZero();
  float out = stof(restoreIO());
    float out = stof(Common::restoreIO());
    if (out * out > 1.99 && out * out < 2.01) {
        return true;
  }
  else {
    } else {
        return false;
    }
}
@@ -77,17 +25,17 @@ bool test_labZero_pOne() {
    string out;

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

    // example 2
  redirIO("1730\n0900\n");
    Common::redirIO("1730\n0900\n");
    labZero_pOne();
  out = restoreIO();
    out = Common::restoreIO();
    if (out != "15 hours 30 minutes") {
        return false;
    }
@@ -97,9 +45,9 @@ bool test_labZero_pOne() {
}

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

@@ -107,9 +55,9 @@ bool test_labZero_pTwo() {
}

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

@@ -120,16 +68,16 @@ bool test_labZero_pFour() {
    string out;

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

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

@@ -140,16 +88,16 @@ bool test_labZero_pFive() {
    string out;

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

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

@@ -157,7 +105,6 @@ bool test_labZero_pFive() {
}



int main() {
    bool t0 = test_labZero_pZero();
    bool t1 = test_labZero_pOne();
@@ -175,8 +122,7 @@ int main() {

    if (t0 && t1 && t2 && t3 && t4 && t5) { // all tests passed
        exit(0);  // passed
  }
  else {
    } else {
        exit(-1); // failed
    }
}
 No newline at end of file
+7 −0
Original line number Diff line number Diff line
#include "Common.h"

// initialize static fields
streambuf* Common::orig_cin_buf = cin.rdbuf();
streambuf* Common::orig_cout_buf = cout.rdbuf();
istringstream Common::inStream("");
ostringstream Common::outStream("");

tests/Support/Common.h

0 → 100644
+72 −0
Original line number Diff line number Diff line
//
// Created by Ari Trachtenberg on 9/10/24.
//

#ifndef PUBLIC_COMMON_H
#define PUBLIC_COMMON_H

#include <iostream>
#include <string>
#include <sstream>
using namespace std;

/**
 * A Singleton class of variables and functions that are common to multiple labs.
 */

class Common {
//
// Created by Ari Trachtenberg on 9/3/24, with support from ChatGPT
//

public:

// global variables
    static streambuf* orig_cin_buf;  // records the standard input buffer
    static streambuf* orig_cout_buf; // records the standard output buffer
    static istringstream inStream;   // input stream to produce from a given string
    static ostringstream outStream;  // captured output stream


// HELPERS

// Function to trim leading and trailing whitespace - from ChatGPT
    static 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 output to strings
 * @param iStr The input string
 */
    static 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.
 * @requires redirIO() was called before this method
 * @return The output string that was captured since the redirect, trimmed of
 *         initial and final whitespace
 */
    static string restoreIO() {
        cin.rdbuf(orig_cin_buf);
        cout.rdbuf(orig_cout_buf);

        return trim(outStream.str());
    }
};
#endif //PUBLIC_COMMON_H