FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/dict.c
Date: 2026-04-24 10:13:59
Exec Total Coverage
Lines: 104 111 93.7%
Functions: 4 4 100.0%
Branches: 17 38 44.7%

Line Branch Exec Source
1 /*
2 * copyright (c) 2009 Michael Niedermayer
3 *
4 * This file is part of FFmpeg.
5 *
6 * FFmpeg is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
10 *
11 * FFmpeg is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
15 *
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with FFmpeg; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21 #include "libavutil/mem.h"
22
23 #include "libavutil/dict.c"
24
25 91 static const AVDictionaryEntry *dict_iterate(const AVDictionary *m,
26 const AVDictionaryEntry *prev)
27 {
28 91 const AVDictionaryEntry *dict_get = av_dict_get(m, "", prev, AV_DICT_IGNORE_SUFFIX);
29 91 const AVDictionaryEntry *dict_iterate = av_dict_iterate(m, prev);
30
31
1/2
✗ Branch 0 not taken.
✓ Branch 1 taken 91 times.
91 if (dict_get != dict_iterate) {
32 #define GET(entry, mem) ((entry) ? (entry)->mem : "N/A")
33 printf("Iterating with av_dict_iterate() yields a different result "
34 "than iterating with av_dict_get() and AV_DICT_IGNORE_SUFFIX "
35 "(prev: %p, key %s; av_dict_iterate() %p, key %s, value %s; "
36 "av_dict_get() %p, key %s, value %s)\n",
37 prev, GET(prev, key),
38 dict_iterate, GET(dict_iterate, key), GET(dict_iterate, value),
39 dict_get, GET(dict_get, key), GET(dict_get, value));
40 #undef GET
41 }
42 91 return dict_iterate;
43 }
44
45 14 static void print_dict(const AVDictionary *m)
46 {
47 14 const AVDictionaryEntry *t = NULL;
48 14 const char *sep = "";
49
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 14 times.
76 while ((t = dict_iterate(m, t))) {
50 62 printf("%s%s %s", sep, t->key, t->value);
51 62 sep = " ";
52 }
53 14 printf("\n");
54 14 }
55
56 7 static void test_separators(const AVDictionary *m, const char pair, const char val)
57 {
58 7 AVDictionary *dict = NULL;
59 7 char pairs[] = {pair , '\0'};
60 7 char vals[] = {val, '\0'};
61
62 7 char *buffer = NULL;
63 int ret;
64
65 7 av_dict_copy(&dict, m, 0);
66 7 print_dict(dict);
67 7 av_dict_get_string(dict, &buffer, val, pair);
68 7 printf("%s\n", buffer);
69 7 av_dict_free(&dict);
70 7 ret = av_dict_parse_string(&dict, buffer, vals, pairs, 0);
71 7 printf("ret %d\n", ret);
72 7 av_freep(&buffer);
73 7 print_dict(dict);
74 7 av_dict_free(&dict);
75 7 }
76
77 1 int main(void)
78 {
79 1 AVDictionary *dict = NULL;
80 const AVDictionaryEntry *e;
81 1 char *buffer = NULL;
82
83 1 printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
84 1 av_dict_get_string(dict, &buffer, '=', ',');
85 1 printf("%s\n", buffer);
86 1 av_freep(&buffer);
87 1 av_dict_set(&dict, "aaa", "aaa", 0);
88 1 av_dict_set(&dict, "b,b", "bbb", 0);
89 1 av_dict_set(&dict, "c=c", "ccc", 0);
90 1 av_dict_set(&dict, "ddd", "d,d", 0);
91 1 av_dict_set(&dict, "eee", "e=e", 0);
92 1 av_dict_set(&dict, "f,f", "f=f", 0);
93 1 av_dict_set(&dict, "g=g", "g,g", 0);
94 1 test_separators(dict, ',', '=');
95 1 av_dict_free(&dict);
96 1 av_dict_set(&dict, "aaa", "aaa", 0);
97 1 av_dict_set(&dict, "bbb", "bbb", 0);
98 1 av_dict_set(&dict, "ccc", "ccc", 0);
99 1 av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
100 1 test_separators(dict, '"', '=');
101 1 test_separators(dict, '\'', '=');
102 1 test_separators(dict, ',', '"');
103 1 test_separators(dict, ',', '\'');
104 1 test_separators(dict, '\'', '"');
105 1 test_separators(dict, '"', '\'');
106 1 av_dict_free(&dict);
107
108 1 printf("\nTesting av_dict_set()\n");
109 1 av_dict_set(&dict, "a", "a", 0);
110 1 av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
111 1 av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
112 1 av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
113 1 av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
114 1 av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
115 1 av_dict_set(&dict, "f", "f", 0);
116 1 av_dict_set(&dict, "f", NULL, 0);
117 1 av_dict_set(&dict, "ff", "f", 0);
118 1 av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
119
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_get(dict, NULL, NULL, 0))
120 printf("av_dict_get() does not correctly handle NULL key.\n");
121 1 e = NULL;
122
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 while ((e = dict_iterate(dict, e)))
123 6 printf("%s %s\n", e->key, e->value);
124 1 av_dict_free(&dict);
125
126
2/4
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
✗ Branch 4 not taken.
2 if (av_dict_set(&dict, NULL, "a", 0) >= 0 ||
127
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 av_dict_set(&dict, NULL, "b", 0) >= 0 ||
128
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 av_dict_set(&dict, NULL, NULL, AV_DICT_DONT_STRDUP_KEY) >= 0 ||
129
1/2
✗ Branch 2 not taken.
✓ Branch 3 taken 1 times.
2 av_dict_set(&dict, NULL, av_strdup("b"), AV_DICT_DONT_STRDUP_VAL) >= 0 ||
130 1 av_dict_count(dict))
131 printf("av_dict_set does not correctly handle NULL key\n");
132
133 1 e = NULL;
134
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 while ((e = dict_iterate(dict, e)))
135 printf("'%s' '%s'\n", e->key, e->value);
136 1 av_dict_free(&dict);
137
138
139 //valgrind sensible test
140 1 printf("\nTesting av_dict_set_int()\n");
141 1 av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
142 1 av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
143 1 av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
144 1 av_dict_set_int(&dict, "4", 4, 0);
145 1 av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
146 1 av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
147 1 av_dict_set_int(&dict, "12", 1, 0);
148 1 av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
149 1 e = NULL;
150
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 while ((e = dict_iterate(dict, e)))
151 6 printf("%s %s\n", e->key, e->value);
152 1 av_dict_free(&dict);
153
154 //valgrind sensible test
155 1 printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, "key", "old", 0) < 0)
157 return 1;
158 1 e = av_dict_get(dict, "key", NULL, 0);
159
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, e->key, "new val OK", 0) < 0)
160 return 1;
161 1 e = av_dict_get(dict, "key", NULL, 0);
162 1 printf("%s\n", e->value);
163
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, e->key, e->value, 0) < 0)
164 return 1;
165 1 e = av_dict_get(dict, "key", NULL, 0);
166 1 printf("%s\n", e->value);
167 1 av_dict_free(&dict);
168
169 1 return 0;
170 }
171