Debian Codegraph @

libctf/testsuite/libctf-writable/slice-of-slice.c

raw
1 /* Make sure that slices of slices are properly resolved. If they're not, both
2 population and lookup will fail. */
3
4 #include <ctf-api.h>
5 #include <stdio.h>
6 #include <stdlib.h>
7 #include <string.h>
8
9 int main (void)
10 {
11 ctf_dict_t *fp;
12 ctf_id_t base;
13 ctf_id_t slice;
14 ctf_id_t slice2;
15 ctf_encoding_t long_encoding = { CTF_INT_SIGNED, 0, sizeof (long) };
16 ctf_encoding_t foo;
17 int val;
18 int err;
19
20 if ((fp = ctf_create (&err)) == NULL)
21 {
22 fprintf (stderr, "Cannot create: %s\n", ctf_errmsg (err));
23 return 1;
24 }
25
26 if ((base = ctf_add_enum_encoded (fp, CTF_ADD_ROOT, "enom", &long_encoding))
27 == CTF_ERR)
28 goto err;
29
30 if (ctf_add_enumerator (fp, base, "a", 1) < 0 ||
31 ctf_add_enumerator (fp, base, "b", 0) < 0)
32 goto err;
33
34 foo.cte_format = 0;
35 foo.cte_bits = 4;
36 foo.cte_offset = 4;
37 if ((slice = ctf_add_slice (fp, CTF_ADD_ROOT, base, &foo)) == CTF_ERR)
38 goto err;
39
40 foo.cte_bits = 6;
41 foo.cte_offset = 2;
42 if ((slice2 = ctf_add_slice (fp, CTF_ADD_ROOT, slice, &foo)) == CTF_ERR)
43 goto err;
44
45 if (ctf_add_variable (fp, "foo", slice) < 0)
46 goto err;
47
48 if (ctf_enum_value (fp, slice, "a", &val) < 0)
49 {
50 fprintf (stderr, "Cannot look up value of sliced enum: %s\n", ctf_errmsg (ctf_errno (fp)));
51 return 1;
52 }
53 if (val != 1)
54 {
55 fprintf (stderr, "sliced enum value is wrong\n");
56 return 1;
57 }
58
59 if (ctf_enum_value (fp, slice2, "b", &val) < 0)
60 {
61 fprintf (stderr, "Cannot look up value of sliced sliced enum: %s\n", ctf_errmsg (ctf_errno (fp)));
62 return 1;
63 }
64 if (val != 0)
65 {
66 fprintf (stderr, "sliced sliced enum value is wrong\n");
67 return 1;
68 }
69
70 ctf_dict_close (fp);
71 fprintf (stderr, "All done.\n");
72 return 0;
73
74 err:
75 fprintf (stderr, "cannot populate: %s\n", ctf_errmsg (ctf_errno (fp)));
76 return 1;
77 }