Dev/define cmd (#1312)
All checks were successful
Build on local RHEL9 / build (push) Successful in 1m25s
Build on RHEL9 / build (push) Successful in 3m15s
Build on local RHEL8 / build (push) Successful in 3m31s
Build on RHEL8 / build (push) Successful in 4m44s

* basic ctb config api for register and bit names

* tests for define and definelist pass. yet to implement using them for reg, setbit, clearbit and getbit

* improved autocomplete for getbit,setbit, clearbit

* validate autocomplete

* definelist has no put

* updating help

* converting char array+int in runtimeerror compiles but throws at runtime.Fixed.Tested for it. Also check if string or int before using getregisterdefinitonbyvalue to see if it threw to call the other function. because both of it can throw and we should differentiate the issues for both

* removed std::vector<std::pair<string,int> to std::map<string, int> for defiitions list

* Dev/define cmd tie bit to reg (#1328)

* strong type

* moved everythign to bit_utils class

* pybindings

* added tests for python

* removed duplicates

* removed bit names in reg

* changed BitPosition to BitAddress

* Using define reg/bit from python (#1344)

* define_bit, define_addr in python. 
* setBit/clearBit takes int or addr

* added example using bits

* split define into 2 commands define_reg and define_bit, definelist into 2: definelist_reg and definelist_bit

* allow string for register and bit names in c++ api

* refactor from github comments

* naming refactoring (getRegisterDefnition to retunr name and address specifically

* added marker for 8 cmd tests connected to define, changed macro to static constexpr

* changed bitPosition from int to uint32_t

* got rid of setbitposition and setaddress, instead overloaded constructor to take in strings so that the conversion from string to bit address members, takes place within the class for easy maintainance in case type changes

* Removing implicit conversions:
RegisterAddresss and RegisterValue: Removed the implicit conversions.
RegisterAddress: Changed member name from address_ to value_ and method as well to value().
RegisterValue: Also added | operator to be able to concatenate with uint32_t. Same in python bindings (but could not find the tests to modify

* Allowed concatenation with other RegisterValue, made them all constexpr

* fix a ctbConfig test

* Maponstack works with integration tests, but need unit tests

* tests on mapstack

* fixed ctb tests and FixedString being initialized with gibberish

* removing parsing from string inside the class RegisterAddress, BitAddress and RegisterValue

* updated python bindings

* fixed bit utils test

* renaming getRegisterDefintiionAddress/Name=>getRegisterAddress/Name and similary for getBitDefinitionAddress/Name

* updated python bindings

* fix tests (format)

* a few python tests added and python bindings corrected

* replaceing str with __str__ for bit.cpp

* repr reimplemented for bit.cpp

* removed make with registerAddress etc

* starting server for tests per session and nor module

* killprocess throws if no process found-> github runs fails, changed to pkill and not throw

* clean shm shouldnt raise, in ci binary not found

* ignoring these tests for CI, which fail on CI because simulators are not generated in CI. This is in another PR, where it should work

---------

Co-authored-by: Erik Fröjdh <erik.frojdh@gmail.com>
Co-authored-by: froejdh_e <erik.frojdh@psi.ch>
This commit is contained in:
2026-01-05 15:10:46 +01:00
committed by GitHub
parent 7d1d5e9809
commit 3dd07bf2be
55 changed files with 3924 additions and 1098 deletions

View File

@@ -1030,37 +1030,41 @@ TEST_CASE("v_abcd", "[.cmdcall]") {
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
std::vector<std::string> cmds{"v_a", "v_b", "v_c", "v_d"};
std::vector<defs::dacIndex> indices{defs::V_POWER_A, defs::V_POWER_B,
defs::V_POWER_C, defs::V_POWER_D};
if (det.isVirtualDetectorServer().tsquash("Inconsistent virtual servers")) {
cmds.push_back("v_io");
indices.push_back(defs::V_POWER_IO);
}
for (size_t i = 0; i < cmds.size(); ++i) {
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_val = det.getPower(indices[i]);
{
std::ostringstream oss;
caller.call(cmds[i], {"0"}, -1, PUT, oss);
REQUIRE(oss.str() == cmds[i] + " 0\n");
}
{
std::ostringstream oss1, oss2;
caller.call(cmds[i], {"1200"}, -1, PUT, oss1);
REQUIRE(oss1.str() == cmds[i] + " 1200\n");
caller.call(cmds[i], {}, -1, GET, oss2);
REQUIRE(oss2.str() == cmds[i] + " 1200\n");
}
for (int i = 0; i != det.size(); ++i) {
det.setPower(indices[i], prev_val[i], {i});
}
std::vector<std::string> cmds{"v_a", "v_b", "v_c", "v_d", "v_io"};
std::vector<defs::dacIndex> indices{defs::V_POWER_A, defs::V_POWER_B,
defs::V_POWER_C, defs::V_POWER_D,
defs::V_POWER_IO};
} else {
REQUIRE_THROWS(caller.call(cmds[i], {}, -1, GET));
for (size_t i = 0; i < cmds.size(); ++i) {
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_val = det.getPower(indices[i]);
{
std::ostringstream oss;
caller.call(cmds[i], {"0"}, -1, PUT, oss);
REQUIRE(oss.str() == cmds[i] + " 0\n");
}
{
std::ostringstream oss1, oss2;
caller.call(cmds[i], {"1200"}, -1, PUT, oss1);
REQUIRE(oss1.str() == cmds[i] + " 1200\n");
caller.call(cmds[i], {}, -1, GET, oss2);
REQUIRE(oss2.str() == cmds[i] + " 1200\n");
}
for (int i = 0; i != det.size(); ++i) {
if (det_type == defs::XILINX_CHIPTESTBOARD &&
prev_val[i] == -100) {
prev_val[i] = 0;
continue;
}
det.setPower(indices[i], prev_val[i], {i});
}
} else {
REQUIRE_THROWS(caller.call(cmds[i], {}, -1, GET));
}
}
}
}
@@ -1330,4 +1334,366 @@ TEST_CASE("led", "[.cmdcall]") {
}
}
TEST_CASE("define_reg", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_reg_defines = det.getRegisterDefinitions();
auto prev_bit_defines = det.getBitDefinitions();
det.clearRegisterDefinitions();
det.clearBitDefinitions();
{
// invalid puts
// missing arg
REQUIRE_THROWS(caller.call("define_reg", {}, -1, GET));
// missing arg
REQUIRE_THROWS(caller.call("define_reg", {"TEST_REG"}, -1, PUT));
// invalid module id
REQUIRE_THROWS(
caller.call("define_reg", {"TEST_REG", "0x201"}, 0, PUT));
// valid put
REQUIRE_NOTHROW(
caller.call("define_reg", {"TEST_REG", "0x200"}, -1, PUT));
// modify reg
REQUIRE_NOTHROW(
caller.call("define_reg", {"TEST_REG", "0x201"}, -1, PUT));
REQUIRE_NOTHROW(
caller.call("define_reg", {"TEST_REG2", "0x202"}, -1, PUT));
// invalid puts
// existing reg addr
REQUIRE_THROWS(
caller.call("define_reg", {"TEST_REG3", "0x201"}, -1, PUT));
// valid gets
{
// get by name
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("define_reg", {"TEST_REG"}, -1, GET, oss));
REQUIRE(oss.str() == "define_reg 0x201\n");
}
{
// get by addr
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("define_reg", {"0x201"}, -1, GET, oss));
REQUIRE(oss.str() == "define_reg TEST_REG\n");
}
// invalid gets
// doesnt exist
REQUIRE_THROWS(caller.call("define_reg", {"TEST_REG3"}, -1, GET));
REQUIRE_THROWS(caller.call("define_reg", {"0x203"}, -1, GET));
// ensure correct exception message
try {
caller.call("define_reg", {"0x203"}, -1, GET);
} catch (const std::exception &e) {
REQUIRE(std::string(e.what()).find(
"No entry found for value") != std::string::npos);
}
}
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinitions(prev_reg_defines);
det.setBitDefinitions(prev_bit_defines);
} else {
REQUIRE_THROWS(
caller.call("define_reg", {"TEST_REG", "0x200"}, -1, PUT));
REQUIRE_THROWS(caller.call("define_reg", {"TEST_REG"}, -1, GET));
}
}
TEST_CASE("define_bit", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_reg_defines = det.getRegisterDefinitions();
auto prev_bit_defines = det.getBitDefinitions();
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinition("TEST_REG", RegisterAddress(0x201));
det.setRegisterDefinition("TEST_REG2", RegisterAddress(0x202));
{
// invalid puts
// skipped register
REQUIRE_THROWS(
caller.call("define_bit", {"TEST_BIT", "1"}, -1, PUT));
// named register doesnt exist
REQUIRE_THROWS(caller.call(
"define_bit", {"TEST_BIT", "RANDOM_REG", "1"}, -1, PUT));
// invalid bit position
REQUIRE_THROWS(
caller.call("define", {"TEST_BIT", "TEST_REG", "32"}, -1, PUT));
// valid puts
REQUIRE_NOTHROW(caller.call(
"define_bit", {"TEST_BIT", "TEST_REG2", "1"}, -1, PUT));
// modify reg
REQUIRE_NOTHROW(caller.call(
"define_bit", {"TEST_BIT", "TEST_REG", "1"}, -1, PUT));
// modify position
REQUIRE_NOTHROW(caller.call(
"define_bit", {"TEST_BIT", "TEST_REG", "2"}, -1, PUT));
// another bit to same reg
REQUIRE_NOTHROW(caller.call(
"define_bit", {"TEST_BIT2", "TEST_REG", "4"}, -1, PUT));
// bit to a different reg
REQUIRE_NOTHROW(caller.call(
"define_bit", {"TEST_BIT3", "TEST_REG2", "3"}, -1, PUT));
// valid gets
{
// get by name
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("define_bit", {"TEST_BIT"}, -1, GET, oss));
REQUIRE(oss.str() == "define_bit [TEST_REG, 2]\n");
}
{
// get by addr+pos name
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("define_bit", {"TEST_REG", "2"}, -1, GET, oss));
REQUIRE(oss.str() == "define_bit TEST_BIT\n");
}
{
// get by addr val + pos
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("define_bit", {"0x201", "2"}, -1, GET, oss));
REQUIRE(oss.str() == "define_bit TEST_BIT\n");
}
// invalid gets
// bit doesnt exist
REQUIRE_THROWS(
caller.call("define_bit", {"TEST_REG", "3"}, -1, GET));
// addr doesnt exist
REQUIRE_THROWS(
caller.call("define_bit", {"TEST_REG3", "2"}, -1, GET));
// ensure correct exception message
try {
caller.call("define_bit", {"TEST_REG", "3"}, -1, GET);
} catch (const std::exception &e) {
REQUIRE(std::string(e.what()).find(
"No entry found for value") != std::string::npos);
}
}
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinitions(prev_reg_defines);
det.setBitDefinitions(prev_bit_defines);
} else {
REQUIRE_THROWS(
caller.call("define_bit", {"TEST_BIT", "0x200", "2"}, -1, PUT));
REQUIRE_THROWS(caller.call("define_bit", {"0x200", "2"}, -1, GET));
}
}
TEST_CASE("using define for reg, setbit, getbit and clearbit",
"[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
if (det.isVirtualDetectorServer().tsquash(
"inconsistent virtual values")) {
auto prev_reg_defines = det.getRegisterDefinitions();
auto prev_bit_defines = det.getBitDefinitions();
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinition("TEST_REG", RegisterAddress(0x201));
det.setRegisterDefinition("TEST_REG2", RegisterAddress(0x202));
det.setBitDefinition("TEST_BIT",
BitAddress(RegisterAddress(0x201), 2));
det.setBitDefinition("TEST_BIT2",
BitAddress(RegisterAddress(0x201), 4));
det.setBitDefinition("TEST_BIT3",
BitAddress(RegisterAddress(0x202), 3));
auto prev_val_addr = det.readRegister(RegisterAddress(0x201));
auto prev_val_addr2 = det.readRegister(RegisterAddress(0x202));
// invalid puts
// doesnt exist addr
REQUIRE_THROWS(
caller.call("reg", {"RANDOM_REG", "0xf00"}, -1, PUT));
REQUIRE_THROWS(
caller.call("clearbit", {"RANDOM_REG", "TEST_BIT"}, -1, PUT));
REQUIRE_THROWS(
caller.call("setbit", {"RANDOM_REG", "TEST_BIT"}, -1, PUT));
REQUIRE_THROWS(
caller.call("getbit", {"RANDOM_REG", "TEST_BIT"}, -1, GET));
// using bit name for reg (only hardcoded values allowed)
REQUIRE_THROWS(
caller.call("reg", {"TEST_REG", "TEST_BIT"}, -1, PUT));
// using bit name and reg (only bit names or both reg and bit
// hardcoded allowed)
REQUIRE_THROWS(
caller.call("clearbit", {"TEST_REG", "TEST_BIT"}, -1, PUT));
REQUIRE_THROWS(
caller.call("setbit", {"TEST_REG", "TEST_BIT"}, -1, PUT));
REQUIRE_THROWS(
caller.call("getbit", {"TEST_REG", "TEST_BIT"}, -1, GET));
// valid puts and gets
{
// reg hard coded value of 0
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("reg", {"TEST_REG", "0x0"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("reg", {"TEST_REG"}, -1, GET, oss));
REQUIRE(oss.str() == "reg 0x0\n");
}
{
// reg hard coded value
std::ostringstream oss;
REQUIRE_NOTHROW(
caller.call("reg", {"TEST_REG", "0x10"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("reg", {"TEST_REG"}, -1, GET, oss));
REQUIRE(oss.str() == "reg 0x10\n");
}
{
// set bit
std::ostringstream oss;
REQUIRE_NOTHROW(caller.call("setbit", {"TEST_BIT"}, -1, PUT));
REQUIRE_NOTHROW(
caller.call("setbit", {"TEST_REG", "2"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("reg", {"TEST_REG"}, -1, GET, oss));
REQUIRE(oss.str() == "reg 0x14\n");
}
{
// get bit
std::ostringstream oss, oss2;
REQUIRE_NOTHROW(
caller.call("getbit", {"TEST_REG", "2"}, -1, GET, oss));
REQUIRE(oss.str() == "getbit 1\n");
REQUIRE_NOTHROW(
caller.call("getbit", {"TEST_BIT"}, -1, GET, oss2));
REQUIRE(oss2.str() == "getbit 1\n");
}
{
// clear bit
std::ostringstream oss;
REQUIRE_NOTHROW(caller.call("clearbit", {"TEST_BIT"}, -1, PUT));
REQUIRE_NOTHROW(
caller.call("clearbit", {"TEST_REG", "2"}, -1, PUT));
REQUIRE_NOTHROW(caller.call("reg", {"TEST_REG"}, -1, GET, oss));
REQUIRE(oss.str() == "reg 0x10\n");
}
for (int i = 0; i != det.size(); ++i) {
det.writeRegister(RegisterAddress(0x201),
RegisterValue(prev_val_addr[i]), false, {i});
det.writeRegister(RegisterAddress(0x202),
RegisterValue(prev_val_addr2[i]), false, {i});
}
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinitions(prev_reg_defines);
det.setBitDefinitions(prev_bit_defines);
}
} else {
REQUIRE_THROWS(caller.call("reg", {"TEST_REG", "0x200"}, -1, PUT));
REQUIRE_THROWS(caller.call("reg", {"TEST_REG"}, -1, GET));
}
}
TEST_CASE("definelist_reg", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_reg_defines = det.getRegisterDefinitions();
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinition("TEST_REG", RegisterAddress(0x201));
det.setRegisterDefinition("TEST_REG2", RegisterAddress(0x202));
// invalid
// cannot put
REQUIRE_THROWS(
caller.call("definelist_reg", {"TEST_REG", "0x201"}, -1, PUT));
// too many args
REQUIRE_THROWS(caller.call("definelist_reg", {"TEST_MACRO"}, -1, GET));
// valid
REQUIRE_NOTHROW(caller.call("definelist_reg", {}, -1, GET));
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinitions(prev_reg_defines);
} else {
REQUIRE_THROWS(caller.call("definelist_reg", {}, -1, GET));
}
}
TEST_CASE("definelist_bit", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
if (det_type == defs::CHIPTESTBOARD ||
det_type == defs::XILINX_CHIPTESTBOARD) {
auto prev_reg_defines = det.getRegisterDefinitions();
auto prev_bit_defines = det.getBitDefinitions();
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinition("TEST_REG", RegisterAddress(0x201));
det.setRegisterDefinition("TEST_REG2", RegisterAddress(0x202));
det.setBitDefinition("TEST_BIT", BitAddress(RegisterAddress(0x201), 2));
det.setBitDefinition("TEST_BIT2",
BitAddress(RegisterAddress(0x201), 4));
det.setBitDefinition("TEST_BIT3",
BitAddress(RegisterAddress(0x202), 3));
// invalid
// cannot put
REQUIRE_THROWS(
caller.call("definelist_bit", {"TEST_BIT", "0x201", "2"}, -1, PUT));
// too many args
REQUIRE_THROWS(caller.call("definelist_bit", {"TEST_BIT"}, -1, GET));
// valid
REQUIRE_NOTHROW(caller.call("definelist_bit", {}, -1, GET));
det.clearRegisterDefinitions();
det.clearBitDefinitions();
det.setRegisterDefinitions(prev_reg_defines);
det.setBitDefinitions(prev_bit_defines);
} else {
REQUIRE_THROWS(caller.call("definelist_bit", {}, -1, GET));
}
}
} // namespace sls

View File

@@ -3267,7 +3267,7 @@ TEST_CASE("update", "[.cmdcall]") {
}
}
TEST_CASE("reg", "[.cmdcall]") {
TEST_CASE("reg", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
@@ -3284,14 +3284,14 @@ TEST_CASE("reg", "[.cmdcall]") {
{
std::ostringstream oss1, oss2;
caller.call("reg", {saddr, "0x6", "--validate"}, -1, PUT, oss1);
REQUIRE(oss1.str() == "reg [" + saddr + ", 0x6]\n");
REQUIRE(oss1.str() == "reg " + saddr + " 0x6\n");
caller.call("reg", {saddr}, -1, GET, oss2);
REQUIRE(oss2.str() == "reg 0x6\n");
}
{
std::ostringstream oss1, oss2;
caller.call("reg", {saddr, "0x5"}, -1, PUT, oss1);
REQUIRE(oss1.str() == "reg [" + saddr + ", 0x5]\n");
REQUIRE(oss1.str() == "reg " + saddr + " 0x5\n");
caller.call("reg", {saddr}, -1, GET, oss2);
REQUIRE(oss2.str() == "reg 0x5\n");
}
@@ -3326,7 +3326,7 @@ TEST_CASE("adcreg", "[.cmdcall]") {
}
}
TEST_CASE("setbit", "[.cmdcall]") {
TEST_CASE("setbit", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
@@ -3356,7 +3356,7 @@ TEST_CASE("setbit", "[.cmdcall]") {
}
}
TEST_CASE("clearbit", "[.cmdcall]") {
TEST_CASE("clearbit", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();
@@ -3386,7 +3386,7 @@ TEST_CASE("clearbit", "[.cmdcall]") {
}
}
TEST_CASE("getbit", "[.cmdcall]") {
TEST_CASE("getbit", "[.cmdcall][.definecmds]") {
Detector det;
Caller caller(&det);
auto det_type = det.getDetectorType().squash();

View File

@@ -6,13 +6,16 @@
#include "CtbConfig.h"
#include "SharedMemory.h"
#include <fstream>
#include <set>
namespace sls {
TEST_CASE("Default construction") {
static_assert(sizeof(CtbConfig) ==
(2 * sizeof(int) + (18 + 32 + 64 + 5 + 8) * 20),
"Size of CtbConfig does not match ");
/*TODO static_assert(sizeof(CtbConfig) ==
(2 * sizeof(int) + (18 + 32 + 64 + 5 + 8) * 32) +
(sizeof(size_t) * 2) + ((sizeof(uint32_t) + 32) * 64) +
((32 + sizeof(uint32_t) + sizeof(int)) * 64), "Size of CtbConfig does not
match "); //8952 (8696)*/
CtbConfig c;
auto dacnames = c.getDacNames();
@@ -46,6 +49,16 @@ TEST_CASE("Default construction") {
REQUIRE(sensenames[1] == "SLOWADC1");
REQUIRE(sensenames[2] == "SLOWADC2");
REQUIRE(sensenames[3] == "SLOWADC3");
auto regisernames = c.getRegisterNames();
REQUIRE(regisernames.size() == 0);
auto bitnames = c.getBitNames();
REQUIRE(bitnames.size() == 0);
REQUIRE(c.getRegisterNamesCount() == 0);
auto registerNames = c.getRegisterNames();
REQUIRE(registerNames.size() == 0);
REQUIRE(c.getBitNamesCount() == 0);
auto bitNames = c.getBitNames();
REQUIRE(bitNames.size() == 0);
}
TEST_CASE("Set and get a single dac name") {
@@ -85,4 +98,164 @@ TEST_CASE("Move CtbConfig ") {
REQUIRE(c2.getDacName(3) == "yetanothername");
}
TEST_CASE("Add or modify a register name", "[.reg]") {
CtbConfig c;
auto addr = RegisterAddress(100);
auto addr1 = RegisterAddress(300);
REQUIRE(c.getRegisterNamesCount() == 0);
REQUIRE_THROWS(
c.setRegisterName("reg1_with_a_really_long_name_to_crash", addr));
// add an entry
REQUIRE_NOTHROW(c.setRegisterName("reg1", addr));
REQUIRE(c.getRegisterName(addr) == "reg1");
REQUIRE(c.getRegisterAddress("reg1") == addr);
REQUIRE(c.getRegisterNamesCount() == 1);
// modify an entry
REQUIRE_NOTHROW(c.setRegisterName("reg1", addr1));
REQUIRE(c.getRegisterAddress("reg1") == addr1);
REQUIRE(c.getRegisterName(addr1) == "reg1");
// clear all entries
REQUIRE_NOTHROW(c.clearRegisterNames());
REQUIRE(c.getRegisterNamesCount() == 0);
REQUIRE_THROWS(c.getRegisterName(addr));
REQUIRE_THROWS(c.getRegisterAddress("reg1"));
REQUIRE_THROWS(c.getRegisterName(addr1));
}
TEST_CASE("Add a register list", "[.reg]") {
CtbConfig c;
REQUIRE(c.getRegisterNamesCount() == 0);
// add a list
std::map<std::string, RegisterAddress> list = {
{"reg1", RegisterAddress(0x100)},
{"reg2", RegisterAddress(0x200)},
{"reg3", RegisterAddress(0x300)}};
REQUIRE_NOTHROW(c.setRegisterNames(list));
REQUIRE(c.getRegisterNamesCount() == static_cast<int>(list.size()));
auto names = c.getRegisterNames();
REQUIRE(names.size() == 3);
// TODO std::set<RegisterAddress> seen_values;
for (const auto &[key, val] : list) {
// check for duplicate keys, and key-value match
REQUIRE(names.count(key) == 1);
REQUIRE(names.at(key) == val);
// check for duplicate values
// TODO REQUIRE(seen_values.count(val) == 0);
// TODO seen_values.insert(val);
}
// clear all entries
REQUIRE_NOTHROW(c.clearRegisterNames());
REQUIRE(c.getRegisterNames().size() == 0);
}
TEST_CASE("Finding a regiser name or address", "[.reg]") {
CtbConfig c;
RegisterAddress addr1(0x100);
RegisterAddress addr2(0x200);
RegisterAddress addr3(0x300);
// find nothing
REQUIRE(c.getRegisterNamesCount() == 0);
REQUIRE_THROWS(c.getRegisterName(addr1));
REQUIRE_THROWS(c.getRegisterAddress("reg1"));
std::map<std::string, RegisterAddress> list = {
{"reg1", addr1}, {"reg2", addr2}, {"reg3", addr3}};
REQUIRE_NOTHROW(c.setRegisterNames(list));
// find
REQUIRE(c.getRegisterName(addr1) == "reg1");
REQUIRE(c.getRegisterName(addr2) == "reg2");
REQUIRE(c.getRegisterAddress("reg3") == addr3);
}
TEST_CASE("Add or modify a bit name", "[.reg]") {
CtbConfig c;
RegisterAddress addr(0x100);
BitAddress pos(addr, 2);
BitAddress pos1(addr, 5);
REQUIRE(c.getBitNamesCount() == 0);
REQUIRE_THROWS(c.setBitName("bit1_with_a_really_long_name_to_crash",
BitAddress(addr, 100)));
REQUIRE_THROWS(c.setBitName("bit1", BitAddress(addr, 32)));
REQUIRE_THROWS(c.setBitName("bit1", BitAddress(addr, -1)));
// add an entry
REQUIRE_NOTHROW(c.setBitName("bit1", pos));
REQUIRE(c.getBitName(pos) == "bit1");
REQUIRE(c.getBitAddress("bit1") == pos);
REQUIRE(c.getBitNamesCount() == 1);
// modify an entry
REQUIRE_NOTHROW(c.setBitName("bit1", pos1));
REQUIRE(c.getBitAddress("bit1") == pos1);
REQUIRE(c.getBitName(pos1) == "bit1");
// clear all entries
REQUIRE_NOTHROW(c.clearBitNames());
REQUIRE(c.getBitNamesCount() == 0);
REQUIRE_THROWS(c.getBitName(pos));
REQUIRE_THROWS(c.getBitAddress("bit1"));
REQUIRE_THROWS(c.getBitName(pos1));
}
TEST_CASE("Add a bit list", "[.reg]") {
CtbConfig c;
REQUIRE(c.getBitNamesCount() == 0);
RegisterAddress addr(0x100);
BitAddress pos1(addr, 2);
BitAddress pos2(addr, 21);
BitAddress pos3(addr, 31);
// add a list
std::map<std::string, BitAddress> list = {
{"bit1", pos1}, {"bit2", pos2}, {"bit3", pos3}};
REQUIRE_NOTHROW(c.setBitNames(list));
REQUIRE(c.getBitNamesCount() == 3);
auto names = c.getBitNames();
REQUIRE(names.size() == 3);
// TODO std::set<BitAddress> seen_values;
for (const auto &[key, val] : list) {
// check for duplicate keys, and key-value match
REQUIRE(names.count(key) == 1);
REQUIRE(names.at(key) == val);
// check for duplicate values
// TODO REQUIRE(seen_values.count(val) == 0);
// TODO seen_values.insert(val);
}
// clear all entries
REQUIRE_NOTHROW(c.clearBitNames());
REQUIRE(c.getBitNames().size() == 0);
}
TEST_CASE("Finding a bit value", "[.reg]") {
CtbConfig c;
RegisterAddress addr(0x100);
BitAddress pos(addr, 2);
BitAddress pos1(addr, 21);
BitAddress pos2(addr, 31);
// find nothing
REQUIRE(c.getBitNamesCount() == 0);
REQUIRE_THROWS(c.getBitAddress("bit"));
std::map<std::string, BitAddress> list = {
{"bit0", pos}, {"bit1", pos1}, {"bit2", pos2}};
REQUIRE_NOTHROW(c.setBitNames(list));
// find
REQUIRE(c.getBitAddress("bit2") == pos2);
REQUIRE(c.getBitName(pos1) == "bit1");
}
} // namespace sls

View File

@@ -0,0 +1,133 @@
#include "MapOnStack.h"
#include "catch.hpp"
#include "sls/bit_utils.h"
#include <string>
namespace sls {
using Key = FixedString<32>;
TEST_CASE("MapOnStack with int values") {
MapOnStack<Key, int, 64, true> map;
map.addKey("bit0", 55);
map.addKeyOrSetValue("bit1", 66);
REQUIRE(map.size() == 2);
REQUIRE(map.capacity() == 64);
REQUIRE(map.empty() == false);
REQUIRE(map.getValue("bit0") == 55);
REQUIRE(map.getValue("bit1") == 66);
// Duplicate key throws
REQUIRE_THROWS(map.addKey("bit0", 77));
// Duplicate value throws
REQUIRE_THROWS(map.addKey("bit2", 66));
// modifies
map.addKeyOrSetValue("bit0", 77);
REQUIRE(map.getValue("bit0") == 77);
// containsKey / hasValue
REQUIRE(map.containsKey("bit1"));
REQUIRE(map.hasValue(77));
// Clear map
map.clear();
REQUIRE(map.empty());
REQUIRE(map.size() == 0);
REQUIRE(map.capacity() == 64);
}
TEST_CASE("MapOnStack with BitAddress values") {
MapOnStack<Key, BitAddress, 64, true> map;
RegisterAddress addr0(0x100);
RegisterAddress addr1(0x200);
BitAddress b0(addr0, 0);
BitAddress b1(addr0, 1);
BitAddress b2(addr1, 0);
map.addKey("bit0", b0);
map.addKeyOrSetValue("bit1", b1);
REQUIRE(map.size() == 2);
REQUIRE(map.capacity() == 64);
REQUIRE(map.empty() == false);
REQUIRE(map.getValue("bit0") == b0);
REQUIRE(map.getValue("bit1") == b1);
// Duplicate key throws
REQUIRE_THROWS(map.addKey("bit0", b2));
// Duplicate value throws
REQUIRE_THROWS(map.addKey("bit2", b1));
// modifies
map.addKeyOrSetValue("bit0", b2);
REQUIRE(map.getValue("bit0") == b2);
// containsKey / hasValue
REQUIRE(map.containsKey("bit1"));
REQUIRE(map.hasValue(b2));
// Clear map
map.clear();
REQUIRE(map.empty());
REQUIRE(map.size() == 0);
REQUIRE(map.capacity() == 64);
}
TEST_CASE("Set and get a single entry") {
MapOnStack<Key, RegisterAddress, 64, true> map;
RegisterAddress addr0(0x100);
map.addKey("bit0", addr0);
auto entries = map.getMap();
REQUIRE(entries.size() == 1);
REQUIRE(map.getValue("bit0") == addr0);
REQUIRE(entries["bit0"] == addr0);
}
TEST_CASE("Set a name that is too large throws") {
MapOnStack<Key, int, 64, true> map;
REQUIRE_THROWS(map.addKey("somestringthatisreallytolongforadatac", 12));
}
TEST_CASE("Length of dac name cannot be 0") {
MapOnStack<Key, int, 64, true> map;
REQUIRE_THROWS(map.addKey("", 12));
}
TEST_CASE("Copy a MapOnStack") {
MapOnStack<Key, int, 64, true> m1;
m1.addKey("key1", 12);
auto m2 = m1;
// change the first object
// to detecto shallow copy
m1.setValue("key1", 34);
REQUIRE(m2.getValue("key1") == 12);
}
TEST_CASE("Move MapOnStack ") {
MapOnStack<Key, int, 64, true> m1;
m1.addKey("key1", 12);
MapOnStack<Key, int, 64, true> m2(std::move(m1));
REQUIRE(m1.size() == 0);
REQUIRE(m2.size() == 1);
}
TEST_CASE("Set map from std::map") {
MapOnStack<Key, int, 64, true> map;
std::map<Key, int> m1 = {{"key1", 11}, {"key2", 22}, {"key3", 33}};
map.setMap(m1);
REQUIRE(map.size() == 3);
REQUIRE(map.getValue("key2") == 22);
auto retrieved_map = map.getMap();
REQUIRE(retrieved_map.size() == 3);
REQUIRE(retrieved_map["key3"] == 33);
} // namespace sls

View File

@@ -39,15 +39,13 @@ void freeShm(const int dindex, const int mIndex) {
constexpr int shm_id = 10;
//macOS does not expose shm in the filesystem
// macOS does not expose shm in the filesystem
#ifndef __APPLE__
const std::string file_path =
std::string("/dev/shm/slsDetectorPackage_detector_") +
std::to_string(shm_id);
TEST_CASE("Free obsolete (without isValid)", "[detector][shm]") {
// ensure its clean to start
@@ -106,8 +104,8 @@ TEST_CASE("Create SharedMemory read and write", "[detector][shm]") {
const char *env_p = std::getenv(SHM_ENV_NAME);
std::string env_name = env_p ? ("_" + std::string(env_p)) : "";
CHECK(shm.getName() == std::string(SHM_DETECTOR_PREFIX) +
std::to_string(shm_id) + env_name);
CHECK(shm.getName() ==
std::string(SHM_DETECTOR_PREFIX) + std::to_string(shm_id) + env_name);
shm()->x = 3;
shm()->y = 5.7;
strcpy_safe(shm()->mess, "Some string");
@@ -180,8 +178,8 @@ TEST_CASE("Move SharedMemory", "[detector][shm]") {
std::string env_name = env_p ? ("_" + std::string(env_p)) : "";
SharedMemory<Data> shm(shm_id, -1);
CHECK(shm.getName() == std::string(SHM_DETECTOR_PREFIX) +
std::to_string(shm_id) + env_name);
CHECK(shm.getName() ==
std::string(SHM_DETECTOR_PREFIX) + std::to_string(shm_id) + env_name);
shm.createSharedMemory();
shm()->x = 9;
@@ -191,8 +189,8 @@ TEST_CASE("Move SharedMemory", "[detector][shm]") {
CHECK(shm2()->x == 9);
REQUIRE_THROWS(
shm()); // trying to access should throw instead of returning a nullptr
CHECK(shm2.getName() == std::string(SHM_DETECTOR_PREFIX) +
std::to_string(shm_id) + env_name);
CHECK(shm2.getName() ==
std::string(SHM_DETECTOR_PREFIX) + std::to_string(shm_id) + env_name);
shm2.removeSharedMemory();
}
@@ -243,7 +241,8 @@ TEST_CASE("Create create a shared memory with a tag when SLSDETNAME is set") {
setenv(SHM_ENV_NAME, "myprefix", 1);
SharedMemory<Data> shm(0, -1, "ctbdacs");
REQUIRE(shm.getName() == std::string(SHM_DETECTOR_PREFIX) + "0_myprefix_ctbdacs");
REQUIRE(shm.getName() ==
std::string(SHM_DETECTOR_PREFIX) + "0_myprefix_ctbdacs");
// Clean up after us
if (old_slsdetname.empty())