Permalink
Cannot retrieve contributors at this time
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?
cse3150-grading/src/asgn1/main.cpp
Go to fileThis commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
101 lines (83 sloc)
3.05 KB
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#include <sstream> | |
#include <string> | |
#include <regex> | |
#define CATCH_CONFIG_MAIN | |
#include <catch2/catch.hpp> | |
#include <subprocess.hpp> | |
#include <internal/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 } } | |
}; | |
TEST_CASE("Method 1: Print the factors directly") { | |
const auto binary = "./asgn1/q1"; | |
// REQUIRE(std::experimental::filesystem::exists(binary)); | |
for (const auto p : tests) { | |
CHECK_THAT(factor(binary, 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") { | |
const std::map<NumberToFactor, std::string> english_tests = { | |
{ 20, "(.|\n)*The prime factors? of 20 are 2, 2(,|, and| and) 5(.|\n)*"}, | |
{ 29, "(.|\n)*The prime factors? of 29 is 29(.|\n)*"}, | |
{ 49, "(.|\n)*The prime factors? of 49 are 7(,|, and| and) 7(.|\n)*"}, | |
{ 625, "(.|\n)*The prime factors? of 625 are 5, 5, 5(,|, and| and) 5(.|\n)*"}, | |
{ 324324, "(.|\n)*The prime factors? of 324324 are 2, 2, 3, 3, 3, 3, 7, 11(,|, and| and) 13(.|\n)*"}, | |
{ 9876543210, "(.|\n)*The prime factors? of 9876543210 are 2, 3, 3, 5, 17, 17(,|, and| and) 379721(.|\n)*"}, | |
{ 1234567890987654321, "(.|\n)*The prime factors? of 1234567890987654321 are 3, 3, 7, 19, 928163(,|, and| and) 1111211111(.|\n)*"} | |
}; | |
for (const auto p : english_tests) { | |
CHECK_THAT( | |
english_factor("./asgn1/q5", p.first), | |
Catch::Matches(p.second, Catch::CaseSensitive::Choice::No) | |
); | |
} | |
} |