FFmpeg coverage


Directory: ../../../ffmpeg/
File: src/libavutil/tests/dict.c
Date: 2024-11-20 23:03:26
Exec Total Coverage
Lines: 102 109 93.6%
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
2/2
✓ Branch 1 taken 62 times.
✓ Branch 2 taken 14 times.
76 while ((t = dict_iterate(m, t)))
49 62 printf("%s %s ", t->key, t->value);
50 14 printf("\n");
51 14 }
52
53 7 static void test_separators(const AVDictionary *m, const char pair, const char val)
54 {
55 7 AVDictionary *dict = NULL;
56 7 char pairs[] = {pair , '\0'};
57 7 char vals[] = {val, '\0'};
58
59 7 char *buffer = NULL;
60 int ret;
61
62 7 av_dict_copy(&dict, m, 0);
63 7 print_dict(dict);
64 7 av_dict_get_string(dict, &buffer, val, pair);
65 7 printf("%s\n", buffer);
66 7 av_dict_free(&dict);
67 7 ret = av_dict_parse_string(&dict, buffer, vals, pairs, 0);
68 7 printf("ret %d\n", ret);
69 7 av_freep(&buffer);
70 7 print_dict(dict);
71 7 av_dict_free(&dict);
72 7 }
73
74 1 int main(void)
75 {
76 1 AVDictionary *dict = NULL;
77 const AVDictionaryEntry *e;
78 1 char *buffer = NULL;
79
80 1 printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
81 1 av_dict_get_string(dict, &buffer, '=', ',');
82 1 printf("%s\n", buffer);
83 1 av_freep(&buffer);
84 1 av_dict_set(&dict, "aaa", "aaa", 0);
85 1 av_dict_set(&dict, "b,b", "bbb", 0);
86 1 av_dict_set(&dict, "c=c", "ccc", 0);
87 1 av_dict_set(&dict, "ddd", "d,d", 0);
88 1 av_dict_set(&dict, "eee", "e=e", 0);
89 1 av_dict_set(&dict, "f,f", "f=f", 0);
90 1 av_dict_set(&dict, "g=g", "g,g", 0);
91 1 test_separators(dict, ',', '=');
92 1 av_dict_free(&dict);
93 1 av_dict_set(&dict, "aaa", "aaa", 0);
94 1 av_dict_set(&dict, "bbb", "bbb", 0);
95 1 av_dict_set(&dict, "ccc", "ccc", 0);
96 1 av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
97 1 test_separators(dict, '"', '=');
98 1 test_separators(dict, '\'', '=');
99 1 test_separators(dict, ',', '"');
100 1 test_separators(dict, ',', '\'');
101 1 test_separators(dict, '\'', '"');
102 1 test_separators(dict, '"', '\'');
103 1 av_dict_free(&dict);
104
105 1 printf("\nTesting av_dict_set()\n");
106 1 av_dict_set(&dict, "a", "a", 0);
107 1 av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
108 1 av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
109 1 av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
110 1 av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
111 1 av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
112 1 av_dict_set(&dict, "f", "f", 0);
113 1 av_dict_set(&dict, "f", NULL, 0);
114 1 av_dict_set(&dict, "ff", "f", 0);
115 1 av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
116
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_get(dict, NULL, NULL, 0))
117 printf("av_dict_get() does not correctly handle NULL key.\n");
118 1 e = NULL;
119
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 while ((e = dict_iterate(dict, e)))
120 6 printf("%s %s\n", e->key, e->value);
121 1 av_dict_free(&dict);
122
123
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 ||
124
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 av_dict_set(&dict, NULL, "b", 0) >= 0 ||
125
1/2
✓ Branch 1 taken 1 times.
✗ Branch 2 not taken.
2 av_dict_set(&dict, NULL, NULL, AV_DICT_DONT_STRDUP_KEY) >= 0 ||
126
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 ||
127 1 av_dict_count(dict))
128 printf("av_dict_set does not correctly handle NULL key\n");
129
130 1 e = NULL;
131
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 while ((e = dict_iterate(dict, e)))
132 printf("'%s' '%s'\n", e->key, e->value);
133 1 av_dict_free(&dict);
134
135
136 //valgrind sensible test
137 1 printf("\nTesting av_dict_set_int()\n");
138 1 av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
139 1 av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
140 1 av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
141 1 av_dict_set_int(&dict, "4", 4, 0);
142 1 av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
143 1 av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
144 1 av_dict_set_int(&dict, "12", 1, 0);
145 1 av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
146 1 e = NULL;
147
2/2
✓ Branch 1 taken 6 times.
✓ Branch 2 taken 1 times.
7 while ((e = dict_iterate(dict, e)))
148 6 printf("%s %s\n", e->key, e->value);
149 1 av_dict_free(&dict);
150
151 //valgrind sensible test
152 1 printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
153
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, "key", "old", 0) < 0)
154 return 1;
155 1 e = av_dict_get(dict, "key", NULL, 0);
156
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, e->key, "new val OK", 0) < 0)
157 return 1;
158 1 e = av_dict_get(dict, "key", NULL, 0);
159 1 printf("%s\n", e->value);
160
1/2
✗ Branch 1 not taken.
✓ Branch 2 taken 1 times.
1 if (av_dict_set(&dict, e->key, e->value, 0) < 0)
161 return 1;
162 1 e = av_dict_get(dict, "key", NULL, 0);
163 1 printf("%s\n", e->value);
164 1 av_dict_free(&dict);
165
166 1 return 0;
167 }
168