tests/lib/conds: store triggers in a vector
authorSimon Marchi <simon.marchi@efficios.com>
Wed, 20 Mar 2024 21:10:30 +0000 (17:10 -0400)
committerSimon Marchi <simon.marchi@efficios.com>
Tue, 26 Mar 2024 18:56:36 +0000 (14:56 -0400)
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 <simon.marchi@efficios.com>
Reviewed-on: https://review.lttng.org/c/babeltrace/+/12115
Reviewed-by: Philippe Proulx <eeppeliteloop@gmail.com>
Tested-by: jenkins <jenkins@lttng.org>
tests/lib/conds/conds-triggers.cpp
tests/lib/conds/utils.cpp
tests/lib/conds/utils.hpp

index 1a463511b0fcff540748ff98ea82882ee41cdb1b..26603cdcbd1d7b096320235e21f9852ef718e7ac 100644 (file)
@@ -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 <typename FuncT>
-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<FuncT>(func), type, condId, nameSuffix};
+    return bt2s::make_unique<SimpleCondTrigger>(std::forward<FuncT>(func), type, condId,
+                                                nameSuffix);
 }
 
 using OnCompInitFunc = std::function<void(bt2::SelfComponent)>;
@@ -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> {RunInDelegator::makeOnCompInit(std::move(func)),
-                                                 type, condId, nameSuffix};
+    return bt2s::make_unique<RunInCondTrigger<RunInDelegator>>(
+        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);
 }
index d393c618a0689bf8fd98efa6a3359f00a7cf41e0..f1d9969e1ed24f2f8806349aa07b47c826866d94 100644 (file)
@@ -34,11 +34,11 @@ SimpleCondTrigger::SimpleCondTrigger(std::function<void()> 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);
 
index d742d9088f317d9a42405752cc17a2244e185462..aa44bba4059f97210e4ee16ba6056ea1b6f66e3c 100644 (file)
@@ -8,13 +8,14 @@
 #define TESTS_LIB_CONDS_UTILS_HPP
 
 #include <functional>
+#include <memory>
 #include <string>
 #include <utility>
+#include <vector>
 
 #include <babeltrace2/babeltrace.h>
 
 #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<CondTrigger>;
+
     /*
      * Condition type.
      */
@@ -135,7 +138,7 @@ private:
 /*
  * List of condition triggers.
  */
-using CondTriggers = bt2s::span<CondTrigger * const>;
+using CondTriggers = std::vector<CondTrigger::UP>;
 
 /*
  * The entry point of a condition trigger program.
@@ -174,6 +177,6 @@ using CondTriggers = bt2s::span<CondTrigger * const>;
  *     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 */
This page took 0.026809 seconds and 4 git commands to generate.