tests: make pre/post-condition assertions test use the run-in framework
[babeltrace.git] / tests / lib / conds / utils.cpp
CommitLineData
5d7e8359
PP
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>
4b4f3ca6 13#include <iostream>
5d7e8359
PP
14
15#include "common/assert.h"
4b4f3ca6 16#include "cpp-common/nlohmann/json.hpp"
053db960 17#include "utils.hpp"
366250bb 18#include "../utils/run-in.hpp"
5d7e8359 19
486428e8 20static void run_trigger(const struct cond_trigger *trigger)
5d7e8359 21{
486428e8
SM
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:
366250bb 27 runInCompClsInit(trigger->func.run_in_comp_cls_init);
486428e8
SM
28 break;
29 default:
30 abort();
31 }
5d7e8359
PP
32}
33
486428e8 34static void list_triggers(const struct cond_trigger triggers[], size_t trigger_count)
5d7e8359 35{
4b4f3ca6 36 nlohmann::json trigger_array = nlohmann::json::array();
5d7e8359 37
4b4f3ca6
SM
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];
5d7e8359 41
486428e8 42 /* Condition ID */
4b4f3ca6 43 trigger_obj["cond-id"] = trigger.cond_id;
5d7e8359 44
486428e8 45 /* Name starts with condition ID */
4b4f3ca6 46 std::string name = trigger.cond_id;
5d7e8359 47
4b4f3ca6
SM
48 if (trigger.suffix) {
49 name += '-';
50 name += trigger.suffix;
486428e8 51 }
5d7e8359 52
4b4f3ca6
SM
53 trigger_obj["name"] = std::move(name);
54 trigger_array.push_back(std::move(trigger_obj));
486428e8 55 }
5d7e8359 56
4b4f3ca6
SM
57 auto str = trigger_array.dump();
58 std::cout << str;
59 std::flush(std::cout);
5d7e8359
PP
60}
61
486428e8
SM
62void cond_main(int argc, const char *argv[], const struct cond_trigger triggers[],
63 size_t trigger_count)
5d7e8359 64{
486428e8
SM
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 }
5d7e8359 77}
This page took 0.033509 seconds and 4 git commands to generate.