From: Nicolas George <george@nsup.org>
To: ffmpeg-devel@ffmpeg.org
Subject: [FFmpeg-devel] [PATCH 6/8] lavu: add JSON writer test (WIP)
Date: Fri, 28 Apr 2023 11:55:06 +0200
Message-ID: <20230428095508.221826-6-george@nsup.org> (raw)
In-Reply-To: <20230428095508.221826-1-george@nsup.org>
Signed-off-by: Nicolas George <george@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 a8a9700778..8800d25831 100644
--- a/libavutil/Makefile
+++ b/libavutil/Makefile
@@ -247,6 +247,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 a9dcc9ec87..7dfbf02527 100644
--- a/tests/fate/libavutil.mak
+++ b/tests/fate/libavutil.mak
@@ -104,6 +104,10 @@ fate-integer: libavutil/tests/integer$(EXESUF)
fate-integer: CMD = run libavutil/tests/integer$(EXESUF)
fate-integer: CMP = null
+# XXX 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.39.2
_______________________________________________
ffmpeg-devel mailing list
ffmpeg-devel@ffmpeg.org
https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
To unsubscribe, visit link above, or email
ffmpeg-devel-request@ffmpeg.org with subject "unsubscribe".
next prev parent reply other threads:[~2023-04-28 9:56 UTC|newest]
Thread overview: 38+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-04-28 9:55 [FFmpeg-devel] [PATCH 1/8] lavu: add macros to help making future-proof structures Nicolas George
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 2/8] lavu: new AVWriter API Nicolas George
2023-04-28 10:37 ` Rodney Baker
2023-04-28 11:20 ` Nicolas George
2023-05-02 15:53 ` Rémi Denis-Courmont
2023-05-02 16:53 ` Nicolas George
2023-05-02 18:29 ` Rémi Denis-Courmont
2023-05-02 18:36 ` Nicolas George
2023-05-02 18:46 ` Rémi Denis-Courmont
2023-05-02 18:47 ` Nicolas George
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 3/8] lavu/writer: add test Nicolas George
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 4/8] lavf/dump: use a writer Nicolas George
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 5/8] lavu: add a JSON writer API (WIP) Nicolas George
2023-04-29 9:11 ` Nicolas George
2023-04-29 9:41 ` Anton Khirnov
2023-04-29 14:06 ` James Almer
2023-04-29 17:17 ` Nicolas George
2023-04-29 15:06 ` Derek Buitenhuis
2023-04-30 0:29 ` Kieran Kunhya
2023-05-01 6:20 ` Vittorio Giovara
2023-04-29 17:11 ` Nicolas George
2023-04-29 18:27 ` Anton Khirnov
2023-04-29 18:33 ` Nicolas George
2023-05-01 6:57 ` Leo Izen
2023-05-01 9:51 ` Nicolas George
2023-05-01 10:18 ` Jean-Baptiste Kempf
2023-04-30 15:06 ` Michael Niedermayer
2023-04-30 21:51 ` Kieran Kunhya
2023-05-01 9:46 ` Nicolas George
2023-04-28 9:55 ` Nicolas George [this message]
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 7/8] lavf/options: add av_disposition_write() Nicolas George
2023-04-28 9:55 ` [FFmpeg-devel] [PATCH 8/8] lavf/dump: use av_disposition_write() Nicolas George
2023-04-29 8:17 ` [FFmpeg-devel] [PATCH 1/8] lavu: add macros to help making future-proof structures Anton Khirnov
2023-04-29 15:11 ` Derek Buitenhuis
2023-05-02 15:36 ` Rémi Denis-Courmont
2023-05-02 16:42 ` Nicolas George
2023-05-02 18:31 ` Rémi Denis-Courmont
2023-05-02 18:38 ` Nicolas George
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20230428095508.221826-6-george@nsup.org \
--to=george@nsup.org \
--cc=ffmpeg-devel@ffmpeg.org \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Git Inbox Mirror of the ffmpeg-devel mailing list - see https://ffmpeg.org/mailman/listinfo/ffmpeg-devel
This inbox may be cloned and mirrored by anyone:
git clone --mirror https://master.gitmailbox.com/ffmpegdev/0 ffmpegdev/git/0.git
# If you have public-inbox 1.1+ installed, you may
# initialize and index your mirror using the following commands:
public-inbox-init -V2 ffmpegdev ffmpegdev/ https://master.gitmailbox.com/ffmpegdev \
ffmpegdev@gitmailbox.com
public-inbox-index ffmpegdev
Example config snippet for mirrors.
AGPL code for this site: git clone https://public-inbox.org/public-inbox.git