From: Nicolas George <george@nsup.org> To: ffmpeg-devel@ffmpeg.org Subject: [FFmpeg-devel] [PATCH 3/8] lavu/writer: add test Date: Fri, 28 Apr 2023 11:55:03 +0200 Message-ID: <20230428095508.221826-3-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/.gitignore | 1 + libavutil/tests/writer.c | 192 +++++++++++++++++++++++++++++++++++++ tests/fate/libavutil.mak | 4 + tests/ref/fate/writer | 36 +++++++ 5 files changed, 234 insertions(+) create mode 100644 libavutil/tests/writer.c create mode 100644 tests/ref/fate/writer diff --git a/libavutil/Makefile b/libavutil/Makefile index eb8de1dfbc..4526ec80ca 100644 --- a/libavutil/Makefile +++ b/libavutil/Makefile @@ -269,6 +269,7 @@ TESTPROGS = adler32 \ uuid \ xtea \ tea \ + writer \ TESTPROGS-$(HAVE_THREADS) += cpu_init TESTPROGS-$(HAVE_LZO1X_999_COMPRESS) += lzo diff --git a/libavutil/tests/.gitignore b/libavutil/tests/.gitignore index 87895912f5..7559e7a24c 100644 --- a/libavutil/tests/.gitignore +++ b/libavutil/tests/.gitignore @@ -51,3 +51,4 @@ /utf8 /uuid /xtea +/writer diff --git a/libavutil/tests/writer.c b/libavutil/tests/writer.c new file mode 100644 index 0000000000..fdb8c4d703 --- /dev/null +++ b/libavutil/tests/writer.c @@ -0,0 +1,192 @@ +/* + * Copyright (c) 2019 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 <stdint.h> +#include <string.h> + +#include "libavutil/avassert.h" +#include "libavutil/error.h" +#include "libavutil/log.h" +#include "libavutil/writer.h" + +/* see 69bd73b3ff873abb43de9db062b04425de153643 */ + +/* AVWriter with only get_buffer/advance_buffer */ + +static char *getbuf_get_buffer(AVWriter wr, size_t min_size, size_t *size); +static void getbuf_advance_buffer(AVWriter wr, size_t size); + +AV_WRITER_DEFINE_METHODS(static, Getbuf, getbuf) { + .self_size = sizeof(AVWriterMethods), + .name = "Getbuf", + .get_buffer = getbuf_get_buffer, + .advance_buffer = getbuf_advance_buffer, +}; + +static char *getbuf_get_buffer(AVWriter wr, size_t min_size, size_t *size) +{ + AVBufWriter *bwr = wr.obj; + + av_assert0(getbuf_check(wr)); + *size = bwr->size - 1 - bwr->pos; + return bwr->buf + bwr->pos; +} + +static void getbuf_advance_buffer(AVWriter wr, size_t size) +{ + AVBufWriter *bwr = wr.obj; + + bwr->pos += size; + bwr->buf[bwr->pos] = 0; +} + +/* AVWriter with only write */ + +static void write_write(AVWriter wr, const char *data, size_t size); + +AV_WRITER_DEFINE_METHODS(static, Write, write) { + .self_size = sizeof(AVWriterMethods), + .name = "Write", + .write = write_write, +}; + +static void write_write(AVWriter wr, const char *data, size_t size) +{ + AVBufWriter *bwr = wr.obj; + + av_assert0(write_check(wr)); + size = FFMIN(bwr->size - 1 - bwr->pos, size); + memcpy(bwr->buf + bwr->pos, data, size); + bwr->pos += size; + bwr->buf[bwr->pos] = 0; +} + +/* AVWriter with only vprintf */ + +static void vprintf_vprintf(AVWriter wr, const char *fmt, va_list va); + +AV_WRITER_DEFINE_METHODS(static, Vprintf, vprintf) { + .self_size = sizeof(AVWriterMethods), + .name = "Vprintf", + .vprintf = vprintf_vprintf, +}; + +static void vprintf_vprintf(AVWriter wr, const char *fmt, va_list va) +{ + AVBufWriter *bwr = wr.obj; + int ret; + + av_assert0(vprintf_check(wr)); + ret = vsnprintf(bwr->buf + bwr->pos, bwr->size - bwr->pos, fmt, va); + if (ret > 0) + bwr->pos += ret; +} + +/* Tests */ + +static void stress_writer(AVWriter wr) +{ + av_writer_add_chars(wr, '*', 72); + av_writer_add_chars(wr, '\n', 1); + av_writer_print(wr, "Stressing the writer\n"); + av_writer_printf(wr, "Answer: %d\n", 42); + av_writer_printf(wr, "Question: %0600d * %0600d\n", 6, 9); /* > sizeof(AVBPrint) */ + av_writer_add_chars(wr, '*', 72); + av_writer_add_chars(wr, '\n', 1); + av_writer_write(wr, "\0Bonus track!\n", 14); +} + +static void test_buf_writer_small(void) +{ + char buf[1024]; + AVWriter wr = av_buf_writer_array(buf); + stress_writer(wr); + printf("AVBufWriter/1024: [%s]\n", buf); +} + +static void test_buf_writer_large(void) +{ + char buf[8192]; + AVWriter wr = av_buf_writer_array(buf); + stress_writer(wr); + printf("AVDynbufWriter/8192: [%s]\n", buf); +} + +static void test_dynbuf_writer(void) +{ + AVWriter wr = av_dynbuf_writer(); + char *data; + size_t size, i; + int ret; + + stress_writer(wr); + data = av_dynbuf_writer_get_buffer(wr, 100, &size); + ret = snprintf(data, size, "Some more?\n"); + av_dynbuf_writer_advance_buffer(wr, ret); + ret = av_writer_get_error(wr, 0); + if (ret < 0) { + av_log(NULL, AV_LOG_ERROR, "DynbufWriter (%zd chars): %s\n", size, av_err2str(ret)); + return; + } + data = av_dynbuf_writer_get_data(wr, &size); + for (i = 0; i < size; i++) + if (data[i] == 0) + data[i] = '@'; /* avoid confusing diff */ + printf("AVDynbufWriter (%zd chars): [", size); + fwrite(data, 1, size, stdout); + printf("]\n"); + av_dynbuf_writer_finalize(wr, NULL, NULL); +} + +static void test_writer_limited(const char *name, const AVWriterMethods *methods) +{ + char buf[8192]; + AVBufWriter bwr = { sizeof(AVBufWriter), buf, sizeof(buf), 0 }; + AVWriter wr = { methods, &bwr }; + stress_writer(wr); + printf("%s: [%s]\n", name, buf); +} + +static void test_writer_get_buffer(void) +{ + test_writer_limited("get_buffer", getbuf_get_methods()); +} + +static void test_writer_write(void) +{ + test_writer_limited("write", write_get_methods()); +} + +static void test_writer_vprintf(void) +{ + test_writer_limited("vprintf", vprintf_get_methods()); +} + +int main(int argc, char **argv) +{ + if (1) test_buf_writer_small(); + if (1) test_buf_writer_large(); + if (1) test_dynbuf_writer(); + if (1) test_writer_get_buffer(); + if (1) test_writer_write(); + if (1) test_writer_vprintf(); + return 0; +} diff --git a/tests/fate/libavutil.mak b/tests/fate/libavutil.mak index 80153f4395..a9dcc9ec87 100644 --- a/tests/fate/libavutil.mak +++ b/tests/fate/libavutil.mak @@ -175,6 +175,10 @@ fate-uuid: libavutil/tests/uuid$(EXESUF) fate-uuid: CMD = run libavutil/tests/uuid$(EXESUF) fate-uuid: CMP = null +FATE_LIBAVUTIL += fate-writer +fate-writer: libavutil/tests/writer$(EXESUF) +fate-writer: CMD = run libavutil/tests/writer$(EXESUF) + FATE_LIBAVUTIL += $(FATE_LIBAVUTIL-yes) FATE-$(CONFIG_AVUTIL) += $(FATE_LIBAVUTIL) fate-libavutil: $(FATE_LIBAVUTIL) diff --git a/tests/ref/fate/writer b/tests/ref/fate/writer new file mode 100644 index 0000000000..3068d83a4a --- /dev/null +++ b/tests/ref/fate/writer @@ -0,0 +1,36 @@ +AVBufWriter/1024: [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 00000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000] +AVDynbufWriter/8192: [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +************************************************************************ +] +AVDynbufWriter (1417 chars): [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +************************************************************************ +@Bonus track! +Some more? +] +get_buffer: [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +************************************************************************ +] +write: [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +************************************************************************ +] +vprintf: [************************************************************************ +Stressing the writer +Answer: 42 +Question: 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000006 * 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000 000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000000009 +************************************************************************ +] -- 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:55 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 ` Nicolas George [this message] 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 ` [FFmpeg-devel] [PATCH 6/8] lavu: add JSON writer test (WIP) Nicolas George 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-3-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