diff mbox series

[FFmpeg-devel] avutil/tests/dict: Explicitly test av_dict_iterate()

Message ID GV1P250MB07374B410C76CAC0F061E9248F169@GV1P250MB0737.EURP250.PROD.OUTLOOK.COM
State Accepted
Commit 6aa7b0c463bffad995eb84730f5c0bcb294f3cad
Headers show
Series [FFmpeg-devel] avutil/tests/dict: Explicitly test av_dict_iterate() | expand

Checks

Context Check Description
yinshiyou/make_loongarch64 success Make finished
yinshiyou/make_fate_loongarch64 success Make fate finished
andriy/make_x86 success Make finished
andriy/make_fate_x86 success Make fate finished

Commit Message

Andreas Rheinhardt Dec. 3, 2022, 5:25 p.m. UTC
This commit tests it in a way that automatically checks
that using av_dict_iterate() is equivalent to using
av_dict_get() with key "" and AV_DICT_IGNORE_SUFFIX.

Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
---
 libavutil/tests/dict.c | 30 +++++++++++++++++++++++++-----
 1 file changed, 25 insertions(+), 5 deletions(-)

Comments

Andreas Rheinhardt Dec. 5, 2022, 6:23 a.m. UTC | #1
Andreas Rheinhardt:
> This commit tests it in a way that automatically checks
> that using av_dict_iterate() is equivalent to using
> av_dict_get() with key "" and AV_DICT_IGNORE_SUFFIX.
> 
> Signed-off-by: Andreas Rheinhardt <andreas.rheinhardt@outlook.com>
> ---
>  libavutil/tests/dict.c | 30 +++++++++++++++++++++++++-----
>  1 file changed, 25 insertions(+), 5 deletions(-)
> 
> diff --git a/libavutil/tests/dict.c b/libavutil/tests/dict.c
> index 91567289c2..bececefb31 100644
> --- a/libavutil/tests/dict.c
> +++ b/libavutil/tests/dict.c
> @@ -20,10 +20,30 @@
>  
>  #include "libavutil/dict.c"
>  
> +static const AVDictionaryEntry *dict_iterate(const AVDictionary *m,
> +                                             const AVDictionaryEntry *prev)
> +{
> +    const AVDictionaryEntry *dict_get = av_dict_get(m, "", prev, AV_DICT_IGNORE_SUFFIX);
> +    const AVDictionaryEntry *dict_iterate = av_dict_iterate(m, prev);
> +
> +    if (dict_get != dict_iterate) {
> +#define GET(entry, mem) ((entry) ? (entry)->mem : "N/A")
> +        printf("Iterating with av_dict_iterate() yields a different result "
> +               "than iterating with av_dict_get() and AV_DICT_IGNORE_SUFFIX "
> +               "(prev: %p, key %s; av_dict_iterate() %p, key %s, value %s; "
> +               "av_dict_get() %p, key %s, value %s)\n",
> +               prev, GET(prev, key),
> +               dict_iterate, GET(dict_iterate, key), GET(dict_iterate, value),
> +               dict_get, GET(dict_get, key), GET(dict_get, value));
> +#undef GET
> +    }
> +    return dict_iterate;
> +}
> +
>  static void print_dict(const AVDictionary *m)
>  {
> -    AVDictionaryEntry *t = NULL;
> -    while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
> +    const AVDictionaryEntry *t = NULL;
> +    while ((t = dict_iterate(m, t)))
>          printf("%s %s   ", t->key, t->value);
>      printf("\n");
>  }
> @@ -94,7 +114,7 @@ int main(void)
>      if (av_dict_get(dict, NULL, NULL, 0))
>          printf("av_dict_get() does not correctly handle NULL key.\n");
>      e = NULL;
> -    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
> +    while ((e = dict_iterate(dict, e)))
>          printf("%s %s\n", e->key, e->value);
>      av_dict_free(&dict);
>  
> @@ -106,7 +126,7 @@ int main(void)
>          printf("av_dict_set does not correctly handle NULL key\n");
>  
>      e = NULL;
> -    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
> +    while ((e = dict_iterate(dict, e)))
>          printf("'%s' '%s'\n", e->key, e->value);
>      av_dict_free(&dict);
>  
> @@ -122,7 +142,7 @@ int main(void)
>      av_dict_set_int(&dict, "12", 1, 0);
>      av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
>      e = NULL;
> -    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
> +    while ((e = dict_iterate(dict, e)))
>          printf("%s %s\n", e->key, e->value);
>      av_dict_free(&dict);
>  

Will apply this patch as well as the remaining one of Marvin's patches
(the one from #7: "avutil: use av_dict_iterate") tonight unless there
are objections.

- Andreas
diff mbox series

Patch

diff --git a/libavutil/tests/dict.c b/libavutil/tests/dict.c
index 91567289c2..bececefb31 100644
--- a/libavutil/tests/dict.c
+++ b/libavutil/tests/dict.c
@@ -20,10 +20,30 @@ 
 
 #include "libavutil/dict.c"
 
+static const AVDictionaryEntry *dict_iterate(const AVDictionary *m,
+                                             const AVDictionaryEntry *prev)
+{
+    const AVDictionaryEntry *dict_get = av_dict_get(m, "", prev, AV_DICT_IGNORE_SUFFIX);
+    const AVDictionaryEntry *dict_iterate = av_dict_iterate(m, prev);
+
+    if (dict_get != dict_iterate) {
+#define GET(entry, mem) ((entry) ? (entry)->mem : "N/A")
+        printf("Iterating with av_dict_iterate() yields a different result "
+               "than iterating with av_dict_get() and AV_DICT_IGNORE_SUFFIX "
+               "(prev: %p, key %s; av_dict_iterate() %p, key %s, value %s; "
+               "av_dict_get() %p, key %s, value %s)\n",
+               prev, GET(prev, key),
+               dict_iterate, GET(dict_iterate, key), GET(dict_iterate, value),
+               dict_get, GET(dict_get, key), GET(dict_get, value));
+#undef GET
+    }
+    return dict_iterate;
+}
+
 static void print_dict(const AVDictionary *m)
 {
-    AVDictionaryEntry *t = NULL;
-    while ((t = av_dict_get(m, "", t, AV_DICT_IGNORE_SUFFIX)))
+    const AVDictionaryEntry *t = NULL;
+    while ((t = dict_iterate(m, t)))
         printf("%s %s   ", t->key, t->value);
     printf("\n");
 }
@@ -94,7 +114,7 @@  int main(void)
     if (av_dict_get(dict, NULL, NULL, 0))
         printf("av_dict_get() does not correctly handle NULL key.\n");
     e = NULL;
-    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
+    while ((e = dict_iterate(dict, e)))
         printf("%s %s\n", e->key, e->value);
     av_dict_free(&dict);
 
@@ -106,7 +126,7 @@  int main(void)
         printf("av_dict_set does not correctly handle NULL key\n");
 
     e = NULL;
-    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
+    while ((e = dict_iterate(dict, e)))
         printf("'%s' '%s'\n", e->key, e->value);
     av_dict_free(&dict);
 
@@ -122,7 +142,7 @@  int main(void)
     av_dict_set_int(&dict, "12", 1, 0);
     av_dict_set_int(&dict, "12", 2, AV_DICT_APPEND);
     e = NULL;
-    while ((e = av_dict_get(dict, "", e, AV_DICT_IGNORE_SUFFIX)))
+    while ((e = dict_iterate(dict, e)))
         printf("%s %s\n", e->key, e->value);
     av_dict_free(&dict);