Skip to content
Permalink
a3a80011e7
Switch branches/tags

Name already in use

A tag already exists with the provided branch name. Many Git commands accept both tag and branch names, so creating this branch may cause unexpected behavior. Are you sure you want to create this branch?
Go to file
 
 
Cannot retrieve contributors at this time
102 lines (80 sloc) 2.89 KB
#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;
}
std::string english_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();
return out;
}
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 } }
};
const std::map<NumberToFactor, std::string> english_tests = {
{ 20, "The prime factors of 20 are 2, 2 and 5.\n"},
{ 29, "The prime factor of 29 is 29.\n"},
{ 49, "The prime factors of 49 are 7 and 7.\n"},
{ 625, "The prime factors of 625 are 5, 5, 5 and 5.\n"},
{ 324324, "The prime factors of 324324 are 2, 2, 3, 3, 3, 3, 7, 11 and 13.\n"},
{ 9876543210, "The prime factors of 9876543210 are 2, 3, 3, 5, 17, 17 and 379721.\n"},
{ 1234567890987654321, "The prime factors of 1234567890987654321 are 3, 3, 7, 19, 928163 and 1111211111.\n"}
};
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));
}
}
TEST_CASE("Method 5: Factoring a number in English"){
for (const auto p : english_tests) {
CHECK_THAT(english_factor("./asgn1/q5", p.first), Catch::Contains(p.second));
}
}