From 25fe6ea5b2626380612aeb0db1a9fa71d3c53952 Mon Sep 17 00:00:00 2001 From: Simon Marchi Date: Wed, 20 Mar 2024 17:10:30 -0400 Subject: [PATCH] tests/lib/conds: store triggers in a vector An upcoming patch needs to dynamically create and append triggers to the trigger list. Make the container a vector. For simplicity, update `condMain` to take a reference to a vector of unique pointers of triggers (rather than inventing some abstraction). Change-Id: Ic5e2fe6da151434ec515c041d4e1096193d29221 Signed-off-by: Simon Marchi Reviewed-on: https://review.lttng.org/c/babeltrace/+/12115 Reviewed-by: Philippe Proulx Tested-by: jenkins --- tests/lib/conds/conds-triggers.cpp | 48 ++++++++++++++++-------------- tests/lib/conds/utils.cpp | 6 ++-- tests/lib/conds/utils.hpp | 9 ++++-- 3 files changed, 34 insertions(+), 29 deletions(-) diff --git a/tests/lib/conds/conds-triggers.cpp b/tests/lib/conds/conds-triggers.cpp index 1a463511..26603cdc 100644 --- a/tests/lib/conds/conds-triggers.cpp +++ b/tests/lib/conds/conds-triggers.cpp @@ -10,6 +10,7 @@ #include "cpp-common/bt2/graph.hpp" #include "cpp-common/bt2c/c-string-view.hpp" +#include "cpp-common/bt2s/make-unique.hpp" #include "utils.hpp" @@ -19,10 +20,12 @@ namespace { * Creates a simple condition trigger, calling `func`. */ template -CondTrigger *makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type, - const std::string& condId, const bt2c::CStringView nameSuffix = {}) +CondTrigger::UP makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type, + const std::string& condId, + const bt2c::CStringView nameSuffix = {}) { - return new SimpleCondTrigger {std::forward(func), type, condId, nameSuffix}; + return bt2s::make_unique(std::forward(func), type, condId, + nameSuffix); } using OnCompInitFunc = std::function; @@ -60,12 +63,12 @@ private: * Creates a condition trigger, calling `func` in a component * initialization context. */ -CondTrigger *makeRunInCompInitTrigger(OnCompInitFunc func, const CondTrigger::Type type, - const std::string& condId, - const bt2c::CStringView nameSuffix = {}) +CondTrigger::UP makeRunInCompInitTrigger(OnCompInitFunc func, const CondTrigger::Type type, + const std::string& condId, + const bt2c::CStringView nameSuffix = {}) { - return new RunInCondTrigger {RunInDelegator::makeOnCompInit(std::move(func)), - type, condId, nameSuffix}; + return bt2s::make_unique>( + RunInDelegator::makeOnCompInit(std::move(func)), type, condId, nameSuffix); } bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self) @@ -73,36 +76,35 @@ bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self) return self.createTraceClass()->createUnsignedIntegerFieldClass(); } -/* Our condition triggers */ -CondTrigger * const triggers[] = { - makeSimpleTrigger( +} /* namespace */ + +int main(const int argc, const char ** const argv) +{ + CondTriggers triggers; + + triggers.emplace_back(makeSimpleTrigger( [] { bt2::Graph::create(292); }, - CondTrigger::Type::PRE, "graph-create:valid-mip-version"), + CondTrigger::Type::PRE, "graph-create:valid-mip-version")); - makeRunInCompInitTrigger( + triggers.emplace_back(makeRunInCompInitTrigger( [](const bt2::SelfComponent self) { createUIntFc(self)->fieldValueRange(0); }, - CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:valid-n", "0"), + CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:valid-n", "0")); - makeRunInCompInitTrigger( + triggers.emplace_back(makeRunInCompInitTrigger( [](const bt2::SelfComponent self) { createUIntFc(self)->fieldValueRange(65); }, - CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:valid-n", "gt-64"), + CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:valid-n", "gt-64")); - makeSimpleTrigger( + triggers.emplace_back(makeSimpleTrigger( [] { bt_field_class_integer_set_field_value_range(nullptr, 23); }, - CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:not-null:field-class"), -}; - -} /* namespace */ + CondTrigger::Type::PRE, "field-class-integer-set-field-value-range:not-null:field-class")); -int main(const int argc, const char ** const argv) -{ condMain(argc, argv, triggers); } diff --git a/tests/lib/conds/utils.cpp b/tests/lib/conds/utils.cpp index d393c618..f1d9969e 100644 --- a/tests/lib/conds/utils.cpp +++ b/tests/lib/conds/utils.cpp @@ -34,11 +34,11 @@ SimpleCondTrigger::SimpleCondTrigger(std::function func, const Type type namespace { -void listCondTriggers(const CondTriggers condTriggers) noexcept +void listCondTriggers(const CondTriggers& condTriggers) noexcept { auto condTriggerArray = nlohmann::json::array(); - for (const auto condTrigger : condTriggers) { + for (const auto& condTrigger : condTriggers) { condTriggerArray.push_back(nlohmann::json { {"cond-id", condTrigger->condId()}, {"name", condTrigger->name()}, @@ -50,7 +50,7 @@ void listCondTriggers(const CondTriggers condTriggers) noexcept } /* namespace */ -void condMain(const int argc, const char ** const argv, const CondTriggers condTriggers) noexcept +void condMain(const int argc, const char ** const argv, const CondTriggers& condTriggers) noexcept { BT_ASSERT(argc >= 2); diff --git a/tests/lib/conds/utils.hpp b/tests/lib/conds/utils.hpp index d742d908..aa44bba4 100644 --- a/tests/lib/conds/utils.hpp +++ b/tests/lib/conds/utils.hpp @@ -8,13 +8,14 @@ #define TESTS_LIB_CONDS_UTILS_HPP #include +#include #include #include +#include #include #include "cpp-common/bt2c/c-string-view.hpp" -#include "cpp-common/bt2s/span.hpp" #include "../utils/run-in.hpp" @@ -28,6 +29,8 @@ class CondTrigger { public: + using UP = std::unique_ptr; + /* * Condition type. */ @@ -135,7 +138,7 @@ private: /* * List of condition triggers. */ -using CondTriggers = bt2s::span; +using CondTriggers = std::vector; /* * The entry point of a condition trigger program. @@ -174,6 +177,6 @@ using CondTriggers = bt2s::span; * The program is expected to abort through a libbabeltrace2 * condition failure. */ -void condMain(int argc, const char **argv, CondTriggers triggers) noexcept; +void condMain(int argc, const char **argv, const CondTriggers& triggers) noexcept; #endif /* TESTS_LIB_CONDS_UTILS_HPP */ -- 2.34.1