Skip to content

Commit

Permalink
Browse files Browse the repository at this point in the history
Binary test for Assignment 1: Methods 1-4
This is still missing tests for method 5.

Issue #4
  • Loading branch information
Brandon committed Feb 18, 2019
0 parents commit ca6e721
Show file tree
Hide file tree
Showing 7 changed files with 14,984 additions and 0 deletions.
1 change: 1 addition & 0 deletions .gitignore
@@ -0,0 +1 @@
asgn1
10 changes: 10 additions & 0 deletions Makefile
@@ -0,0 +1,10 @@
CC=c++
CFLAGS=-std=c++14

all: asgn1

asgn1: assignments/asgn1.cpp
$(CC) $(CFLAGS) $< -o $@

clean:
rm -rf *.o *~ asgn1
71 changes: 71 additions & 0 deletions assignments/asgn1.cpp
@@ -0,0 +1,71 @@
#include <sstream>
#include <string>
#include <regex>

#define CATCH_CONFIG_MAIN
#include "../includes/catch.hpp"
#include "../includes/subprocess.hpp"

#include "../shared/explode.hpp"

typedef std::vector<unsigned long int> FactorsContainer;
typedef unsigned long long int NumberToFactor;

FactorsContainer factor(const std::string path, const NumberToFactor n) {
subprocess::popen cmd(path, {});
cmd.stdin() << n << std::endl;
cmd.close();

std::stringstream s;
s << cmd.stdout().rdbuf();
std::string out = s.str();

std::regex factors_regex("Enter a number to factor: ?(.*?)\n");
std::smatch factors_match;
std::regex_search(out, factors_match, factors_regex);

const auto factor_strs = explode(factors_match[1], ' ');
FactorsContainer factors;
for (const auto& str : factor_strs) {
factors.push_back(std::stoul(str));
}

return factors;
}

const std::map<NumberToFactor, FactorsContainer> tests = {
{ 20, { 2, 2, 5 } },
{ 29, { 29 } },
{ 49, { 7, 7 }},
{ 625, { 5, 5, 5, 5 } },
{ 324324, { 2, 2, 3, 3, 3, 3, 7, 11, 13 } },
{ 9876543210, { 2, 3, 3, 5, 17, 17, 379721 } },
{ 1234567890987654321, { 3, 3, 7, 19, 928163, 1111211111 } }
};

TEST_CASE("Method 1: Print the factors directly") {
for (const auto p : tests) {
CHECK_THAT(factor("./asgn1/q1", p.first), Catch::Equals(p.second));
}
}

TEST_CASE("Method 2: Store the factors in an array") {
for (const auto p : tests) {
CHECK_THAT(factor("./asgn1/q2", p.first), Catch::Equals(p.second));
}
}

TEST_CASE("Method 3: Store the factors in a vector") {
for (const auto p : tests) {
CHECK_THAT(factor("./asgn1/q3", p.first), Catch::Equals(p.second));
}
}

TEST_CASE("Method 4: Store the factors in a stack") {
for (const auto p : tests) {
FactorsContainer reversed;
std::copy(p.second.rbegin(), p.second.rend(), std::back_inserter(reversed));

CHECK_THAT(factor("./asgn1/q4", p.first), Catch::Equals(reversed));
}
}

0 comments on commit ca6e721

Please sign in to comment.