From 85516e42c0ff2d4a0d50c9f75d1030693071e71d Mon Sep 17 00:00:00 2001 From: Erik Frojdh Date: Thu, 4 Apr 2019 12:19:30 +0200 Subject: [PATCH] trimEn now takes vector --- .../include/multiSlsDetector.h | 27 +-- slsDetectorSoftware/include/slsDetector.h | 15 +- slsDetectorSoftware/src/multiSlsDetector.cpp | 27 +-- slsDetectorSoftware/src/slsDetector.cpp | 29 +-- .../src/slsDetectorCommand.cpp | 41 ++-- slsDetectorSoftware/tests/CMakeLists.txt | 6 +- .../tests/test-multiSlsDetector.cpp | 63 ++++++ .../tests/test-slsDetector.cpp | 49 +++++ slsReceiverSoftware/tests/test.cpp | 3 - slsSupportLib/include/container_utils.h | 82 ++++---- slsSupportLib/src/string_utils.cpp | 11 +- slsSupportLib/tests/test-container_utils.cpp | 186 +++++++++--------- slsSupportLib/tests/test-string_utils.cpp | 5 - slsSupportLib/tests/test.cpp | 3 - 14 files changed, 318 insertions(+), 229 deletions(-) create mode 100644 slsDetectorSoftware/tests/test-multiSlsDetector.cpp create mode 100644 slsDetectorSoftware/tests/test-slsDetector.cpp delete mode 100644 slsReceiverSoftware/tests/test.cpp delete mode 100644 slsSupportLib/tests/test.cpp diff --git a/slsDetectorSoftware/include/multiSlsDetector.h b/slsDetectorSoftware/include/multiSlsDetector.h index db2118643..ebccea081 100644 --- a/slsDetectorSoftware/include/multiSlsDetector.h +++ b/slsDetectorSoftware/include/multiSlsDetector.h @@ -1355,16 +1355,15 @@ class multiSlsDetector : public virtual slsDetectorDefs { * @param detPos -1 for all detectors in list or specific detector position * @returns number of trim energies */ - int setTrimEn(int nen, int *en = nullptr, int detPos = -1); + int setTrimEn(std::vector energies, int detPos = -1); /** * Returns the number of trim energies and their value (Eiger) * - * @param en array of energies * @param detPos -1 for all detectors in list or specific detector position - * @returns number of trim energies + * @returns vector of trim energies */ - int getTrimEn(int *en = nullptr, int detPos = -1); + std::vector getTrimEn(int detPos = -1); /** * Pulse Pixel (Eiger) @@ -1953,6 +1952,8 @@ class multiSlsDetector : public virtual slsDetectorDefs { */ void processData(); + void addSlsDetector(std::unique_ptr det); + private: /** * Initialize (open/create) shared memory for the sharedMultiDetector @@ -1992,14 +1993,16 @@ class multiSlsDetector : public virtual slsDetectorDefs { */ void addSlsDetector(const std::string &hostname); - /** - * add gap pixels to the image (only for Eiger in 4 bit mode) - * @param image pointer to image without gap pixels - * @param gpImage poiner to image with gap pixels, if NULL, allocated inside - * function - * @returns number of data bytes of image with gap pixels - */ - int processImageWithGapPixels(char *image, char *&gpImage); + + + /** + * add gap pixels to the image (only for Eiger in 4 bit mode) + * @param image pointer to image without gap pixels + * @param gpImage poiner to image with gap pixels, if NULL, allocated + * inside function + * @returns number of data bytes of image with gap pixels + */ + int processImageWithGapPixels(char *image, char *&gpImage); /** * Set total progress (total number of frames/images in an acquisition) diff --git a/slsDetectorSoftware/include/slsDetector.h b/slsDetectorSoftware/include/slsDetector.h index c38037d4e..122f4bb72 100644 --- a/slsDetectorSoftware/include/slsDetector.h +++ b/slsDetectorSoftware/include/slsDetector.h @@ -1208,18 +1208,17 @@ class slsDetector : public virtual slsDetectorDefs{ * Sets the number of trim energies and their value (Eiger) * \sa sharedSlsDetector * @param nen number of energies - * @param en array of energies + * @param vector os trimmed energies * @returns number of trim energies */ - int setTrimEn(int nen, int *en = nullptr); + int setTrimEn(std::vector energies={}); /** - * Returns the number of trim energies and their value (Eiger) - * \sa sharedSlsDetector - * @param en array of energies - * @returns number of trim energies - */ - int getTrimEn(int *en = nullptr); + * Returns a vector with the trimmed energies (Eiger) + * \sa sharedSlsDetector + * @returns vector with the trimmed energies + */ + std::vector getTrimEn(); /** * Pulse Pixel (Eiger) diff --git a/slsDetectorSoftware/src/multiSlsDetector.cpp b/slsDetectorSoftware/src/multiSlsDetector.cpp index 94b3717c7..430ab4a73 100644 --- a/slsDetectorSoftware/src/multiSlsDetector.cpp +++ b/slsDetectorSoftware/src/multiSlsDetector.cpp @@ -426,6 +426,15 @@ void multiSlsDetector::addSlsDetector(const std::string &hostname) { detectors[pos]->setOnline(true); } +void multiSlsDetector::addSlsDetector(std::unique_ptr det){ + detectors.push_back(std::move(det)); + multi_shm()->numberOfDetectors = detectors.size(); + multi_shm()->dataBytes += detectors.back()->getDataBytes(); + multi_shm()->dataBytesInclGapPixels += + detectors.back()->getDataBytesInclGapPixels(); + multi_shm()->numberOfChannels += detectors.back()->getTotalNumberOfChannels(); +} + slsDetectorDefs::detectorType multiSlsDetector::getDetectorTypeAsEnum(int detPos) { // single if (detPos >= 0) { @@ -2524,25 +2533,19 @@ int multiSlsDetector::enableGapPixels(int val, int detPos) { return ret; } -int multiSlsDetector::setTrimEn(int ne, int *ene, int detPos) { - // single +int multiSlsDetector::setTrimEn(std::vector energies, int detPos) { if (detPos >= 0) { - return detectors[detPos]->setTrimEn(ne, ene); + return detectors[detPos]->setTrimEn(energies); } - - // multi - auto r = serialCall(&slsDetector::setTrimEn, ne, ene); + auto r = parallelCall(&slsDetector::setTrimEn, energies); return sls::minusOneIfDifferent(r); } -int multiSlsDetector::getTrimEn(int *ene, int detPos) { - // single +std::vector multiSlsDetector::getTrimEn(int detPos) { if (detPos >= 0) { - return detectors[detPos]->getTrimEn(ene); + return detectors[detPos]->getTrimEn(); } - - // multi - auto r = serialCall(&slsDetector::getTrimEn, ene); + auto r = parallelCall(&slsDetector::getTrimEn); return sls::minusOneIfDifferent(r); } diff --git a/slsDetectorSoftware/src/slsDetector.cpp b/slsDetectorSoftware/src/slsDetector.cpp index c906e7653..9dfff1b83 100644 --- a/slsDetectorSoftware/src/slsDetector.cpp +++ b/slsDetectorSoftware/src/slsDetector.cpp @@ -3125,23 +3125,28 @@ int slsDetector::enableGapPixels(int val) { return detector_shm()->gappixels; } -int slsDetector::setTrimEn(int nen, int *en) { - if (en) { - for (int ien = 0; ien < nen; ++ien) { - detector_shm()->trimEnergies[ien] = en[ien]; - } - detector_shm()->nTrimEn = nen; +int slsDetector::setTrimEn(std::vector energies) { + if (energies.size()>MAX_TRIMEN){ + std::ostringstream os; + os << "Size of trim energies: " << energies.size() << " exceeds what can " + << "be stored in shared memory: " << MAX_TRIMEN << "\n"; + throw RuntimeError(os.str()); } + + for (int i = 0; i != energies.size(); ++i) { + detector_shm()->trimEnergies[i] = energies[i]; + } + detector_shm()->nTrimEn = energies.size(); return (detector_shm()->nTrimEn); } -int slsDetector::getTrimEn(int *en) { - if (en) { - for (int ien = 0; ien < detector_shm()->nTrimEn; ++ien) { - en[ien] = detector_shm()->trimEnergies[ien]; - } +std::vector slsDetector::getTrimEn() { + std::vector energies; + energies.reserve(detector_shm()->nTrimEn); + for (int i = 0; i != detector_shm()->nTrimEn; ++i) { + energies.push_back(detector_shm()->trimEnergies[i]); } - return (detector_shm()->nTrimEn); + return energies; } int slsDetector::pulsePixel(int n, int x, int y) { diff --git a/slsDetectorSoftware/src/slsDetectorCommand.cpp b/slsDetectorSoftware/src/slsDetectorCommand.cpp index 4cb797258..201b15d20 100644 --- a/slsDetectorSoftware/src/slsDetectorCommand.cpp +++ b/slsDetectorSoftware/src/slsDetectorCommand.cpp @@ -2505,41 +2505,24 @@ std::string slsDetectorCommand::helpSettingsDir(int action) { } std::string slsDetectorCommand::cmdTrimEn(int narg, char *args[], int action, int detPos) { - int ival; - int ip; - - char answer[1000]; - - if (action == HELP_ACTION) + std::vector energies; + if (action == HELP_ACTION) return helpTrimEn(action); if (action == PUT_ACTION) { - if (sscanf(args[1], "%d", &ival)) { - int pos[ival]; - for (ip = 0; ip < ival; ++ip) { - if ((2 + ip) < narg) { - if (sscanf(args[2 + ip], "%d", pos + ip)) { - } else - break; - } - } - myDet->setTrimEn(ip, pos, detPos); + energies.reserve(narg-1); + for(int i=1; i!=narg; ++i){ + energies.push_back(std::stoi(args[i])); } + myDet->setTrimEn(energies, detPos); + energies.clear(); } - int npos = myDet->getTrimEn(nullptr, detPos); - if (npos != -1) { - sprintf(answer, "%d", npos); - int opos[npos]; - npos = myDet->getTrimEn(opos, detPos); - if (npos != -1) { - for (int ip = 0; ip < npos; ++ip) { - sprintf(answer, "%s %d", answer, opos[ip]); - } - } + energies = myDet->getTrimEn(detPos); + std::ostringstream os; + for(const auto& en : energies){ + os << en << ' '; } - if (npos == -1) - sprintf(answer, "%d", -1); - return std::string(answer); + return os.str(); } std::string slsDetectorCommand::helpTrimEn(int action) { diff --git a/slsDetectorSoftware/tests/CMakeLists.txt b/slsDetectorSoftware/tests/CMakeLists.txt index 1f1fb923c..a30e9f3ac 100644 --- a/slsDetectorSoftware/tests/CMakeLists.txt +++ b/slsDetectorSoftware/tests/CMakeLists.txt @@ -1 +1,5 @@ -target_sources(tests PRIVATE ${CMAKE_CURRENT_SOURCE_DIR}/test-SharedMemory.cpp) \ No newline at end of file +target_sources(tests PRIVATE + ${CMAKE_CURRENT_SOURCE_DIR}/test-SharedMemory.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test-slsDetector.cpp + ${CMAKE_CURRENT_SOURCE_DIR}/test-multiSlsDetector.cpp +) \ No newline at end of file diff --git a/slsDetectorSoftware/tests/test-multiSlsDetector.cpp b/slsDetectorSoftware/tests/test-multiSlsDetector.cpp new file mode 100644 index 000000000..847221990 --- /dev/null +++ b/slsDetectorSoftware/tests/test-multiSlsDetector.cpp @@ -0,0 +1,63 @@ + +#include "catch.hpp" +#include "container_utils.h" +#include "multiSlsDetector.h" +#include "slsDetector.h" +#include "string_utils.h" + +#include + +using namespace sls; + +SCENARIO("Multi detector operation", "[detector]") { + + multiSlsDetector::freeSharedMemory(0, -1); + + GIVEN("An empty multi detector") { + multiSlsDetector m(0); + THEN("the size is zero") { + CHECK(m.getNumberOfDetectors() == 0); + CHECK(m.getDataBytes() == 0); + CHECK(m.getTotalNumberOfChannels() == 0); + } + + WHEN("we add a detector") { + m.addSlsDetector(sls::make_unique( + slsDetectorDefs::detectorType::EIGER, 0, 0)); + THEN("the size and number of detector changes") { + CHECK(m.getNumberOfDetectors() == 1); + CHECK(m.getTotalNumberOfChannels() == 256 * 1024); + } + + WHEN("we add another detector") { + m.addSlsDetector(sls::make_unique( + slsDetectorDefs::detectorType::EIGER, 0, 1)); + THEN("the size and number of detector changes") { + CHECK(m.getNumberOfDetectors() == 2); + CHECK(m.getTotalNumberOfChannels() == 2 * 256 * 1024); + } + + WHEN("We set the trimen") { + std::vector energies{5000, 6000, 7000, 8000, 9000}; + m.setTrimEn(energies); + THEN("we read back the same values") { + CHECK(m.getTrimEn() == energies); + } + } + WHEN("We set the trimen to different values") { + std::vector en0{5000, 6000, 7000, 8000, 9000}; + std::vector en1{6000, 7000, 8000, 9000}; + m.setTrimEn(en0, 0); + m.setTrimEn(en1, 1); + THEN("we read back the same values") { + CHECK(m.getTrimEn(0) == en0); + CHECK(m.getTrimEn(1) == en1); + CHECK(m.getTrimEn() == std::vector{-1}); + } + } + } + } + + m.freeSharedMemory(); + } +} diff --git a/slsDetectorSoftware/tests/test-slsDetector.cpp b/slsDetectorSoftware/tests/test-slsDetector.cpp new file mode 100644 index 000000000..eed73cfd7 --- /dev/null +++ b/slsDetectorSoftware/tests/test-slsDetector.cpp @@ -0,0 +1,49 @@ + +#include "catch.hpp" +#include "container_utils.h" +#include "slsDetector.h" +#include "string_utils.h" + +#include + +using namespace sls; + +TEST_CASE("Set and get trimen", "[detector]") { + // Free shared memory to be sure that we start in a clean state + slsDetector::freeSharedMemory(0, 0); + + // Create a detector and check that the type is set correctly + slsDetector d(slsDetectorDefs::detectorType::EIGER, 0, 0); + CHECK(d.getDetectorTypeAsEnum() == slsDetectorDefs::detectorType::EIGER); + + // At the beginning there should be no trimen set + auto res = d.getTrimEn(); + CHECK(res.size() == 0); + + std::vector energies{5200, 6400, 8500, 9900, 12000}; + d.setTrimEn(energies); + auto res2 = d.getTrimEn(); + + // Check that the size and every element matches what we set + CHECK(res2.size() == energies.size()); + for (size_t i = 0; i != res2.size(); ++i) + CHECK(res2[i] == energies[i]); + + + + // Setting trimen with too many vales throws an exception and keeps the + // old values + std::vector too_many(150, 1000); + CHECK_THROWS(d.setTrimEn(too_many)); + auto res3 = d.getTrimEn(); + CHECK(res3.size() == energies.size()); + for (size_t i = 0; i != res3.size(); ++i) + CHECK(res3[i] == energies[i]); + + // Setting trimen without arguments resets to zero + d.setTrimEn(); + CHECK(d.getTrimEn().size() == 0); + + // Clean up before next test + d.freeSharedMemory(); +} diff --git a/slsReceiverSoftware/tests/test.cpp b/slsReceiverSoftware/tests/test.cpp deleted file mode 100644 index 8daed99c4..000000000 --- a/slsReceiverSoftware/tests/test.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// tests-main.cpp -#define CATCH_CONFIG_MAIN -#include "catch.hpp" \ No newline at end of file diff --git a/slsSupportLib/include/container_utils.h b/slsSupportLib/include/container_utils.h index 6eb710fe9..39f5de6c8 100644 --- a/slsSupportLib/include/container_utils.h +++ b/slsSupportLib/include/container_utils.h @@ -2,17 +2,15 @@ #define CONTAINER_UTILS_H #include +#include #include +#include #include #include #include -#include -#include namespace sls { - - // C++11 make_unique implementation for exception safety // already available as std::make_unique in C++14 template @@ -23,94 +21,88 @@ make_unique(Args &&... args) { template typename std::enable_if::value, std::unique_ptr>::type -make_unique(std::size_t n) -{ +make_unique(std::size_t n) { typedef typename std::remove_extent::type RT; return std::unique_ptr(new RT[n]); } - -template -bool allEqual(const std::vector& container) -{ +template bool allEqual(const std::vector &container) { if (container.empty()) return false; - const auto& first = container[0]; + const auto &first = container[0]; return std::all_of(container.cbegin(), container.cend(), - [first](const T& element) { return element == first; }); + [first](const T &element) { return element == first; }); } template typename std::enable_if::value, bool>::type -allEqualWithTol(const std::vector& container, const T tol) -{ +allEqualWithTol(const std::vector &container, const T tol) { if (container.empty()) return false; - const auto& first = container[0]; + const auto &first = container[0]; return std::all_of(container.cbegin(), container.cend(), - [first, tol](const T& element) { - return (std::abs(element - first) < tol); - }); + [first, tol](const T &element) { + return (std::abs(element - first) < tol); + }); } template -bool allEqualTo(const std::vector& container, const T value) -{ +bool allEqualTo(const std::vector &container, const T value) { if (container.empty()) return false; return std::all_of(container.cbegin(), container.cend(), - [value](const T& element) { return element == value; }); + [value](const T &element) { return element == value; }); } template -bool allEqualToWithTol(const std::vector& container, const T value, - const T tol) -{ +bool allEqualToWithTol(const std::vector &container, const T value, + const T tol) { if (container.empty()) return false; return std::all_of(container.cbegin(), container.cend(), - [value, tol](const T& element) { - return (std::abs(element - value) < tol); - }); + [value, tol](const T &element) { + return (std::abs(element - value) < tol); + }); } template -bool anyEqualTo(const std::vector& container, const T value) -{ +bool anyEqualTo(const std::vector &container, const T value) { return std::any_of(container.cbegin(), container.cend(), - [value](const T& element) { return element == value; }); + [value](const T &element) { return element == value; }); } template -bool anyEqualToWithTol(const std::vector& container, const T value, - const T tol) -{ +bool anyEqualToWithTol(const std::vector &container, const T value, + const T tol) { return std::any_of(container.cbegin(), container.cend(), - [value, tol](const T& element) { return (std::abs(element - value) < tol); }); + [value, tol](const T &element) { + return (std::abs(element - value) < tol); + }); } template typename std::enable_if::value, T>::type -sum(const std::vector& container) -{ - return std::accumulate(container.cbegin(), container.cend(), T{ 0 }); +sum(const std::vector &container) { + return std::accumulate(container.cbegin(), container.cend(), T{0}); +} + +template T minusOneIfDifferent(const std::vector &container) { + if (allEqual(container)) + return container.front(); + return static_cast(-1); } template -T minusOneIfDifferent(const std::vector& container) -{ - if (allEqual(container)) { +std::vector +minusOneIfDifferent(const std::vector> &container) { + if (allEqual(container)) return container.front(); - } else { - return static_cast(-1); - } + return std::vector{-1}; } - - } // namespace sls #endif // CONTAINER_UTILS_H diff --git a/slsSupportLib/src/string_utils.cpp b/slsSupportLib/src/string_utils.cpp index 7c90eab25..2ddfe6785 100644 --- a/slsSupportLib/src/string_utils.cpp +++ b/slsSupportLib/src/string_utils.cpp @@ -47,17 +47,8 @@ std::string concatenateIfDifferent(const std::vector &container) { } } + template std::string concatenateIfDifferent(const std::vector &); template std::string concatenateIfDifferent(const std::vector &); -std::string stringIpToHex(const std::string &ip) { - std::istringstream iss(ip); - std::ostringstream oss; - std::string item; - while (std::getline(iss, item, '.')) { - oss << std::setw(2) << std::setfill('0') << std::hex << std::stoi(item); - } - return oss.str(); -} - }; // namespace sls \ No newline at end of file diff --git a/slsSupportLib/tests/test-container_utils.cpp b/slsSupportLib/tests/test-container_utils.cpp index d4572f131..ccddd517f 100644 --- a/slsSupportLib/tests/test-container_utils.cpp +++ b/slsSupportLib/tests/test-container_utils.cpp @@ -4,103 +4,111 @@ #include #include -using sls::allEqual; -using sls::allEqualTo; -using sls::anyEqualTo; -using sls::allEqualToWithTol; -using sls::allEqualWithTol; -using sls::anyEqualToWithTol; +using namespace sls; -using sls::sum; - -TEST_CASE("Equality of an empty vector") { - std::vector 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") { - std::vector 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") { - std::vector 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); +TEST_CASE("Equality of an empty vector", "[support]") { + std::vector v; + REQUIRE(v.empty()); REQUIRE_FALSE(allEqual(v)); - - REQUIRE_FALSE(allEqualWithTol(v, 1)); - REQUIRE(allEqualWithTol(v, 3)); - + REQUIRE_FALSE(allEqualWithTol(v, 2)); REQUIRE_FALSE(allEqualTo(v, 5)); - - REQUIRE_FALSE(allEqualToWithTol(v, 5, 1)); - REQUIRE(allEqualToWithTol(v, 5, 3)); - REQUIRE(anyEqualTo(v, 5)); - } + REQUIRE_FALSE(anyEqualTo(v, 5)); + REQUIRE_FALSE(anyEqualToWithTol(v, 5, 1)); } -TEST_CASE("A vector of double with different values") { - std::vector 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") { - std::vector v; - REQUIRE(sls::sum(v) == Approx(0)); -} - -TEST_CASE("Sum of vector") { - std::vector v{1.2, 2., 4.2, 4, 1.13}; - REQUIRE(sls::sum(v) == Approx(12.53)); -} - -TEST_CASE("Minus one if different") { - std::vector v; - REQUIRE(v.empty()); - double d = -1; - REQUIRE(sls::minusOneIfDifferent(v) == d); - - SECTION("single element") { - v.push_back(7.3); +TEST_CASE("Equality of a vector with one element", "[support]") { + std::vector v{5}; 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)); - } + 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("minus one does not have side effects"){ - std::vector v{1,1,1}; +TEST_CASE("A larger vector of the same elements", "[support]") { + std::vector 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 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 v; + REQUIRE(sls::sum(v) == Approx(0)); +} + +TEST_CASE("Sum of vector", "[support]") { + std::vector 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 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 v{1, 1, 1}; int i = sls::minusOneIfDifferent(v); - REQUIRE(i==1); - i=5; - REQUIRE(v[0]==1); + REQUIRE(i == 1); + i = 5; + REQUIRE(v[0] == 1); } +TEST_CASE("Compare a vector containing two vectors", "[support]") { + + std::vector> a{{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 4, 5}}; + std::vector> b{{0, 1, 2, 3, 4, 5}, {0, 1, 2, 3, 3, 5}}; + std::vector> c{{0, 1, 2, 3, 4}, {0, 1, 2, 3, 3, 5}}; + std::vector> d{ + {0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}, {0, 1, 2, 3, 4}}; + std::vector e{0, 1, 2, 3, 4, 5}; + + CHECK(minusOneIfDifferent(a) == a[0]); + CHECK(minusOneIfDifferent(a) == e); + CHECK(minusOneIfDifferent(b) == std::vector{-1}); + CHECK(minusOneIfDifferent(c) == std::vector{-1}); + CHECK(minusOneIfDifferent(d) == d[2]); +} diff --git a/slsSupportLib/tests/test-string_utils.cpp b/slsSupportLib/tests/test-string_utils.cpp index aedbd2347..efbf99b76 100644 --- a/slsSupportLib/tests/test-string_utils.cpp +++ b/slsSupportLib/tests/test-string_utils.cpp @@ -90,11 +90,6 @@ TEST_CASE("concatenate non empty strings with one element"){ REQUIRE(ret=="hej+"); } -TEST_CASE("Convert ip address"){ - std::string address = "101.255.103.1"; - REQUIRE(sls::stringIpToHex(address) == "65ff6701"); - -} TEST_CASE("Remove char from string"){ char str[] = "sometest"; diff --git a/slsSupportLib/tests/test.cpp b/slsSupportLib/tests/test.cpp deleted file mode 100644 index 8daed99c4..000000000 --- a/slsSupportLib/tests/test.cpp +++ /dev/null @@ -1,3 +0,0 @@ -// tests-main.cpp -#define CATCH_CONFIG_MAIN -#include "catch.hpp" \ No newline at end of file