[vlc-devel] [PATCH] test: Provide tests for our strnstr implementation

Hugo Beauzée-Luyssen hugo at beauzee.fr
Wed Dec 9 18:07:13 CET 2015


I'm not exactly sure that's the way/place for it, hence the patch.
Another thing that bothers me is should we even build this when
HAVE_STRNSTR == 1 ?
---
 test/Makefile.am      |  4 ++++
 test/compat/strnstr.c | 58 +++++++++++++++++++++++++++++++++++++++++++++++++++
 2 files changed, 62 insertions(+)
 create mode 100644 test/compat/strnstr.c

diff --git a/test/Makefile.am b/test/Makefile.am
index b732acf..767eab6 100644
--- a/test/Makefile.am
+++ b/test/Makefile.am
@@ -22,6 +22,7 @@ check_PROGRAMS = \
 	test_src_misc_variables \
 	test_src_crypto_update \
 	test_src_input_stream \
+	test_compat_strnstr \
 	$(NULL)
 
 check_SCRIPTS = \
@@ -60,6 +61,7 @@ AM_CFLAGS = -DSRCDIR=\"$(srcdir)\"
 AM_LDFLAGS = -no-install -static
 LIBVLCCORE = ../src/libvlccore.la
 LIBVLC = ../lib/libvlc.la
+LIBCOMPAT = ../compat/libcompat.la
 
 test_libvlc_core_SOURCES = libvlc/core.c
 test_libvlc_core_LDADD = $(LIBVLC)
@@ -86,6 +88,8 @@ test_src_input_stream_LDADD = $(LIBVLCCORE) $(LIBVLC)
 test_src_input_stream_net_SOURCES = src/input/stream.c
 test_src_input_stream_net_CFLAGS = $(AM_CFLAGS) -DTEST_NET
 test_src_input_stream_net_LDADD = $(LIBVLCCORE) $(LIBVLC)
+test_compat_strnstr_SOURCES = compat/strnstr.c
+test_compat_strnstr_LDADD = $(LIBCOMPAT)
 
 checkall:
 	$(MAKE) check_PROGRAMS="$(check_PROGRAMS) $(EXTRA_PROGRAMS)" check
diff --git a/test/compat/strnstr.c b/test/compat/strnstr.c
new file mode 100644
index 0000000..1704b65
--- /dev/null
+++ b/test/compat/strnstr.c
@@ -0,0 +1,58 @@
+/*****************************************************************************
+ * strnstr.c: Test for strnstr implementation API
+ *****************************************************************************
+ * Copyright © 2015 VideoLAN & VLC authors
+ *
+ * Authors: Hugo Beauzée-Luyssen <hugo at beauzee.fr>
+ *
+ * This program 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.
+ *
+ * This program 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 this program; if not, write to the Free Software Foundation,
+ * Inc., 51 Franklin Street, Fifth Floor, Boston MA 02110-1301, USA.
+ *****************************************************************************/
+
+#ifndef HAVE_STRNSTR
+
+#include "config.h"
+
+#include <stdbool.h>
+#include <assert.h>
+#include <string.h>
+
+const char* haystack = "Lorem ipsum dolor sit amet";
+
+void test( const char* haystack, const char* needle, size_t len, bool res );
+
+void test( const char* haystack, const char* needle, size_t len, bool res )
+{
+    if ( len == 0 )
+        len = strlen( haystack );
+    char* str = strnstr( haystack, needle, len );
+    assert( res == ( str != NULL ) );
+}
+
+int main(void)
+{
+    test( haystack, "Lorem", 0, true );
+    test( haystack, "Sea Otters", 0, false );
+    test( haystack, "", 0, true );
+    test( haystack, "Lorem ipsum dolor sit amet, but bigger", 0, false );
+    test( haystack, haystack, 0, true );
+    test( haystack, "amet", 0, true );
+    test( haystack, "dolor", 5, false );
+}
+
+#else
+
+int main() { return 0; }
+
+#endif
-- 
2.6.2



More information about the vlc-devel mailing list