SoW-2020-0003: Trace Hit Counters
[lttng-tools.git] / tests / unit / test_map_key.c
1 /*
2 * test_map_key.c
3 *
4 * Unit tests for the map-key API.
5 *
6 * Copyright (C) 2021 Francis Deslauriers <francis.deslauriers@efficios.com>
7 *
8 * SPDX-License-Identifier: GPL-2.0-only
9 *
10 */
11
12 #include <assert.h>
13 #include <inttypes.h>
14 #include <stdio.h>
15 #include <string.h>
16 #include <unistd.h>
17
18 #include <tap/tap.h>
19
20 #include <lttng/map-key-internal.h>
21
22 #define NUM_TESTS 38
23
24 const char *key_str1 = "simple_string";
25 const char *key_str2 = "${EVENT_NAME}";
26 const char *key_str3 = "foo_${EVENT_NAME}";
27 const char *key_str4 = "foo_${EVENT_NAME}_bar";
28 const char *key_str5 = "${EVENT_NAME}_bar_${EVENT_NAME}";
29 const char *key_str6 = "foo_${NON_EXISTING_VAR}";
30 const char *key_str7 = "foo_${}";
31 const char *key_str8 = "foo_${PROVIDER_NAME}";
32
33 static
34 void test_map_key(void)
35 {
36 struct lttng_map_key *key;
37 enum lttng_map_key_status status;
38 const struct lttng_map_key_token *token;
39 const struct lttng_map_key_token_variable *var_token;
40 unsigned int count;
41
42 key = lttng_map_key_parse_from_string(key_str6);
43 ok(!key, "Failed to create key from \"%s\" as expected", key_str6);
44
45 key = lttng_map_key_parse_from_string(key_str7);
46 ok(!key, "Failed to create key from \"%s\" as expected", key_str7);
47
48 key = lttng_map_key_parse_from_string(key_str1);
49 ok(key, "Created key from \"%s\"", key_str1);
50 status = lttng_map_key_get_token_count(key, &count);
51 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str1");
52 ok(count == 1, "Got correct token count for key_str1");
53 token = lttng_map_key_get_token_at_index(key, 0);
54 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "First token of string type");
55 lttng_map_key_destroy(key);
56
57 key = lttng_map_key_parse_from_string(key_str2);
58 ok(key, "Created key from \"%s\"", key_str2);
59 status = lttng_map_key_get_token_count(key, &count);
60 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str2");
61 ok(count == 1, "Got correct token count for key_str2");
62 token = lttng_map_key_get_token_at_index(key, 0);
63 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "First token of variable type");
64 var_token = (typeof(var_token)) token;
65 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME, "EVENT_NAME variable type");
66 lttng_map_key_destroy(key);
67
68 key = lttng_map_key_parse_from_string(key_str3);
69 ok(key, "Created key from \"%s\"", key_str3);
70 status = lttng_map_key_get_token_count(key, &count);
71 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str3");
72 ok(count == 2, "Got correct token count for key_str3");
73 token = lttng_map_key_get_token_at_index(key, 0);
74 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "First token of string type");
75 token = lttng_map_key_get_token_at_index(key, 1);
76 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "Second token of variable type");
77 var_token = (typeof(var_token)) token;
78 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME, "EVENT_NAME variable type");
79 lttng_map_key_destroy(key);
80
81 key = lttng_map_key_parse_from_string(key_str4);
82 ok(key, "Created key from \"%s\"", key_str4);
83 status = lttng_map_key_get_token_count(key, &count);
84 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str4");
85 ok(count == 3, "Got correct token count for key_str4");
86 token = lttng_map_key_get_token_at_index(key, 0);
87 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "First token of string type");
88 token = lttng_map_key_get_token_at_index(key, 1);
89 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "Second token of variable type");
90 var_token = (typeof(var_token)) token;
91 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME, "EVENT_NAME variable type");
92 token = lttng_map_key_get_token_at_index(key, 2);
93 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "Third token of string type");
94 lttng_map_key_destroy(key);
95
96 key = lttng_map_key_parse_from_string(key_str5);
97 ok(key, "Created key from \"%s\"", key_str5);
98 status = lttng_map_key_get_token_count(key, &count);
99 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str5");
100 ok(count == 3, "Got correct token count for key_str5");
101 token = lttng_map_key_get_token_at_index(key, 0);
102 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "First token of variable type");
103 var_token = (typeof(var_token)) token;
104 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME, "EVENT_NAME variable type");
105 token = lttng_map_key_get_token_at_index(key, 1);
106 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "Second token of string type");
107 token = lttng_map_key_get_token_at_index(key, 2);
108 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "Third token of variable type");
109 var_token = (typeof(var_token)) token;
110 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_EVENT_NAME, "EVENT_NAME variable type");
111 lttng_map_key_destroy(key);
112
113 key = lttng_map_key_parse_from_string(key_str8);
114 ok(key, "Created key from \"%s\"", key_str8);
115 status = lttng_map_key_get_token_count(key, &count);
116 ok(status == LTTNG_MAP_KEY_STATUS_OK, "Got count for key_str8");
117 ok(count == 2, "Got correct token count for key_str8");
118 token = lttng_map_key_get_token_at_index(key, 0);
119 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_STRING, "First token of string type");
120 token = lttng_map_key_get_token_at_index(key, 1);
121 ok(token->type == LTTNG_MAP_KEY_TOKEN_TYPE_VARIABLE, "Second token of variable type");
122 var_token = (typeof(var_token)) token;
123 ok(var_token->type == LTTNG_MAP_KEY_TOKEN_VARIABLE_TYPE_PROVIDER_NAME, "PROVIDER_NAME variable type");
124 lttng_map_key_destroy(key);
125 }
126
127 int main(int argc, const char *argv[])
128 {
129 plan_tests(NUM_TESTS);
130 test_map_key();
131 return exit_status();
132 }
This page took 0.032574 seconds and 5 git commands to generate.