summaryrefslogtreecommitdiff
path: root/src/libaudcore/tests
diff options
context:
space:
mode:
authorMateusz Łukasik <mati75@linuxmint.pl>2015-06-05 16:46:36 +0200
committerMateusz Łukasik <mati75@linuxmint.pl>2015-06-05 16:46:36 +0200
commitb541fedc97ad4ed5e658ce34ee50c74ad756f330 (patch)
tree5da244a1063a2529a5419083b2ea23f8fa76cb63 /src/libaudcore/tests
parent5f4fd4397e8b75ad152aec919f59ecb039ac6105 (diff)
Imported Upstream version 3.6.2
Diffstat (limited to 'src/libaudcore/tests')
-rw-r--r--src/libaudcore/tests/Makefile40
-rw-r--r--src/libaudcore/tests/test-mainloop.cc118
-rw-r--r--src/libaudcore/tests/test.cc305
3 files changed, 463 insertions, 0 deletions
diff --git a/src/libaudcore/tests/Makefile b/src/libaudcore/tests/Makefile
new file mode 100644
index 0000000..cb56cb1
--- /dev/null
+++ b/src/libaudcore/tests/Makefile
@@ -0,0 +1,40 @@
+all: test test-mainloop
+
+SRCS = ../audstrings.cc \
+ ../charset.cc \
+ ../hook.cc \
+ ../index.cc \
+ ../logger.cc \
+ ../multihash.cc \
+ ../ringbuf.cc \
+ ../stringbuf.cc \
+ ../strpool.cc \
+ ../tinylock.cc \
+ ../tuple.cc \
+ ../tuple-compiler.cc \
+ test.cc
+
+FLAGS = -I.. -I../.. -DEXPORT= -DPACKAGE=\"audacious\" -DICONV_CONST= \
+ $(shell pkg-config --cflags --libs glib-2.0) \
+ -std=c++11 -Wall -g -O0 -fno-elide-constructors \
+ -fprofile-arcs -ftest-coverage -pthread
+
+MAINLOOP_SRCS = ../mainloop.cc test-mainloop.cc
+
+test: ${SRCS}
+ g++ ${SRCS} ${FLAGS} -o test
+
+test-mainloop: ${MAINLOOP_SRCS}
+ g++ ${MAINLOOP_SRCS} ${FLAGS} -DUSE_QT -fPIC \
+ $(shell pkg-config --cflags --libs Qt5Core) \
+ -o test-mainloop
+
+cov: all
+ rm -f *.gcda
+ ./test
+ ./test-mainloop
+ ./test-mainloop --qt
+ gcov --object-directory . ${SRCS} ${MAINLOOP_SRCS}
+
+clean:
+ rm -f test test-mainloop test-mainloop *.gcno *.gcda *.gcov
diff --git a/src/libaudcore/tests/test-mainloop.cc b/src/libaudcore/tests/test-mainloop.cc
new file mode 100644
index 0000000..577fbad
--- /dev/null
+++ b/src/libaudcore/tests/test-mainloop.cc
@@ -0,0 +1,118 @@
+/*
+ * test-mainloop.cc - Main loop test for libaudcore
+ * Copyright 2014 John Lindgren
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions, and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions, and the following disclaimer in the documentation
+ * provided with the distribution.
+ *
+ * This software is provided "as is" and without any warranty, express or
+ * implied. In no event shall the authors be liable for any damages arising from
+ * the use of this software.
+ */
+
+#include "mainloop.h"
+#include "runtime.h"
+
+#include <assert.h>
+#include <pthread.h>
+#include <stdio.h>
+#include <string.h>
+
+static bool use_qt = false;
+
+MainloopType aud_get_mainloop_type ()
+{
+ return use_qt ? MainloopType::Qt : MainloopType::GLib;
+}
+
+static QueuedFunc counters[70];
+static QueuedFunc fast, slow;
+
+static int count;
+static pthread_t main_thread;
+
+static void count_up (void * data)
+{
+ assert (pthread_self () == main_thread);
+ assert (count == (int) (size_t) data);
+
+ if (! (count % 10))
+ printf ("UP: ");
+
+ count ++;
+
+ printf ("%d%c", count, (count % 10) ? ' ' : '\n');
+}
+
+static void count_down (void * data)
+{
+ assert (pthread_self () == main_thread);
+ assert (data == & count);
+
+ count -= 10;
+
+ printf ("DOWN: %d\n", count);
+
+ if (! count)
+ {
+ fast.stop ();
+ slow.stop ();
+ mainloop_quit ();
+ }
+}
+
+static void check_count (void * data)
+{
+ assert (pthread_self () == main_thread);
+ assert (count == (int) (size_t) data);
+
+ printf ("CHECK: %d\n", count);
+}
+
+static void * worker (void * data)
+{
+ for (int i = 50; i < 70; i ++)
+ counters[i].queue (count_up, (void *) (size_t) (i - 10));
+
+ slow.start (350, check_count, (void *) (size_t) 30);
+
+ return nullptr;
+}
+
+int main (int argc, const char * * argv)
+{
+ if (argc >= 2 && ! strcmp (argv[1], "--qt"))
+ use_qt = true;
+
+ main_thread = pthread_self ();
+
+ for (int j = 0; j < 2; j ++)
+ {
+ for (int i = 0; i < 50; i ++)
+ counters[i].queue (count_up, (void *) (size_t) (i - 30));
+
+ for (int i = 10; i < 30; i ++)
+ counters[i].stop ();
+
+ for (int i = 0; i < 20; i ++)
+ counters[i].queue (count_up, (void *) (size_t) (20 + i));
+
+ fast.start (100, count_down, & count);
+
+ pthread_t thread;
+ pthread_create (& thread, nullptr, worker, nullptr);
+
+ mainloop_run ();
+
+ pthread_join (thread, nullptr);
+ }
+
+ return 0;
+}
diff --git a/src/libaudcore/tests/test.cc b/src/libaudcore/tests/test.cc
new file mode 100644
index 0000000..7e902ca
--- /dev/null
+++ b/src/libaudcore/tests/test.cc
@@ -0,0 +1,305 @@
+/*
+ * test.cc - Various tests for libaudcore
+ * Copyright 2014 John Lindgren
+ *
+ * Redistribution and use in source and binary forms, with or without
+ * modification, are permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice,
+ * this list of conditions, and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice,
+ * this list of conditions, and the following disclaimer in the documentation
+ * provided with the distribution.
+ *
+ * This software is provided "as is" and without any warranty, express or
+ * implied. In no event shall the authors be liable for any damages arising from
+ * the use of this software.
+ */
+
+#include "audstrings.h"
+#include "internal.h"
+#include "ringbuf.h"
+#include "tuple.h"
+#include "tuple-compiler.h"
+#include "vfs.h"
+
+#include <assert.h>
+#include <stdio.h>
+#include <stdlib.h>
+#include <string.h>
+
+/* stubs */
+bool aud_get_bool (const char *, const char *)
+ { return false; }
+String aud_get_str (const char *, const char *)
+ { return String (""); }
+String VFSFile::get_metadata (const char *)
+ { return String (); }
+const char * get_home_utf8 ()
+ { return "/home/user"; }
+
+size_t misc_bytes_allocated;
+
+static void test_tuple_format (const char * format, Tuple & tuple, const char * expected)
+{
+ TupleCompiler compiler;
+ compiler.compile (format);
+ compiler.format (tuple);
+
+ String result = tuple.get_str (Tuple::FormattedTitle);
+ if (strcmp (result, expected))
+ {
+ printf ("For format [%s]\n", format);
+ printf ("Expected [%s]\n", expected);
+ printf ("Got [%s]\n", (const char *) result);
+ exit (1);
+ }
+}
+
+static void test_tuple_formats ()
+{
+ Tuple tuple;
+
+ /* fallback tests */
+ test_tuple_format ("", tuple, "");
+ tuple.set_filename ("http://Path%20To/File%20Name");
+ test_tuple_format ("", tuple, "File Name");
+ tuple.set_str (Tuple::Title, "Song Title");
+ test_tuple_format ("", tuple, "Song Title");
+
+ /* basic variable tests */
+ test_tuple_format ("$", tuple, "Song Title");
+ test_tuple_format ("${", tuple, "Song Title");
+ test_tuple_format ("${file-name", tuple, "Song Title");
+ test_tuple_format ("${file-name}", tuple, "File Name");
+ test_tuple_format ("${file-name}}", tuple, "Song Title");
+ test_tuple_format ("${invalid}", tuple, "Song Title");
+ test_tuple_format ("${}", tuple, "Song Title");
+ test_tuple_format ("\\$\\{\\}", tuple, "${}");
+ test_tuple_format ("\\\0" "a", tuple, "Song Title");
+ test_tuple_format ("{}", tuple, "Song Title");
+
+ /* integer variable tests */
+ test_tuple_format ("${year}", tuple, "Song Title");
+ tuple.set_int (Tuple::Year, -1);
+ test_tuple_format ("${year}", tuple, "-1");
+ tuple.set_int (Tuple::Year, 0);
+ test_tuple_format ("${year}", tuple, "0");
+ tuple.set_int (Tuple::Year, 1990);
+ test_tuple_format ("${year}", tuple, "1990");
+
+ /* filename variable tests */
+ test_tuple_format ("${file-path}", tuple, "http://Path To/");
+ test_tuple_format ("${file-ext}", tuple, "Song Title");
+ tuple.set_filename ("http://Path%20To/File%20Name.Ext?3");
+ test_tuple_format ("${file-name}", tuple, "File Name");
+ test_tuple_format ("${file-ext}", tuple, "Ext");
+ test_tuple_format ("${subsong-id}", tuple, "3");
+
+ /* existence tests */
+ test_tuple_format ("x${?invalid:Field Exists}", tuple, "Song Title");
+ test_tuple_format ("x${?subsong-id:Field Exists", tuple, "Song Title");
+ test_tuple_format ("x${?subsong-id:Field Exists}", tuple, "xField Exists");
+ test_tuple_format ("x${?subsong-id:${invalid}}", tuple, "Song Title");
+ test_tuple_format ("x${?subsong-id:(${subsong-id})}", tuple, "x(3)");
+ test_tuple_format ("x${?track-number:Field Exists}", tuple, "x");
+ test_tuple_format ("x${?title:Field Exists}", tuple, "xField Exists");
+ test_tuple_format ("x${?artist:Field Exists}", tuple, "x");
+ test_tuple_format ("x${?artist}", tuple, "Song Title");
+
+ /* equality tests */
+ test_tuple_format ("x${=}", tuple, "Song Title");
+ test_tuple_format ("x${==}", tuple, "Song Title");
+ test_tuple_format ("x${==a,}", tuple, "Song Title");
+ test_tuple_format ("x${==a,a:}", tuple, "Song Title");
+ test_tuple_format ("x${==\"a\",a:}", tuple, "Song Title");
+ test_tuple_format ("x${==\"a\",\"a:Equal}", tuple, "Song Title");
+ test_tuple_format ("x${==\"a\",\"a\":Equal}", tuple, "xEqual");
+ test_tuple_format ("x${==\"a\",\"a\"\":Equal}", tuple, "Song Title");
+ test_tuple_format ("x${==\"a\",\"b\":Equal}", tuple, "x");
+ test_tuple_format ("x${==year,\"a\":Equal}", tuple, "x");
+ test_tuple_format ("x${==\"a\",year:Equal}", tuple, "x");
+ test_tuple_format ("x${==year,1990:Equal}", tuple, "xEqual");
+ test_tuple_format ("x${==1990,year:Equal}", tuple, "xEqual");
+ test_tuple_format ("x${==title,\"a\":Equal}", tuple, "x");
+ test_tuple_format ("x${==\"a\",title:Equal}", tuple, "x");
+ test_tuple_format ("x${==title,\"Song Title\":Equal}", tuple, "xEqual");
+ test_tuple_format ("x${==\"Song Title\",title:Equal}", tuple, "xEqual");
+ tuple.set_str (Tuple::Artist, "{}");
+ test_tuple_format ("x${==artist,\"\\{\\}\":Equal}", tuple, "xEqual");
+
+ /* inequality tests */
+ test_tuple_format ("x${!}", tuple, "Song Title");
+ test_tuple_format ("x${!=}", tuple, "Song Title");
+ test_tuple_format ("x${!=\"a\",\"a\":Unequal}", tuple, "x");
+ test_tuple_format ("x${!=\"a\",\"b\":Unequal}", tuple, "xUnequal");
+ test_tuple_format ("x${!=year,\"a\":Unequal}", tuple, "xUnequal");
+ test_tuple_format ("x${!=\"a\",year:Unequal}", tuple, "xUnequal");
+ test_tuple_format ("x${!=year,1990:Unequal}", tuple, "x");
+ test_tuple_format ("x${!=1990,year:Unequal}", tuple, "x");
+ test_tuple_format ("x${>}", tuple, "Song Title");
+ test_tuple_format ("x${>year,1989:Greater}", tuple, "xGreater");
+ test_tuple_format ("x${>year,1990:Greater}", tuple, "x");
+ test_tuple_format ("x${>=year,1990:NotLess}", tuple, "xNotLess");
+ test_tuple_format ("x${>=year,1991:NotLess}", tuple, "x");
+ test_tuple_format ("x${<}", tuple, "Song Title");
+ test_tuple_format ("x${<year,1991:Less}", tuple, "xLess");
+ test_tuple_format ("x${<year,1990:Less}", tuple, "x");
+ test_tuple_format ("x${<=year,1990:NotGreater}", tuple, "xNotGreater");
+ test_tuple_format ("x${<=year,1989:NotGreater}", tuple, "x");
+
+ /* emptiness tests */
+ tuple.set_int (Tuple::Year, 0);
+ tuple.set_str (Tuple::Artist, "");
+ test_tuple_format ("x${(invalid)}", tuple, "Song Title");
+ test_tuple_format ("x${(empty)?invalid:Empty}", tuple, "Song Title");
+ test_tuple_format ("x${(empty)?subsong-id:Empty}", tuple, "x");
+ test_tuple_format ("x${(empty)?subsong-id:${invalid}}", tuple, "Song Title");
+ test_tuple_format ("x${(empty)?year:Empty}", tuple, "x");
+ test_tuple_format ("x${(empty)?track-number:Empty}", tuple, "xEmpty");
+ test_tuple_format ("x${(empty)?title:Empty}", tuple, "x");
+ test_tuple_format ("x${(empty)?artist:Empty}", tuple, "x");
+ test_tuple_format ("x${(empty)?album:Empty}", tuple, "xEmpty");
+ test_tuple_format ("x${(empty)?\"Literal\":Empty}", tuple, "Song Title");
+}
+
+static void test_ringbuf ()
+{
+ String nums[10];
+ for (int i = 0; i < 10; i ++)
+ nums[i] = String (int_to_str (i));
+
+ RingBuf<String> ring;
+
+ ring.alloc (7);
+
+ for (int i = 0; i < 7; i ++)
+ assert (ring.push (nums[i]) == nums[i]);
+
+ for (int i = 0; i < 5; i ++)
+ {
+ assert (ring.head () == nums[i]);
+ ring.pop ();
+ }
+
+ for (int i = 7; i < 10; i ++)
+ assert (ring.push (nums[i]) == nums[i]);
+
+ assert (ring.size () == 7);
+ assert (ring.len () == 5);
+ assert (ring.linear () == 2);
+ assert (ring.space () == 2);
+
+ ring.alloc (5);
+
+ for (int i = 0; i < 5; i ++)
+ assert (ring[i] == nums[5 + i]);
+
+ assert (ring.size () == 5);
+ assert (ring.len () == 5);
+ assert (ring.linear () == 2);
+ assert (ring.space () == 0);
+
+ ring.alloc (10);
+
+ for (int i = 0; i < 5; i ++)
+ assert (ring[i] == nums[5 + i]);
+
+ assert (ring.size () == 10);
+ assert (ring.len () == 5);
+ assert (ring.linear () == 2);
+ assert (ring.space () == 5);
+
+ for (int i = 0; i < 5; i ++)
+ assert (ring[i] == nums[5 + i]);
+
+ for (int i = 5; i --; )
+ assert (ring.push (nums[i]) == nums[i]);
+
+ for (int i = 0; i < 5; i ++)
+ {
+ assert (ring.head () == nums[5 + i]);
+ ring.pop ();
+ }
+
+ for (int i = 0; i < 5; i ++)
+ {
+ assert (ring.head () == nums[4 - i]);
+ ring.pop ();
+ }
+
+ ring.copy_in (& nums[5], 5);
+ ring.copy_in (& nums[0], 5);
+
+ for (int i = 0; i < 5; i ++)
+ {
+ assert (ring.head () == nums[5 + i]);
+ ring.pop ();
+ }
+
+ for (int i = 0; i < 5; i ++)
+ {
+ assert (ring.head () == nums[i]);
+ ring.pop ();
+ }
+
+ ring.move_in (nums, 10);
+
+ for (int i = 0; i < 10; i ++)
+ {
+ assert (! nums[i]);
+ assert (ring[i] == String (int_to_str (i)));
+ }
+
+ ring.move_out (& nums[5], 5);
+ ring.move_out (& nums[0], 5);
+
+ for (int i = 0; i < 10; i ++)
+ assert (nums[i] == String (int_to_str ((5 + i) % 10)));
+
+ ring.move_in (nums, 10);
+
+ Index<String> index;
+ ring.move_out (index, -1, 5);
+
+ assert (ring.len () == 5);
+ assert (index.len () == 5);
+
+ ring.move_out (index, 0, -1);
+
+ assert (ring.len () == 0);
+ assert (index.len () == 10);
+
+ for (int i = 0; i < 10; i ++)
+ assert (index[i] == String (int_to_str (i)));
+
+ ring.move_in (index, 5, 5);
+
+ assert (ring.len () == 5);
+ assert (index.len () == 5);
+
+ ring.move_in (index, 0, -1);
+
+ assert (ring.len () == 10);
+ assert (index.len () == 0);
+
+ for (int i = 0; i < 10; i ++)
+ assert (ring[i] == String (int_to_str ((5 + i) % 10)));
+
+ ring.discard (5);
+ assert (ring.len () == 5);
+
+ ring.discard ();
+ assert (ring.len () == 0);
+
+ string_leak_check ();
+}
+
+int main ()
+{
+ test_tuple_formats ();
+ test_ringbuf ();
+
+ return 0;
+}