diff mbox

[FFmpeg-devel] libavcodec/tests: Added test for libavcodec/avpacket.c

Message ID 1478174140-10366-1-git-send-email-thomastdt@googlemail.com
State Superseded
Headers show

Commit Message

Thomas Turner Nov. 3, 2016, 11:55 a.m. UTC
Function(s) Tested: av_packet_clone().

This test checks if av_packet_clone() can successfully make a copy of an
AVPacket. Compares all data members in AVPacket EXCEPT for "buf" because "buf" is
initialized to NIL in the original AVPacket [to be cloned].

Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
---
 libavcodec/Makefile         |   3 +-
 libavcodec/tests/avpacket.c | 205 ++++++++++++++++++++++++++++++++++++++++++++
 tests/fate/libavcodec.mak   |   5 ++
 3 files changed, 212 insertions(+), 1 deletion(-)
 create mode 100644 libavcodec/tests/avpacket.c

Comments

Michael Niedermayer Nov. 3, 2016, 4:12 p.m. UTC | #1
On Thu, Nov 03, 2016 at 04:55:40AM -0700, Thomas Turner wrote:
> Function(s) Tested: av_packet_clone().
> 
> This test checks if av_packet_clone() can successfully make a copy of an
> AVPacket. Compares all data members in AVPacket EXCEPT for "buf" because "buf" is
> initialized to NIL in the original AVPacket [to be cloned].
> 
> Signed-off-by: Thomas Turner <thomastdt@googlemail.com>
> ---
>  libavcodec/Makefile         |   3 +-
>  libavcodec/tests/avpacket.c | 205 ++++++++++++++++++++++++++++++++++++++++++++
>  tests/fate/libavcodec.mak   |   5 ++
>  3 files changed, 212 insertions(+), 1 deletion(-)
>  create mode 100644 libavcodec/tests/avpacket.c
> 
> diff --git a/libavcodec/Makefile b/libavcodec/Makefile
> index f1d5bf1..46e3af7 100644
> --- a/libavcodec/Makefile
> +++ b/libavcodec/Makefile
> @@ -1019,7 +1019,8 @@ SKIPHEADERS-$(CONFIG_VDA)              += vda.h vda_vt_internal.h
>  SKIPHEADERS-$(CONFIG_VDPAU)            += vdpau.h vdpau_internal.h
>  SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX)     += videotoolbox.h vda_vt_internal.h
>  
> -TESTPROGS = imgconvert                                                  \
> +TESTPROGS = avpacket                                                    \
> +            imgconvert                                                  \
>              jpeg2000dwt                                                 \
>              mathops                                                    \
>              options                                                     \
> diff --git a/libavcodec/tests/avpacket.c b/libavcodec/tests/avpacket.c
> new file mode 100644
> index 0000000..784a5a4
> --- /dev/null
> +++ b/libavcodec/tests/avpacket.c
> @@ -0,0 +1,205 @@
> +/*
> + * 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 <inttypes.h>
> +#include <string.h>
> +#include "libavcodec/avcodec.h"
> +#include "libavutil/error.h"
> +
> +
> +
> +static const char* str = "Error @";
> +
> +static int compare_AVPacketsSideData(AVPacketSideData* sdata1,
> +                                     AVPacketSideData* sdata2)
> +{
> +    int ret = 0;
> +
> +    if(!sdata1 || !sdata2)
> +        return;
> +    av_log(NULL, AV_LOG_INFO, "Comparing sidedata data memebers...\n");
> +
> +    if(sdata1->size != sdata2->size){
> +        fprintf(stderr, "%s size\n", str);
> +        ret = 1;
> +    }
> +    if(sdata1->size > 0){
> +        if(memcmp(sdata1->data, sdata2->data, sdata1->size) != 0){
> +            fprintf(stderr, "%s data\n", str);
> +            ret = 1;
> +        }
> +    }
> +    else {
> +        av_log(NULL, AV_LOG_INFO, "size is <= 0");
> +        ret = 1;
> +    }
> +    if(sdata1->type != sdata2->type){
> +            fprintf(stderr, "%s type\n", str);
> +            ret = 1;
> +        }

indention is wrong here

also forcing a failure in one of these if() does not cause the
fate test to fail, the test still succeeds as it returns 0 and its
stdout/err output is not checked

make fate-avpacket
TEST    avpacket

Also i think the whole test is much more verbose than needed.

I think spending time on testing more functions makes more sense
than analysing the error case of one.
Code to analyze errors can be written if we ever hit an error.

Its different with tests outputing checksums, there details are
usefull so the developer can determine if a change is an error or a
intended consequence of some change to the codebase.
Here simply saying "error" if theres a difference is fine.
Code to print the details can be written if it actually gets hit.


[...]
diff mbox

Patch

diff --git a/libavcodec/Makefile b/libavcodec/Makefile
index f1d5bf1..46e3af7 100644
--- a/libavcodec/Makefile
+++ b/libavcodec/Makefile
@@ -1019,7 +1019,8 @@  SKIPHEADERS-$(CONFIG_VDA)              += vda.h vda_vt_internal.h
 SKIPHEADERS-$(CONFIG_VDPAU)            += vdpau.h vdpau_internal.h
 SKIPHEADERS-$(CONFIG_VIDEOTOOLBOX)     += videotoolbox.h vda_vt_internal.h
 
-TESTPROGS = imgconvert                                                  \
+TESTPROGS = avpacket                                                    \
+            imgconvert                                                  \
             jpeg2000dwt                                                 \
             mathops                                                    \
             options                                                     \
diff --git a/libavcodec/tests/avpacket.c b/libavcodec/tests/avpacket.c
new file mode 100644
index 0000000..784a5a4
--- /dev/null
+++ b/libavcodec/tests/avpacket.c
@@ -0,0 +1,205 @@ 
+/*
+ * 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 <inttypes.h>
+#include <string.h>
+#include "libavcodec/avcodec.h"
+#include "libavutil/error.h"
+
+
+
+static const char* str = "Error @";
+
+static int compare_AVPacketsSideData(AVPacketSideData* sdata1,
+                                     AVPacketSideData* sdata2)
+{
+    int ret = 0;
+
+    if(!sdata1 || !sdata2)
+        return;
+    av_log(NULL, AV_LOG_INFO, "Comparing sidedata data memebers...\n");
+
+    if(sdata1->size != sdata2->size){
+        fprintf(stderr, "%s size\n", str);
+        ret = 1;
+    }
+    if(sdata1->size > 0){
+        if(memcmp(sdata1->data, sdata2->data, sdata1->size) != 0){
+            fprintf(stderr, "%s data\n", str);
+            ret = 1;
+        }
+    }
+    else {
+        av_log(NULL, AV_LOG_INFO, "size is <= 0");
+        ret = 1;
+    }
+    if(sdata1->type != sdata2->type){
+            fprintf(stderr, "%s type\n", str);
+            ret = 1;
+        }
+    return ret;
+}
+
+static int compare_AVPackets(AVPacket* p1, AVPacket* p2)
+{
+    int ret = 0;
+
+    if(!p1 || !p2)
+        return;
+
+    av_log(NULL, AV_LOG_INFO, "Comparing AVPacket data memebers...\n");
+
+    if(p1->size != p2->size){
+        fprintf(stderr, "%s size\n", str);
+        ret = 1;
+    }
+    if(p1->size > 0){
+        if(memcmp(p1->data, p2->data, p1->size) != 0){
+            fprintf(stderr, "%s data\n", str);
+            ret = 1;
+        }
+    }
+    else {
+        av_log(NULL, AV_LOG_INFO, "size is <= 0");
+        ret = 1;
+    }
+    if(p1->pts != p2->pts){
+        fprintf(stderr, "%s pts\n", str);
+        ret = 1;
+    }
+    if(p1->dts != p2->dts){
+        fprintf(stderr, "%s dts\n", str);
+        ret = 1;
+    }
+    if(p1->stream_index != p2->stream_index){
+        fprintf(stderr, "%s stream_index\n", str);
+        ret = 1;
+    }
+    if(p1->flags != p2->flags){
+        fprintf(stderr, "%s flags\n", str);
+        ret = 1;
+    }
+    if(p1->side_data_elems != p2->side_data_elems){
+        fprintf(stderr, "%s side_data_elems\n", str);
+        ret = 1;
+    }
+    if(p1->duration != p2->duration){
+        fprintf(stderr, "%s duration\n", str);
+        ret = 1;
+    }
+    if(p1->pos != p2->pos){
+        fprintf(stderr, "%s pos\n", str);
+        ret = 1;
+    }
+
+    if(compare_AVPacketsSideData(p1->side_data, p2->side_data) > 0)
+        ret = 1;
+
+    av_log(NULL, AV_LOG_INFO, "Done comparing!\n");
+
+    return ret;
+}
+
+static void setup_side_data_entry(AVPacket* avpkt)
+{
+    const uint8_t *data_name = NULL;
+    int ret = 0, bytes;
+    uint8_t *extra_data = NULL;
+
+
+    /* get side_data_name string */
+    data_name = av_packet_side_data_name(AV_PKT_DATA_NEW_EXTRADATA);
+
+    /* Allocate a memory bloc */
+    bytes = strlen(data_name);
+
+    if(!(extra_data = av_malloc(bytes))){
+        ret = AVERROR(ENOMEM);
+        goto print_error;
+    }
+    /* copy side_data_name to extra_data array */
+    memcpy(extra_data, data_name, bytes);
+
+    /* create side data for AVPacket */
+    ret = av_packet_add_side_data(avpkt, AV_PKT_DATA_NEW_EXTRADATA,
+                                        extra_data, bytes);
+    if(ret < 0){
+        goto print_error;
+    }
+
+    return;
+
+print_error:
+    fprintf(stderr, "Error occurred: %s\n", av_err2str(ret));
+    exit(1);
+}
+
+static void initializations(AVPacket* avpkt)
+{
+    static uint8_t data[] = "selftest for av_packet_clone(...)";
+
+    /* initialize avpkt */
+    av_init_packet(avpkt);
+
+    /* set values for avpkt */
+    avpkt->pts = 17;
+    avpkt->dts = 2;
+    avpkt->data = data;
+    avpkt->size = strlen(data);
+    avpkt->flags = AV_PKT_FLAG_DISCARD;
+    avpkt->duration = 100;
+    avpkt->pos = 3;
+
+    setup_side_data_entry(avpkt);
+}
+
+static void test_av_packet_clone(void)
+{
+    AVPacket avpkt;
+    AVPacket *avpkt_clone = NULL;
+
+    initializations(&avpkt);
+
+    /* clone avpkt */
+    avpkt_clone = av_packet_clone(&avpkt);
+
+    if(avpkt_clone) {
+        /* compare avpkt and avpkt_clone*/
+        if(compare_AVPackets(&avpkt, avpkt_clone))
+            av_log(NULL, AV_LOG_ERROR, "Test failed\n");
+        else
+            av_log(NULL, AV_LOG_INFO, "Test passed\n");
+
+        /* cleanup */
+        av_packet_free(&avpkt_clone);
+        av_packet_unref(&avpkt);
+    }
+    else {
+        av_log(NULL, AV_LOG_ERROR, "av_packet_clone error\n");
+        exit(1);
+    }
+}
+
+int main(void)
+{
+    test_av_packet_clone();
+
+    return 0;
+}
diff --git a/tests/fate/libavcodec.mak b/tests/fate/libavcodec.mak
index cf25285..3bc74c1 100644
--- a/tests/fate/libavcodec.mak
+++ b/tests/fate/libavcodec.mak
@@ -1,3 +1,8 @@ 
+FATE_LIBAVCODEC-yes += fate-avpacket
+fate-avpacket: libavcodec/tests/avpacket$(EXESUF)
+fate-avpacket: CMD = run libavcodec/tests/avpacket
+fate-avpacket: REF = /dev/null
+
 FATE_LIBAVCODEC-$(CONFIG_CABAC) += fate-cabac
 fate-cabac: libavcodec/tests/cabac$(EXESUF)
 fate-cabac: CMD = run libavcodec/tests/cabac