[FFmpeg-devel] [PATCH 7/7] lavu: add JSON writer test.

Nicolas George george at nsup.org
Wed Apr 21 15:27:06 EEST 2021


Signed-off-by: Nicolas George <george at nsup.org>
---
 libavutil/Makefile       |   1 +
 libavutil/tests/json.c   | 139 +++++++++++++++++++++++++++++++++++++++
 tests/fate/libavutil.mak |   4 ++
 3 files changed, 144 insertions(+)
 create mode 100644 libavutil/tests/json.c

diff --git a/libavutil/Makefile b/libavutil/Makefile
index 6ad31426e5..6729eb81ae 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -231,6 +231,7 @@ TESTPROGS = adler32                                                     \
             hwdevice                                                    \
             integer                                                     \
             imgutils                                                    \
+	    json                                                        \
             lfg                                                         \
             lls                                                         \
             log                                                         \
diff --git a/libavutil/tests/json.c b/libavutil/tests/json.c
new file mode 100644
index 0000000000..4e4106889e
--- /dev/null
+++ b/libavutil/tests/json.c
@@ -0,0 +1,139 @@
+/*
+ * Copyright (c) 2021 Nicolas George
+ *
+ * This file is part of FFmpeg.
+ *
+ * FFmpeg is free software; you can redistribute it and/or
+ * modify it under the terms of the GNU Lesser General Public
+ * License as published by the Free Software Foundation; either
+ * version 2.1 of the License, or (at your option) any later version.
+ *
+ * FFmpeg is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
+ * Lesser General Public License for more details.
+ *
+ * You should have received a copy of the GNU Lesser General Public
+ * License along with FFmpeg; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include <stdio.h>
+#include <stdlib.h>
+
+#include "libavutil/json.h"
+#include "libavutil/opt.h"
+
+static void print_success(AVWriter wr)
+{
+    printf(" (%s)\n", av_writer_get_error(wr, 0) ? "error" : "ok");
+}
+
+static void test_escape_writer(void)
+{
+    AVWriter out = av_stdio_writer(stdout);
+    AVWriter wr;
+
+    wr = av_json_escape_writer(out, 0);
+    av_writer_printf(wr, "Test of the \"JSON\" writer.\n"
+                         "It will \aring.\n"
+                         "c∈UBMP.\n"
+                         "𝄞 in UTF-16");
+    print_success(wr);
+    wr = av_json_escape_writer(out, AV_JSON_FLAG_BAD_ENCODING_REPLACE);
+    av_writer_printf(wr, "Text\bbadly\222encoded");
+    print_success(wr);
+}
+
+static void test_json_context(void)
+{
+    AVJson *jc = AV_JSON_DEFINE(8);
+    AVWriter out = av_stdio_writer(stdout);
+
+    av_opt_set_int(jc, "indent", 2, 0);
+    av_json_init(jc, out, 0, NULL);
+    av_json_begin_object(jc);
+      av_json_add_string(jc, "filename");
+      av_json_add_string(jc, "example.nut");
+      av_json_add_string(jc, "streams");
+      av_json_begin_array(jc);
+        av_json_begin_object(jc);
+          av_json_add_string(jc, "type");
+          av_json_add_string(jc, "video");
+        av_json_end_object(jc);
+        av_json_add_string(jc, "null");
+        av_json_begin_object(jc);
+          av_json_add_string(jc, "type");
+          av_json_add_string(jc, "audio");
+        av_json_end_object(jc);
+      av_json_end_array(jc);
+      av_json_add_string(jc, "duration");
+      av_json_add_string(jc, "42");
+    av_json_end_object(jc);
+}
+
+static unsigned rnd_next(unsigned *rnd, unsigned bits)
+{
+    *rnd = (*rnd * 1664525 + 1013904223) & 0xFFFFFFFF;
+    return *rnd >> (32 - bits);
+}
+
+static void test_json_context_rec(AVJson *jc, unsigned max_depth, unsigned obj, unsigned *rnd)
+{
+    unsigned n, i, t, d;
+
+    n = rnd_next(rnd, 3);
+    for (i = 0; i < n; i++) {
+        if (obj)
+            av_json_add_string_printf(jc, "key %d/%d[%d]", i, n, max_depth);
+        t = rnd_next(rnd, max_depth > 0 ? 3 : 2);
+        switch (t) {
+        case 0:
+            av_json_add_string_printf(jc, "random string #%u", rnd_next(rnd, 32));
+            break;
+        case 1:
+            av_json_add_int(jc, rnd_next(rnd, 32));
+            break;
+        case 2:
+            d = rnd_next(rnd, 4) + 8;
+            av_json_add_double(jc, rnd_next(rnd, 32) / (double)(1 << d));
+            break;
+        case 3:
+            av_json_add_bool(jc, rnd_next(rnd, 1));
+            break;
+        case 4:
+        case 5:
+            av_json_begin_object(jc);
+            test_json_context_rec(jc, max_depth - 1, 1, rnd);
+            av_json_end_object(jc);
+            break;
+        case 6:
+        case 7:
+            av_json_begin_array(jc);
+            test_json_context_rec(jc, max_depth - 1, 0, rnd);
+            av_json_end_array(jc);
+            break;
+        }
+    }
+}
+
+static void test_json_context_deep(void)
+{
+    unsigned rnd = 0x12345678;
+    AVJson *jc = AV_JSON_DEFINE();
+    AVWriter out = av_stdio_writer(stdout);
+
+    av_opt_set_int(jc, "indent", 2, 0);
+    av_json_init(jc, out, AV_JSON_FLAG_PRETTY_PRINT, NULL);
+    av_json_begin_object(jc);
+    test_json_context_rec(jc, 5, 1, &rnd);
+    av_json_end_object(jc);
+}
+
+int main(int argc, char **argv)
+{
+    if (0) test_escape_writer();
+    if (0) test_json_context();
+    if (1) test_json_context_deep();
+    return 0;
+}
diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak
index 651f065d10..b683c99edd 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -100,6 +100,10 @@ fate-integer: libavutil/tests/integer$(EXESUF)
 fate-integer: CMD = run libavutil/tests/integer$(EXESUF)
 fate-integer: CMP = null
 
+FATE_LIBAVUTIL += fate-json
+fate-json: libavutil/tests/json$(EXESUF)
+fate-json: CMD = run libavutil/tests/json$(EXESUF)
+
 FATE_LIBAVUTIL += fate-lfg
 fate-lfg: libavutil/tests/lfg$(EXESUF)
 fate-lfg: CMD = run libavutil/tests/lfg$(EXESUF)
-- 
2.30.2



More information about the ffmpeg-devel mailing list