]> git.sesse.net Git - ffmpeg/blob - libavutil/tests/dict.c
Merge commit '6f19bbcf8532d018d8d6d82e000738d0ac2385c9'
[ffmpeg] / libavutil / tests / dict.c
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/dict.c"
22
23 static void print_dict(const AVDictionary *m)
24 {
25     AVDictionaryEntry *t = NULL;
26     while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
27         printf("%s %s   ", t->key, t->value);
28     printf("\n");
29 }
30
31 static void test_separators(const AVDictionary *m, const char pair, const char val)
32 {
33     AVDictionary *dict = NULL;
34     char pairs[] = {pair , '\0'};
35     char vals[]  = {val, '\0'};
36
37     char *buffer = NULL;
38     av_dict_copy(&dict, m, 0);
39     print_dict(dict);
40     av_dict_get_string(dict, &buffer, val, pair);
41     printf("%s\n", buffer);
42     av_dict_free(&dict);
43     av_dict_parse_string(&dict, buffer, vals, pairs, 0);
44     av_freep(&buffer);
45     print_dict(dict);
46     av_dict_free(&dict);
47 }
48
49 int main(void)
50 {
51     AVDictionary *dict = NULL;
52     AVDictionaryEntry *e;
53     char *buffer = NULL;
54
55     printf("Testing av_dict_get_string() and av_dict_parse_string()\n");
56     av_dict_get_string(dict, &buffer, '=', ',');
57     printf("%s\n", buffer);
58     av_freep(&buffer);
59     av_dict_set(&dict, "aaa", "aaa", 0);
60     av_dict_set(&dict, "b,b", "bbb", 0);
61     av_dict_set(&dict, "c=c", "ccc", 0);
62     av_dict_set(&dict, "ddd", "d,d", 0);
63     av_dict_set(&dict, "eee", "e=e", 0);
64     av_dict_set(&dict, "f,f", "f=f", 0);
65     av_dict_set(&dict, "g=g", "g,g", 0);
66     test_separators(dict, ',', '=');
67     av_dict_free(&dict);
68     av_dict_set(&dict, "aaa", "aaa", 0);
69     av_dict_set(&dict, "bbb", "bbb", 0);
70     av_dict_set(&dict, "ccc", "ccc", 0);
71     av_dict_set(&dict, "\\,=\'\"", "\\,=\'\"", 0);
72     test_separators(dict, '"',  '=');
73     test_separators(dict, '\'', '=');
74     test_separators(dict, ',', '"');
75     test_separators(dict, ',', '\'');
76     test_separators(dict, '\'', '"');
77     test_separators(dict, '"', '\'');
78     av_dict_free(&dict);
79
80     printf("\nTesting av_dict_set()\n");
81     av_dict_set(&dict, "a", "a", 0);
82     av_dict_set(&dict, "b", av_strdup("b"), AV_DICT_DONT_STRDUP_VAL);
83     av_dict_set(&dict, av_strdup("c"), "c", AV_DICT_DONT_STRDUP_KEY);
84     av_dict_set(&dict, av_strdup("d"), av_strdup("d"), AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
85     av_dict_set(&dict, "e", "e", AV_DICT_DONT_OVERWRITE);
86     av_dict_set(&dict, "e", "f", AV_DICT_DONT_OVERWRITE);
87     av_dict_set(&dict, "f", "f", 0);
88     av_dict_set(&dict, "f", NULL, 0);
89     av_dict_set(&dict, "ff", "f", 0);
90     av_dict_set(&dict, "ff", "f", AV_DICT_APPEND);
91     e = NULL;
92     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
93         printf("%s %s\n", e->key, e->value);
94     av_dict_free(&dict);
95
96     av_dict_set(&dict, NULL, "a", 0);
97     av_dict_set(&dict, NULL, "b", 0);
98     av_dict_get(dict, NULL, NULL, 0);
99     e = NULL;
100     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
101         printf("'%s' '%s'\n", e->key, e->value);
102     av_dict_free(&dict);
103
104
105     //valgrind sensible test
106     printf("\nTesting av_dict_set_int()\n");
107     av_dict_set_int(&dict, "1", 1, AV_DICT_DONT_STRDUP_VAL);
108     av_dict_set_int(&dict, av_strdup("2"), 2, AV_DICT_DONT_STRDUP_KEY);
109     av_dict_set_int(&dict, av_strdup("3"), 3, AV_DICT_DONT_STRDUP_KEY | AV_DICT_DONT_STRDUP_VAL);
110     av_dict_set_int(&dict, "4", 4, 0);
111     av_dict_set_int(&dict, "5", 5, AV_DICT_DONT_OVERWRITE);
112     av_dict_set_int(&dict, "5", 6, AV_DICT_DONT_OVERWRITE);
113     av_dict_set_int(&dict, "12", 1, 0);
114     av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
115     e = NULL;
116     while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
117         printf("%s %s\n", e->key, e->value);
118     av_dict_free(&dict);
119
120     //valgrind sensible test
121     printf("\nTesting av_dict_set() with existing AVDictionaryEntry.key as key\n");
122     av_dict_set(&dict, "key", "old", 0);
123     e = av_dict_get(dict, "key", NULL, 0);
124     av_dict_set(&dict, e->key, "new val OK", 0);
125     e = av_dict_get(dict, "key", NULL, 0);
126     printf("%s\n", e->value);
127     av_dict_set(&dict, e->key, e->value, 0);
128     e = av_dict_get(dict, "key", NULL, 0);
129     printf("%s\n", e->value);
130     av_dict_free(&dict);
131
132     return 0;
133 }