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

This commit is contained in:
maliakal_d 2019-08-12 14:15:34 +02:00
commit d9be3250ad
8 changed files with 173 additions and 75 deletions

View File

@ -1,18 +1,21 @@
#include "Result.h"
#include "TypeTraits.h"
#include "catch.hpp"
#include <type_traits>
#include <string>
using sls::Result;
TEST_CASE("Default construction", "[detector][n2]") {
TEST_CASE("Result looks and behaves like a standard container") {
REQUIRE(sls::is_container<Result<int>>::value == true);
}
TEST_CASE("Default construction is possible and gives an empty result") {
Result<int> res;
REQUIRE(res.size() == 0);
REQUIRE(res.empty() == true);
}
TEST_CASE("Initializer list construction", "[n2]") {
TEST_CASE("Result can be constructed from std::initializer_list") {
Result<int> res{1, 2, 5};
REQUIRE(res.empty() == false);
REQUIRE(res.size() == 3);
@ -21,7 +24,7 @@ TEST_CASE("Initializer list construction", "[n2]") {
REQUIRE(res[2] == 5);
}
TEST_CASE("Construct with value and number", "[n2]") {
TEST_CASE("Like vector it can be constructed from size and value") {
Result<int> res(5, 7);
REQUIRE(res.size() == 5);
REQUIRE(res[0] == 7);
@ -31,56 +34,103 @@ TEST_CASE("Construct with value and number", "[n2]") {
REQUIRE(res[4] == 7);
}
TEST_CASE("Squash empty", "[n2]") {
Result<double> res;
REQUIRE(res.squash() == 0.);
TEST_CASE("Result can be iterated using modern syntax"){
Result<int> res{0,1,2,3,4,5};
int i = 0;
for (const auto& r:res)
REQUIRE(r == i++);
}
TEST_CASE("Squash gives either value or default constructed value", "[n2]") {
TEST_CASE("Calling squash on an empty Result produces default value") {
Result<double> res;
REQUIRE(res.squash() == 0.);
Result<unsigned> res2;
REQUIRE(res2.squash() == 0u);
Result<std::string> res3;
REQUIRE(res3.squash() == "");
}
TEST_CASE("When equal squash gives the front value") {
Result<int> res{3, 3, 3};
REQUIRE(res.squash() == 3);
}
res.push_back(5);
TEST_CASE("When elements are not equal squash gives default value") {
Result<int> res{3, 3, 3, 5};
REQUIRE(res.squash() == 0);
}
TEST_CASE("String compare with squash") {
Result<std::string> res{"hej", "hej", "hej"};
REQUIRE(res.squash() == "hej");
}
TEST_CASE("tsquash throws for different elements") {
Result<int> res{1, 2, 3};
REQUIRE_THROWS(res.tsquash("something is wrong"));
}
TEST_CASE("tsquash returns front element when equal") {
Result<std::string> res{"word", "word"};
REQUIRE(res.tsquash("error") == "word");
}
TEST_CASE("When provided squash gives default value if elements differ") {
Result<int> res{5, 5, 5};
REQUIRE(res.squash(-1) == 5);
res.push_back(7); // adding a different value
REQUIRE(res.squash(-1) == -1);
}
TEST_CASE("Updating an element", "[n2]") {
TEST_CASE("It is possible to update elements by index access") {
Result<int> res{1, 2, 3};
REQUIRE(res[0] == 1);
REQUIRE(res[1] == 2);
REQUIRE(res[2] == 3);
res[0] = 5;
REQUIRE(res[0] == 5);
REQUIRE(res[1] == 2);
REQUIRE(res[2] == 3);
}
TEST_CASE("equal", "[n2]"){
TEST_CASE("Check if elements are equal") {
Result<float> res;
REQUIRE(res.equal() == false); // no elements to compare
// There are no elements to compare
REQUIRE(res.equal() == false);
//all (the one) elements are equal
res.push_back(1.2);
res.push_back(1.2); // one element "all" equal
REQUIRE(res.equal() == true);
res.push_back(1.2);
res.push_back(1.2); // two elements
REQUIRE(res.equal() == true);
res.push_back(1.3);
res.push_back(1.3); // three elements 1.2, 1.2, 1.3
REQUIRE(res.equal() == false);
}
TEST_CASE("throws for tsquash", "[n2]"){
Result<int> res{1,2,3};
REQUIRE_THROWS(res.tsquash("something is wrong"));
TEST_CASE("Result can be converted to std::vector") {
Result<short> res{1, 2, 3, 4, 5};
std::vector<short> vec{1, 2, 3, 4, 5};
std::vector<short> vec2 = res;
REQUIRE(vec2 == vec);
}
TEST_CASE("", "[n2]"){
Result<std::string> res{"hej", "hej", "hej"};
REQUIRE(res.squash() == "hej");
}
TEST_CASE("Result can be printed using <<") {
Result<int> res{1, 2, 3};
std::ostringstream os;
os << res;
REQUIRE(os.str() == "[1, 2, 3]");
}
TEST_CASE("Convert from Result<int> to Result<ns>") {
// This function is used when the detector class
// returns time as integers
using ns = std::chrono::nanoseconds;
Result<int> res{10, 50, 236};
Result<std::chrono::nanoseconds> res2 = res;
REQUIRE(res2[0] == ns(10));
REQUIRE(res2[1] == ns(50));
REQUIRE(res2[2] == ns(236));
}

View File

@ -7,7 +7,6 @@ set(SOURCES
src/ServerSocket.cpp
src/ServerInterface2.cpp
src/network_utils.cpp
src/ToString.cpp
)
set(HEADERS

View File

@ -19,8 +19,8 @@
namespace sls {
std::string ToString(const std::vector<std::string> &vec,
const char delimiter = ' ');
// std::string ToString(const std::vector<std::string> &vec,
// const char delimiter = ' ');
/** Convert std::chrono::duration with specified output unit */
template <typename T, typename Rep = double>

View File

@ -4,7 +4,7 @@
namespace sls {
/**
* Type trait to check if atemplate parameter is a std::chrono::duration
* Type trait to check if a template parameter is a std::chrono::duration
*/
template <typename T, typename _ = void>
@ -22,6 +22,19 @@ struct is_duration<T,
decltype(std::declval<T>().zero())>,
void>::type> : public std::true_type {};
/**
* Has str method
*/
template <typename T, typename _ = void> struct has_str : std::false_type {};
template <typename... Ts> struct has_str_helper {};
template <typename T>
struct has_str<T, typename std::conditional<
false,
has_str_helper<decltype(std::declval<T>().str())>,
void>::type> : public std::true_type {};
/**
* Type trait to evaluate if template parameter is
* complying with a standard container

View File

@ -1,18 +0,0 @@
#include "ToString.h"
namespace sls {
std::string ToString(const std::vector<std::string> &vec,
const char delimiter) {
std::ostringstream os;
if (vec.empty())
return os.str();
auto it = vec.cbegin();
os << *it++;
if (vec.size() > 1) {
while (it != vec.cend())
os << delimiter << *it++;
}
return os.str();
}
} // namespace sls

View File

@ -8,4 +8,5 @@ target_sources(tests PRIVATE
${CMAKE_CURRENT_SOURCE_DIR}/test-Sockets.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-FixedCapacityContainer.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-ToString.cpp
${CMAKE_CURRENT_SOURCE_DIR}/test-TypeTraits.cpp
)

View File

@ -2,10 +2,16 @@
#include "ToString.h"
#include "catch.hpp"
#include <vector>
using namespace sls;
#include <array>
TEST_CASE("Integer conversions", "[support][now]"){
// using namespace sls;
using sls::ToString;
using sls::StringTo;
using namespace sls::time;
TEST_CASE("Integer conversions", "[support]"){
REQUIRE(ToString(0) == "0");
REQUIRE(ToString(1) == "1");
REQUIRE(ToString(-1) == "-1");
@ -14,7 +20,8 @@ TEST_CASE("Integer conversions", "[support][now]"){
}
TEST_CASE("floating point conversions", "[support][now]"){
TEST_CASE("floating point conversions", "[support]"){
//Should strip trailing zeros
REQUIRE(ToString(0.) == "0");
REQUIRE(ToString(1.) == "1");
REQUIRE(ToString(-1.) == "-1");
@ -28,33 +35,33 @@ TEST_CASE("floating point conversions", "[support][now]"){
}
TEST_CASE("conversion from duration to string", "[support][now]") {
REQUIRE(ToString(time::ns(150)) == "150ns");
REQUIRE(ToString(time::ms(783)) == "0.783s");
REQUIRE(ToString(time::ms(783), "ms") == "783ms");
REQUIRE(ToString(time::us(0)) == "0ns"); // Defaults to the lowest unit
REQUIRE(ToString(time::us(0), "s") == "0s");
REQUIRE(ToString(time::s(-1)) == "-1s");
REQUIRE(ToString(time::us(-100)) == "-100us");
TEST_CASE("conversion from duration to string", "[support]") {
REQUIRE(ToString(ns(150)) == "150ns");
REQUIRE(ToString(ms(783)) == "0.783s");
REQUIRE(ToString(ms(783), "ms") == "783ms");
REQUIRE(ToString(us(0)) == "0ns"); // Defaults to the lowest unit
REQUIRE(ToString(us(0), "s") == "0s");
REQUIRE(ToString(s(-1)) == "-1s");
REQUIRE(ToString(us(-100)) == "-100us");
}
TEST_CASE("string to std::chrono::duration", "[support][now]") {
REQUIRE(StringTo<time::ns>("150", "ns") == time::ns(150));
REQUIRE(StringTo<time::ns>("150ns") == time::ns(150));
REQUIRE(StringTo<time::ns>("150s") == time::s(150));
REQUIRE(StringTo<time::s>("3 s") == time::s(3));
TEST_CASE("string to std::chrono::duration", "[support]") {
REQUIRE(StringTo<ns>("150", "ns") == ns(150));
REQUIRE(StringTo<ns>("150ns") == ns(150));
REQUIRE(StringTo<ns>("150s") == s(150));
REQUIRE(StringTo<s>("3 s") == s(3));
REQUIRE_THROWS(StringTo<time::ns>("5xs"));
REQUIRE_THROWS(StringTo<time::ns>("asvn"));
REQUIRE_THROWS(StringTo<ns>("5xs"));
REQUIRE_THROWS(StringTo<ns>("asvn"));
}
TEST_CASE("Convert vector of time", "[support][now]"){
std::vector<time::ns> vec{time::ns(150), time::us(10), time::ns(600)};
TEST_CASE("Convert vector of time", "[support]"){
std::vector<ns> vec{ns(150), us(10), ns(600)};
REQUIRE(ToString(vec) == "[150ns, 10us, 600ns]");
REQUIRE(ToString(vec, "ns") == "[150ns, 10000ns, 600ns]");
}
TEST_CASE("Vector of int", "[support][now]"){
TEST_CASE("Vector of int", "[support]"){
std::vector<int> vec;
REQUIRE(ToString(vec) == "[]");
@ -69,7 +76,7 @@ TEST_CASE("Vector of int", "[support][now]"){
}
TEST_CASE("Vector of double", "[support][now]"){
TEST_CASE("Vector of double", "[support]"){
std::vector<double> vec;
REQUIRE(ToString(vec) == "[]");
@ -81,5 +88,10 @@ TEST_CASE("Vector of double", "[support][now]"){
vec.push_back(-5669.325005);
REQUIRE(ToString(vec) == "[1.3, 5669.325, -5669.325005]");
}
TEST_CASE("Array"){
std::array<int, 3> arr{1,2,3};
REQUIRE(ToString(arr) == "[1, 2, 3]");
}

View File

@ -0,0 +1,41 @@
#include "TypeTraits.h"
#include "catch.hpp"
#include <array>
#include <vector>
#include <sstream>
#include <chrono>
//Dummy classes only used here for testing
class DummyWithStr {
public:
std::string str();
};
class DummyNoStr {
public:
std::string somethingelse();
};
TEST_CASE("sls::is_container") {
CHECK(sls::is_container<std::vector<int>>::value == true);
CHECK(sls::is_container<std::array<double, 3>>::value == true);
}
TEST_CASE("Check for str() method") {
REQUIRE(sls::has_str<DummyWithStr>::value == true);
REQUIRE(sls::has_str<DummyNoStr>::value == false);
}
TEST_CASE("Check for str() on ostream") {
REQUIRE(sls::has_str<std::ostringstream>::value == true);
}
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);
REQUIRE(sls::is_duration<int>::value == false);
REQUIRE(sls::is_duration<std::vector<int>>::value == false);
}