Skip to content
#include <iostream>
#include <stdexcept>
#include <string>
#include "lab5.cpp"
using namespace std;
bool testLogicError(Graph &g, int value) {
try {
// Call the student's function with input that should trigger the exception.
g+=value;
} catch (const logic_error& e) {
return true; //if it returns logic error
} catch (...) {
return false;
}
return false;
}
bool test_labFive(){
Graph G;
if(testLogicError(G, 3)){
cout << "Test 5a failed" << endl;
return false;
}
if(testLogicError(G, 5)){
cout << "Test 5b failed" << endl;
return false;
}
if(!testLogicError(G, 3)){ //should throw a logic error
cout << "Test 5c failed" << endl;
return false;
}
if(G.hasEdge(3,5)){
cout << "Test 5d failed" << endl;
return false;
}
G.addEdge(5,3);
if(!G.hasEdge(3,5)){
cout << "Test 5e failed" << endl;
return false;
}
if(G[0] != 3){
cout << "Test 5f failed" << endl;
return false;
}
if(G.numVertices() != 2){
cout << "Test 5g failed" << endl;
return false;
}
if(G.numEdges() != 1){
cout << "Test 5h failed" << endl;
return false;
}
// All tests passed
return true;
}
int main(){
bool t0 = test_labFive();
cout << "Lab Five: " << (t0 ? "passed" : "failed") << endl;
if (t0) { // all tests passed
exit(0); // passed
} else {
exit(1); // failed
}
}
/**
** Created by James Knee on 11/17/2024
** With help from ChatGPT
*/
#include <iostream>
#include <string>
#include <cstring>
#include <cmath>
#include <sstream>
#include "Support/Common.h"
#include "lab4.cpp"
using namespace std;
bool test_labFour_pZero(){
labFour_pZero_Person person1("James");
person1.setBirthday("2010-02-28");
labFour_pZero_Student person2("Jane","2020-03-15");
person2.setMajor("COMPUTER_ENGINEERING");
labFour_pZero_Instructor person3("Gabi");
person3.setBirthday("2000-01-03");
person3.setSalary(100000);
labFour_pZero_Person person4;
labFour_pZero_Student person5;
labFour_pZero_Instructor person6;
stringstream input0("Pleiade 2004-02-26");
stringstream input1("Jake 2000-11-22 Computer_Science");
stringstream input2("Olivia 2000-01-30 123456");
input0 >> person4;
input1 >> person5;
input2 >> person6;
Common::redirIO("");
cout << person4;
string output0 = Common::restoreIO();
Common::redirIO("");
cout << person5;
string output1 = Common::restoreIO();
Common::redirIO("");
cout << person6;
string output2 = Common::restoreIO();
if(person1.getName() != "James"){
cout << "Test 0a failed" << endl;
return false;
}
if(person1.getBirthday() != "2010-02-28"){
cout << "Test 0b failed" << endl;
return false;
}
if(person2.getName() != "Jane"){
cout << "Test 0c failed" << endl;
return false;
}
if(person2.getBirthday() != "2020-03-15"){
cout << "Test 0d failed" << endl;
return false;
}
if(person2.getMajor() != "COMPUTER_ENGINEERING"){
cout << "Test 0e failed" << endl;
return false;
}
if(person3.getName() != "Gabi"){
cout << "Test 0f failed" << endl;
return false;
}
if(person3.getBirthday() != "2000-01-03"){
cout << "Test 0g failed" << endl;
return false;
}
if(person3.getSalary() != 100000){
cout << "Test 0h failed" << endl;
return false;
}
//Quick test of the << operators for each class
//Since we don't have a specific way to format the output, we have to check it is included
if(output0.find("Pleiade")==string::npos || output0.find("2004-02-26")==string::npos){
cout << "Test 0i failed" << endl;
return false;
}
if(output1.find("Jake")==string::npos || output1.find("2000-11-22")==string::npos || output1.find("Computer_Science")==string::npos){
cout << "Test 0j failed" << endl;
return false;
}
if(output2.find("Olivia")==string::npos || output2.find("2000-01-30")==string::npos || output2.find("123456")==string::npos){
cout << "Test 0k failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labFour_pOne(){
labFour_pOne_Quadrilateral q1(
{1, 1},
{1, 2},
{2, 2},
{2, 1}); // area 1, perimeter 4
labFour_pOne_Rectangle re1({0, 0}, 1, 2); // area 2, perimeter 6
labFour_pOne_Square s1({1, 3}, 3); //area 9, perimeter 12
labFour_pOne_Rhombus rh1({1, 1}, 1, 45); // area sqrt(2)/2, perimeter 4
labFour_pOne_Circle c1({0, 0}, 1); // area pi, perimeter 2*pi
vector<labFour_pOne_GeometricThing *> things = {&q1, &re1, &s1, &rh1, &c1};
if(q1.getArea() != 1){
cout << "Test 1a failed" << endl;
return false;
}
if(q1.getPerimeter() != 4){
cout << "Test 1b failed" << endl;
return false;
}
if(re1.getArea() != 2){
cout << "Test 1c failed" << endl;
return false;
}
if(re1.getPerimeter() != 6){
cout << "Test 1d failed" << endl;
return false;
}
if(s1.getArea() != 9){
cout << "Test 1e failed" << endl;
return false;
}
if(s1.getPerimeter() != 12){
cout << "Test 1f failed" << endl;
return false;
}
float area0 = sqrt(2) / 2.0;
if(rh1.getArea() != area0){
cout << "Test 1g failed" << endl;
return false;
}
if(rh1.getPerimeter() != 4){
cout << "Test 1h failed" << endl;
return false;
}
float area1 = M_PI;
if(c1.getArea() != area1){
cout << "Test 1i failed" << endl;
return false;
}
float perimeter0 = 2 * M_PI;
if(c1.getPerimeter() != perimeter0){
cout << "Test 1j failed" << endl;
return false;
}
float sumAreas0 = 1 + 2 + 9 + sqrt(2) / 2.0 + M_PI;
if(labFour_pOne_GeometricThing::sumAreas(things) != sumAreas0){
cout << "Test 1k failed" << endl;
return false;
}
float sumAreas1 = 4 + 6 + 12 + 4 + 2 * M_PI;
if(labFour_pOne_GeometricThing::sumPerimeters(things) != sumAreas1){
cout << "Test 1l failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labFour_pTwo(){
if(labFour_pTwo_exchange(8) != 7){
cout << "Test 2a failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labFour_pThree(){
float fib0 = labFour_pThree_genFib({1, 1}, 3);
float fib1 = labFour_pThree_genFib({1, 0, 1}, 4);
float fib2 = labFour_pThree_genFib({0.25, 0.25 ,0.5, 0.5}, 3);
if((fib0 <= 7.99) || (fib0 >= 8.01)){
cout << "Test 3a failed" << endl;
return false;
}
if((fib1 <= 8.99) || (fib1 >= 9.01)){
cout << "Test 3b failed" << endl;
return false;
}
cout << fib2 << endl;
if( (fib2 <= 2.562) || (fib2 >= 2.563)){
cout << "Test 3c failed" << endl;
return false;
}
// All tests passed
return true;
}
int main() {
bool t0 = test_labFour_pZero();
bool t1 = test_labFour_pOne();
bool t2 = test_labFour_pTwo();
bool t3 = test_labFour_pThree();
cout << "pZero: " << (t0 ? "passed" : "failed") << endl;
cout << "pOne: " << (t1 ? "passed" : "failed") << endl;
cout << "pTwo: " << (t2 ? "passed" : "failed") << endl;
cout << "pThree: " << (t3 ? "passed" : "failed") << endl;
if (t0 && t1 && t2 && t3) { // all tests passed
exit(0); // passed
} else {
exit(-1); // failed
}
}
\ No newline at end of file
...@@ -17,9 +17,7 @@ bool test_labOne_pZero() { ...@@ -17,9 +17,7 @@ bool test_labOne_pZero() {
// simple, but incomplete tests // simple, but incomplete tests
// example 1 // example 1
Common::redirIO("3 4"); // redirect input string out = Common::testFunc(labOne_pZero,"3 4");
labOne_pZero();
string out = Common::restoreIO();
// count occurrences of **** // count occurrences of ****
size_t pos=0; int count=0; size_t pos=0; int count=0;
while ((pos = out.find("****", pos)) != string::npos) { while ((pos = out.find("****", pos)) != string::npos) {
...@@ -30,9 +28,7 @@ bool test_labOne_pZero() { ...@@ -30,9 +28,7 @@ bool test_labOne_pZero() {
return false; return false;
// example 2 // example 2
Common::redirIO("1 10"); // redirect input out = Common::testFunc(labOne_pZero,"1 10");
labOne_pZero();
out = Common::restoreIO();
if (out.find("**********") == string::npos) if (out.find("**********") == string::npos)
return false; return false;
...@@ -41,67 +37,61 @@ bool test_labOne_pZero() { ...@@ -41,67 +37,61 @@ bool test_labOne_pZero() {
bool test_labOne_pOne() { bool test_labOne_pOne() {
// example 1 // example 1
Common::redirIO("2"); int out = Common::testFuncInt(labOne_pOne, "2");
labOne_pOne();
int out = Common::getInt(Common::restoreIO());
if (out!=5) if (out!=5)
return false; return false;
// example 2 // example 2
Common::redirIO("9"); out = Common::testFuncInt(labOne_pOne,"9");
labOne_pOne(); if (out!=48)
out = stoi(Common::restoreIO());
if (out!=47)
return false; return false;
return true; return true;
} }
bool test_labOne_pTwo() { bool test_labOne_pTwo() {
Common::redirIO("20"); string out = Common::testFunc(labOne_pTwo,"rum");
labOne_pTwo();
string out = Common::restoreIO(); // sum the length of each line
istringstream stream(out);
// sum the length of each line string line;
istringstream stream(out); int sum = 0;
string line; while (getline(stream, line)) {
int sum = 0; // ignore lines that have "Input:" or "Output:"
while (getline(stream, line)) { if (
// ignore lines that have "Input:" or "Output:" (line.find("Input:") == string::npos)
if ( && (line.find("Output:") == string::npos)
(line.find("Input:") == string::npos) )
&& (line.find("Output:") == string::npos) sum += line.length();
) }
sum += line.length();
} if (sum != 10) {
return false;
if (sum == 10) }
// All tests passed
return true; return true;
else
return false;
} }
bool test_labOne_pThree() { bool test_labOne_pThree() {
Common::redirIO("20"); string out = Common::testFunc(labOne_pThree, "20");
labOne_pThree();
string out = Common::restoreIO();
// sum the numbers on each line // sum the numbers on each line
istringstream stream(out); istringstream stream(out);
string line; string line;
int sum = 0; int sum = 0;
while (getline(stream, line)) { while (getline(stream, line)) {
if ( if (
(line.find("Input:") == string::npos) (line.find("Input:") == string::npos)
&& (line.find("Output:") == string::npos) && (line.find("Output:") == string::npos)
) )
sum += Common::getInt(line); sum += Common::getInt(line);
} }
if (sum==132) if (sum!=132)
return true;
else
return false; return false;
return true;
} }
bool test_labOne_pFour() { bool test_labOne_pFour() {
......
/**
** Created by Ari Trachtenberg on 10/31/2024
** With help from ChatGPT
*/
#include <iostream>
#include <string>
#include <cstring>
#include "lab3.cpp"
using namespace std;
bool test_labThree_pZero() {
int *result = labThree_pZero("Support/example.txt");
//test a
if(result[0]!=1 || result[3]!=2 || result[14]!=4){
cout << "Test 0a failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labThree_pOne() {
// test a
if (labThree_pOne("Support/example.csv") !=4) {
cout << "Test 1a failed" << endl;
return false;
}
// test b
if (labThree_pOne("Support/example2.csv") !=4) {
cout << "Test 1b failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labThree_pTwo() {
labThree_pTwo student("Jack Daniels");
student.addQuizScore(100);
student.addQuizScore(70);
student.addQuizScore(70);
if (student.getAverageScore()!=80) {
cout << "Test 2a failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labThree_pThree() {
labThree_pThree person1("Jim");
labThree_pThree person2("Jane");
labThree_pThree person3("Bob");
person1.befriend(person2);
person1.befriend(person3);
person2.befriend(person1);
if (person1.getFriendCount()!=2) {
cout << "Test 3a failed" << endl;
return false;
}
if (person2.getFriendCount()!=1) {
cout << "Test 3b failed" << endl;
return false;
}
if (person3.getFriendCount()!=0) {
cout << "Test 3c failed" << endl;
return false;
}
string result = person1.getFriends();
if (result.find("Jane")==string::npos || result.find("Bob")==string::npos) {
cout << "Test 3d failed" << endl;
return false;
}
string result2 = person2.getFriends();
if (result2.find("Jim")==string::npos || result2.length()!=3) {
cout << "Test 3e failed" << endl;
return false;
}
string result3 = person1.getFriendsOfFriends();
if (result3.find("Jim")==string::npos || result3.length()!=3) {
cout << "Test 3f failed" << endl;
return false;
}
string result4 = person2.getFriendsOfFriends();
if (result4.find("Jane")==string::npos || result4.find("Bob")==string::npos) {
cout << "Test 3g failed" << endl;
return false;
}
// All tests passed
return true;
}
int main() {
bool t0 = test_labThree_pZero();
bool t1 = test_labThree_pOne();
bool t2 = test_labThree_pTwo();
bool t3 = test_labThree_pThree();
cout << endl << endl << endl;
cout << "pZero: " << (t0 ? "passed" : "failed") << endl;
cout << "pOne: " << (t1 ? "passed" : "failed") << endl;
cout << "pTwo: " << (t2 ? "passed" : "failed") << endl;
cout << "pThree: " << (t3 ? "passed" : "failed") << endl;
if (t0 && t1 && t2 && t3) { // all tests passed
exit(0); // passed
} else {
exit(-1); // failed
}
}
\ No newline at end of file
/**
** Created by James Knee on 10/21/24.
** With help from ChatGPT
*/
#include <iostream>
#include <string>
#include <cstring>
#include "../problems/LabTwo_ChapterSix.h"
#include "../problems/LabTwo_ChapterSeven.h"
using namespace std;
bool test_labTwo_pZero() {
// simple, but incomplete tests
int arr0 [] = {1,2,3};
int arr1 [] = {1,2,3,4};
//test 0
if(labTwo_pZero(arr0,3) != 9){
cout << "Test 0a failed" << endl;
return false;
}
//test 1
if(labTwo_pZero(arr1,4) != 14){
cout << "Test 0b failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labTwo_pOne() {
int A0[] = {1,4,9,16,9,7,4,9,11};
int A1[] = {11,1,4,9,16,9,7,4,9};
int B0[] = {1,4,9,16,9,7,4,9,11};
int B1[] = {11,11,7,9,16,4,1,4,9};
//test 0
if (!labTwo_pOne(A0,A1,9)){
cout << "Test 1a failed" << endl;
return false;
}
//test 1
if(labTwo_pOne(B0,B1,9)){
cout << "Test 1b failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labTwo_pTwo() {
int arr0[] = {1,2,3,5};
int arr1[] = {2,7,6,9,5,1,4,3,8};
int arr2[] = {16,3,2,13,5,10,11,8,9,6,7,12,4,15,14,1};
//test 0
if (labTwo_pTwo(arr0,4)){
cout << "Test 2a failed" << endl;
return false;
}
//test 1
if (!labTwo_pTwo(arr1,9)){
cout << "Test 2b failed" << endl;
return false;
}
//test 2
if (!labTwo_pTwo(arr2,16)){
cout << "Test 2c failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labTwo_pThree() {
double arr0[] = {1,5,3,2};
double arr1[] = {3,1,4,1,5,9,2,6,5,3,5,6};
double *test0 = labTwo_pThree(arr0,4);
double *test1 = labTwo_pThree(arr1,12);
//test 0
if (*test0 != 2){
cout << "Test 3a failed" << endl;
return false;
}
//test 1
if (*test1 != 6){
cout << "Test 3b failed" << endl;
return false;
}
// All tests passed
return true;
}
bool test_labTwo_pFour() {
char *result0;
const char arr00[] = {'H','e','l','l','o','\0'};
const char arr01[] = {'W','o','r','l','d','\0'};
char *result1;
const char arr10[] = {'B','o','s','t','o','n','\0'};
const char arr11[] = {'U','n','i','v','e','r','s','i','t','y','\0'};
labTwo_pFour(arr00,arr01,result0);
labTwo_pFour(arr10,arr11,result1);
//test 0
if (strcmp(result0,"HelloWorld") != 0){
cout << "Test 4a failed" << endl;
return false;
}
//test 1
if (strcmp(result1,"BostonUniversity") != 0){
cout << "Test 4b failed" << endl;
return false;
}
//All tests passed
return true;
}
bool test_labTwo_pFive() {
const char *src0 = "Hatty";
const char *seek0 = "t";
const char *replace0 = "p";
const char *src1 = "Hatty";
const char *seek1 = "tt";
const char *replace1 = "ppil";
const char *src2 = "Happy";
const char *seek2 = "p";
const char *replace2 = "";
char *test0 = labTwo_pFive(src0,seek0,replace0);
char *test1 = labTwo_pFive(src1,seek1,replace1);
char *test2 = labTwo_pFive(src2,seek2,replace2);
//test 0
if (strcmp(test0,"Happy") != 0){
cout << "Test 5a failed" << endl;
return false;
}
//test 1
if (strcmp(test1,"Happily") != 0){
cout << "Test 5b failed" << endl;
return false;
}
//test 2
if (strcmp(test2,"Hay") != 0){
cout << "Test 5c failed" << endl;
return false;
}
//All tests passed
return true;
}
int main() {
bool t0 = test_labTwo_pZero();
bool t1 = test_labTwo_pOne();
bool t2 = test_labTwo_pTwo();
bool t3 = test_labTwo_pThree();
bool t4 = test_labTwo_pFour();
bool t5 = test_labTwo_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
...@@ -16,7 +16,7 @@ bool test_labZero_pZero() { ...@@ -16,7 +16,7 @@ bool test_labZero_pZero() {
labZero_pZero(); labZero_pZero();
float out = Common::getFloat(Common::restoreIO()); float out = Common::getFloat(Common::restoreIO());
if (out > 2.23 && out < 2.24) { if (out > 2.2 && out < 2.4) {
return true; return true;
} else { } else {
return false; return false;
...@@ -70,14 +70,14 @@ bool test_labZero_pFour() { ...@@ -70,14 +70,14 @@ bool test_labZero_pFour() {
string out; string out;
// example 1 // example 1
Common::redirIO("0 0\n0 1\n1 0\n1 1\n"); Common::redirIO("0 0\n0 1\n1 1\n1 0\n");
labZero_pFour(); labZero_pFour();
out = Common::restoreIO(); out = Common::restoreIO();
if (out.find("square") == string::npos) if (out.find("square") == string::npos)
return false; return false;
// example 2 // example 2
Common::redirIO("0 0\n0 1\n2 0\n1 1\n"); Common::redirIO("0 0\n0 1\n1 1\n2 0\n");
labZero_pFour(); labZero_pFour();
out = Common::restoreIO(); out = Common::restoreIO();
if (out.find("trapezoid") == string::npos) if (out.find("trapezoid") == string::npos)
......
...@@ -8,18 +8,32 @@ CXXFLAGS = -Wall -std=c++17 ...@@ -8,18 +8,32 @@ CXXFLAGS = -Wall -std=c++17
COMMON_SRCS = Support/Common.cpp COMMON_SRCS = Support/Common.cpp
LAB0_SRCS = LabZero.cpp lab0.cpp $(COMMON_SRCS) LAB0_SRCS = LabZero.cpp lab0.cpp $(COMMON_SRCS)
LAB1_SRCS = LabOne.cpp lab1.cpp $(COMMON_SRCS) LAB1_SRCS = LabOne.cpp lab1.cpp $(COMMON_SRCS)
LAB2_SRCS = LabTwo.cpp lab2.cpp
LAB3_SRCS = LabThree.cpp
LAB4_SRCS = LabFour.cpp $(COMMON_SRCS)
LAB5_SRCS = LabFive.cpp
# Object files # Object files
COMM_OBJS = Support/Common.o COMM_OBJS = Support/Common.o
LAB0_OBJS = LabZero.o lab0.o $(COMM_OBJS) LAB0_OBJS = LabZero.o lab0.o $(COMM_OBJS)
LAB1_OBJS = LabOne.o lab1.o $(COMM_OBJS) LAB1_OBJS = LabOne.o lab1.o $(COMM_OBJS)
LAB2_OBJS = LabTwo.o lab2.o
LAB3_OBJS = LabThree.o
LAB4_OBJS = LabFour.o $(COMM_OBJS)
LAB5_OBJS = LabFive.o
OBJS = $(COMM_OBJS) $(LAB0_OBJS) $(LAB1_OBJS) $(LAB2_OBJS) $(LAB3_OBJS) $(LAB4_OBJS) $(LAB5_OBJS)
# Executable names # Executable names
LAB0_EXEC = lab0 LAB0_EXEC = lab0
LAB1_EXEC = lab1 LAB1_EXEC = lab1
LAB2_EXEC = lab2
LAB3_EXEC = lab3
LAB4_EXEC = lab4
LAB5_EXEC = lab5
EXEC = $(LAB0_EXEC) $(LAB1_EXEC) $(LAB2_EXEC) $(LAB3_EXEC) $(LAB4_EXEC) $(LAB5_EXEC)
# Default target to build the executable # Default target to build the executable
all: $(LAB0_EXEC) $(LAB1_EXEC) all: $(LAB0_EXEC) $(LAB1_EXEC) $(LAB2_EXEC) $(LAB3_EXEC) $(LAB4_EXEC) $(LAB5_EXEC)
# Rule to build the executable from object files # Rule to build the executable from object files
$(LAB0_EXEC): $(LAB0_OBJS) Makefile $(LAB0_EXEC): $(LAB0_OBJS) Makefile
...@@ -30,6 +44,23 @@ $(LAB1_EXEC): $(LAB1_OBJS) Makefile ...@@ -30,6 +44,23 @@ $(LAB1_EXEC): $(LAB1_OBJS) Makefile
$(CXX) $(CXXFLAGS) -o $(LAB1_EXEC) $(LAB1_OBJS) $(CXX) $(CXXFLAGS) -o $(LAB1_EXEC) $(LAB1_OBJS)
./$(LAB1_EXEC) ./$(LAB1_EXEC)
$(LAB2_EXEC): $(LAB2_OBJS) Makefile
$(CXX) $(CXXFLAGS) -o $(LAB2_EXEC) $(LAB2_OBJS)
./$(LAB2_EXEC)
$(LAB3_EXEC): $(LAB3_OBJS) Makefile lab3.cpp
$(CXX) $(CXXFLAGS) -o $(LAB3_EXEC) $(LAB3_OBJS)
./$(LAB3_EXEC)
$(LAB4_EXEC): $(LAB4_OBJS) Makefile lab4.cpp
$(CXX) $(CXXFLAGS) -o $(LAB4_EXEC) $(LAB4_OBJS)
./$(LAB4_EXEC)
$(LAB5_EXEC): $(LAB5_OBJS) Makefile lab5.cpp
$(CXX) $(CXXFLAGS) -o $(LAB5_EXEC) $(LAB5_OBJS)
./$(LAB5_EXEC)
# Rules to build object files from source files, with dependency on the Common.h header # Rules to build object files from source files, with dependency on the Common.h header
%.o: %.cpp Support/Common.h Makefile %.o: %.cpp Support/Common.h Makefile
$(CXX) $(CXXFLAGS) -c $< -o $@ $(CXX) $(CXXFLAGS) -c $< -o $@
...@@ -39,4 +70,4 @@ $(SUPPORT_DIR)/Common.o: Support/Common.cpp Support/Common.h ...@@ -39,4 +70,4 @@ $(SUPPORT_DIR)/Common.o: Support/Common.cpp Support/Common.h
# Clean target to remove compiled files # Clean target to remove compiled files
clean: clean:
rm -f $(OBJS) $(EXEC) rm -f $(OBJS) $(EXEC)
\ No newline at end of file
...@@ -9,6 +9,9 @@ ...@@ -9,6 +9,9 @@
#include <string> #include <string>
#include <sstream> #include <sstream>
#include <regex> #include <regex>
#include <cmath>
#include <limits>
using namespace std; using namespace std;
/** /**
...@@ -23,8 +26,8 @@ class Common { ...@@ -23,8 +26,8 @@ class Common {
public: public:
// global variables // global variables
static streambuf* orig_cin_buf; // records the standard input buffer static streambuf *orig_cin_buf; // records the standard input buffer
static streambuf* orig_cout_buf; // records the standard output buffer static streambuf *orig_cout_buf; // records the standard output buffer
static istringstream inStream; // input stream to produce from a given string static istringstream inStream; // input stream to produce from a given string
static ostringstream outStream; // captured output stream static ostringstream outStream; // captured output stream
...@@ -32,7 +35,7 @@ public: ...@@ -32,7 +35,7 @@ public:
// HELPERS // HELPERS
// Function to trim leading and trailing whitespace - from ChatGPT // Function to trim leading and trailing whitespace - from ChatGPT
static string trim(const string& str) { static string trim(const string &str) {
size_t first = str.find_first_not_of(" \t\n\r\f\v"); size_t first = str.find_first_not_of(" \t\n\r\f\v");
if (first == std::string::npos) if (first == std::string::npos)
return ""; // The string is all whitespace or empty return ""; // The string is all whitespace or empty
...@@ -48,41 +51,46 @@ public: ...@@ -48,41 +51,46 @@ public:
* Based on ChatGPT output * Based on ChatGPT output
*/ */
static float getFloat(string str) { static float getFloat(string str) {
std::regex floatRe(R"(-?\d+(\.\d+)?)"); std::regex floatRe(R"(-?\d+(\.\d+)?)");
std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe); std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe);
std::sregex_iterator lastMatch; // end iterator std::sregex_iterator lastMatch; // end iterator
std::string lastFloat; std::string lastFloat;
while (currentMatch != lastMatch) { while (currentMatch != lastMatch) {
lastFloat = currentMatch->str(); // Update with the current match lastFloat = currentMatch->str(); // Update with the current match
++currentMatch; // Move to the next match ++currentMatch; // Move to the next match
} }
return stof(lastFloat); // Return the last matched float return stof(lastFloat); // Return the last matched float
} }
/** /**
* Attempts to extract the last viable int from a string. * Attempts to extract the last viable int from a string.
* @param str The string to search. * @param str The string to search.
* @return A int that was found * @return A int that was found
* Based on ChatGPT output * Based on ChatGPT output
*/ */
static float getInt(string str) { static int getInt(string str) {
std::regex floatRe(R"(-?\d+)"); std::regex intRe(R"(-?\d+)");
std::sregex_iterator currentMatch(str.begin(), str.end(), floatRe); std::sregex_iterator currentMatch(str.begin(), str.end(), intRe);
std::sregex_iterator lastMatch; // end iterator std::sregex_iterator lastMatch; // end iterator
std::string lastFloat; std::string lastInt;
while (currentMatch != lastMatch) { while (currentMatch != lastMatch) {
lastFloat = currentMatch->str(); // Update with the current match lastInt = currentMatch->str(); // Update with the current match
++currentMatch; // Move to the next match ++currentMatch; // Move to the next match
}
try {
return stoi(lastInt); // Return the last matched float
} catch (exception& ignored) {
cerr << "I could not parse " << lastInt << " into an integer." << endl;
return numeric_limits<int>::min();
}
} }
return stoi(lastFloat); // Return the last matched float
}
/** /**
* Redirects input and output to strings * Redirects input and output to strings
* @param iStr The input string * @param iStr The input string
...@@ -94,7 +102,8 @@ public: ...@@ -94,7 +102,8 @@ public:
// set up streams // set up streams
inStream.str(iStr); inStream.str(iStr);
outStream.str(""); outStream.clear(); outStream.str("");
outStream.clear();
cin.rdbuf(inStream.rdbuf()); cin.rdbuf(inStream.rdbuf());
cout.rdbuf(outStream.rdbuf()); cout.rdbuf(outStream.rdbuf());
} }
...@@ -111,5 +120,33 @@ public: ...@@ -111,5 +120,33 @@ public:
return trim(outStream.str()); return trim(outStream.str());
} }
// Testing functions
/**
* Test a given function and returns a float result
* @param func A reference to the function. to be evaluated
* @param input The input to be provided, through stdin, to the function.
* @return The contents of what func directs to standard output
*/
static string testFunc(void (*func)(void), string input) {
redirIO(input);
func();
return restoreIO();
}
/**
* Same as testFuncFloat, but with a float output
*/
static float testFuncFloat(void (*func)(void), string input) {
return getFloat(testFunc(func, input));
}
/**
* Same as testFuncFloat, but with an int output
*/
static int testFuncInt(void (*func)(void), string input) {
return getInt(testFunc(func, input));
}
}; };
#endif //PUBLIC_COMMON_H #endif //PUBLIC_COMMON_H
1729, San Francisco, "Hello, World", "He asked: ""Quo vadis?"""
\ No newline at end of file
The quick brown fox jumped over the lazy dog.
\ No newline at end of file
a,b,c
"a", "b", "c", "d"
1, 2
\ No newline at end of file