tests: make pre/post-condition assertions test use the run-in framework
[babeltrace.git] / tests / lib / conds / utils.cpp
1 /*
2 * SPDX-License-Identifier: GPL-2.0-only
3 *
4 * Copyright (C) 2020 Philippe Proulx <pproulx@efficios.com>
5 */
6
7 #include <stdlib.h>
8 #include <stdio.h>
9 #include <string.h>
10 #include <assert.h>
11 #include <babeltrace2/babeltrace.h>
12 #include <glib.h>
13 #include <iostream>
14
15 #include "common/assert.h"
16 #include "cpp-common/nlohmann/json.hpp"
17 #include "utils.hpp"
18 #include "../utils/run-in.hpp"
19
20 static void run_trigger(const struct cond_trigger *trigger)
21 {
22 switch (trigger->func_type) {
23 case COND_TRIGGER_FUNC_TYPE_BASIC:
24 trigger->func.basic();
25 break;
26 case COND_TRIGGER_FUNC_TYPE_RUN_IN_COMP_CLS_INIT:
27 runInCompClsInit(trigger->func.run_in_comp_cls_init);
28 break;
29 default:
30 abort();
31 }
32 }
33
34 static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
35 {
36 nlohmann::json trigger_array = nlohmann::json::array();
37
38 for (size_t i = 0; i < trigger_count; i++) {
39 nlohmann::json trigger_obj = nlohmann::json::object();
40 const cond_trigger& trigger = triggers[i];
41
42 /* Condition ID */
43 trigger_obj["cond-id"] = trigger.cond_id;
44
45 /* Name starts with condition ID */
46 std::string name = trigger.cond_id;
47
48 if (trigger.suffix) {
49 name += '-';
50 name += trigger.suffix;
51 }
52
53 trigger_obj["name"] = std::move(name);
54 trigger_array.push_back(std::move(trigger_obj));
55 }
56
57 auto str = trigger_array.dump();
58 std::cout << str;
59 std::flush(std::cout);
60 }
61
62 void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
63 size_t trigger_count)
64 {
65 BT_ASSERT(argc >= 2);
66
67 if (strcmp(argv[1], "list") == 0) {
68 list_triggers(triggers, trigger_count);
69 } else if (strcmp(argv[1], "run") == 0) {
70 int index;
71
72 BT_ASSERT(argc >= 3);
73 index = atoi(argv[2]);
74 BT_ASSERT(index >= 0 && index < trigger_count);
75 run_trigger(&triggers[index]);
76 }
77 }
This page took 0.030943 seconds and 4 git commands to generate.