format support lib

This commit is contained in:
Erik Frojdh
2020-05-05 10:07:19 +02:00
parent e599bb7c24
commit ea4044e4b1
38 changed files with 775 additions and 854 deletions

View File

@@ -10,19 +10,18 @@ TEST_CASE("FixedCapacityContainer is a container") {
REQUIRE(sls::is_container<FixedCapacityContainer<int, 7>>::value == true);
}
TEST_CASE("Comparing FixedCapacity containers"){
FixedCapacityContainer<int, 5> a{0,1,2};
FixedCapacityContainer<int, 5> b{0,1,2};
FixedCapacityContainer<int, 5> c{0,1,2,4};
REQUIRE(a==b);
REQUIRE_FALSE(a!=b);
REQUIRE_FALSE(a==c);
REQUIRE(a!=c);
REQUIRE(c!=a);
REQUIRE_FALSE(c==a);
REQUIRE_FALSE(b==c);
TEST_CASE("Comparing FixedCapacity containers") {
FixedCapacityContainer<int, 5> a{0, 1, 2};
FixedCapacityContainer<int, 5> b{0, 1, 2};
FixedCapacityContainer<int, 5> c{0, 1, 2, 4};
REQUIRE(a == b);
REQUIRE_FALSE(a != b);
REQUIRE_FALSE(a == c);
REQUIRE(a != c);
REQUIRE(c != a);
REQUIRE_FALSE(c == a);
REQUIRE_FALSE(b == c);
}
TEST_CASE("Compare array and fixed capacity container") {

View File

@@ -13,7 +13,7 @@ std::vector<char> server() {
s.Receive(buffer.data(), buffer.size());
std::cout << "ServerReceived: " << std::string(buffer.begin(), buffer.end())
<< '\n';
std::vector<char> to_send(100, '\0');
to_send[0] = 'O';
to_send[1] = 'K';
@@ -27,7 +27,7 @@ TEST_CASE("The server recive the same message as we send", "[support]") {
std::vector<char> sent_message(100, '\0');
const char m[]{"some message"};
std::copy(std::begin(m), std::end(m), sent_message.data());
auto s = std::async(std::launch::async, server);
std::this_thread::sleep_for(std::chrono::milliseconds(100));
auto client = sls::DetectorSocket("localhost", 1950);
@@ -37,10 +37,10 @@ TEST_CASE("The server recive the same message as we send", "[support]") {
auto server_message = s.get();
CHECK(server_message == sent_message);
CHECK(std::string(received_message.data()) == "OK" );
CHECK(std::string(received_message.data()) == "OK");
CHECK(client.getSocketId() == -1);
}
TEST_CASE("throws on no server", "[support]"){
TEST_CASE("throws on no server", "[support]") {
CHECK_THROWS(sls::DetectorSocket("localhost", 1950));
}

View File

@@ -1,16 +1,16 @@
#include "TimeHelper.h"
#include "ToString.h"
#include "catch.hpp"
#include "network_utils.h"
#include "sls_detector_defs.h"
#include "catch.hpp"
#include <array>
#include <map>
#include <vector>
// using namespace sls;
using sls::defs;
using sls::StringTo;
using sls::ToString;
using sls::defs;
using namespace sls::time;
TEST_CASE("Integer conversions", "[support]") {
@@ -45,7 +45,6 @@ TEST_CASE("conversion from duration to string", "[support]") {
REQUIRE(ToString(us(-100)) == "-100us");
}
TEST_CASE("Convert vector of time", "[support]") {
std::vector<ns> vec{ns(150), us(10), ns(600)};
REQUIRE(ToString(vec) == "[150ns, 10us, 600ns]");
@@ -85,27 +84,26 @@ TEST_CASE("Array") {
REQUIRE(ToString(arr) == "[1, 2, 3]");
}
TEST_CASE("Convert types with str method"){
TEST_CASE("Convert types with str method") {
sls::IpAddr addr;
REQUIRE(ToString(addr) == "0.0.0.0");
REQUIRE(ToString(sls::IpAddr{}) == "0.0.0.0");
}
TEST_CASE("String to string", "[support]"){
TEST_CASE("String to string", "[support]") {
std::string s = "hej";
REQUIRE(ToString(s) == "hej");
}
TEST_CASE("vector of strings"){
TEST_CASE("vector of strings") {
std::vector<std::string> vec{"5", "s"};
REQUIRE(ToString(vec) == "[5, s]");
std::vector<std::string> vec2{"some", "strange", "words", "75"};
REQUIRE(ToString(vec2) == "[some, strange, words, 75]");
}
TEST_CASE("run status"){
TEST_CASE("run status") {
using defs = slsDetectorDefs;
REQUIRE(ToString(defs::runStatus::ERROR) == "error");
REQUIRE(ToString(defs::runStatus::WAITING) == "waiting");
@@ -126,7 +124,7 @@ TEST_CASE("string to std::chrono::duration", "[support]") {
REQUIRE_THROWS(StringTo<ns>("asvn"));
}
TEST_CASE("string to detectorType"){
TEST_CASE("string to detectorType") {
using dt = slsDetectorDefs::detectorType;
REQUIRE(StringTo<dt>("Eiger") == dt::EIGER);
REQUIRE(StringTo<dt>("Gotthard") == dt::GOTTHARD);
@@ -137,14 +135,13 @@ TEST_CASE("string to detectorType"){
REQUIRE(StringTo<dt>("Gotthard2") == dt::GOTTHARD2);
}
TEST_CASE("vec"){
TEST_CASE("vec") {
using rs = slsDetectorDefs::runStatus;
std::vector<rs> vec{rs::ERROR, rs::IDLE};
REQUIRE(ToString(vec) == "[error, idle]");
}
TEST_CASE("uint32 from string"){
TEST_CASE("uint32 from string") {
REQUIRE(StringTo<uint32_t>("0") == 0);
REQUIRE(StringTo<uint32_t>("5") == 5u);
REQUIRE(StringTo<uint32_t>("16") == 16u);
@@ -153,10 +150,9 @@ TEST_CASE("uint32 from string"){
REQUIRE(StringTo<uint32_t>("0x15") == 21u);
REQUIRE(StringTo<uint32_t>("0x15") == 0x15);
REQUIRE(StringTo<uint32_t>("0xffffff") == 0xffffff);
}
TEST_CASE("uint64 from string"){
TEST_CASE("uint64 from string") {
REQUIRE(StringTo<uint64_t>("0") == 0);
REQUIRE(StringTo<uint64_t>("5") == 5u);
REQUIRE(StringTo<uint64_t>("16") == 16u);
@@ -164,11 +160,9 @@ TEST_CASE("uint64 from string"){
REQUIRE(StringTo<uint64_t>("0x14") == 20u);
REQUIRE(StringTo<uint64_t>("0x15") == 21u);
REQUIRE(StringTo<uint64_t>("0xffffff") == 0xffffff);
}
TEST_CASE("int from string"){
TEST_CASE("int from string") {
REQUIRE(StringTo<int>("-1") == -1);
REQUIRE(StringTo<int>("-0x1") == -0x1);
REQUIRE(StringTo<int>("-0x1") == -1);
@@ -179,11 +173,9 @@ TEST_CASE("int from string"){
REQUIRE(StringTo<int>("0x14") == 20);
REQUIRE(StringTo<int>("0x15") == 21);
REQUIRE(StringTo<int>("0xffffff") == 0xffffff);
}
TEST_CASE("int64_t from string"){
TEST_CASE("int64_t from string") {
REQUIRE(StringTo<int64_t>("-1") == -1);
REQUIRE(StringTo<int64_t>("-0x1") == -0x1);
REQUIRE(StringTo<int64_t>("-0x1") == -1);
@@ -194,11 +186,9 @@ TEST_CASE("int64_t from string"){
REQUIRE(StringTo<int64_t>("0x14") == 20);
REQUIRE(StringTo<int64_t>("0x15") == 21);
REQUIRE(StringTo<int64_t>("0xffffff") == 0xffffff);
}
TEST_CASE("std::map of strings"){
TEST_CASE("std::map of strings") {
std::map<std::string, std::string> m;
m["key"] = "value";
auto s = ToString(m);
@@ -209,10 +199,9 @@ TEST_CASE("std::map of strings"){
m["test"] = "tree";
REQUIRE(ToString(m) == "{chrusi: musi, key: value, test: tree}");
}
TEST_CASE("std::map of ints"){
TEST_CASE("std::map of ints") {
std::map<int, int> m;
m[5] = 10;
@@ -221,10 +210,9 @@ TEST_CASE("std::map of ints"){
REQUIRE(ToString(m) == "{5: 10, 500: 50}");
m[372] = 999;
REQUIRE(ToString(m) == "{5: 10, 372: 999, 500: 50}");
}
TEST_CASE("Detector type"){
TEST_CASE("Detector type") {
auto dt = defs::detectorType::EIGER;
REQUIRE(ToString(dt) == "Eiger");
REQUIRE(StringTo<defs::detectorType>("Eiger") == dt);

View File

@@ -1,12 +1,12 @@
#include "TypeTraits.h"
#include "catch.hpp"
#include <array>
#include <vector>
#include <sstream>
#include <chrono>
#include <initializer_list>
#include <sstream>
#include <vector>
//Dummy classes only used here for testing
// Dummy classes only used here for testing
class DummyWithStr {
public:
std::string str();
@@ -32,7 +32,7 @@ TEST_CASE("Check for str() on ostream") {
REQUIRE(sls::has_str<std::ostringstream>::value == true);
}
TEST_CASE("sls::is_duration"){
TEST_CASE("sls::is_duration") {
REQUIRE(sls::is_duration<std::chrono::nanoseconds>::value == true);
REQUIRE(sls::is_duration<std::chrono::seconds>::value == true);
REQUIRE(sls::is_duration<std::chrono::hours>::value == true);
@@ -41,11 +41,11 @@ TEST_CASE("sls::is_duration"){
REQUIRE(sls::is_duration<std::vector<int>>::value == false);
}
TEST_CASE("initializer list"){
TEST_CASE("initializer list") {
REQUIRE(sls::is_light_container<std::initializer_list<int>>::value == true);
}
TEST_CASE("Check for emplace back"){
//we know vector should have this its the type trait that is tested
TEST_CASE("Check for emplace back") {
// we know vector should have this its the type trait that is tested
REQUIRE(sls::has_emplace_back<std::vector<int>>::value == true);
}

View File

@@ -1,17 +1,17 @@
#include "UdpRxSocket.h"
#include "catch.hpp"
#include "sls_detector_exceptions.h"
#include <future>
#include <thread>
#include <vector>
#include <cstdint>
#include <errno.h>
#include <future>
#include <iostream>
#include <netdb.h>
#include <netinet/in.h>
#include <string.h>
#include <sys/socket.h>
#include <thread>
#include <unistd.h>
#include <vector>
constexpr int default_port = 50001;
@@ -44,7 +44,7 @@ int open_socket(int port) {
return fd;
}
TEST_CASE("Get packet size returns the packet size we set in the constructor"){
TEST_CASE("Get packet size returns the packet size we set in the constructor") {
constexpr int port = 50001;
constexpr ssize_t packet_size = 8000;
sls::UdpRxSocket s{port, packet_size};
@@ -57,17 +57,16 @@ TEST_CASE("Receive data from a vector") {
std::vector<int> data_received(data_to_send.size());
ssize_t packet_size =
sizeof(decltype(data_to_send)::value_type) * data_to_send.size();
sls::UdpRxSocket udpsock{port, packet_size};
int fd = open_socket(port);
auto n = write(fd, data_to_send.data(), packet_size);
CHECK(n == packet_size);
CHECK(udpsock.ReceivePacket((char*)data_received.data()));
CHECK(udpsock.ReceivePacket((char *)data_received.data()));
close(fd);
CHECK(data_to_send == data_received);
}
TEST_CASE("Shutdown socket without hanging when waiting for data") {

12
slsSupportLib/tests/test-container_utils.cpp Executable file → Normal file
View File

@@ -113,8 +113,7 @@ TEST_CASE("Compare a vector containing two vectors", "[support]") {
CHECK(minusOneIfDifferent(d) == d[2]);
}
TEST_CASE("vector of bool", "[support]"){
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};
@@ -124,14 +123,13 @@ TEST_CASE("vector of bool", "[support]"){
CHECK(minusOneIfDifferent(c) == -1);
}
TEST_CASE("compare a vector of arrays", "[support]"){
TEST_CASE("compare a vector of arrays", "[support]") {
std::vector<std::array<uint64_t, 3>> vec0{{5,6,8},{5,6,8},{5,6,8}};
CHECK(minusOneIfDifferent(vec0) == std::array<uint64_t, 3>{5,6,8});
std::vector<std::array<uint64_t, 3>> vec0{{5, 6, 8}, {5, 6, 8}, {5, 6, 8}};
CHECK(minusOneIfDifferent(vec0) == std::array<uint64_t, 3>{5, 6, 8});
std::array<uint64_t, 3> arr;
arr.fill(-1);
std::vector<std::array<uint64_t, 3>> vec1{{5,90,8},{5,6,8},{5,6,8}};
std::vector<std::array<uint64_t, 3>> vec1{{5, 90, 8}, {5, 6, 8}, {5, 6, 8}};
CHECK(minusOneIfDifferent(vec1) == arr);
}

View File

@@ -1,8 +1,8 @@
#include "catch.hpp"
#include "logger.h"
#include <iostream>
#include <fstream>
#include <chrono>
#include <fstream>
#include <iostream>
using sls::Logger;
@@ -27,25 +27,23 @@ TEST_CASE("Test output") {
Logger::ReportingLevel() = logERROR;
//Redirect std::clog to local buffer
// Redirect std::clog to local buffer
std::ostringstream local;
auto clog_buff = std::clog.rdbuf();
std::clog.rdbuf(local.rdbuf());
//Try printing something with too low level
// Try printing something with too low level
LOG(logDEBUG) << "This should not be printed";
CHECK(local.str().empty());
//Try printing something with a higher level
// Try printing something with a higher level
LOG(logERROR) << "This should be printed";
CHECK(!local.str().empty());
std::clog.rdbuf(clog_buff); // restore
Logger::ReportingLevel() = old_value; //reset
std::clog.rdbuf(clog_buff); // restore
Logger::ReportingLevel() = old_value; // reset
//Check that the message is in the printed string
// Check that the message is in the printed string
auto r = local.str();
auto pos = r.find("This should be printed");
CHECK(pos != std::string::npos);
}

2
slsSupportLib/tests/test-network_utils.cpp Executable file → Normal file
View File

@@ -105,7 +105,7 @@ TEST_CASE("MAC Output operator gives same result as string", "[support]") {
CHECK(os.str() == addr.str());
}
TEST_CASE("Copy construct a MacAddr"){
TEST_CASE("Copy construct a MacAddr") {
MacAddr addr{"00:50:c2:46:d9:a6"};
MacAddr addr2(addr);
CHECK(addr == addr2);

129
slsSupportLib/tests/test-string_utils.cpp Executable file → Normal file
View File

@@ -5,107 +5,102 @@
#include "string_utils.h"
TEST_CASE("copy a string") {
char src[10] = "hej";
REQUIRE(src[3]=='\0');
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');
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"){
// 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');
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();
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);
// 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+");
}
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 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("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") {
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 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("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 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("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("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")