Commit 854e0124 authored by Volker Friese's avatar Volker Friese
Browse files

Introduce unit tests for CbmDigiEvent and CbmDigiTimeslice. Refs #2261.

parent c2c404b4
......@@ -9,6 +9,7 @@
#include "CbmStsDigi.h"
#include <boost/serialization/access.hpp>
#include <boost/serialization/vector.hpp>
#include <vector>
......
......@@ -20,17 +20,22 @@ EndMacro(CreateGTestExeAndAddTest)
FIND_PACKAGE(Threads REQUIRED)
add_subdirectory(global)
add_subdirectory(psd)
add_subdirectory(trd)
Set(INCLUDE_DIRECTORIES
${CBMBASE_DIR}
${CBMDATA_DIR}
${CBMDATA_DIR}/base
${CBMDATA_DIR}/global
${CBMDATA_DIR}/sts
${CBMDATA_DIR}/tof
${CBMDATA_DIR}/rich
${CBMDATA_DIR}/much
${CBMDATA_DIR}/test/sts
${CBMDATA_DIR}/test/global
${CMAKE_SOURCE_DIR}/external/ipc/ipc/lib/fles_ipc
${BASE_INCLUDE_DIRECTORIES}
${GTEST_INCLUDE_DIR}
)
......@@ -55,6 +60,7 @@ Set(DEPENDENCIES
GeoBase
MbsAPI
${CMAKE_THREAD_LIBS_INIT}
boost_serialization
)
set(SPECIAL_DEPENDENCIES
......@@ -77,6 +83,8 @@ if (CMAKE_SYSTEM_NAME MATCHES Linux)
set(DEPENDENCIES ${DEPENDENCIES} rt)
endif()
Set(CbmLinkSources
${CBMDATA_DIR}/CbmLink.cxx
_GTestCbmLink.cxx
......@@ -159,13 +167,6 @@ Set(CbmAddressSources
CreateGTestExeAndAddTest(_GTestCbmAddress "${INCLUDE_DIRECTORIES}" "${LINK_DIRECTORIES}"
"${CbmAddressSources}" "${DEPENDENCIES}" "_GTestCbmErrorMessage")
Set(CbmVertexSources
${CBMDATA_DIR}/global/CbmVertex.cxx
global/_GTestCbmVertex.cxx
)
CreateGTestExeAndAddTest(_GTestCbmVertex "${INCLUDE_DIRECTORIES}" "${LINK_DIRECTORIES}"
"${CbmVertexSources}" "${DEPENDENCIES}" "_GTestCbmAddress")
#Set(CbmModuleListSources
......
# --- CMake steering file for core/data/test/global
# --- V. Friese, 2021
Set(INCLUDE_DIRECTORIES
${CBMDATA_DIR}
${CBMDATA_DIR}/base
${CBMDATA_DIR}/global
${CBMDATA_DIR}/sts
${CBMDATA_DIR}/test/sts
${CMAKE_SOURCE_DIR}/external/ipc/ipc/lib/fles_ipc
${BASE_INCLUDE_DIRECTORIES}
${GTEST_INCLUDE_DIR}
)
MESSAGE("FAIRROOT_LIBRARY_DIR: ${FAIRROOT_LIBRARY_DIR}")
Set(LINK_DIRECTORIES
${ROOT_LIBRARY_DIR}
${FAIRROOT_LIBRARY_DIR}
${SIMPATH}/lib
)
Set(DEPENDENCIES
Core
Matrix
Physics
${GTEST_BOTH_LIBRARIES}
FairTools
CbmData
boost_serialization
${CMAKE_THREAD_LIBS_INIT}
)
If(FAIRLOGGER_FOUND)
set(DEPENDENCIES
${DEPENDENCIES}
FairLogger
)
EndIf()
if (CMAKE_SYSTEM_NAME MATCHES Linux)
set(DEPENDENCIES ${DEPENDENCIES} rt)
endif()
# --- Test CbmDigiEvent
Set(CbmDigiEventSources
${CBMDATA_DIR}/global/CbmDigiEvent.h
_GTestCbmDigiEvent.cxx
)
CreateGTestExeAndAddTest(_GTestCbmDigiEvent "${INCLUDE_DIRECTORIES}" "${LINK_DIRECTORIES}"
"${CbmDigiEventSources}" "${DEPENDENCIES}" "")
# --- Test CbmDigiTimeslice
Set(CbmDigiTimesliceSources
${CBMDATA_DIR}/global/CbmDigiTimeslice.h
_GTestCbmDigiTimeslice.cxx
)
CreateGTestExeAndAddTest(_GTestCbmDigiTimeslice "${INCLUDE_DIRECTORIES}" "${LINK_DIRECTORIES}"
"${CbmDigiTimesliceSources}" "${DEPENDENCIES}" "_GTestCbmDigiEvent")
# --- Test CbmVertex
Set(CbmVertexSources
${CBMDATA_DIR}/global/CbmVertex.cxx
_GTestCbmVertex.cxx
)
CreateGTestExeAndAddTest(_GTestCbmVertex "${INCLUDE_DIRECTORIES}" "${LINK_DIRECTORIES}"
"${CbmVertexSources}" "${DEPENDENCIES}" "_GTestCbmDigiTimeslice")
/* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
SPDX-License-Identifier: GPL-3.0-only
Authors: Volker Friese [committer] */
#include "CbmDigiEvent.h"
#include "CbmStsDigi.h"
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <array>
#include <random>
#include "compareStsDigi.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
TEST(_GTestCbmDigiEvent, CheckSerialization)
{
// Limits for STS digi member values
int32_t maxUnit = (1 << 6) - 1; // 6 bits for address - unit
int32_t maxLadd = (1 << 5) - 1; // 5 bits for address - ladder
int32_t maxHlad = (1 << 1) - 1; // 1 bit for address - halfladder
int32_t maxModu = (1 << 5) - 1; // 5 bits for address - module
uint16_t maxChan = (1 << 11) - 1; // 11 bits for channel number
int32_t maxTime = (1 << 30) - 1; // 31 bits for time
uint16_t maxChrg = (1 << 5) - 1; // 5 bits for ADC
// Random generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<int32_t> rEvtNumber(1, 1000); // Event number
std::uniform_real_distribution<double> rEvtTime(0., 1.e10); // Event time
std::uniform_int_distribution<int32_t> rNumDigis(1, 10); // Number of digis
std::uniform_int_distribution<int32_t> rUnit(0, maxUnit); // Digi address: unit
std::uniform_int_distribution<int32_t> rLadd(0, maxLadd); // Digi address: ladder
std::uniform_int_distribution<int32_t> rHlad(0, maxHlad); // Digi address: halfladder
std::uniform_int_distribution<int32_t> rModu(0, maxModu); // Digi address: module
std::uniform_int_distribution<uint16_t> rChan(0, maxChan); // Digi channel
std::uniform_int_distribution<int32_t> rTime(0, maxTime); // Digi time
std::uniform_int_distribution<uint16_t> rChrg(0, maxChrg); // Digi charge
// Create DigiEvent
CbmDigiEvent eventOut;
int32_t evtNumber = rEvtNumber(gen);
double evtTime = rEvtTime(gen);
eventOut.fNumber = evtNumber;
eventOut.fTime = evtTime;
// Create a number of STS digis and add them to the event
int32_t numDigis = rNumDigis(gen);
std::vector<int32_t> vAddr(numDigis);
std::vector<uint16_t> vChan(numDigis);
std::vector<int32_t> vTime(numDigis);
std::vector<uint16_t> vChrg(numDigis);
for (int32_t iDigi = 0; iDigi < numDigis; iDigi++) {
int32_t unit = rUnit(gen);
int32_t ladd = rLadd(gen);
int32_t hlad = rHlad(gen);
int32_t modu = rModu(gen);
int32_t addr = CbmStsAddress::GetAddress(unit, ladd, hlad, modu, 0, 0);
uint16_t chan = rChan(gen);
int32_t time = rTime(gen);
uint16_t chrg = rChrg(gen);
vAddr[iDigi] = addr;
vChan[iDigi] = chan;
vTime[iDigi] = time;
vChrg[iDigi] = chrg;
eventOut.fData.fSts.fDigis.emplace_back(addr, chan, time, chrg);
}
// Serialize event to string
std::stringstream s;
boost::archive::binary_oarchive outArchive(s);
outArchive << eventOut;
// Deserialize event from string
CbmDigiEvent eventIn;
boost::archive::binary_iarchive inArchive(s);
inArchive >> eventIn;
// Compare output and input
EXPECT_EQ(eventIn.fNumber, evtNumber);
EXPECT_FLOAT_EQ(eventIn.fTime, evtTime);
EXPECT_EQ(eventIn.fData.fSts.fDigis.size(), numDigis);
for (int32_t iDigi = 0; iDigi < numDigis; iDigi++) {
compareStsDigiDataMembers(eventIn.fData.fSts.fDigis[iDigi], vAddr[iDigi], vChrg[iDigi], vChan[iDigi],
ECbmModuleId::kSts, vTime[iDigi]);
}
}
/* Copyright (C) 2021 GSI Helmholtzzentrum fuer Schwerionenforschung, Darmstadt
SPDX-License-Identifier: GPL-3.0-only
Authors: Volker Friese [committer] */
#include "CbmDigiTimeslice.h"
#include "CbmStsDigi.h"
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include <array>
#include <random>
#include "compareStsDigi.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
TEST(_GTestCbmDigiTimeslice, CheckSerialization)
{
// Limits for STS digi member values
int32_t maxUnit = (1 << 6) - 1; // 6 bits for address - unit
int32_t maxLadd = (1 << 5) - 1; // 5 bits for address - ladder
int32_t maxHlad = (1 << 1) - 1; // 1 bit for address - halfladder
int32_t maxModu = (1 << 5) - 1; // 5 bits for address - module
uint16_t maxChan = (1 << 11) - 1; // 11 bits for channel number
int32_t maxTime = (1 << 30) - 1; // 31 bits for time
uint16_t maxChrg = (1 << 5) - 1; // 5 bits for ADC
// Random generator
std::random_device rd;
std::mt19937 gen(rd());
std::uniform_int_distribution<uint64_t> rTsIndex(0); // Timeslice index
std::uniform_int_distribution<uint64_t> rTsPos(0); // Timeslice offset
std::uniform_int_distribution<uint32_t> rTsNumMs(0); // Number of microslices
std::uniform_int_distribution<uint32_t> rTsNumCp(0); // Number of components
std::uniform_int_distribution<int32_t> rNumDigis(1, 10); // Number of digis
std::uniform_int_distribution<int32_t> rUnit(0, maxUnit); // Digi address: unit
std::uniform_int_distribution<int32_t> rLadd(0, maxLadd); // Digi address: ladder
std::uniform_int_distribution<int32_t> rHlad(0, maxHlad); // Digi address: halfladder
std::uniform_int_distribution<int32_t> rModu(0, maxModu); // Digi address: module
std::uniform_int_distribution<uint16_t> rChan(0, maxChan); // Digi channel
std::uniform_int_distribution<int32_t> rTime(0, maxTime); // Digi time
std::uniform_int_distribution<uint16_t> rChrg(0, maxChrg); // Digi charge
// Create DigiTimeslice
CbmDigiTimeslice tsWrite;
uint64_t tsIndex = rTsIndex(gen);
uint64_t tsPos = rTsPos(gen);
uint32_t tsNumMs = rTsNumMs(gen);
uint32_t tsNumCp = rTsNumCp(gen);
tsWrite.fDesc.index = tsIndex;
tsWrite.fDesc.ts_pos = tsPos;
tsWrite.fDesc.num_core_microslices = tsNumMs;
tsWrite.fDesc.num_components = tsNumCp;
// Create a number of STS digis and add them to the event
int32_t numDigis = rNumDigis(gen);
numDigis = 1;
std::vector<int32_t> vAddr(numDigis);
std::vector<uint16_t> vChan(numDigis);
std::vector<int32_t> vTime(numDigis);
std::vector<uint16_t> vChrg(numDigis);
for (int32_t iDigi = 0; iDigi < numDigis; iDigi++) {
int32_t unit = rUnit(gen);
int32_t ladd = rLadd(gen);
int32_t hlad = rHlad(gen);
int32_t modu = rModu(gen);
int32_t addr = CbmStsAddress::GetAddress(unit, ladd, hlad, modu, 0, 0);
uint16_t chan = rChan(gen);
int32_t time = rTime(gen);
uint16_t chrg = rChrg(gen);
vAddr[iDigi] = addr;
vChan[iDigi] = chan;
vTime[iDigi] = time;
vChrg[iDigi] = chrg;
tsWrite.fData.fSts.fDigis.emplace_back(addr, chan, time, chrg);
}
// Serialize timeslice to string
std::stringstream s;
boost::archive::binary_oarchive outArchive(s);
outArchive << tsWrite;
// Deserialize event from string
CbmDigiTimeslice tsRead;
boost::archive::binary_iarchive inArchive(s);
inArchive >> tsRead;
// Compare output and input
EXPECT_EQ(tsRead.fDesc.index, tsIndex);
EXPECT_EQ(tsRead.fDesc.ts_pos, tsPos);
EXPECT_EQ(tsRead.fDesc.num_core_microslices, tsNumMs);
EXPECT_EQ(tsRead.fDesc.num_components, tsNumCp);
for (int32_t iDigi = 0; iDigi < numDigis; iDigi++) {
compareStsDigiDataMembers(tsRead.fData.fSts.fDigis[iDigi], vAddr[iDigi], vChrg[iDigi], vChan[iDigi],
ECbmModuleId::kSts, vTime[iDigi]);
}
}
......@@ -5,12 +5,16 @@
#include "CbmStsAddress.h"
#include "CbmStsDigi.h"
#include <boost/archive/binary_iarchive.hpp>
#include <boost/archive/binary_oarchive.hpp>
#include "compareStsDigi.h"
#include "gtest/gtest-spi.h"
#include "gtest/gtest.h"
static const Int_t kTestAddress = CbmStsAddress::GetAddress(5, 6, 1, 8, 0, 0, 1);
TEST(_GTestCbmStsDigi, CheckDefaultConstructor)
{
// Create object
......@@ -143,3 +147,15 @@ TEST(_GTestCbmStsDigi, CheckGetClassName)
EXPECT_STREQ("CbmStsDigi", test.GetClassName());
}
TEST(_GTestCbmStsDigi, CheckSerialization)
{
CbmStsDigi write(kTestAddress, 42, 987654321, 23);
std::stringstream s;
boost::archive::binary_oarchive outArchive(s);
outArchive << write;
CbmStsDigi read;
boost::archive::binary_iarchive inArchive(s);
inArchive >> read;
compareStsDigiDataMembers(read, kTestAddress, 23, 42, ECbmModuleId::kSts, 987654321);
}
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment