Commit fc38cea1 authored by Rayan Syed's avatar Rayan Syed
Browse files

more tests

parent b84c0237
Loading
Loading
Loading
Loading
+14 −4
Original line number Diff line number Diff line
@@ -7,26 +7,36 @@ CXXFLAGS = -std=c++17
# Source files
PROBLEM0_SRCS = testIntegerPP.cpp IntegerPP.cpp IntegerP.cpp BigNum.cpp
PROBLEM1_SRCS = testBinPrime.cpp binPrime.cpp IntegerPP.cpp IntegerP.cpp BigNum.cpp
SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS)
MORE_PROBLEM0_SRCS = testMoreIntegerPP.cpp IntegerPP.cpp IntegerP.cpp BigNum.cpp
MORE_PROBLEM1_SRCS = testMoreBinPrime.cpp binPrime.cpp IntegerPP.cpp IntegerP.cpp BigNum.cpp
SRCS = $(PROBLEM0_SRCS) $(PROBLEM1_SRCS) $(MORE_PROBLEM0_SRCS) $(MORE_PROBLEM1_SRCS)

# Object files
PROBLEM0_OBJS = testIntegerPP.o IntegerPP.o IntegerP.o BigNum.o
PROBLEM1_OBJS = testBinPrime.o binPrime.o IntegerPP.o IntegerP.o BigNum.o
OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS)
MORE_PROBLEM0_OBJS = testMoreIntegerPP.o IntegerPP.o IntegerP.o BigNum.o
MORE_PROBLEM1_OBJS = testMoreBinPrime.o binPrime.o IntegerPP.o IntegerP.o BigNum.o
OBJS = $(PROBLEM0_OBJS) $(PROBLEM1_OBJS) $(MORE_PROBLEM0_OBJS) $(MORE_PROBLEM1_OBJS)

# Executable names
PROBLEM0_EXEC = problem2a
PROBLEM1_EXEC = problem2b
EXECS = $(PROBLEM0_EXEC) $(PROBLEM1_EXEC)
MORE_PROBLEM0_EXEC = more2a
MORE_PROBLEM1_EXEC = more2b
EXECS = $(PROBLEM0_EXEC) $(PROBLEM1_EXEC) $(MORE_PROBLEM0_EXEC) $(MORE_PROBLEM1_EXEC)

# 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
	$(CXX) $(CXXFLAGS) -o $(PROBLEM0_EXEC) $(PROBLEM0_OBJS)
$(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 Makefile
+69 −0
Original line number Diff line number Diff line
// Developed by Rayan 2/13/2026, adapted from Kevin's testMoreIntegerP.cpp
#include <iostream>
#include <string>
#include <sstream>

#include "BigNum.h"

using namespace std;

const float PTS = 1.25;
float total = 0;

BigNum binPrime(const int pascalRow);

/**
 * 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);
        total += points;
    }
    else {
        score = "incorrect:  +0";
    }
    cout << category << " - " << test << ": " << score << endl;
}

static string bnToString(const BigNum& x) {
    std::ostringstream oss;
    oss << x;
    return oss.str();
}

// -------------------- Regression: assignment examples --------------------
void test_a() {
    const char* TEST_CATEGORY = "Regression: assignment examples";

    scoreExample(TEST_CATEGORY, "binPrime(2) == 1",  string("1"),  bnToString(binPrime(2)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(4) == 10", string("10"), bnToString(binPrime(4)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(7) == 78", string("78"), bnToString(binPrime(7)), PTS);
}

// -------------------- Regression: known small rows --------------------
void test_b() {
    const char* TEST_CATEGORY = "Regression: known small rows";

    scoreExample(TEST_CATEGORY, "binPrime(3) == 4",   string("4"),   bnToString(binPrime(3)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(5) == 21",  string("21"),  bnToString(binPrime(5)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(6) == 41",  string("41"),  bnToString(binPrime(6)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(8) == 148", string("148"), bnToString(binPrime(8)), PTS);
    scoreExample(TEST_CATEGORY, "binPrime(9) == 282", string("282"), bnToString(binPrime(9)), PTS);
}

int main() {
    test_a();
    test_b();

    cout << "Total points:  " << total << endl;
    cout << total << endl; // for the score recorder in the gitlab job
    return 0;
}
+110 −0
Original line number Diff line number Diff line
// Developed by Rayan 2/13/2026, adapted from Kevin's testMoreIntegerP.cpp
#include <iostream>
#include <string>
#include <sstream>

#include "IntegerPP.h"
#include "BigNum.h"

using namespace std;

const float PTS = 3.0;
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);
    total += points;
  } else {
    score = "incorrect:  +0";
  }
  cout << category << " - " << test << ": " << score << endl;
}

// helper: BigNum -> string
static string bnToString(const BigNum& x) {
  std::ostringstream oss;
  oss << x;
  return oss.str();
}

// -------------------- Corner cases: multiple representations of 1 --------------------
void test_a() {
  const string CAT = "Corner: multiple representations of 1 (IntegerPP)";

  IntegerPP one_default;             // should represent 1
  IntegerPP one_zero_list({0});      // also 1 mathematically (different internal basis)
  IntegerPP one_many_zeros({0,0,0}); // also 1
  IntegerPP one_from_bn(BigNum(1UL));

  scoreExample(CAT, "default one -> BigNum == 1", string("1"), bnToString((BigNum)one_default), PTS);
  scoreExample(CAT, "IntegerPP({0}) -> BigNum == 1", string("1"), bnToString((BigNum)one_zero_list), PTS);
  scoreExample(CAT, "IntegerPP({0,0,0}) -> BigNum == 1", string("1"), bnToString((BigNum)one_many_zeros), PTS);
  scoreExample(CAT, "IntegerPP(BigNum(1)) -> BigNum == 1", string("1"), bnToString((BigNum)one_from_bn), PTS);
}

// -------------------- Corner cases: identity element via * and / with 1 --------------------
void test_b() {
  const string CAT = "Corner: identity via * and / with 1 (IntegerPP)";

  IntegerPP one_default;        // 1
  IntegerPP one_zero_list({0}); // 1
  IntegerPP x(30UL);            // 30 = 2*3*5

  // operator* and operator/ are inherited from IntegerP (return IntegerP), then lift to IntegerPP
  IntegerPP p1 = IntegerPP(static_cast<const IntegerP&>(x) * static_cast<const IntegerP&>(one_default));
  IntegerPP q2 = IntegerPP(static_cast<const IntegerP&>(x) / static_cast<const IntegerP&>(one_zero_list));

  scoreExample(CAT, "x*one_default -> BigNum == 30", string("30"), bnToString((BigNum)p1), PTS);
  scoreExample(CAT, "x/{0} -> BigNum == 30", string("30"), bnToString((BigNum)q2), PTS);
}

// -------------------- Corner cases: BigNum round-trip (factor + rebuild) --------------------
void test_c() {
  const string CAT = "Corner: BigNum round-trip (IntegerPP)";

  BigNum a(2928UL);
  IntegerPP ip_a(a);
  scoreExample(CAT, "BigNum(2928) -> IntegerPP -> BigNum == 2928", string("2928"), bnToString((BigNum)ip_a), PTS);

  BigNum b(5184UL); // 2^6 * 3^4 = 5184
  IntegerPP ip_b(b);
  scoreExample(CAT, "BigNum(5184) -> IntegerPP -> BigNum == 5184", string("5184"), bnToString((BigNum)ip_b), PTS);
}

// -------------------- Corner cases: operator+ and operator= --------------------
void test_d() {
  const string CAT = "Corner: + and = (IntegerPP)";

  // regression: the spec example
  IntegerPP got = IntegerPP({1,1}) + IntegerPP({1,0,0,1}); // 6 + 14 = 20
  scoreExample(CAT, "{1,1}+{1,0,0,1} -> BigNum == 20", string("20"), bnToString((BigNum)got), PTS);

  // assignment should copy representation correctly
  IntegerPP a(42UL);
  IntegerPP b(1UL);
  b = a;
  scoreExample(CAT, "b=a; -> b BigNum == 42", string("42"), bnToString((BigNum)b), PTS);
}

int main() {
  test_a(); // 4 tests
  test_b(); // 2 tests
  test_c(); // 2 tests
  test_d(); // 2 tests
  // total = 10 tests * 3.0 = 30 points

  cout << "Total points:  " << total << endl;
  cout << total << endl; // for the score recorder in the gitlab job
  return 0;
}