Commit 539540b7 authored by Rayan Syed's avatar Rayan Syed
Browse files

ci & tests for hw1

parent 6b4afe8e
Loading
Loading
Loading
Loading

ci_cd/gitlab-ci.yml

0 → 100644
+11 −0
Original line number Diff line number Diff line
stages:
  - prebuild
  - compile
  - test

include:
  - local: 'ci_cd/problem2a.yml'   # IntegerPP
  - local: 'ci_cd/problem2b.yml'   # binPrime

default:
  timeout: 5m

ci_cd/problem2a.yml

0 → 100644
+47 −0
Original line number Diff line number Diff line
prebuild_problem_2a:
  stage: prebuild
  script:
    - |
      # Check if source files exist
      if [ ! -f "IntegerPP.cpp" ]; then
        echo "IntegerPP.cpp does not exist";
        exit 1;
      fi
    - git clone https://agile.bu.edu/gitlab/configs/ec330/homeworks/homeworkone.git hw1
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2a"'
  tags: [c++-17]

compile_problem_2a:
  stage: compile
  needs:
    - job: prebuild_problem_2a
      artifacts: true
  script:
    - cp IntegerPP.cpp hw1/tests/
    - cd hw1/tests
    - make problem2a
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2a"'
  tags: [c++-17]

exec_problem_2a:
  stage: test
  needs:
    - job: compile_problem_2a
      artifacts: true
  script:
    - cd hw1/tests
    - ./problem2a
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2a"'
  tags: [c++-17]

ci_cd/problem2b.yml

0 → 100644
+47 −0
Original line number Diff line number Diff line
prebuild_problem_2b:
  stage: prebuild
  script:
    - |
      # Check if source files exist
      if [ ! -f "binPrime.cpp" ]; then
        echo "binPrime.cpp does not exist";
        exit 1;
      fi
    - git clone https://agile.bu.edu/gitlab/configs/ec330/homeworks/homeworkone.git hw1
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2b"'
  tags: [c++-17]

compile_problem_2b:
  stage: compile
  needs:
    - job: prebuild_problem_2b
      artifacts: true
  script:
    - cp binPrime.cpp hw1/tests/
    - cd hw1/tests
    - make problem2b
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2b"'
  tags: [c++-17]

exec_problem_2b:
  stage: test
  needs:
    - job: compile_problem_2b
      artifacts: true
  script:
    - cd hw1/tests
    - ./problem2b
  artifacts:
    paths:
      - hw1/
  rules:
    - if: '$CI_COMMIT_REF_NAME == "problem2b"'
  tags: [c++-17]

tests/BigNum.cpp

0 → 100644
+191 −0
Original line number Diff line number Diff line
//
// Created by Ari Trachtenberg on 1/30/17.
//

#include <sstream>
#include <memory>
#include "BigNum.h"

// IMPLEMENTATION

BigNum::BigNum(const string &num) {
    bool leading = true; // used to ignore leading zeroes
    for (int ii = 0; ii < num.length(); ii++)
        if (leading && num[ii] == '0')
            continue;
        else {
            leading = false;
            digits.insert(digits.begin(), num[ii] - '0');
        }
}

BigNum::BigNum(const unsigned long num) {
    stringstream ss; ss<<num;
    *this=BigNum(ss.str());
}


BigNum::BigNum(const BigNum& otherNum):digits(otherNum.digits) {
}

BigNum& BigNum::operator=(const BigNum& otherNum) {
    digits = otherNum.digits;
    return *this;
}

BigNum& BigNum::operator+=(const BigNum& addor) {
    BigNum first, second; // second always has at least as many digits as first
    const unique_ptr<BigNum> result(new BigNum("0"));
    if (digits.size() > addor.digits.size()) {
        first = addor;
        second = *this;
    } else {
        first = *this;
        second = addor;
    }

    int ii;
    int carry = 0;
    for (ii = 0; ii < first.digits.size(); ii++) {
        const int sum = first.digits[ii] + second.digits[ii];
        result->digits.push_back((sum + carry) % base);
        if (sum + carry > (base - 1)) {
            carry = 1;
        } else {
            carry = 0;
        }
    }

    // finish up with the larger number
    for (; ii < second.digits.size(); ii++)
        if (carry) {
            const int dig = (second.digits[ii] + carry);
            result->digits.push_back(dig % base);
            carry = (dig > (base - 1) ? dig / base : 0);
        } else
            result->digits.push_back(second.digits[ii]);
    if (carry)
        result->digits.push_back(carry);

    *this = *result;
    return *this;
}

BigNum &BigNum::operator*=(const int cc) {
    // multiplies by small number cc
    int carry = 0;
    const unique_ptr<BigNum> result(new BigNum());
    
    for (int digit : digits) {
        const int prod = digit * cc + carry;
        result->digits.push_back(prod % base);
        if (prod > (base - 1))
            carry = prod / base;
        else
            carry = 0;
    }

    while (carry) {
        result->digits.push_back(carry % base);
        carry /= base;
    }

    *this=*result;
    return *this;
}

BigNum &BigNum::operator*=(const BigNum& multiplicator) {
    const unique_ptr<BigNum> sum(new BigNum());
    for (int ii = 0; ii < multiplicator.digits.size(); ii++) {
        BigNum smallProd = *this * multiplicator.digits[ii];
        smallProd << ii;
        *sum += smallProd;
    }
    *this = *sum;
    return *this;
}

BigNum &BigNum::operator/=(const BigNum& divisor) {
    // pretty naive algorithm that is easy to implement
    const unique_ptr<BigNum> guess(new BigNum());
    for (int ii = digits.size() - 1; ii >= 0; ii--) {
        int dig;
        BigNum trial("");
        for (dig = 9; dig > 0; dig--) {
            BigNum num("1");
            num << ii;
            trial = (num * dig);
            if ((*guess + trial) * divisor <= *this)
                break;
        }
        if (dig != 0)
            *guess = *guess + trial;
    }
    if (guess->digits.size() == 1 && guess->digits[0] == 0)
        guess->digits.pop_back(); // make it the empty representation
    *this = *guess;
    return *this;
}

bool BigNum::operator==(const BigNum& otherNum) const {
    if (digits.size() != otherNum.digits.size()) {
        return false;
    }
    for (int ii = 0; ii < digits.size(); ii++)
        if (digits[ii] != otherNum.digits[ii])
            return false;
    return true;
}

bool BigNum::operator<=(const BigNum& otherNum) const {
    if (digits.size() > otherNum.digits.size())
        return false;
    if (digits.size() < otherNum.digits.size())
        return true;
    // same number of digits
    for (int ii = digits.size() - 1; ii >= 0; ii--)
        if (digits[ii] < otherNum.digits[ii])
            return true;
        else if (digits[ii] > otherNum.digits[ii])
            return false;
    return true; // they are equal
}

void BigNum::operator<<(const int cc) {
    digits.insert(digits.begin(), cc, 0);
}

BigNum BigNum::operator*(const int cc) const {
    BigNum *result = new BigNum(*this);
    *result *= cc;
    return *result;
}

BigNum BigNum::operator+(const BigNum &addor) const {
    BigNum result(*this);
    result += addor;
    return result;
}

BigNum BigNum::operator*(const BigNum& multiplicator) const {
    BigNum result(*this);
    result *= multiplicator;
    return result;
}

BigNum BigNum::operator/(const BigNum& divisor) const {
    BigNum result(*this);
    result /= divisor;
    return result;
}

// friends and helpers
std::ostream & operator<<(std::ostream& stream, const BigNum& num) {
    if (num.digits.size() == 0)
        stream << "[0]";
    else
        for (int ii = num.digits.size() - 1; ii >= 0; ii--)
            stream << num.digits[ii];
    return stream;
}

tests/BigNum.h

0 → 100644
+111 −0
Original line number Diff line number Diff line
//
// Originally written by Ari Trachtenberg on 1/30/17.
// Adapted for EC330.
//

#ifndef HW1_BIGNUM_H
#define HW1_BIGNUM_H

#include <iostream>
#include <vector>
#include <string>
using namespace std;

/**
 * A class to represent arbitrarily large non-negative integers.
 */
class BigNum {
public:

    // CONSTRUCTORS
    
    /**
     * Constructs a bigNum from a given non-negative int
     */
    BigNum(unsigned long num);
    
    /**
     * Constructs a bigNum from a given string.
     * @param num contains only characters 0-9 and represents a positive integer >= 0
     */
    explicit BigNum(const string &num = "");
    
    /**
     * Constructs a copy of {@code otherNum}
     */
    BigNum(const BigNum& otherNum);

    // OPERATORS
    /**
     * @return true iff this bigNum is exactly numerically equal to otherNum
     */
    bool operator==(const BigNum& otherNum) const;

    /**
     * 
     * @param otherNum
     * @return true iff this number is numerically less than or equal to other number
     */
    bool operator<=(const BigNum& otherNum) const;

    /**
     * This bigNum becomes the same as otherNum
     * @return the (new) version of this bigNum
     */
    BigNum& operator=(const BigNum& otherNum);

    /**
     * @return the [index]-th least-significant digit of the number
     */
    int operator[](const size_t index) const { return digits.at(index); }
    
    /**
     * 
     * @return The number of significant digits in the number
     */
    size_t numDigits() const { return digits.size(); }

    /**
     * Arithmetic
     */
    BigNum operator+(const BigNum& addor) const;
    BigNum operator*(const BigNum& multiplicator) const;
    BigNum operator/(const BigNum& divisor) const;

    /**
     * Optimized multiplication of a bigNum by an int
     * @param cc > 0 is assumed
     * @return the product of this number and cc
     */
    BigNum operator*(int cc) const;

    /**
     * ARITHMETIC ASSIGNMENT OPERATORS
     */
    BigNum& operator+=(const BigNum& addor);
    BigNum& operator*=(int cc);
    BigNum& operator*=(const BigNum& multiplicator);
    BigNum& operator/=(const BigNum& divisor);

protected:
    vector<int> digits; /** stores the digits of the current number */

    /**
 * @param cc > 0
 * @return shifts the number to the left cc digits;
 *         equivalent to multiplying the current number by base^cc
 */
    void operator<<(int cc);

private:
    // Constants
    constexpr static int base = 10; // the base of the number

    // FRIENDS
    /**
     * Prints a human-readable version of bigNum [num]
     * onto a given output stream
     */
    friend std::ostream & operator<<(std::ostream& stream, const BigNum& num);
};
#endif //HW1_BIGNUM_H
Loading