From e1ec60c9c1466653c211ad518bf44b3dabde2d6f Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Mon, 21 Jan 2019 10:45:43 +0100 Subject: [PATCH] cleaning up tests for command line parser --- slsDetectorSoftware/slsDetector/slsDetector.h | 4 +- slsSupportLib/include/CmdLineParser.h | 20 +- slsSupportLib/src/CmdLineParser.cpp | 55 ++--- slsSupportLib/tests/test-CmdLineParser.cpp | 189 ++++++++---------- 4 files changed, 123 insertions(+), 145 deletions(-) diff --git a/slsDetectorSoftware/slsDetector/slsDetector.h b/slsDetectorSoftware/slsDetector/slsDetector.h index ee1933ef1..76a440add 100644 --- a/slsDetectorSoftware/slsDetector/slsDetector.h +++ b/slsDetectorSoftware/slsDetector/slsDetector.h @@ -273,7 +273,7 @@ public: * @param id sls detector id (position in detectors list) * @param verify true to verify if shared memory version matches existing one */ - slsDetector(detectorType type, int multiId = 0, int id = 0, bool verify = true); + explicit slsDetector(detectorType type, int multiId = 0, int id = 0, bool verify = true); /** * Constructor called when opening existing shared memory @@ -281,7 +281,7 @@ public: * @param id sls detector id (position in detectors list) * @param verify true to verify if shared memory version matches existing one */ - slsDetector(int multiId = 0, int id = 0, bool verify = true); + explicit slsDetector(int multiId = 0, int id = 0, bool verify = true); /** * Destructor diff --git a/slsSupportLib/include/CmdLineParser.h b/slsSupportLib/include/CmdLineParser.h index d9f57cca4..8066438d3 100644 --- a/slsSupportLib/include/CmdLineParser.h +++ b/slsSupportLib/include/CmdLineParser.h @@ -5,22 +5,22 @@ #include class CmdLineParser { -public: - void Parse(int argc, char* argv[]); - void Parse(std::string s); + public: + void Parse(int argc, char *argv[]); + void Parse(const std::string &s); void Print(); //getters int multi_id() const { return multi_id_; }; int detector_id() const { return detector_id_; }; - int n_arguments() const {return arguments_.size();} - const std::string& command() const { return command_; } - const std::string& executable() const { return executable_;} - const std::vector& arguments() const{ return arguments_; }; - std::vector argv(); + int n_arguments() const { return arguments_.size(); } + const std::string &command() const { return command_; } + const std::string &executable() const { return executable_; } + const std::vector &arguments() const { return arguments_; }; + std::vector argv(); -private: - void DecodeIdAndPosition(const char* c); + private: + void DecodeIdAndPosition(const char *c); int multi_id_ = 0; int detector_id_ = -1; std::string command_; diff --git a/slsSupportLib/src/CmdLineParser.cpp b/slsSupportLib/src/CmdLineParser.cpp index 356800947..7a63147a9 100644 --- a/slsSupportLib/src/CmdLineParser.cpp +++ b/slsSupportLib/src/CmdLineParser.cpp @@ -2,26 +2,24 @@ #include "CmdLineParser.h" #include #include -#include -#include #include +#include +#include //printing function for debugging -void CmdLineParser::Print() -{ +void CmdLineParser::Print() { std::cout << "\nCmdLineParser::Print()\n"; std::cout << "\tmulti_id: " << multi_id_ << ", detector_id: " << detector_id_ << std::endl; std::cout << "\texecutable: " << executable_ << '\n'; std::cout << "\tcommand: " << command_ << '\n'; std::cout << "\tn_arguments: " << n_arguments() << '\n'; std::cout << "\targuments: "; - for (size_t i = 0; i < arguments_.size(); ++i) { - std::cout << arguments_[i] << " "; + for (const auto &argument : arguments_) { + std::cout << argument << " "; } std::cout << "\n\n"; }; -void CmdLineParser::Parse(int argc, char* argv[]) -{ +void CmdLineParser::Parse(int argc, char *argv[]) { //first element of argv is the command used to call the executable ->skipping //and if this is the only command skip all executable_ = argv[0]; @@ -29,50 +27,55 @@ void CmdLineParser::Parse(int argc, char* argv[]) //second element is cmd string that needs to be decoded DecodeIdAndPosition(argv[1]); //The rest of the arguments goes into a vector for later processing - for (int i = 2; i < argc; ++i) + for (int i = 2; i < argc; ++i) { arguments_.emplace_back(std::string(argv[i])); + } } }; -void CmdLineParser::Parse(std::string s){ - std::istringstream iss(s); - auto it = std::istream_iterator(iss); - //read the first element and increment - command_ = *it++; - arguments_ = std::vector(it, std::istream_iterator());; - DecodeIdAndPosition(command_.c_str()); +void CmdLineParser::Parse(const std::string &s) { + std::istringstream iss(s); + auto it = std::istream_iterator(iss); + //read the first element and increment + command_ = *it++; + arguments_ = std::vector(it, std::istream_iterator()); + ; + DecodeIdAndPosition(command_.c_str()); } -void CmdLineParser::DecodeIdAndPosition(const char* c) -{ - bool contains_id = std::strchr(c, '-'); - bool contains_pos = std::strchr(c, ':'); +void CmdLineParser::DecodeIdAndPosition(const char *c) { + bool contains_id = std::strchr(c, '-') != nullptr; + bool contains_pos = std::strchr(c, ':') != nullptr; char tmp[100]; if (contains_id && contains_pos) { int r = sscanf(c, "%d-%d:%s", &multi_id_, &detector_id_, tmp); - if (r != 3) + if (r != 3) { throw(std::invalid_argument("Cannot decode client or detector id from: \"" + std::string(c) + "\"\n")); + } command_ = tmp; } else if (contains_id && !contains_pos) { int r = sscanf(c, "%d-%s", &multi_id_, tmp); - if (r != 2) + if (r != 2) { throw(std::invalid_argument("Cannot decode client id from: \"" + std::string(c) + "\"\n")); + } command_ = tmp; } else if (!contains_id && contains_pos) { int r = sscanf(c, "%d:%s", &detector_id_, tmp); - if (r != 2) + if (r != 2) { throw(std::invalid_argument("Cannot decode detector id from: \"" + std::string(c) + "\"\n")); + } command_ = tmp; } else { command_ = c; } } -std::vector CmdLineParser::argv(){ - std::vector vec; +std::vector CmdLineParser::argv() { + std::vector vec; vec.push_back(&command_.front()); - for (auto& arg: arguments_) + for (auto &arg : arguments_) { vec.push_back(&arg.front()); + } return vec; } diff --git a/slsSupportLib/tests/test-CmdLineParser.cpp b/slsSupportLib/tests/test-CmdLineParser.cpp index da469178b..aca86b1c5 100644 --- a/slsSupportLib/tests/test-CmdLineParser.cpp +++ b/slsSupportLib/tests/test-CmdLineParser.cpp @@ -6,88 +6,82 @@ //help for all docs //command for all depreciated commands -TEST_CASE("Parse with no arguments results in no command and default id") -{ +TEST_CASE("Parse with no arguments results in no command and default id") { //build up argc and argv //first argument is the command used to call the binary int argc = 1; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; - argv[0] = a0; + argv[0] = static_cast(a0); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command().empty()); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Parse empty string") -{ - std::string s = ""; +TEST_CASE("Parse empty string") { + std::string s; CmdLineParser p; p.Parse(s); REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command().empty()); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Parse a command without client id and detector id results in default") -{ +TEST_CASE("Parse a command without client id and detector id results in default") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Parse a string without client id and detector id results in default") -{ +TEST_CASE("Parse a string without client id and detector id results in default") { std::string s = "vrf"; CmdLineParser p; p.Parse(s); REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Parse a command with value but without client or detector id") -{ +TEST_CASE("Parse a command with value but without client or detector id") { int argc = 3; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "vrf"; char a2[] = "3000"; - argv[0] = a0; - argv[1] = a1; - argv[2] = a2; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); + argv[2] = static_cast(a2); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.command() == "vrf"); REQUIRE(p.arguments().size() == 1); - REQUIRE(p.arguments()[0] == std::string("3000")); + REQUIRE(p.arguments()[0] == "3000"); } -TEST_CASE("Parse a string with value but without client or detector id") -{ +TEST_CASE("Parse a string with value but without client or detector id") { std::string s = "vrf 3000\n"; CmdLineParser p; @@ -95,61 +89,57 @@ TEST_CASE("Parse a string with value but without client or detector id") REQUIRE(p.detector_id() == -1); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); + REQUIRE(p.command() == "vrf"); REQUIRE(p.arguments().size() == 1); - REQUIRE(p.arguments()[0] == std::string("3000")); + REQUIRE(p.arguments()[0] == "3000"); } -TEST_CASE("Decodes position") -{ +TEST_CASE("Decodes position") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "7:vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == 7); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Decodes position from string") -{ - std::string s = "7:vrf\n"; +TEST_CASE("Decodes position from string") { + std::string s = "7:vrf\n"; CmdLineParser p; p.Parse(s); REQUIRE(p.detector_id() == 7); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Decodes double digit position") -{ +TEST_CASE("Decodes double digit position") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "73:vcmp"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == 73); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vcmp")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vcmp"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Decodes double digit position from string") -{ +TEST_CASE("Decodes double digit position from string") { std::string s = "73:vcmp"; CmdLineParser p; @@ -157,122 +147,107 @@ TEST_CASE("Decodes double digit position from string") REQUIRE(p.detector_id() == 73); REQUIRE(p.multi_id() == 0); - REQUIRE(p.command() == std::string("vcmp")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vcmp"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Decodes position and id") -{ +TEST_CASE("Decodes position and id") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "5-8:vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == 8); REQUIRE(p.multi_id() == 5); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Decodes position and id from string") -{ - +TEST_CASE("Decodes position and id from string") { std::string s = "5-8:vrf"; - - CmdLineParser p; p.Parse(s); REQUIRE(p.detector_id() == 8); REQUIRE(p.multi_id() == 5); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Double digit id") -{ +TEST_CASE("Double digit id") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "56-8:vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; p.Parse(argc, argv); REQUIRE(p.detector_id() == 8); REQUIRE(p.multi_id() == 56); - REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.command() == "vrf"); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Double digit id from string") -{ +TEST_CASE("Double digit id from string") { std::string s = "56-8:vrf"; - - CmdLineParser p; p.Parse(s); REQUIRE(p.detector_id() == 8); REQUIRE(p.multi_id() == 56); REQUIRE(p.command() == std::string("vrf")); - REQUIRE(p.arguments().size() == 0); + REQUIRE(p.arguments().empty()); } -TEST_CASE("Calling with wrong id throws invalid_argument") -{ +TEST_CASE("Calling with wrong id throws invalid_argument") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "asvldkn:vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; CHECK_THROWS(p.Parse(argc, argv)); } -TEST_CASE("Calling with string with wrong id throws invalid_argument") -{ +TEST_CASE("Calling with string with wrong id throws invalid_argument") { std::string s = "asvldkn:vrf"; CmdLineParser p; CHECK_THROWS(p.Parse(s)); } - -TEST_CASE("Calling with wrong client throws invalid_argument") -{ - +TEST_CASE("Calling with wrong client throws invalid_argument") { int argc = 2; - char* argv[argc]; + char *argv[argc]; char a0[] = "call"; char a1[] = "lki-3:vrf"; - argv[0] = a0; - argv[1] = a1; + argv[0] = static_cast(a0); + argv[1] = static_cast(a1); CmdLineParser p; CHECK_THROWS(p.Parse(argc, argv)); } -TEST_CASE("Calling with string with wrong client throws invalid_argument") -{ +TEST_CASE("Calling with string with wrong client throws invalid_argument") { std::string s = "lki-3:vrf"; CmdLineParser p; CHECK_THROWS(p.Parse(s)); } -TEST_CASE("Parses string with two arguments"){ - std::string s = "trimen 3000 4000\n"; - CmdLineParser p; - p.Parse(s); +TEST_CASE("Parses string with two arguments") { + std::string s = "trimen 3000 4000\n"; + CmdLineParser p; + p.Parse(s); - REQUIRE("trimen" == p.command()); - REQUIRE("3000" == p.arguments()[0]); - REQUIRE("4000" == p.arguments()[1]); - REQUIRE(2 == p.arguments().size()); + REQUIRE("trimen" == p.command()); + REQUIRE("3000" == p.arguments()[0]); + REQUIRE("4000" == p.arguments()[1]); + REQUIRE(p.arguments().size() == 2); } \ No newline at end of file