Add a basic .clang-tidy file and fix typedef warnings
[lttng-tools.git] / tests / unit / test_rate_policy.cpp
CommitLineData
7f4d5b07
JR
1/*
2 * Unit tests for the rate policy object API.
3 *
4 * Copyright (C) 2019 Jonathan Rajotte <jonathan.rajotte-julien@efficios.com>
5 *
6 * SPDX-License-Identifier: LGPL-2.1-only
7 *
8 */
9
c9e313bc
SM
10#include <common/payload-view.hpp>
11#include <common/payload.hpp>
28ab034a 12
c9e313bc 13#include <lttng/action/rate-policy-internal.hpp>
7f4d5b07
JR
14#include <lttng/action/rate-policy.h>
15
28ab034a
JG
16#include <inttypes.h>
17#include <stdio.h>
18#include <string.h>
19#include <tap/tap.h>
20#include <unistd.h>
21
7f4d5b07
JR
22/* For error.h. */
23int lttng_opt_quiet = 1;
24int lttng_opt_verbose;
25int lttng_opt_mi;
26
27#define NUM_TESTS 42
28
29static void test_rate_policy_every_n(void)
30{
31 enum lttng_rate_policy_status status;
32 struct lttng_rate_policy *policy_a = NULL; /* Interval of 100. */
33 struct lttng_rate_policy *policy_b = NULL; /* Interval of 100 */
34 struct lttng_rate_policy *policy_c = NULL; /* Interval of 1 */
35 struct lttng_rate_policy *policy_from_buffer = NULL;
36 uint64_t interval_a_b = 100;
37 uint64_t interval_c = 1;
38 uint64_t interval_query = 0;
39 struct lttng_payload payload;
40
41 lttng_payload_init(&payload);
42
43 policy_a = lttng_rate_policy_every_n_create(interval_a_b);
44 policy_b = lttng_rate_policy_every_n_create(interval_a_b);
45 policy_c = lttng_rate_policy_every_n_create(interval_c);
28ab034a
JG
46 ok(policy_a != NULL, "Rate policy every n A created: interval: %" PRIu64, interval_a_b);
47 ok(policy_b != NULL, "Rate policy every n B created: interval: %" PRIu64, interval_a_b);
48 ok(policy_c != NULL, "Rate policy every n C created: interval: %" PRIu64, interval_c);
49
50 ok(LTTNG_RATE_POLICY_TYPE_EVERY_N == lttng_rate_policy_get_type(policy_a),
51 "Type is LTTNG_RATE_POLICY_TYPE_EVERY_N");
7f4d5b07
JR
52
53 /* Getter tests */
54 status = lttng_rate_policy_every_n_get_interval(NULL, NULL);
28ab034a 55 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID");
7f4d5b07
JR
56
57 status = lttng_rate_policy_every_n_get_interval(NULL, &interval_query);
28ab034a 58 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID");
7f4d5b07
JR
59
60 status = lttng_rate_policy_every_n_get_interval(policy_a, NULL);
28ab034a
JG
61 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get interval returns INVALID");
62
63 status = lttng_rate_policy_every_n_get_interval(policy_a, &interval_query);
64 ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_a_b,
65 " Getting interval A");
66
67 status = lttng_rate_policy_every_n_get_interval(policy_b, &interval_query);
68 ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_a_b,
69 " Getting interval B");
70
71 status = lttng_rate_policy_every_n_get_interval(policy_c, &interval_query);
72 ok(status == LTTNG_RATE_POLICY_STATUS_OK && interval_query == interval_c,
73 " Getting interval C");
7f4d5b07
JR
74
75 /* is_equal tests */
76 /* TODO: this is the behaviour introduced by the
77 * lttng_condition_is_equal back in 2017 do we want to fix this and
78 * return true if both are NULL?
79 */
28ab034a
JG
80 ok(false == lttng_rate_policy_is_equal(NULL, NULL), "is equal (NULL,NULL)");
81 ok(false == lttng_rate_policy_is_equal(policy_a, NULL), "is equal (object, NULL)");
82 ok(false == lttng_rate_policy_is_equal(NULL, policy_a), " is equal (NULL, object)");
83 ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)");
84
85 ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), "is equal (object A, object B");
86 ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), "is equal (object B, object A");
7f4d5b07
JR
87
88 ok(false == lttng_rate_policy_is_equal(policy_a, policy_c),
28ab034a 89 "is equal (object A, object C)");
7f4d5b07 90 ok(false == lttng_rate_policy_is_equal(policy_c, policy_a),
28ab034a 91 "is equal (object C, object A)");
7f4d5b07
JR
92
93 /* Serialization and create_from buffer. */
94 ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing");
95 {
28ab034a
JG
96 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
97
98 ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 &&
99 policy_from_buffer != NULL,
100 "Deserializing");
7f4d5b07
JR
101 }
102
103 ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer),
28ab034a 104 "serialized and from buffer are equal");
7f4d5b07
JR
105
106 lttng_rate_policy_destroy(policy_a);
107 lttng_rate_policy_destroy(policy_b);
108 lttng_rate_policy_destroy(policy_c);
1f38e81c 109 lttng_rate_policy_destroy(policy_from_buffer);
7f4d5b07
JR
110 lttng_payload_reset(&payload);
111}
112
113static void test_rate_policy_once_after_n(void)
114{
115 enum lttng_rate_policy_status status;
116 struct lttng_rate_policy *policy_a = NULL; /* Threshold of 100. */
117 struct lttng_rate_policy *policy_b = NULL; /* threshold of 100 */
118 struct lttng_rate_policy *policy_c = NULL; /* threshold of 1 */
119 struct lttng_rate_policy *policy_from_buffer = NULL;
120 uint64_t threshold_a_b = 100;
121 uint64_t threshold_c = 1;
122 uint64_t threshold_query = 0;
123 struct lttng_payload payload;
124
125 lttng_payload_init(&payload);
126
127 policy_a = lttng_rate_policy_once_after_n_create(threshold_a_b);
128 policy_b = lttng_rate_policy_once_after_n_create(threshold_a_b);
129 policy_c = lttng_rate_policy_once_after_n_create(threshold_c);
28ab034a
JG
130 ok(policy_a != NULL, "Rate policy every n A created: threshold: %" PRIu64, threshold_a_b);
131 ok(policy_b != NULL, "Rate policy every n B created: threshold: %" PRIu64, threshold_a_b);
132 ok(policy_c != NULL, "Rate policy every n C created: threshold: %" PRIu64, threshold_c);
133
134 ok(LTTNG_RATE_POLICY_TYPE_ONCE_AFTER_N == lttng_rate_policy_get_type(policy_a),
135 "Type is LTTNG_RATE_POLICY_TYPE_once_after_n");
7f4d5b07
JR
136
137 /* Getter tests */
138 status = lttng_rate_policy_once_after_n_get_threshold(NULL, NULL);
28ab034a 139 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID");
7f4d5b07 140
28ab034a
JG
141 status = lttng_rate_policy_once_after_n_get_threshold(NULL, &threshold_query);
142 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID");
7f4d5b07
JR
143
144 status = lttng_rate_policy_once_after_n_get_threshold(policy_a, NULL);
28ab034a
JG
145 ok(status == LTTNG_RATE_POLICY_STATUS_INVALID, "Get threshold returns INVALID");
146
147 status = lttng_rate_policy_once_after_n_get_threshold(policy_a, &threshold_query);
148 ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_a_b,
149 " Getting threshold A");
150
151 status = lttng_rate_policy_once_after_n_get_threshold(policy_b, &threshold_query);
152 ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_a_b,
153 " Getting threshold B");
154
155 status = lttng_rate_policy_once_after_n_get_threshold(policy_c, &threshold_query);
156 ok(status == LTTNG_RATE_POLICY_STATUS_OK && threshold_query == threshold_c,
157 " Getting threshold C");
7f4d5b07
JR
158
159 /* is_equal tests */
28ab034a
JG
160 ok(false == lttng_rate_policy_is_equal(NULL, NULL), "is equal (NULL,NULL)");
161 ok(false == lttng_rate_policy_is_equal(policy_a, NULL), "is equal (object, NULL)");
162 ok(false == lttng_rate_policy_is_equal(NULL, policy_a), " is equal (NULL, object)");
163 ok(true == lttng_rate_policy_is_equal(policy_a, policy_a), "is equal (object A, object A)");
164
165 ok(true == lttng_rate_policy_is_equal(policy_a, policy_b), "is equal (object A, object B");
166 ok(true == lttng_rate_policy_is_equal(policy_b, policy_a), "is equal (object B, object A");
7f4d5b07
JR
167
168 ok(false == lttng_rate_policy_is_equal(policy_a, policy_c),
28ab034a 169 "is equal (object A, object C)");
7f4d5b07 170 ok(false == lttng_rate_policy_is_equal(policy_c, policy_a),
28ab034a 171 "is equal (object C, object A)");
7f4d5b07
JR
172
173 /* Serialization and create_from buffer. */
174 ok(lttng_rate_policy_serialize(policy_a, &payload) == 0, "Serializing");
175 {
28ab034a
JG
176 struct lttng_payload_view view = lttng_payload_view_from_payload(&payload, 0, -1);
177
178 ok(lttng_rate_policy_create_from_payload(&view, &policy_from_buffer) > 0 &&
179 policy_from_buffer != NULL,
180 "Deserializing");
7f4d5b07
JR
181 }
182
183 ok(lttng_rate_policy_is_equal(policy_a, policy_from_buffer),
28ab034a 184 "serialized and from buffer are equal");
7f4d5b07
JR
185
186 lttng_rate_policy_destroy(policy_a);
187 lttng_rate_policy_destroy(policy_b);
188 lttng_rate_policy_destroy(policy_c);
1f38e81c 189 lttng_rate_policy_destroy(policy_from_buffer);
7f4d5b07
JR
190 lttng_payload_reset(&payload);
191}
192
f46376a1 193int main(void)
7f4d5b07
JR
194{
195 plan_tests(NUM_TESTS);
196 test_rate_policy_every_n();
197 test_rate_policy_once_after_n();
198 return exit_status();
199}
This page took 0.039693 seconds and 4 git commands to generate.