Merge branch 'refactor' of github.com:slsdetectorgroup/slsDetectorPackage into refactor

This commit is contained in:
maliakal_d 2018-12-04 16:05:17 +01:00
commit a4de4bb475
6 changed files with 108 additions and 26 deletions

View File

@ -6,7 +6,7 @@ option (USE_HDF5 "HDF5 File format" OFF)
option (USE_TEXTCLIENT "Text Client" OFF) option (USE_TEXTCLIENT "Text Client" OFF)
option (USE_RECEIVER "Receiver" OFF) option (USE_RECEIVER "Receiver" OFF)
option (USE_GUI "GUI" OFF) option (USE_GUI "GUI" OFF)
option (USE_TESTS "TESTS" OFF) option (USE_TESTS "TESTS" ON)
if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0) if (CMAKE_CXX_COMPILER_VERSION VERSION_GREATER 6.0)
set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -Wno-misleading-indentation") set(CMAKE_CXX_FLAGS "${CMAKE_CXX_FLAGS} -Wall -std=c++11 -Wno-misleading-indentation")

View File

@ -5,34 +5,28 @@
#include <string> #include <string>
class Timer { class Timer {
using clock = std::chrono::high_resolution_clock; using clock = std::chrono::high_resolution_clock;
using time_point = std::chrono::time_point<clock>; using time_point = std::chrono::time_point<clock>;
public: public:
Timer(std::string name = "0") Timer(std::string name = "0")
: t0(clock::now()) : t0(clock::now()), name_(name) {
, name_(name)
{
} }
double elapsed_ms() double elapsed_ms() {
{
return std::chrono::duration<double, std::milli>(clock::now() - t0).count(); return std::chrono::duration<double, std::milli>(clock::now() - t0).count();
} }
double elapsed_s() double elapsed_s() {
{
return std::chrono::duration<double>(clock::now() - t0).count(); return std::chrono::duration<double>(clock::now() - t0).count();
} }
void print_elapsed() void print_elapsed() {
{
std::cout << "Timer \"" << name_ << "\": Elapsed time " << elapsed_ms() << " ms\n"; std::cout << "Timer \"" << name_ << "\": Elapsed time " << elapsed_ms() << " ms\n";
} }
void restart() void restart() {
{
t0 = clock::now(); t0 = clock::now();
} }
private: private:
time_point t0; time_point t0;
std::string name_; std::string name_;
}; };

View File

@ -7,6 +7,7 @@ include_directories(
${PROJECT_SOURCE_DIR}/catch ${PROJECT_SOURCE_DIR}/catch
${PROJECT_SOURCE_DIR}/slsSupportLib/include ${PROJECT_SOURCE_DIR}/slsSupportLib/include
${PROJECT_SOURCE_DIR}/slsDetectorSoftware/multiSlsDetector ${PROJECT_SOURCE_DIR}/slsDetectorSoftware/multiSlsDetector
${PROJECT_SOURCE_DIR}/slsDetectorSoftware/sharedMemory
) )
@ -15,7 +16,8 @@ if(USE_TESTS)
set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src) set(LOCAL_TEST_DIR ${CMAKE_CURRENT_SOURCE_DIR}/src)
set(TEST_SOURCES set(TEST_SOURCES
${LOCAL_TEST_DIR}/test-container_utils.cpp ${LOCAL_TEST_DIR}/test-container_utils.cpp
${LOCAL_TEST_DIR}/test-multiDetector.cpp ${LOCAL_TEST_DIR}/test-MySocketTCP.cpp
#${LOCAL_TEST_DIR}/test-multiDetector.cpp
${LOCAL_TEST_DIR}/test.cpp ${LOCAL_TEST_DIR}/test.cpp
# PARENT_SCOPE # PARENT_SCOPE
) )
@ -23,6 +25,7 @@ if(USE_TESTS)
target_link_libraries(test target_link_libraries(test
slsDetectorShared slsDetectorShared
pthread pthread
rt
) )
set_target_properties(test PROPERTIES set_target_properties(test PROPERTIES
RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/bin

View File

@ -0,0 +1,86 @@
#include "MySocketTCP.h"
#include "catch.hpp"
// #include "multiSlsDetector.h"
#include "logger.h"
#include <iostream>
#include <vector>
#define VERBOSE
TEST_CASE("Sending and receiving data with two sockets") {
const int port_number{1966}; //TODO! Avoid hardcoded port number!!!
auto sender = MySocketTCP("localhost", port_number);
auto receiver = MySocketTCP(port_number);
auto s = sender.Connect();
auto r = receiver.Connect();
REQUIRE(s > 0);
REQUIRE(r > 0);
REQUIRE(sender.getPortNumber() == port_number);
REQUIRE(receiver.getPortNumber() == port_number);
std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
std::vector<char> received_message(message_to_send.size());
auto sent = sender.SendDataOnly(message_to_send.data(), message_to_send.size());
auto received = receiver.ReceiveDataOnly(received_message.data(), message_to_send.size());
REQUIRE(sent == message_to_send.size());
REQUIRE(received == received_message.size());
REQUIRE(sent == received);
REQUIRE(message_to_send == received_message);
receiver.CloseServerTCPSocketDescriptor();
receiver.Disconnect();
sender.Disconnect();
REQUIRE(receiver.getsocketDescriptor() == -1);
REQUIRE(receiver.getFileDes() == -1);
REQUIRE(sender.getFileDes() == -1);
}
TEST_CASE("Open two sockets on the same port fails and throws") {
const int port_number{1966};
auto server = MySocketTCP(port_number);
CHECK_THROWS(MySocketTCP(port_number));
}
// TEST_CASE("Conversions"){
// std::cout << "name " << MySocketTCP::nameToMac("enp10s0u1u3u3") << '\n';
// }
TEST_CASE("Have two clients connect to the same server") {
const int port_number{1966};
auto server = MySocketTCP(port_number);
auto client1 = MySocketTCP("localhost", port_number);
auto client2 = MySocketTCP("localhost", port_number);
client1.SetTimeOut(1);
client2.SetTimeOut(1);
server.SetTimeOut(1);
auto fd1 = client1.Connect();
auto fd2 = client2.Connect();
server.Connect();
REQUIRE(fd1 > 0);
REQUIRE(fd2 > 0);
std::cout << "fd1 " << fd1 << '\n';
std::cout << "fd2 " << fd2 << '\n';
std::vector<char> message_to_send{'H', 'e', 'l', 'l', 'o'};
std::vector<char> received_message(message_to_send.size());
client1.SendDataOnly(message_to_send.data(), message_to_send.size());
auto n1 = server.ReceiveDataOnly(received_message.data(), received_message.size());
std::cout << "n1 " << n1 << '\n';
client2.SendDataOnly(message_to_send.data(), message_to_send.size());
auto n2 = server.ReceiveDataOnly(received_message.data(), received_message.size());
std::cout << "n2 " << n2 << '\n';
}

View File

@ -7,7 +7,6 @@
using sls::allEqual; using sls::allEqual;
using sls::allEqualTo; using sls::allEqualTo;
using sls::anyEqualTo; using sls::anyEqualTo;
using sls::allEqualToWithTol; using sls::allEqualToWithTol;
using sls::allEqualWithTol; using sls::allEqualWithTol;
using sls::anyEqualToWithTol; using sls::anyEqualToWithTol;

View File

@ -1,11 +1,11 @@
#include "catch.hpp" // #include "catch.hpp"
#include "multiSlsDetector.h" // #include "multiSlsDetector.h"
#include <iostream> // #include <iostream>
TEST_CASE("Initialize a detector") { // TEST_CASE("Initialize a detector") {
multiSlsDetector det(0, true, true); // multiSlsDetector det(0, true, true);
std::cout << "Size: " << det.getNumberOfDetectors() << std::endl; // std::cout << "Size: " << det.getNumberOfDetectors() << std::endl;
std::cout << "Hostname: " << det.getHostname() << std::endl; // std::cout << "Hostname: " << det.getHostname() << std::endl;
REQUIRE(false); // REQUIRE(false);
} // }