Files
slsDetectorPackage/slsReceiverSoftware/tests/test-ArrangeDataBasedOnBitList.cpp
AliceMazzoleni99 28b2aa9673
All checks were successful
Build on RHEL9 / build (push) Successful in 3m50s
Build on RHEL8 / build (push) Successful in 4m46s
Run Simulator Tests on local RHEL9 / build (push) Successful in 14m37s
Build on local RHEL9 / build (push) Successful in 1m26s
Run Simulator Tests on local RHEL8 / build (push) Successful in 16m57s
Build on local RHEL8 / build (push) Successful in 3m33s
Dev/add simulator tests in GitHub workflows (#1337)
* added simulator tests in github workflows

* indentation error

* typo

* debug

* Logging for debugging

* added more debug lines

* more debugging

* debug

* debug

* debug

* dont throw if process does not exist

* debug

* added absolute path to sls_detector commands

* some refactoring in test scripts

* added absolute path to all slsdet command

* typo

* ../tests/scripts/test_frame_synchronizer.py

* raise exception upon failure for github workflows

* removed hidden tags

* some refactoring in test scripts

* some refactoring

* fixed CMakeLists

* fixed unsuccesful merge

* updated python tests using simulators

* debug import error

* debug module import

* python -m runs module pytest as script - everything in path available

* removed integartion tests

* enable file write not to log files

* run tests without log files

* increased sleep time for udp packets

* added logg level variable to cmake

* added testing policies to documenattion

* disabled check for num_frames for jungfrau & xilinx

* set log level as cmake cached variable

* disable tests for jungfrau and xilinx_ctb

* check frames for HDF5

* updated Documentation of Testing

* changed withdetectorsimulators to detectorintegration

* replaced [.cmdcall] with [.detectorintegration]

* check_file_size only disabled for jungfrau - disable for all roi tests

* changed time to wait after receive to 5 ms

* take into account half modules of eiger

* num udp interfaces needs to be consistent across modules

* suppressed warning enclosing if

* config added 2 udp ports per default for moench and jungfrau

* write detector output to console

* allow jungfrau to tests num frames, remove unused variable (numinterfaces), add comment for future to handle traceback to know which calling function threw the files unmatched, added documentation for tests (examples for .detectoritnegration and how to disable marked tests, removed addditional argumetns to disable for test_simulator as one can just use ~, removed the check that checks for jungfrau checking number of frames at master attributes and at rx test, removed unused advanced_test_settings in test_simulator script, the num_mods check for multiple modules is removed and default num  modules set to 1 for test_simulator (to be increased later), back to raising exception for killprocess

* removed integration tests from cmakelists.txt and cmk.sh, modified the tests workflow command to reflect the disable argument and removed xilinx_ctb from test (fix fromdeveloper merge to be done)

* filtering by actual name for disable certain tests on github workflow

* minor refactor

* wip

* wip

* changes to run on local rh9 runner instead of github workfloa

* modified yml to remove some leftover from github workflow

* test

* fix build_dir in scripts (github workflow) and pytest dir in gitea workflow

* making the local machine use python3.13 binary

* pythonpath added

* changes for build_Dir back

* allowing ctb api tests

* allowed ctb api tests and set up slsdetname envt variable for shared memory being reserved just for these tests

* added rh8 workflow for local runner on gitea

* remnants from rh9 local runner

* remnants from rh9 local runner

* conda env for all shell for local runner

* allowing hdf5 to build on local runner

* run all tests for both the runners

* refactored fixtures a bit and merged some tests that use one session for entire server

* test fail

* test fix

* adding github workflow to test without data file checks and without logs

* documentation changes

* unnecessary import in conftest

* allowing the session_simulator to test for multiple modules and interfaces etc

* allow test_simulator script to run for 2 modules for all modules except ctb and xilinx ctb

* run upon push

* removing the disable file check on github workflow

* minor adjustment

* testing without synch

* reverting to previous

* with log file

* without the space

* summary from file and more error extracts from file to terminal

* minor

* trying nlf for more details

* updated with no log file to print everything to screen  also for det and rxr

* trying a no throw

* stoi was more about indent in yaml

* tries

* wip

* debug

* number of frames inconsistent fix=>just take first one, only test xilinx

* jungfrau tests without frames caught check

* extend the disable file check to everywhere that creates files

* specify path for test_simulator

* withoutprinting ==

* wip

* back with printing===, but not parsing file for errors anymore

* lang?

* wip

* safe log?

* wip2

* wip

* dont split error as its streaming live, just raise

* with log files

* lang?

* last resort

* wip

* test no det with general tests

* show tests live

* also include hidden integration tests

* without extra summary?

* revert

* last resort again

* tsquash on int64_t?

* tsquash on int64_t? mroe print

* writing to /tmp?

* all tests

* might be the fix?

* write to file

* fixed a few quiet mode no log file tests

* work on any branch for github tests, work on also release candidates for gitea tests

* added frame synchronizer tests to github workflow

* moved tests to run_tests.yaml from cmake.yaml

* documentation

* disabled general tests

---------

Co-authored-by: Dhanya Thattil <dhanya.thattil@psi.ch>
2026-02-03 11:45:12 +01:00

417 lines
14 KiB
C++

// SPDX-License-Identifier: LGPL-3.0-or-other
// Copyright (C) 2025 Contributors to the SLS Detector Package
/************************************************
* @file test-ArrangeDataBasedOnBitList.cpp
* @short test case for DataProcessor rearrange functions,
***********************************************/
#include "DataProcessor.h"
#include "GeneralData.h"
#include "catch.hpp"
#include <vector>
namespace sls {
// dummy GeneralData class for testing
class GeneralDataTest : public GeneralData {
public:
GeneralDataTest() { detType = slsDetectorDefs::CHIPTESTBOARD; }
int GetNumberOfAnalogDatabytes() { return nAnalogBytes; };
int GetNumberOfDigitalDatabytes() { return nDigitalBytes; };
int GetNumberOfTransceiverDatabytes() { return nTransceiverBytes; };
void SetNumberOfAnalogDatabytes(int value) { nAnalogBytes = value; }
void SetNumberOfDigitalDatabytes(int value) { nDigitalBytes = value; }
void SetNumberOfTransceiverDatabytes(int value) {
nTransceiverBytes = value;
}
void SetCtbDbitOffset(const int value) { ctbDbitOffset = value; }
void SetCtbDbitList(const std::vector<int> &value) { ctbDbitList = value; }
void SetCtbDbitReorder(const bool value) { ctbDbitReorder = value; }
private:
int nAnalogBytes{};
int nDigitalBytes{};
int nTransceiverBytes{};
};
// dummy DataProcessor class for testing
class DataProcessorTest : public DataProcessor {
public:
DataProcessorTest() : DataProcessor(0){};
~DataProcessorTest(){};
void ArrangeDbitData(size_t &size, char *data) {
DataProcessor::ArrangeDbitData(size, data);
}
void RemoveTrailingBits(size_t &size, char *data) {
DataProcessor::RemoveTrailingBits(size, data);
}
};
/**
* test fixture for Testing,
* num_analog_bytes = 1 byte has a value of 125
* num_transceiver_bytes = 2 both bytes have a value of 125
* num_digital_bytes is variable and is defined by number of samples
* default num sample is 5
* all bytes in digital data take a value of 0xFF (alternating bits between 0,
* 1)
*/
class DataProcessorTestFixture {
public:
DataProcessorTestFixture() {
// setup Test Fixture
dataprocessor = new DataProcessorTest;
generaldata = new GeneralDataTest;
generaldata->SetNumberOfAnalogDatabytes(num_analog_bytes);
generaldata->SetNumberOfTransceiverDatabytes(num_transceiver_bytes);
generaldata->SetNumberOfDigitalDatabytes(num_digital_bytes +
num_random_offset_bytes);
dataprocessor->SetGeneralData(generaldata);
}
~DataProcessorTestFixture() {
delete[] data;
delete dataprocessor;
delete generaldata;
}
size_t get_size() const {
return num_analog_bytes + num_digital_bytes + num_transceiver_bytes +
num_random_offset_bytes;
}
void set_num_samples(const size_t value) {
num_samples = value;
num_digital_bytes = num_samples * 8; // 64 (8 bytes) per sample
generaldata->SetNumberOfDigitalDatabytes(num_digital_bytes +
num_random_offset_bytes);
}
void set_random_offset_bytes(const size_t value) {
num_random_offset_bytes = value;
generaldata->SetNumberOfDigitalDatabytes(num_digital_bytes +
num_random_offset_bytes);
}
void set_data(const std::bitset<8> pattern = 0xFF) {
delete[] data;
uint64_t max_bytes_per_bit =
num_samples % 8 == 0 ? num_samples / 8 : num_samples / 8 + 1;
uint64_t reserved_size =
get_size() - num_digital_bytes + max_bytes_per_bit * 64;
data = new char[reserved_size];
// set testing data
memset(data, dummy_value, num_analog_bytes); // set to dummy value
memset(data + num_analog_bytes, 0,
num_random_offset_bytes); // set to zero
memset(data + num_analog_bytes + num_random_offset_bytes,
static_cast<uint8_t>(pattern.to_ulong()),
num_digital_bytes); // all digital bits are one
memset(data + num_digital_bytes + num_analog_bytes +
num_random_offset_bytes,
dummy_value,
num_transceiver_bytes); // set to dummy value
}
DataProcessorTest *dataprocessor;
GeneralDataTest *generaldata;
const size_t num_analog_bytes = 1;
const size_t num_transceiver_bytes = 2;
const char dummy_value = static_cast<char>(125);
size_t num_digital_bytes = 40; // num_samples * 8 = 5 * 8 = 40
size_t num_random_offset_bytes = 0;
size_t num_samples = 5;
char *data = nullptr;
};
TEST_CASE_METHOD(DataProcessorTestFixture, "Remove Trailing Bits",
"[dataprocessor][bitoffset]") {
const size_t num_random_offset_bytes = 3;
set_random_offset_bytes(num_random_offset_bytes);
set_data();
generaldata->SetCtbDbitOffset(num_random_offset_bytes);
size_t expected_size = get_size() - num_random_offset_bytes;
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes); // set to 125
memset(expected_data + num_analog_bytes, 0xFF,
num_digital_bytes); // set to 1
memset(expected_data + num_digital_bytes + num_analog_bytes, dummy_value,
num_transceiver_bytes); // set to 125
size_t size = get_size();
dataprocessor->RemoveTrailingBits(size, data);
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
// parametric test tested with num_samples = 5, num_samples = 10, num_samples =
// 8
TEST_CASE_METHOD(DataProcessorTestFixture, "Reorder all",
"[dataprocessor][reorder]") {
// parameters: num_samples, expected_num_digital_bytes,
// expected_digital_part_for_each_bit
auto parameters = GENERATE(
std::make_tuple(5, 64, std::vector<uint8_t>{0b00011111}),
std::make_tuple(10, 2 * 64, std::vector<uint8_t>{0xFF, 0b00000011}),
std::make_tuple(8, 64, std::vector<uint8_t>{0xFF}));
size_t num_samples, expected_num_digital_bytes;
std::vector<uint8_t> expected_digital_part;
std::tie(num_samples, expected_num_digital_bytes, expected_digital_part) =
parameters;
// set number of samples for test fixture -> create data
set_num_samples(num_samples);
set_data();
std::vector<int> bitlist(64);
std::iota(bitlist.begin(), bitlist.end(), 0);
generaldata->SetCtbDbitList(bitlist);
generaldata->SetCtbDbitReorder(true); // set reorder to true
const size_t expected_size =
num_analog_bytes + num_transceiver_bytes + expected_num_digital_bytes;
// create expected data
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes); // set to 125
for (size_t bit = 0; bit < 64; ++bit) {
memcpy(expected_data + num_analog_bytes +
expected_digital_part.size() * bit,
expected_digital_part.data(), expected_digital_part.size());
}
memset(expected_data + expected_num_digital_bytes + num_analog_bytes,
dummy_value,
num_transceiver_bytes); // set to 125
size_t size = get_size();
dataprocessor->ArrangeDbitData(size, data); // call reorder
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
TEST_CASE_METHOD(DataProcessorTestFixture,
"Reorder all and remove trailing bits",
"[dataprocessor][reorder]") {
// set number of samples for test fixture -> create data
const size_t num_random_offset_bytes = 3;
set_random_offset_bytes(num_random_offset_bytes);
set_data();
std::vector<int> bitlist(64);
std::iota(bitlist.begin(), bitlist.end(), 0);
generaldata->SetCtbDbitList(bitlist);
generaldata->SetCtbDbitOffset(num_random_offset_bytes);
generaldata->SetCtbDbitReorder(true); // set reorder to true
const size_t expected_num_digital_bytes = 64;
std::vector<uint8_t> expected_digital_part{0b00011111};
const size_t expected_size =
num_analog_bytes + num_transceiver_bytes + expected_num_digital_bytes;
// create expected data
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes); // set to 125
for (size_t bit = 0; bit < 64; ++bit) {
memcpy(expected_data + num_analog_bytes +
expected_digital_part.size() * bit,
expected_digital_part.data(), expected_digital_part.size());
}
memset(expected_data + expected_num_digital_bytes + num_analog_bytes,
dummy_value,
num_transceiver_bytes); // set to 125
size_t size = get_size();
dataprocessor->ArrangeDbitData(size, data); // call reorder
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
TEST_CASE_METHOD(DataProcessorTestFixture, "Arrange bitlist with reorder false",
"[dataprocessor][retrievebitlist]") {
// parameters: num_samples, bitlist, expected_num_digital_bytes,
// expected_digital_part
auto parameters = GENERATE(
std::make_tuple(5, std::vector<int>{1, 4, 5}, 5,
std::vector<uint8_t>{0b00000010}),
std::make_tuple(5, std::vector<int>{1, 5, 4}, 5,
std::vector<uint8_t>{0b00000100}),
std::make_tuple(5, std::vector<int>{1, 5, 3, 7, 8, 50, 42, 60, 39}, 10,
std::vector<uint8_t>{0b11110000, 0b00000000}),
std::make_tuple(5, std::vector<int>{1, 5, 3, 7, 8, 50, 42, 60}, 5,
std::vector<uint8_t>{0b11110000}));
size_t num_samples, expected_num_digital_bytes;
std::vector<uint8_t> expected_digital_part;
std::vector<int> bitlist;
std::tie(num_samples, bitlist, expected_num_digital_bytes,
expected_digital_part) = parameters;
generaldata->SetCtbDbitList(bitlist);
generaldata->SetCtbDbitReorder(false);
set_num_samples(num_samples);
set_data(0b01010101); // set digital data to 0x55 to have alternating bits
size_t expected_size =
num_analog_bytes + num_transceiver_bytes + expected_num_digital_bytes;
// create expected data
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes);
for (size_t sample = 0; sample < num_samples; ++sample) {
memcpy(expected_data + num_analog_bytes +
expected_digital_part.size() * sample,
expected_digital_part.data(), expected_digital_part.size());
}
memset(expected_data + expected_num_digital_bytes + num_analog_bytes,
dummy_value, num_transceiver_bytes);
size_t size = get_size();
dataprocessor->ArrangeDbitData(size, data);
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
TEST_CASE_METHOD(DataProcessorTestFixture, "Arrange bitlist with reorder true",
"[dataprocessor][retrievebitlist]") {
// parameters: num_samples, bitlist, expected_num_digital_bytes,
// expected_digital_part
auto parameters = GENERATE(
std::make_tuple(5, std::vector<int>{1, 4, 5}, 3,
std::vector<uint8_t>{0x00, 0b00011111, 0x00}),
std::make_tuple(5, std::vector<int>{1, 5, 4}, 3,
std::vector<uint8_t>{0x00, 0x00, 0b00011111}),
std::make_tuple(10, std::vector<int>{1, 4, 5}, 6,
std::vector<uint8_t>{0x00, 0x00, 0b11111111, 0b00000011,
0x00, 0x00}),
std::make_tuple(8, std::vector<int>{1, 5, 3, 7, 8, 50, 42, 60, 39}, 9,
std::vector<uint8_t>{0x00, 0x00, 0x00, 0x00, 0xFF, 0xFF,
0xFF, 0xFF, 0x00}));
size_t num_samples, expected_num_digital_bytes;
std::vector<uint8_t> expected_digital_part;
std::vector<int> bitlist;
std::tie(num_samples, bitlist, expected_num_digital_bytes,
expected_digital_part) = parameters;
generaldata->SetCtbDbitList(bitlist);
generaldata->SetCtbDbitReorder(true);
set_num_samples(num_samples);
set_data(0b01010101);
size_t expected_size =
num_analog_bytes + num_transceiver_bytes + expected_num_digital_bytes;
// create expected data
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes);
memcpy(expected_data + num_analog_bytes, expected_digital_part.data(),
expected_digital_part.size());
memset(expected_data + expected_num_digital_bytes + num_analog_bytes,
dummy_value, num_transceiver_bytes);
size_t size = get_size();
dataprocessor->ArrangeDbitData(size, data);
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
TEST_CASE_METHOD(DataProcessorTestFixture,
"Arrange bitlist and remove trailing bits",
"[dataprocessor][retrievebitlist]") {
size_t num_random_offset_bytes = 3;
std::vector<int> bitlist{1, 4, 5};
set_random_offset_bytes(num_random_offset_bytes);
set_data();
generaldata->SetCtbDbitList(bitlist);
generaldata->SetCtbDbitReorder(false);
generaldata->SetCtbDbitOffset(num_random_offset_bytes);
std::vector<uint8_t> expected_digital_part{0b00000111};
const size_t expected_num_digital_bytes = 5;
size_t expected_size =
num_analog_bytes + num_transceiver_bytes + expected_num_digital_bytes;
// create expected data
char *expected_data = new char[expected_size];
memset(expected_data, dummy_value, num_analog_bytes);
for (size_t sample = 0; sample < num_samples; ++sample) {
memcpy(expected_data + num_analog_bytes +
expected_digital_part.size() * sample,
expected_digital_part.data(), expected_digital_part.size());
}
memset(expected_data + expected_num_digital_bytes + num_analog_bytes,
dummy_value, num_transceiver_bytes);
size_t size = get_size();
dataprocessor->ArrangeDbitData(size, data);
CHECK(size == expected_size);
CHECK(memcmp(data, expected_data, expected_size) == 0);
delete[] expected_data;
}
} // namespace sls