1 /*
2 * Copyright (c) 2024 Mustafa Abdullah Kus, Sparse Technology
3 *
4 * SPDX-License-Identifier: Apache-2.0
5 */
6
7 #include <zephyr/ztest.h>
8
9 #include <zephyr/net/prometheus/counter.h>
10
11 PROMETHEUS_COUNTER_DEFINE(test_counter_m, "Test counter",
12 ({ .key = "test_counter", .value = "test" }),
13 NULL);
14
15 /**
16 * @brief Test prometheus_counter_inc
17 * @details The test shall increment the counter value by 1 and check if the
18 * value is incremented correctly.
19 */
ZTEST(test_counter,test_prometheus_counter_01_inc)20 ZTEST(test_counter, test_prometheus_counter_01_inc)
21 {
22 int ret;
23
24 zassert_equal(test_counter_m.value, 0, "Counter value is not 0");
25
26 ret = prometheus_counter_inc(&test_counter_m);
27 zassert_ok(ret, "Error incrementing counter");
28
29 zassert_equal(test_counter_m.value, 1, "Counter value is not 1");
30
31 ret = prometheus_counter_inc(&test_counter_m);
32 zassert_ok(ret, "Error incrementing counter");
33
34 zassert_equal(test_counter_m.value, 2, "Counter value is not 2");
35 }
36
37 /**
38 * @brief Test prometheus_counter_add
39 * @details The test shall increment the counter value by arbitrary value
40 * and check if the value is incremented correctly.
41 */
ZTEST(test_counter,test_prometheus_counter_02_add)42 ZTEST(test_counter, test_prometheus_counter_02_add)
43 {
44 int ret;
45
46 ret = prometheus_counter_add(&test_counter_m, 2);
47 zassert_ok(ret, "Error adding counter");
48
49 zassert_equal(test_counter_m.value, 4, "Counter value is not 4");
50
51 ret = prometheus_counter_add(&test_counter_m, 0);
52 zassert_ok(ret, "Error adding counter");
53
54 zassert_equal(test_counter_m.value, 4, "Counter value is not 4");
55 }
56
57 /**
58 * @brief Test prometheus_counter_set
59 * @details The test shall set the counter value by arbitrary value
60 * and check if the value is incremented correctly.
61 */
ZTEST(test_counter,test_prometheus_counter_03_set)62 ZTEST(test_counter, test_prometheus_counter_03_set)
63 {
64 int ret;
65
66 ret = prometheus_counter_set(&test_counter_m, 20);
67 zassert_ok(ret, "Error setting counter");
68
69 zassert_equal(test_counter_m.value, 20, "Counter value is not 20");
70
71 ret = prometheus_counter_set(&test_counter_m, 15);
72 zassert_equal(ret, -EINVAL, "Error setting counter");
73
74 zassert_equal(test_counter_m.value, 20, "Counter value is not 20");
75 }
76
77 ZTEST_SUITE(test_counter, NULL, NULL, NULL, NULL, NULL);
78