Forked from
Configs / EC327 / Labs / Current
-
Ari Trachtenberg authoredAri Trachtenberg authored
Code owners
Assign users and groups as approvers for specific file changes. Learn more.
LabOne.cpp 3.58 KiB
/**
** Created by Ari Trachtenberg on 9/10/24.
** With help from ChatGPT
*/
#include <iostream>
#include <string>
#include "../problems/LabOne_ChapterFour.h"
#include "../problems/LabOne_ChapterFive.h"
#include "Support/Common.h"
using namespace std;
bool test_labOne_pZero() {
// simple, but incomplete tests
// example 1
Common::redirIO("3 4"); // redirect input
labOne_pZero();
string out = Common::restoreIO();
// count occurrences of ****
size_t pos=0; int count=0;
while ((pos = out.find("****", pos)) != string::npos) {
count++; // found a match
pos += 4; // Move the position forward to avoid overlapping matches
}
if (count!=3)
return false;
// example 2
Common::redirIO("1 10"); // redirect input
labOne_pZero();
out = Common::restoreIO();
if (out.find("**********") == string::npos)
return false;
return true;
}
bool test_labOne_pOne() {
// example 1
Common::redirIO("2");
labOne_pOne();
int out = stoi(Common::restoreIO());
if (out!=5)
return false;
// example 2
Common::redirIO("9");
labOne_pOne();
out = stoi(Common::restoreIO());
if (out!=47)
return false;
return true;
}
bool test_labOne_pTwo() {
Common::redirIO("20");
labOne_pTwo();
string out = Common::restoreIO();
// sum the length of each line
istringstream stream(out);
string line;
int sum = 0;
while (getline(stream, line)) {
sum += line.length();
}
if (sum==10)
return true;
else
return false;
}
bool test_labOne_pThree() {
Common::redirIO("20");
labOne_pThree();
string out = Common::restoreIO();
// sum the numbers on each line
istringstream stream(out);
string line;
int sum = 0;
while (getline(stream, line)) {
sum += stoi(line);
}
if (sum==132)
return true;
else
return false;
}
bool test_labOne_pFour() {
if (labOne_pFour(1729) == 14)
return true;
else
return false;
}
bool test_labOne_pFive() {
if (labOne_pFive("wolf") == "flow")
return true;
else
return false;
}
bool test_labOne_pSix() {
// example 1
if (!labOne_pSix("deed"))
return false;
// example 2
if (labOne_pSix("deeds"))
return false;
// example 3
if (!labOne_pSix("aibophpobia"))
return false;
return true;
}
bool test_labOne_pSeven() {
// example 1
if (labOne_pSeven(3)!=11)
return false;
// example 2
if (labOne_pSeven(10)!=120)
return false;
// example 3
if (labOne_pSeven(463)!=34101)
return false;
return true;
}
int main() {
bool t0 = test_labOne_pZero();
bool t1 = test_labOne_pOne();
bool t2 = test_labOne_pTwo();
bool t3 = test_labOne_pThree();
bool t4 = test_labOne_pFour();
bool t5 = test_labOne_pFive();
bool t6 = test_labOne_pSix();
bool t7 = test_labOne_pSeven();
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;
cout << "pSix: " << (t6 ? "passed" : "failed") << endl;
cout << "pSeven: " << (t7 ? "passed" : "failed") << endl;
if (t0 && t1 && t2 && t3 && t4 && t5 && t6 && t7) { // all tests passed
exit(0); // passed
} else {
exit(-1); // failed
}
}