merging refactor (replacing)

This commit is contained in:
2019-04-12 10:53:09 +02:00
parent 0bb800cc8a
commit 89a06f099c
1176 changed files with 82698 additions and 159058 deletions

View File

@ -0,0 +1,8 @@
target_sources(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test-ClientInterface.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-CmdLineParser.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-container_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-network_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-string_utils.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-Timer.cpp
)

View File

@ -0,0 +1,12 @@
#include "ClientInterface.h"
#include "MySocketTCP.h"
#include "catch.hpp"
//tests to add
//help for all docs
//command for all depreciated commands
TEST_CASE("hopp") {
REQUIRE(true);
}

View File

@ -0,0 +1,266 @@
#include "CmdLineParser.h"
#include "catch.hpp"
#include <exception>
#include <string>
//tests to add
//help for all docs
//command for all depreciated commands
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 a0[] = "call";
argv[0] = static_cast<char *>(a0);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command().empty());
REQUIRE(p.arguments().empty());
}
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().empty());
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse a command without client id and detector id results in default") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().empty());
}
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() == "vrf");
REQUIRE(p.arguments().empty());
}
TEST_CASE("Parse a command with value but without client or detector id") {
int argc = 3;
char *argv[argc];
char a0[] = "call";
char a1[] = "vrf";
char a2[] = "3000";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
argv[2] = static_cast<char *>(a2);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 1);
REQUIRE(p.arguments()[0] == "3000");
}
TEST_CASE("Parse a string with value but without client or detector id") {
std::string s = "vrf 3000\n";
CmdLineParser p;
p.Parse(s);
REQUIRE(p.detector_id() == -1);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().size() == 1);
REQUIRE(p.arguments()[0] == "3000");
}
TEST_CASE("Decodes position") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "7:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == 7);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().empty());
}
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() == "vrf");
REQUIRE(p.arguments().empty());
}
TEST_CASE("Decodes double digit position") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "73:vcmp";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == 73);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vcmp");
REQUIRE(p.arguments().empty());
}
TEST_CASE("Decodes double digit position from string") {
std::string s = "73:vcmp";
CmdLineParser p;
p.Parse(s);
REQUIRE(p.detector_id() == 73);
REQUIRE(p.multi_id() == 0);
REQUIRE(p.command() == "vcmp");
REQUIRE(p.arguments().empty());
}
TEST_CASE("Decodes position and id") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "5-8:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 5);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().empty());
}
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() == "vrf");
REQUIRE(p.arguments().empty());
}
TEST_CASE("Double digit id") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "56-8:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
p.Parse(argc, argv);
REQUIRE(p.detector_id() == 8);
REQUIRE(p.multi_id() == 56);
REQUIRE(p.command() == "vrf");
REQUIRE(p.arguments().empty());
}
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().empty());
}
TEST_CASE("Calling with wrong id throws invalid_argument") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "asvldkn:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
CHECK_THROWS(p.Parse(argc, argv));
}
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") {
int argc = 2;
char *argv[argc];
char a0[] = "call";
char a1[] = "lki-3:vrf";
argv[0] = static_cast<char *>(a0);
argv[1] = static_cast<char *>(a1);
CmdLineParser p;
CHECK_THROWS(p.Parse(argc, argv));
}
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);
REQUIRE("trimen" == p.command());
REQUIRE("3000" == p.arguments()[0]);
REQUIRE("4000" == p.arguments()[1]);
REQUIRE(p.arguments().size() == 2);
}
TEST_CASE("Build up argv"){
CmdLineParser p;
// p.argv();
REQUIRE(p.argv().empty());
REQUIRE(p.argv().data() == nullptr);
std::string s = "trimen 3000 4000\n";
p.Parse(s);
REQUIRE(p.argv().data() != nullptr);
REQUIRE(p.argv().size() == 3);
}

View File

@ -0,0 +1,23 @@
#include "Timer.h"
#include "catch.hpp"
#include <chrono>
#include <thread>
TEST_CASE("Time 1s restart then time 2s") {
auto sleep_duration = std::chrono::seconds(1);
auto t = sls::Timer();
std::this_thread::sleep_for(sleep_duration);
REQUIRE(t.elapsed_s() == Approx(1).epsilon(0.01));
t.restart();
std::this_thread::sleep_for(sleep_duration * 2);
REQUIRE(t.elapsed_s() == Approx(2).epsilon(0.01));
}
TEST_CASE("Return ms") {
auto sleep_duration = std::chrono::milliseconds(1300);
auto t = sls::Timer();
std::this_thread::sleep_for(sleep_duration);
REQUIRE(t.elapsed_ms() == Approx(1300).epsilon(0.5));
}

View File

@ -0,0 +1,125 @@
#include "catch.hpp"
#include "container_utils.h"
#include <exception>
#include <string>
#include <vector>
using namespace sls;
TEST_CASE("Equality of an empty vector", "[support]") {
std::vector<int> v;
REQUIRE(v.empty());
REQUIRE_FALSE(allEqual(v));
REQUIRE_FALSE(allEqualWithTol(v, 2));
REQUIRE_FALSE(allEqualTo(v, 5));
REQUIRE_FALSE(anyEqualTo(v, 5));
REQUIRE_FALSE(anyEqualToWithTol(v, 5, 1));
}
TEST_CASE("Equality of a vector with one element", "[support]") {
std::vector<int> v{5};
REQUIRE(v.size() == 1);
REQUIRE(allEqual(v));
REQUIRE(allEqualWithTol(v, 1));
REQUIRE(allEqualTo(v, 5));
REQUIRE(allEqualToWithTol(v, 5, 2));
REQUIRE(anyEqualTo(v, 5));
REQUIRE(anyEqualToWithTol(v, 5, 1));
}
TEST_CASE("A larger vector of the same elements", "[support]") {
std::vector<int> v(101, 5);
REQUIRE(v.size() == 101);
REQUIRE(allEqual(v));
REQUIRE(allEqualWithTol(v, 1));
REQUIRE(allEqualTo(v, 5));
REQUIRE(anyEqualTo(v, 5));
SECTION("Push back another element to create a vector where not all are "
"equal") {
v.push_back(7);
REQUIRE(v.size() == 102);
REQUIRE_FALSE(allEqual(v));
REQUIRE_FALSE(allEqualWithTol(v, 1));
REQUIRE(allEqualWithTol(v, 3));
REQUIRE_FALSE(allEqualTo(v, 5));
REQUIRE_FALSE(allEqualToWithTol(v, 5, 1));
REQUIRE(allEqualToWithTol(v, 5, 3));
REQUIRE(anyEqualTo(v, 5));
}
}
TEST_CASE("A vector of double with different values", "[support]") {
std::vector<double> v{1.2, 2., 4.2, 4, 1.1};
REQUIRE(allEqual(v) == false);
REQUIRE(allEqualWithTol(v, 0.3) == false);
REQUIRE(allEqualWithTol(v, 3.2));
}
TEST_CASE("Sum of empty vector", "[support]") {
std::vector<float> v;
REQUIRE(sls::sum(v) == Approx(0));
}
TEST_CASE("Sum of vector", "[support]") {
std::vector<double> v{1.2, 2., 4.2, 4, 1.13};
REQUIRE(sls::sum(v) == Approx(12.53));
}
TEST_CASE("Minus one if different", "[support]") {
std::vector<double> v;
REQUIRE(v.empty());
double d = -1;
REQUIRE(sls::minusOneIfDifferent(v) == d);
SECTION("single element") {
v.push_back(7.3);
REQUIRE(v.size() == 1);
REQUIRE(sls::minusOneIfDifferent(v) == Approx(7.3));
}
SECTION("different elements") {
v.push_back(7.3);
v.push_back(1.0);
v.push_back(62.1);
REQUIRE(sls::minusOneIfDifferent(v) == Approx(-1.0));
}
}
TEST_CASE("minus one does not have side effects", "[support]") {
std::vector<int> v{1, 1, 1};
int i = sls::minusOneIfDifferent(v);
REQUIRE(i == 1);
i = 5;
REQUIRE(v[0] == 1);
}
TEST_CASE("Compare a vector containing two vectors", "[support]") {
std::vector<std::vector<int>> a{{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}};
std::vector<std::vector<int>> b{{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 3, 5}};
std::vector<std::vector<int>> c{{0, 1, 2, 3, 4}, {0, 1, 2, 3, 3, 5}};
std::vector<std::vector<int>> d{
{0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}};
std::vector<int> e{0, 1, 2, 3, 4, 5};
CHECK(minusOneIfDifferent(a) == a[0]);
CHECK(minusOneIfDifferent(a) == e);
CHECK(minusOneIfDifferent(b) == std::vector<int>{-1});
CHECK(minusOneIfDifferent(c) == std::vector<int>{-1});
CHECK(minusOneIfDifferent(d) == d[2]);
}
TEST_CASE("vector of bool", "[support]"){
std::vector<bool> a{true, true, true};
std::vector<bool> b{false, false, false};
std::vector<bool> c{true, false, true};
CHECK(minusOneIfDifferent(a) == 1);
CHECK(minusOneIfDifferent(b) == 0);
CHECK(minusOneIfDifferent(c) == -1);
}

View File

@ -0,0 +1,99 @@
#include "catch.hpp"
#include "network_utils.h"
#include <iostream>
#include <vector>
#include "string_utils.h"
#include "sls_detector_exceptions.h"
using namespace sls;
TEST_CASE("Convert mac address using classes", "[support]") {
std::vector<uint64_t> vec_addr{346856806822, 346856806852, 262027939863028};
std::vector<std::string> vec_ans{"00:50:c2:46:d9:a6", "00:50:c2:46:d9:c4",
"ee:50:22:46:d9:f4"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto mac0 = MacAddr(vec_addr[i]);
auto mac1 = MacAddr(vec_ans[i]);
CHECK(mac0 == vec_addr[i]);
CHECK(mac1 == vec_addr[i]);
CHECK(mac0 == vec_ans[i]);
CHECK(mac1 == vec_ans[i]);
CHECK(mac0.str() == vec_ans[i]);
CHECK(mac1.str() == vec_ans[i]);
}
}
TEST_CASE("Hex representation of MAC", "[support]") {
MacAddr m{346856806822};
CHECK(m.hex() == "0050c246d9a6");
CHECK(m.str() == "00:50:c2:46:d9:a6");
CHECK_FALSE(m == 7);
MacAddr m2{"00:50:c2:46:d9:c4"};
CHECK(m2 == 346856806852);
CHECK(m2.hex() == "0050c246d9c4");
CHECK(m2.str() == "00:50:c2:46:d9:c4");
CHECK_FALSE(m2 == 3);
}
TEST_CASE("Convert IP using classes ", "[support]") {
std::vector<uint32_t> vec_addr{4073554305, 2747957633, 2697625985};
std::vector<std::string> vec_ans{"129.129.205.242", "129.129.202.163",
"129.129.202.160"};
for (size_t i = 0; i != vec_addr.size(); ++i) {
auto ip0 = IpAddr(vec_addr[i]);
auto ip1 = IpAddr(vec_ans[i]);
CHECK(ip0 == ip1);
CHECK(ip0 == vec_addr[i]);
CHECK(ip1 == vec_addr[i]);
CHECK(ip0 == vec_ans[i]);
CHECK(ip1 == vec_ans[i]);
CHECK(ip0.str() == vec_ans[i]);
CHECK(ip1.str() == vec_ans[i]);
}
}
TEST_CASE("Strange input gives 0", "[support]") {
CHECK(IpAddr("hej") == 0);
CHECK(MacAddr("hej") == 0);
}
TEST_CASE("Convert to uint for sending over network", "[support]") {
MacAddr addr{346856806822};
uint64_t a = addr.uint64();
CHECK(a == 346856806822);
IpAddr addr2{"129.129.205.242"};
uint32_t b = addr2.uint32();
CHECK(b == 4073554305);
}
TEST_CASE("Hostname lookup failed throws", "[support]"){
CHECK_THROWS_AS(HostnameToIp("pippifax"), RuntimeError);
}
TEST_CASE("IP Output operator gives same result as string", "[support]") {
IpAddr addr{"129.129.205.242"};
std::ostringstream os;
os << addr;
CHECK(os.str() == "129.129.205.242");
CHECK(os.str() == addr.str());
}
TEST_CASE("MAC Output operator gives same result as string", "[support]") {
MacAddr addr{"00:50:c2:46:d9:a6"};
std::ostringstream os;
os << addr;
CHECK(os.str() == "00:50:c2:46:d9:a6");
CHECK(os.str() == addr.str());
}
//TODO!(Erik) Look up a real hostname and verify the IP

View File

@ -0,0 +1,112 @@
#include "MySocketTCP.h"
#include "catch.hpp"
#include "logger.h"
#include <iostream>
#include <vector>
#include "string_utils.h"
TEST_CASE("copy a string") {
char src[10] = "hej";
REQUIRE(src[3]=='\0');
char dst[20];
sls::strcpy_safe(dst, src);
REQUIRE(dst[0]=='h');
REQUIRE(dst[1]=='e');
REQUIRE(dst[2]=='j');
REQUIRE(dst[3]=='\0');
}
#ifdef NDEBUG
//This test can only run in release since we assert on the length of the string
TEST_CASE("copy a long string"){
auto src = "some very very long sting that does not fit";
char dst[3];
sls::strcpy_safe(dst, src);
REQUIRE(dst[0]=='s');
REQUIRE(dst[1]=='o');
REQUIRE(dst[2]=='\0');
}
#endif
TEST_CASE("Concat") {
std::vector<std::string> v{"one", "one", "one"};
std::vector<std::string> v2{"one", "one", "one"};
auto r = sls::concatenateIfDifferent(v);
REQUIRE(r == std::string("one"));
r.clear();
// make sure we didn't modify the string
REQUIRE(v == v2);
SECTION("add a different value"){
v.emplace_back("two");
REQUIRE(v!=v2);
REQUIRE( sls::concatenateIfDifferent(v) == "one+one+one+two+");
}
}
TEST_CASE("split a string with end delimiter"){
std::string s("abra+kadabra+");
auto r =sls::split(s, '+');
REQUIRE(r.size()==2);
REQUIRE(r[0]=="abra");
REQUIRE(r[1]=="kadabra");
}
TEST_CASE("split a string without end delimiter"){
std::string s("abra+kadabra+filibom");
auto r =sls::split(s, '+');
REQUIRE(r.size()==3);
REQUIRE(r[0]=="abra");
REQUIRE(r[1]=="kadabra");
REQUIRE(r[2]=="filibom");
}
TEST_CASE("concatenate non empty strings"){
std::vector<std::string> vec{"hej", "kalas", "", "foto"};
REQUIRE(vec.size()==4);
auto ret = sls::concatenateNonEmptyStrings(vec);
REQUIRE(ret == "hej+kalas+foto+");
}
TEST_CASE("concatenate non empty strings with only emty"){
std::vector<std::string> vec{"", "", ""};
REQUIRE(vec.size()==3);
auto ret = sls::concatenateNonEmptyStrings(vec);
REQUIRE(ret.empty());
}
TEST_CASE("concatenate non empty strings with one element"){
std::vector<std::string> vec{"", "hej", "", "", ""};
REQUIRE(vec.size()==5);
auto ret = sls::concatenateNonEmptyStrings(vec);
REQUIRE(ret=="hej+");
}
TEST_CASE("Remove char from string"){
char str[] = "sometest";
sls::removeChar(str, 'e');
REQUIRE(std::string(str) == "somtst");
}
TEST_CASE("Remove char from empty string"){
char str[50] = {};
sls::removeChar(str, 'e');
REQUIRE(std::string(str) == "");
}
TEST_CASE("Many characters in a row"){
char str[] = "someeequitellll::ongstring";
sls::removeChar(str, 'l');
REQUIRE(std::string(str) == "someeequite::ongstring");
}
// TEST_CASE("concat things not being strings")