Loading tests/Makefile +15 −3 Original line number Diff line number Diff line Loading @@ -7,19 +7,25 @@ CXXFLAGS = -Wall -std=c++17 # Source files PROBLEM0_SRCS = testIntegerP.cpp IntegerP.cpp PROBLEM1_SRCS = testRationalP.cpp RationalP.cpp IntegerP.cpp SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS) MORE_PROBLEM0_SRCS = testMoreIntegerP.cpp RationalP.cpp IntegerP.cpp MORE_PROBLEM1_SRCS = testMoreRationalP RationalP.cpp IntegerP.cpp SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS) $(MORE_PROBLEM0_SRCS) $(MORE_PROBLEM1_SRCS) # Object files PROBLEM0_OBJS = testIntegerP.o IntegerP.o PROBLEM1_OBJS = testRationalP.o RationalP.o IntegerP.o OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS) MORE_PROBLEM0_OBJS = testMoreIntegerP.o IntegerP.o MORE_PROBLEM1_OBJS = testMoreRationalP.o RationalP.o IntegerP.o OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS) $(MORE_PROBLEM0_OBJS) $(MORE_PROBLEM1_OBJS) # Executable names PROBLEM0_EXEC = problem1b PROBLEM1_EXEC = problem1c MORE_PROBLEM0_EXEC = more1b MORE_PROBLEM1_EXEC = more1c # Default target to build the executable all: $(PROBLEM0_EXEC) $(PROBLEM1_EXEC) all: $(PROBLEM0_EXEC) $(PROBLEM1_EXEC) $(MORE_PROBLEM0_EXEC) $(MORE_PROBLEM1_EXEC) # Rule to build the executable from object files $(PROBLEM0_EXEC): $(PROBLEM0_OBJS) Makefile Loading @@ -28,6 +34,12 @@ $(PROBLEM0_EXEC): $(PROBLEM0_OBJS) Makefile $(PROBLEM1_EXEC): $(PROBLEM1_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(PROBLEM1_EXEC) $(PROBLEM1_OBJS) $(MORE_PROBLEM0_EXEC): $(MORE_PROBLEM0_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(MORE_PROBLEM0_EXEC) $(MORE_PROBLEM0_OBJS) $(MORE_PROBLEM1_EXEC): $(MORE_PROBLEM1_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(MORE_PROBLEM1_EXEC) $(MORE_PROBLEM1_OBJS) # Rules to build object files from source files, with dependency on the Common.h header %.o: %.cpp %.h Makefile $(CXX) $(CXXFLAGS) -c $< -o $@ Loading tests/testMoreIntegerP.cpp +52 −21 Original line number Diff line number Diff line #include <iostream> #include <string> #include "IntegerP.h" using namespace std; const float PTS = 0.25; float total = 0; /** * If expected == actual, increments the total by points. Prints out information about what happened * @param category The category of test * @param test The specific test * @param expected Expected outcome * @param actual Actual outcome * @param points Points for a correct response. */ template <typename T> void scoreExample(string category, string test, T expected, T actual, float points) { string score; if (expected == actual) { score = "CORRECT! +" + to_string(points); } else { score = "incorrect: +0"; } cout << category << " - " << test << ": " << score << endl; } // -------------------- Corner cases: representation of 1 and trailing zeros -------------------- bool test_9() { const char* T = "Corner: multiple representations of 1"; bool test_a() { const string TEST_CATEGORY = "Corner: multiple representations of 1"; IntegerP one_default; Loading @@ -10,22 +38,20 @@ bool test_9() { IntegerP empty({}); // All should truncate to 1 if (one_default.trunc() != 1) return failExample(T, "default one trunc()","1",to_string(one_default.trunc())); if (one_zero_list.trunc() != 1) return failExample(T, "IntegerP({0}).trunc()","1",to_string(one_zero_list.trunc())); if (one_many_zeros.trunc() != 1) return failExample(T, "IntegerP({0,0,0}).trunc()","1",to_string(one_many_zeros.trunc())); if (empty.trunc() != 1) return failExample(T, "IntegerP({}).trunc()","1",to_string(empty.trunc())); if (one_default.trunc() != 1) scoreExample(TEST_CATEGORY, "default one trunc()",string("1"),to_string(one_default.trunc()),PTS); if (one_zero_list.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({0}).trunc()",string("1"),to_string(one_zero_list.trunc()),PTS); if (one_many_zeros.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({0,0,0}).trunc()",string("1"),to_string(one_many_zeros.trunc()),PTS); if (empty.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({}).trunc()",string("1"),to_string(empty.trunc()),PTS); // Check equality of 1's if (!(one_zero_list==one_many_zeros)) return failExample(T, "{0}=={0,0,0}", 1, one_many_zeros==one_many_zeros); if (!(one_zero_list==empty)) return failExample(T, "{0}=={}", 1, one_zero_list==empty); if (!(one_many_zeros==empty)) return failExample(T, "{0,0,0}=={}", 1, one_many_zeros==empty); return true; if (!(one_zero_list==one_many_zeros)) scoreExample(TEST_CATEGORY, "{0}=={0,0,0}", true, one_many_zeros==one_many_zeros,PTS); if (!(one_zero_list==empty)) scoreExample(TEST_CATEGORY, "{0}=={}", true, one_zero_list==empty,PTS); if (!(one_many_zeros==empty)) scoreExample(TEST_CATEGORY, "{0,0,0}=={}", true, one_many_zeros==empty,PTS); } // -------------------- Corner cases: identity element through operations -------------------- bool test_10() { const char* T = "Corner: identity element via * and / with 1"; bool test_b() { string TEST_CATEGORY = "Corner: identity element via * and / with 1"; IntegerP one_default; // empty vector in your implementation IntegerP one_zero_list({0}); // vector {0} Loading @@ -33,23 +59,28 @@ bool test_10() { // Multiplying by 1 should keep the value IntegerP p1 = x * one_default; if (p1.trunc() != 30) return failExample(T, "x * one_default trunc()","30",to_string(p1.trunc())); if (p1.trunc() != 30) scoreExample(TEST_CATEGORY, "x * one_default trunc()",string("30"),to_string(p1.trunc()), PTS); IntegerP p2 = x * one_zero_list; if (p2.trunc() != 30) return failExample(T, "x * IntegerP({0}) trunc()","30",to_string(p2.trunc())); if (p2.trunc() != 30) scoreExample(TEST_CATEGORY, "x * IntegerP({0}) trunc()",string("30"),to_string(p2.trunc()), PTS); // Dividing by 1 should keep the value IntegerP q1 = x / one_default; if (q1.trunc() != 30) return failExample(T, "x / one_default trunc()","30",to_string(q1.trunc())); if (q1.trunc() != 30) scoreExample(TEST_CATEGORY, "x / one_default trunc()",string("30"),to_string(q1.trunc()), PTS); IntegerP q2 = x / one_zero_list; if (q2.trunc() != 30) return failExample(T, "x / IntegerP({0}) trunc()","30",to_string(q2.trunc())); if (q2.trunc() != 30) scoreExample(TEST_CATEGORY, "x / IntegerP({0}) trunc()",string("30"),to_string(q2.trunc()), PTS); // Also check divisibleBy(1) is true for both representations of 1 if (!x.divisibleBy(one_default)) return failExample(T, "x.divisibleBy(one_default)","true","false"); scoreExample(TEST_CATEGORY, "x.divisibleBy(one_default)","true","false", PTS); if (!x.divisibleBy(one_zero_list)) return failExample(T, "x.divisibleBy(IntegerP({0}))","true","false"); scoreExample(TEST_CATEGORY, "x.divisibleBy(IntegerP({0}))","true","false", PTS); } int main() { test_a(); test_b(); return true; cout << "Total points: " << total << endl; } No newline at end of file tests/testMoreRationalP.cpp +2 −1 Original line number Diff line number Diff line // -------------------- multiple representations of 1 -------------------- #include "RationalP.h" bool test_8() { const char* T = "multiple representations of 1 (RationalP)"; Loading Loading
tests/Makefile +15 −3 Original line number Diff line number Diff line Loading @@ -7,19 +7,25 @@ CXXFLAGS = -Wall -std=c++17 # Source files PROBLEM0_SRCS = testIntegerP.cpp IntegerP.cpp PROBLEM1_SRCS = testRationalP.cpp RationalP.cpp IntegerP.cpp SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS) MORE_PROBLEM0_SRCS = testMoreIntegerP.cpp RationalP.cpp IntegerP.cpp MORE_PROBLEM1_SRCS = testMoreRationalP RationalP.cpp IntegerP.cpp SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS) $(MORE_PROBLEM0_SRCS) $(MORE_PROBLEM1_SRCS) # Object files PROBLEM0_OBJS = testIntegerP.o IntegerP.o PROBLEM1_OBJS = testRationalP.o RationalP.o IntegerP.o OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS) MORE_PROBLEM0_OBJS = testMoreIntegerP.o IntegerP.o MORE_PROBLEM1_OBJS = testMoreRationalP.o RationalP.o IntegerP.o OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS) $(MORE_PROBLEM0_OBJS) $(MORE_PROBLEM1_OBJS) # Executable names PROBLEM0_EXEC = problem1b PROBLEM1_EXEC = problem1c MORE_PROBLEM0_EXEC = more1b MORE_PROBLEM1_EXEC = more1c # Default target to build the executable all: $(PROBLEM0_EXEC) $(PROBLEM1_EXEC) all: $(PROBLEM0_EXEC) $(PROBLEM1_EXEC) $(MORE_PROBLEM0_EXEC) $(MORE_PROBLEM1_EXEC) # Rule to build the executable from object files $(PROBLEM0_EXEC): $(PROBLEM0_OBJS) Makefile Loading @@ -28,6 +34,12 @@ $(PROBLEM0_EXEC): $(PROBLEM0_OBJS) Makefile $(PROBLEM1_EXEC): $(PROBLEM1_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(PROBLEM1_EXEC) $(PROBLEM1_OBJS) $(MORE_PROBLEM0_EXEC): $(MORE_PROBLEM0_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(MORE_PROBLEM0_EXEC) $(MORE_PROBLEM0_OBJS) $(MORE_PROBLEM1_EXEC): $(MORE_PROBLEM1_OBJS) Makefile $(CXX) $(CXXFLAGS) -o $(MORE_PROBLEM1_EXEC) $(MORE_PROBLEM1_OBJS) # Rules to build object files from source files, with dependency on the Common.h header %.o: %.cpp %.h Makefile $(CXX) $(CXXFLAGS) -c $< -o $@ Loading
tests/testMoreIntegerP.cpp +52 −21 Original line number Diff line number Diff line #include <iostream> #include <string> #include "IntegerP.h" using namespace std; const float PTS = 0.25; float total = 0; /** * If expected == actual, increments the total by points. Prints out information about what happened * @param category The category of test * @param test The specific test * @param expected Expected outcome * @param actual Actual outcome * @param points Points for a correct response. */ template <typename T> void scoreExample(string category, string test, T expected, T actual, float points) { string score; if (expected == actual) { score = "CORRECT! +" + to_string(points); } else { score = "incorrect: +0"; } cout << category << " - " << test << ": " << score << endl; } // -------------------- Corner cases: representation of 1 and trailing zeros -------------------- bool test_9() { const char* T = "Corner: multiple representations of 1"; bool test_a() { const string TEST_CATEGORY = "Corner: multiple representations of 1"; IntegerP one_default; Loading @@ -10,22 +38,20 @@ bool test_9() { IntegerP empty({}); // All should truncate to 1 if (one_default.trunc() != 1) return failExample(T, "default one trunc()","1",to_string(one_default.trunc())); if (one_zero_list.trunc() != 1) return failExample(T, "IntegerP({0}).trunc()","1",to_string(one_zero_list.trunc())); if (one_many_zeros.trunc() != 1) return failExample(T, "IntegerP({0,0,0}).trunc()","1",to_string(one_many_zeros.trunc())); if (empty.trunc() != 1) return failExample(T, "IntegerP({}).trunc()","1",to_string(empty.trunc())); if (one_default.trunc() != 1) scoreExample(TEST_CATEGORY, "default one trunc()",string("1"),to_string(one_default.trunc()),PTS); if (one_zero_list.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({0}).trunc()",string("1"),to_string(one_zero_list.trunc()),PTS); if (one_many_zeros.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({0,0,0}).trunc()",string("1"),to_string(one_many_zeros.trunc()),PTS); if (empty.trunc() != 1) scoreExample(TEST_CATEGORY, "IntegerP({}).trunc()",string("1"),to_string(empty.trunc()),PTS); // Check equality of 1's if (!(one_zero_list==one_many_zeros)) return failExample(T, "{0}=={0,0,0}", 1, one_many_zeros==one_many_zeros); if (!(one_zero_list==empty)) return failExample(T, "{0}=={}", 1, one_zero_list==empty); if (!(one_many_zeros==empty)) return failExample(T, "{0,0,0}=={}", 1, one_many_zeros==empty); return true; if (!(one_zero_list==one_many_zeros)) scoreExample(TEST_CATEGORY, "{0}=={0,0,0}", true, one_many_zeros==one_many_zeros,PTS); if (!(one_zero_list==empty)) scoreExample(TEST_CATEGORY, "{0}=={}", true, one_zero_list==empty,PTS); if (!(one_many_zeros==empty)) scoreExample(TEST_CATEGORY, "{0,0,0}=={}", true, one_many_zeros==empty,PTS); } // -------------------- Corner cases: identity element through operations -------------------- bool test_10() { const char* T = "Corner: identity element via * and / with 1"; bool test_b() { string TEST_CATEGORY = "Corner: identity element via * and / with 1"; IntegerP one_default; // empty vector in your implementation IntegerP one_zero_list({0}); // vector {0} Loading @@ -33,23 +59,28 @@ bool test_10() { // Multiplying by 1 should keep the value IntegerP p1 = x * one_default; if (p1.trunc() != 30) return failExample(T, "x * one_default trunc()","30",to_string(p1.trunc())); if (p1.trunc() != 30) scoreExample(TEST_CATEGORY, "x * one_default trunc()",string("30"),to_string(p1.trunc()), PTS); IntegerP p2 = x * one_zero_list; if (p2.trunc() != 30) return failExample(T, "x * IntegerP({0}) trunc()","30",to_string(p2.trunc())); if (p2.trunc() != 30) scoreExample(TEST_CATEGORY, "x * IntegerP({0}) trunc()",string("30"),to_string(p2.trunc()), PTS); // Dividing by 1 should keep the value IntegerP q1 = x / one_default; if (q1.trunc() != 30) return failExample(T, "x / one_default trunc()","30",to_string(q1.trunc())); if (q1.trunc() != 30) scoreExample(TEST_CATEGORY, "x / one_default trunc()",string("30"),to_string(q1.trunc()), PTS); IntegerP q2 = x / one_zero_list; if (q2.trunc() != 30) return failExample(T, "x / IntegerP({0}) trunc()","30",to_string(q2.trunc())); if (q2.trunc() != 30) scoreExample(TEST_CATEGORY, "x / IntegerP({0}) trunc()",string("30"),to_string(q2.trunc()), PTS); // Also check divisibleBy(1) is true for both representations of 1 if (!x.divisibleBy(one_default)) return failExample(T, "x.divisibleBy(one_default)","true","false"); scoreExample(TEST_CATEGORY, "x.divisibleBy(one_default)","true","false", PTS); if (!x.divisibleBy(one_zero_list)) return failExample(T, "x.divisibleBy(IntegerP({0}))","true","false"); scoreExample(TEST_CATEGORY, "x.divisibleBy(IntegerP({0}))","true","false", PTS); } int main() { test_a(); test_b(); return true; cout << "Total points: " << total << endl; } No newline at end of file
tests/testMoreRationalP.cpp +2 −1 Original line number Diff line number Diff line // -------------------- multiple representations of 1 -------------------- #include "RationalP.h" bool test_8() { const char* T = "multiple representations of 1 (RationalP)"; Loading