From 9ad634029a185332261e87745341e69a1ea01e32 Mon Sep 17 00:00:00 2001 From: Sreerenj Balachandran Date: Tue, 18 Mar 2014 16:01:04 +0200 Subject: [PATCH] bitwriter: Add unit tests https://bugzilla.gnome.org/show_bug.cgi?id=707543 --- tests/check/Makefile.am | 1 + tests/check/libs/.gitignore | 1 + tests/check/libs/bitwriter.c | 167 +++++++++++++++++++++++++++++++++++ tests/check/meson.build | 1 + 4 files changed, 170 insertions(+) create mode 100644 tests/check/libs/bitwriter.c diff --git a/tests/check/Makefile.am b/tests/check/Makefile.am index 13b916da23..4c9d3446f3 100644 --- a/tests/check/Makefile.am +++ b/tests/check/Makefile.am @@ -154,6 +154,7 @@ check_PROGRAMS = \ libs/adapter \ libs/aggregator \ libs/bitreader \ + libs/bitwriter \ libs/bytereader \ libs/bytewriter \ libs/bitreader-noinline \ diff --git a/tests/check/libs/.gitignore b/tests/check/libs/.gitignore index 43cb049a1b..cdf0c9d989 100644 --- a/tests/check/libs/.gitignore +++ b/tests/check/libs/.gitignore @@ -6,6 +6,7 @@ basesink basesrc bitreader bitreader-noinline +bitwriter bytereader bytereader-noinline bytewriter diff --git a/tests/check/libs/bitwriter.c b/tests/check/libs/bitwriter.c new file mode 100644 index 0000000000..d6f68d32d9 --- /dev/null +++ b/tests/check/libs/bitwriter.c @@ -0,0 +1,167 @@ +/* GStreamer + * + * unit test for GstBitWriter + * + * Copyright (C) <2014> Intel Corporation + * Copyright (C) <2014> Sreerenj Balachandran + * + * This library is free software; you can redistribute it and/or + * modify it under the terms of the GNU Library General Public + * License as published by the Free Software Foundation; either + * version 2 of the License, or (at your option) any later version. + * + * This library 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 + * Library General Public License for more details. + * + * You should have received a copy of the GNU Library General Public + * License along with this library; if not, write to the + * Free Software Foundation, Inc., 51 Franklin St, Fifth Floor, + * Boston, MA 02110-1301, USA. + */ + +#ifdef HAVE_CONFIG_H +#include "config.h" +#endif + +#include +#include +#include +#include + +GST_START_TEST (test_initialization) +{ + GstBitWriter writer; + GstBitReader reader; + + static guint8 sdata[] = { 1, 2, 3, 4, 5, 6, 7, 8, 9 }; + + gst_bit_writer_init_with_size (&writer, 4, FALSE); + /* should be aligned to 256 bytes */ + fail_unless_equals_int (gst_bit_writer_get_remaining (&writer), 2048); + fail_unless_equals_int (gst_bit_writer_get_size (&writer), 0); + fail_unless_equals_int (gst_bit_writer_set_pos (&writer, 32), 1); + fail_unless_equals_int (gst_bit_writer_get_size (&writer), 32); + fail_unless_equals_int (gst_bit_writer_get_remaining (&writer), 2048 - 32); + gst_bit_writer_reset (&writer); + + gst_bit_writer_init_with_data (&writer, sdata, 8, FALSE); + gst_bit_reader_init (&reader, sdata, 8); + fail_unless_equals_int (gst_bit_reader_get_size (&reader), 64); + fail_unless_equals_int (gst_bit_reader_get_remaining (&reader), 64); + gst_bit_writer_reset (&writer); +} + +GST_END_TEST; + +GST_START_TEST (test_data) +{ + GstBitWriter writer; + GstBitReader reader; + guint8 val_uint8 = 0; + guint16 val_uint16 = 0; + guint32 val_uint32 = 0; + guint64 val_uint64 = 0; + static guint8 sdata[] = { 0xff, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 }; + guint8 *data, i; + + gst_bit_writer_init_with_size (&writer, 32, TRUE); + fail_unless_equals_int (gst_bit_writer_get_remaining (&writer), 2048); + + fail_unless (gst_bit_writer_put_bits_uint8 (&writer, 0x3f, 6)); + fail_unless (gst_bit_writer_put_bits_uint8 (&writer, 0x3, 2)); + fail_unless (gst_bit_writer_put_bits_uint16 (&writer, 0x15, 5)); + fail_unless (gst_bit_writer_put_bits_uint32 (&writer, 0x31, 10)); + fail_unless (gst_bit_writer_put_bits_uint64 (&writer, 0x45, 48)); + fail_unless_equals_int (gst_bit_writer_get_remaining (&writer), 2048 - 71); + fail_unless (gst_bit_writer_align_bytes (&writer, 0)); + data = g_memdup (sdata, sizeof (sdata)); + fail_unless (gst_bit_writer_put_bytes (&writer, data, sizeof (sdata))); + + gst_bit_reader_init (&reader, gst_bit_writer_get_data (&writer), 256); + fail_unless_equals_int (gst_bit_reader_get_size (&reader), 256 * 8); + fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &val_uint8, 6)); + fail_unless_equals_int (val_uint8, 0x3f); + fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &val_uint8, 2)); + fail_unless_equals_int (val_uint8, 0x3); + fail_unless (gst_bit_reader_get_bits_uint16 (&reader, &val_uint16, 5)); + fail_unless_equals_int_hex (val_uint16, 0x15); + fail_unless (gst_bit_reader_get_bits_uint32 (&reader, &val_uint32, 10)); + fail_unless_equals_int_hex (val_uint32, 0x31); + fail_unless (gst_bit_reader_get_bits_uint64 (&reader, &val_uint64, 48)); + fail_unless_equals_int_hex (val_uint64, 0x45); + fail_unless (gst_bit_reader_set_pos (&reader, 72)); + + for (i = 0; i < 8; i++) { + fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &val_uint8, 8)); + fail_unless (memcmp (&val_uint8, &data[i], 1) == 0); + } + + g_free (data); + gst_bit_writer_reset (&writer); +} + +GST_END_TEST; + +GST_START_TEST (test_reset) +{ + GstBitWriter writer, *writer2; + GstBitReader reader; + GstBuffer *buf; + GstMapInfo info; + static guint8 sdata[] = { 0xff, 0xf1, 0xf2, 0xf3, 0xf4, 0xf5, 0xf6, 0xf7 }; + guint8 *data; + guint8 val_uint8 = 0; + + gst_bit_writer_init_with_data (&writer, sdata, sizeof (sdata), TRUE); + data = gst_bit_writer_reset_and_get_data (&writer); + fail_unless (data != NULL); + + gst_bit_writer_init_with_data (&writer, sdata, sizeof (sdata), TRUE); + buf = gst_bit_writer_reset_and_get_buffer (&writer); + fail_unless (buf != NULL); + fail_unless (gst_buffer_map (buf, &info, GST_MAP_READWRITE)); + fail_unless (info.data); + fail_unless_equals_int (info.size, 8); + gst_bit_reader_init (&reader, info.data, 8); + fail_unless (gst_bit_reader_set_pos (&reader, 64 - 10)); + fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &val_uint8, 8)); + fail_unless_equals_int_hex (val_uint8, 0xbd); + fail_unless (gst_bit_reader_get_bits_uint8 (&reader, &val_uint8, 2)); + fail_unless_equals_int_hex (val_uint8, 0x3); + gst_buffer_unmap (buf, &info); + gst_buffer_unref (buf); + + writer2 = gst_bit_writer_new_with_size (3, TRUE); + data = gst_bit_writer_free_and_get_data (writer2); + /* no data, no buffer allocated */ + fail_unless (data == NULL); + + writer2 = gst_bit_writer_new_with_size (1, FALSE); + fail_unless (gst_bit_writer_put_bits_uint8 (writer2, 0xff, 8)); + data = gst_bit_writer_free_and_get_data (writer2); + fail_unless (data != NULL); + fail_unless_equals_int_hex (*data, 0xff); + g_free (data); +} + +GST_END_TEST; + + +static Suite * +gst_bit_writer_suite (void) +{ + Suite *s = suite_create ("GstBitWriter"); + TCase *tc_chain = tcase_create ("general"); + + suite_add_tcase (s, tc_chain); + + tcase_add_test (tc_chain, test_initialization); + tcase_add_test (tc_chain, test_data); + tcase_add_test (tc_chain, test_reset); + + return s; +} + +GST_CHECK_MAIN (gst_bit_writer); diff --git a/tests/check/meson.build b/tests/check/meson.build index cbc74b2d53..31dc1a8d4a 100644 --- a/tests/check/meson.build +++ b/tests/check/meson.build @@ -59,6 +59,7 @@ core_tests = [ [ 'libs/basesrc.c', not gst_registry ], [ 'libs/basesink.c', not gst_registry ], [ 'libs/bitreader.c' ], + [ 'libs/bitwriter.c' ], [ 'libs/bytereader.c' ], [ 'libs/bytewriter.c' ], [ 'libs/bitreader-noinline.c' ],