X-Git-Url: https://git.efficios.com/?a=blobdiff_plain;f=tests%2Flib%2Fconds%2Fconds-triggers.cpp;h=857cfbcc59284764ef82018c13f0714ae14a1a4b;hb=fb472529347b066d5b60f9e4f1d2554ecb733aca;hp=83182f9ab566a1682680f20c2c685dd61d177a08;hpb=c802cacb9f0879a42e01575595a75bbefe7d3db9;p=babeltrace.git diff --git a/tests/lib/conds/conds-triggers.cpp b/tests/lib/conds/conds-triggers.cpp index 83182f9a..857cfbcc 100644 --- a/tests/lib/conds/conds-triggers.cpp +++ b/tests/lib/conds/conds-triggers.cpp @@ -4,56 +4,108 @@ * Copyright (C) 2020 Philippe Proulx */ +#include + #include -#include "common/assert.h" +#include "cpp-common/bt2/graph.hpp" +#include "cpp-common/bt2c/c-string-view.hpp" +#include "cpp-common/bt2c/span.hpp" +#include "cpp-common/bt2s/make-unique.hpp" #include "utils.hpp" -static void trigger_graph_mip_version(void) +namespace { + +/* + * Creates a simple condition trigger, calling `func`. + */ +template +CondTrigger::UP makeSimpleTrigger(FuncT&& func, const CondTrigger::Type type, + const std::string& condId, + const bt2c::CStringView nameSuffix = {}) { - bt_graph_create(292); + return bt2s::make_unique(std::forward(func), type, condId, + nameSuffix); } -static bt_field_class *get_uint_fc(bt_self_component *self_comp) +using OnCompInitFunc = std::function; + +/* + * A "run in" class that delegates the execution to stored callables. + * + * Use the makeRunIn*Trigger() helpers below. + */ +class RunInDelegator final : public RunIn { - bt_trace_class *tc = bt_trace_class_create(self_comp); - bt_field_class *fc; +public: + static RunInDelegator makeOnCompInit(OnCompInitFunc func) + { + return RunInDelegator {std::move(func)}; + } - BT_ASSERT(tc); - fc = bt_field_class_integer_unsigned_create(tc); - BT_ASSERT(fc); - return fc; -} + void onCompInit(const bt2::SelfComponent self) override + { + if (_mOnCompInitFunc) { + _mOnCompInitFunc(self); + } + } -static void trigger_fc_int_set_field_value_range_n_0(bt_self_component *self_comp) -{ - bt_field_class_integer_set_field_value_range(get_uint_fc(self_comp), 0); -} +private: + explicit RunInDelegator(OnCompInitFunc onCompInitFunc) : + _mOnCompInitFunc {std::move(onCompInitFunc)} + { + } -static void trigger_fc_int_set_field_value_range_n_gt_64(bt_self_component *self_comp) + OnCompInitFunc _mOnCompInitFunc; +}; + +/* + * Creates a condition trigger, calling `func` in a component + * initialization context. + */ +CondTrigger::UP makeRunInCompInitTrigger(OnCompInitFunc func, const CondTrigger::Type type, + const std::string& condId, + const bt2c::CStringView nameSuffix = {}) { - bt_field_class_integer_set_field_value_range(get_uint_fc(self_comp), 65); + return bt2s::make_unique>( + RunInDelegator::makeOnCompInit(std::move(func)), type, condId, nameSuffix); } -static void trigger_fc_int_set_field_value_range_null(bt_self_component *) +bt2::IntegerFieldClass::Shared createUIntFc(const bt2::SelfComponent self) { - bt_field_class_integer_set_field_value_range(NULL, 23); + return self.createTraceClass()->createUnsignedIntegerFieldClass(); } -static const struct cond_trigger triggers[] = { - COND_TRIGGER_PRE_BASIC("pre:graph-create:valid-mip-version", NULL, trigger_graph_mip_version), - COND_TRIGGER_PRE_RUN_IN_COMP_CLS_INIT("pre:field-class-integer-set-field-value-range:valid-n", - "0", trigger_fc_int_set_field_value_range_n_0), - COND_TRIGGER_PRE_RUN_IN_COMP_CLS_INIT("pre:field-class-integer-set-field-value-range:valid-n", - "gt-64", trigger_fc_int_set_field_value_range_n_gt_64), - COND_TRIGGER_PRE_RUN_IN_COMP_CLS_INIT( - "pre:field-class-integer-set-field-value-range:not-null:field-class", NULL, - trigger_fc_int_set_field_value_range_null), -}; +} /* namespace */ -int main(int argc, const char *argv[]) +int main(const int argc, const char ** const argv) { - cond_main(argc, argv, triggers, sizeof(triggers) / sizeof(*triggers)); - return 0; + CondTriggers triggers; + + triggers.emplace_back(makeSimpleTrigger( + [] { + bt2::Graph::create(292); + }, + CondTrigger::Type::PRE, "graph-create:valid-mip-version")); + + 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")); + + 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")); + + 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")); + + condMain(bt2c::makeSpan(argv, argc), triggers); }