Commit 48cde00e authored by Ari Trachtenberg's avatar Ari Trachtenberg
Browse files

Fixed up testing

parent f457c10f
Loading
Loading
Loading
Loading
+1 −1
Original line number Diff line number Diff line
@@ -12,7 +12,7 @@
 * Side a: 1
 * Side b: 2
 * gamma: 90
 * 1.41421
 * 2.236
 */
void labZero_pZero();

+11 −10
Original line number Diff line number Diff line
@@ -14,8 +14,9 @@ using namespace std;
bool test_labZero_pZero() {
    Common::redirIO("1 2 90"); // redirect input
    labZero_pZero();
    float out = stof(Common::restoreIO());
    if (out * out > 1.99 && out * out < 2.01) {
    float out = Common::getFloat(Common::restoreIO());

    if (out > 2.23 && out < 2.24) {
        return true;
    } else {
        return false;
@@ -29,7 +30,7 @@ bool test_labZero_pOne() {
    Common::redirIO("0900\n1730\n");
    labZero_pOne();
    out = Common::restoreIO();
    if (out != "8 hours 30 minutes") {
    if (out.find("8 hours 30 minutes")==string::npos) {
        return false;
    }

@@ -37,7 +38,7 @@ bool test_labZero_pOne() {
    Common::redirIO("1730\n0900\n");
    labZero_pOne();
    out = Common::restoreIO();
    if (out != "15 hours 30 minutes") {
    if (out.find("15 hours 30 minutes")==string::npos) {
        return false;
    }

@@ -48,7 +49,7 @@ bool test_labZero_pOne() {
bool test_labZero_pTwo() {
    Common::redirIO("123456");
    labZero_pTwo();
    int out = stoi(Common::restoreIO());
    int out = Common::getInt(Common::restoreIO());
    if (out != 21)
        return false;

@@ -59,7 +60,7 @@ bool test_labZero_pThree() {
    Common::redirIO("1 2 3 4");
    labZero_pThree();
    string out = Common::restoreIO();
    if (out != "in order")
    if (out.find("not")!=string::npos)
        return false;

    return true;
@@ -72,14 +73,14 @@ bool test_labZero_pFour() {
    Common::redirIO("0 0\n0 1\n1 0\n1 1\n");
    labZero_pFour();
    out = Common::restoreIO();
    if (out != "square")
    if (out.find("square") == string::npos)
        return false;

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

    return true;
@@ -92,14 +93,14 @@ bool test_labZero_pFive() {
    Common::redirIO("14\n");
    labZero_pFive();
    out = Common::restoreIO();
    if (out != "XIV")
    if (out.find("XIV")==string::npos)
        return false;

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

    return true;
+43 −0
Original line number Diff line number Diff line
@@ -8,6 +8,7 @@
#include <iostream>
#include <string>
#include <sstream>
#include <regex>
using namespace std;

/**
@@ -40,6 +41,48 @@ public:
        return str.substr(first, (last - first + 1));
    }

    /**
     * Attempts to extract the last viable floating point number from a string.
     * @param str The string to search.
     * @return A floating point number that was found
     * Based on ChatGPT output
     */
    static float getFloat(string str) {
      std::regex floatRe(R"(-?\d+(\.\d+)?)");
      std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe);
      std::sregex_iterator lastMatch; // end iterator

      std::string lastFloat;

      while (currentMatch != lastMatch) {
        lastFloat = currentMatch->str();  // Update with the current match
        ++currentMatch;  // Move to the next match
      }

      return stof(lastFloat);  // Return the last matched float
    }

  /**
* Attempts to extract the last viable int from a string.
* @param str The string to search.
* @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);
    std::sregex_iterator lastMatch; // end iterator

    std::string lastFloat;

    while (currentMatch != lastMatch) {
      lastFloat = currentMatch->str();  // Update with the current match
      ++currentMatch;  // Move to the next match
    }

    return stoi(lastFloat);  // Return the last matched float
  }

/**
 * Redirects input and output to strings
 * @param iStr The input string