EXTRA_DIST = Doxyfile HACKING contrib/sigrok-logo-notext.png
if HAVE_CHECK
-TESTS = tests/check_main
+TESTS = tests/main
check_PROGRAMS = ${TESTS}
-tests_check_main_SOURCES = \
+tests_main_SOURCES = \
libsigrokdecode.h \
tests/lib.h \
- tests/check_main.c \
- tests/check_core.c \
- tests/check_decoder.c \
- tests/check_inst.c \
- tests/check_session.c
-tests_check_main_CFLAGS = $(AM_CFLAGS) @check_CFLAGS@
-tests_check_main_LDADD = $(top_builddir)/libsigrokdecode.la @check_LIBS@
-tests_check_main_CPPFLAGS = $(CPPFLAGS_PYTHON) \
+ tests/main.c \
+ tests/core.c \
+ tests/decoder.c \
+ tests/inst.c \
+ tests/session.c
+tests_main_CFLAGS = $(AM_CFLAGS) @check_CFLAGS@
+tests_main_LDADD = $(top_builddir)/libsigrokdecode.la @check_LIBS@
+tests_main_CPPFLAGS = $(CPPFLAGS_PYTHON) \
-DDECODERS_DIR='"$(abs_top_srcdir)/decoders"'
endif
+++ /dev/null
-/*
- * This file is part of the libsigrokdecode project.
- *
- * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
-#include <stdlib.h>
-#include <check.h>
-#include "lib.h"
-
-/*
- * Check various basic init related things.
- *
- * - Check whether an srd_init() call with path == NULL works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- *
- * - Check whether a subsequent srd_exit() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_init_exit)
-{
- int ret;
-
- ret = srd_init(NULL);
- fail_unless(ret == SRD_OK, "srd_init() failed: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() failed: %d.", ret);
-}
-END_TEST
-
-/*
- * Check whether nested srd_init()/srd_exit() calls work/fail correctly.
- * Two consecutive srd_init() calls without any srd_exit() inbetween are
- * not allowed and should fail. However, two consecutive srd_exit() calls
- * are currently allowed, the second one will just be a NOP basically.
- */
-START_TEST(test_init_exit_2)
-{
- int ret;
-
- ret = srd_init(NULL);
- fail_unless(ret == SRD_OK, "srd_init() 1 failed: %d.", ret);
- ret = srd_init(NULL);
- fail_unless(ret != SRD_OK, "srd_init() 2 didn't fail: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() 2 failed: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() 1 failed: %d.", ret);
-}
-END_TEST
-
-/*
- * Check whether three nested srd_init()/srd_exit() calls work/fail correctly.
- */
-START_TEST(test_init_exit_3)
-{
- int ret;
-
- ret = srd_init(NULL);
- fail_unless(ret == SRD_OK, "srd_init() 1 failed: %d.", ret);
- ret = srd_init(NULL);
- fail_unless(ret != SRD_OK, "srd_init() 2 didn't fail: %d.", ret);
- ret = srd_init(NULL);
- fail_unless(ret != SRD_OK, "srd_init() 3 didn't fail: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() 3 failed: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() 2 failed: %d.", ret);
- ret = srd_exit();
- fail_unless(ret == SRD_OK, "srd_exit() 1 failed: %d.", ret);
-}
-END_TEST
-
-Suite *suite_core(void)
-{
- Suite *s;
- TCase *tc;
-
- s = suite_create("core");
-
- tc = tcase_create("init_exit");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_init_exit);
- tcase_add_test(tc, test_init_exit_2);
- tcase_add_test(tc, test_init_exit_3);
- suite_add_tcase(s, tc);
-
- return s;
-}
+++ /dev/null
-/*
- * This file is part of the libsigrokdecode project.
- *
- * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
-#include <stdlib.h>
-#include <check.h>
-#include "lib.h"
-
-/*
- * Check whether srd_decoder_load_all() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_load_all)
-{
- int ret;
-
- srd_init(DECODERS_DIR);
- ret = srd_decoder_load_all();
- fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load_all() fails without prior srd_init().
- * If it returns != SRD_OK (or segfaults) this test will fail.
- * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
- */
-START_TEST(test_load_all_no_init)
-{
- int ret;
-
- ret = srd_decoder_load_all();
- fail_unless(ret != SRD_OK, "srd_decoder_load_all() didn't fail properly.");
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_load)
-{
- int ret;
-
- srd_init(DECODERS_DIR);
- ret = srd_decoder_load("uart");
- fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
- ret = srd_decoder_load("spi");
- fail_unless(ret == SRD_OK, "srd_decoder_load(spi) failed: %d.", ret);
- ret = srd_decoder_load("usb_signalling");
- fail_unless(ret == SRD_OK, "srd_decoder_load(usb_signalling) failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load() fails for non-existing or bogus PDs.
- * If it returns SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_load_bogus)
-{
- srd_init(DECODERS_DIR);
- /* http://sigrok.org/bugzilla/show_bug.cgi?id=176 */
- fail_unless(srd_decoder_load(NULL) != SRD_OK);
- fail_unless(srd_decoder_load("") != SRD_OK);
- fail_unless(srd_decoder_load(" ") != SRD_OK);
- fail_unless(srd_decoder_load("nonexisting") != SRD_OK);
- fail_unless(srd_decoder_load("UART") != SRD_OK);
- fail_unless(srd_decoder_load("UaRt") != SRD_OK);
- fail_unless(srd_decoder_load("u a r t") != SRD_OK);
- fail_unless(srd_decoder_load("uart ") != SRD_OK);
- fail_unless(srd_decoder_load(" uart") != SRD_OK);
- fail_unless(srd_decoder_load(" uart ") != SRD_OK);
- fail_unless(srd_decoder_load("uart spi") != SRD_OK);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load() works/fails for valid/bogus PDs.
- * If it returns incorrect values (or segfaults) this test will fail.
- */
-START_TEST(test_load_valid_and_bogus)
-{
- srd_init(DECODERS_DIR);
- fail_unless(srd_decoder_load("") != SRD_OK);
- fail_unless(srd_decoder_load("uart") == SRD_OK);
- fail_unless(srd_decoder_load("") != SRD_OK);
- fail_unless(srd_decoder_load("spi") == SRD_OK);
- fail_unless(srd_decoder_load("") != SRD_OK);
- fail_unless(srd_decoder_load("can") == SRD_OK);
- fail_unless(srd_decoder_load("") != SRD_OK);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load() fails when run multiple times.
- * If it returns a value != SRD_OK (or segfaults) this test will fail.
- * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=177
- */
-START_TEST(test_load_multiple)
-{
- int ret;
-
- srd_init(DECODERS_DIR);
- ret = srd_decoder_load("uart");
- fail_unless(ret == SRD_OK, "Loading uart PD 1x failed: %d", ret);
- ret = srd_decoder_load("uart");
- fail_unless(ret == SRD_OK, "Loading uart PD 2x failed: %d", ret);
- ret = srd_decoder_load("uart");
- fail_unless(ret == SRD_OK, "Loading uart PD 3x failed: %d", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_load() fails if a non-existing PD dir is used.
- * If it returns SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_load_nonexisting_pd_dir)
-{
-#if 0
- /* TODO: Build libsigrokdecode with no default PD dir. */
- srd_init("/nonexisting_dir");
- fail_unless(srd_decoder_load("spi") != SRD_OK);
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
- srd_exit();
-#endif
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_list() returns a non-empty list.
- * If it returns an empty list (or segfaults) this test will fail.
- */
-START_TEST(test_decoder_list)
-{
- srd_init(DECODERS_DIR);
- srd_decoder_load_all();
- fail_unless(srd_decoder_list() != NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_list() without prior srd_decoder_load_all()
- * returns an empty list (return value != NULL).
- * If it returns a non-empty list (or segfaults) this test will fail.
- */
-START_TEST(test_decoder_list_no_load)
-{
- srd_init(DECODERS_DIR);
- fail_unless(srd_decoder_list() == NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_list() without prior srd_init()
- * returns an empty list.
- * If it returns a non-empty list (or segfaults) this test will fail.
- * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
- */
-START_TEST(test_decoder_list_no_init)
-{
- srd_decoder_load_all();
- fail_unless(srd_decoder_list() == NULL);
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_list() without prior srd_init() and without
- * prior srd_decoder_load_all() returns an empty list.
- * If it returns a non-empty list (or segfaults) this test will fail.
- */
-START_TEST(test_decoder_list_no_init_no_load)
-{
- fail_unless(srd_decoder_list() == NULL);
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_list() returns the correct number of PDs.
- * If it returns a wrong number (or segfaults) this test will fail.
- */
-START_TEST(test_decoder_list_correct_numbers)
-{
- srd_init(DECODERS_DIR);
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
- srd_decoder_load("spi");
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 1);
- srd_decoder_load("uart");
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 2);
- srd_decoder_load("can");
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
- srd_decoder_load("can"); /* Load same PD twice. */
- fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_get_by_id() works.
- * If it returns NULL for valid PDs (or segfaults) this test will fail.
- */
-START_TEST(test_get_by_id)
-{
- srd_init(DECODERS_DIR);
- srd_decoder_load("uart");
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- fail_unless(srd_decoder_get_by_id("can") == NULL);
- srd_decoder_load("can");
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- fail_unless(srd_decoder_get_by_id("can") != NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_get_by_id() works multiple times in a row.
- * If it returns NULL for valid PDs (or segfaults) this test will fail.
- */
-START_TEST(test_get_by_id_multiple)
-{
- srd_init(DECODERS_DIR);
- srd_decoder_load("uart");
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- fail_unless(srd_decoder_get_by_id("uart") != NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_get_by_id() fails for bogus PDs.
- * If it returns a value != NULL (or segfaults) this test will fail.
- */
-START_TEST(test_get_by_id_bogus)
-{
- srd_init(DECODERS_DIR);
- fail_unless(srd_decoder_get_by_id(NULL) == NULL);
- fail_unless(srd_decoder_get_by_id("") == NULL);
- fail_unless(srd_decoder_get_by_id(" ") == NULL);
- fail_unless(srd_decoder_get_by_id("nonexisting") == NULL);
- fail_unless(srd_decoder_get_by_id("sPi") == NULL);
- fail_unless(srd_decoder_get_by_id("SPI") == NULL);
- fail_unless(srd_decoder_get_by_id("s p i") == NULL);
- fail_unless(srd_decoder_get_by_id(" spi") == NULL);
- fail_unless(srd_decoder_get_by_id("spi ") == NULL);
- fail_unless(srd_decoder_get_by_id(" spi ") == NULL);
- fail_unless(srd_decoder_get_by_id("spi uart") == NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_doc_get() works.
- * If it returns NULL for valid PDs (or segfaults) this test will fail.
- */
-START_TEST(test_doc_get)
-{
- struct srd_decoder *dec;
-
- srd_init(DECODERS_DIR);
- srd_decoder_load("uart");
- dec = srd_decoder_get_by_id("uart");
- fail_unless(srd_decoder_doc_get(dec) != NULL);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_decoder_doc_get() fails with NULL as argument.
- * If it returns a value != NULL (or segfaults) this test will fail.
- * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=179
- */
-START_TEST(test_doc_get_null)
-{
- srd_init(DECODERS_DIR);
- fail_unless(srd_decoder_doc_get(NULL) == NULL);
- srd_exit();
-}
-END_TEST
-
-Suite *suite_decoder(void)
-{
- Suite *s;
- TCase *tc;
-
- s = suite_create("decoder");
-
- tc = tcase_create("load");
- tcase_set_timeout(tc, 0);
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_load_all);
- tcase_add_test(tc, test_load_all_no_init);
- tcase_add_test(tc, test_load);
- tcase_add_test(tc, test_load_bogus);
- tcase_add_test(tc, test_load_valid_and_bogus);
- tcase_add_test(tc, test_load_multiple);
- tcase_add_test(tc, test_load_nonexisting_pd_dir);
- suite_add_tcase(s, tc);
-
- tc = tcase_create("list");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_decoder_list);
- tcase_add_test(tc, test_decoder_list_no_load);
- tcase_add_test(tc, test_decoder_list_no_init);
- tcase_add_test(tc, test_decoder_list_no_init_no_load);
- tcase_add_test(tc, test_decoder_list_correct_numbers);
- suite_add_tcase(s, tc);
-
- tc = tcase_create("get_by_id");
- tcase_add_test(tc, test_get_by_id);
- tcase_add_test(tc, test_get_by_id_multiple);
- tcase_add_test(tc, test_get_by_id_bogus);
- suite_add_tcase(s, tc);
-
- tc = tcase_create("doc_get");
- tcase_add_test(tc, test_doc_get);
- tcase_add_test(tc, test_doc_get_null);
- suite_add_tcase(s, tc);
-
- return s;
-}
+++ /dev/null
-/*
- * This file is part of the libsigrokdecode project.
- *
- * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
-#include <stdlib.h>
-#include <check.h>
-#include "lib.h"
-
-/*
- * Check whether srd_inst_new() works.
- * If it returns NULL (or segfaults) this test will fail.
- */
-START_TEST(test_inst_new)
-{
- struct srd_session *sess;
- struct srd_decoder_inst *inst;
-
- srd_init(DECODERS_DIR);
- srd_decoder_load("uart");
- srd_session_new(&sess);
- inst = srd_inst_new(sess, "uart", NULL);
- fail_unless(inst != NULL, "srd_inst_new() failed.");
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether multiple srd_inst_new() calls work.
- * If any of them returns NULL (or segfaults) this test will fail.
- */
-START_TEST(test_inst_new_multiple)
-{
- struct srd_session *sess;
- struct srd_decoder_inst *inst1, *inst2, *inst3;
-
- inst1 = inst2 = inst3 = NULL;
-
- srd_init(DECODERS_DIR);
- srd_decoder_load_all();
- srd_session_new(&sess);
-
- /* Multiple srd_inst_new() calls must work. */
- inst1 = srd_inst_new(sess, "uart", NULL);
- fail_unless(inst1 != NULL, "srd_inst_new() 1 failed.");
- inst2 = srd_inst_new(sess, "spi", NULL);
- fail_unless(inst2 != NULL, "srd_inst_new() 2 failed.");
- inst3 = srd_inst_new(sess, "can", NULL);
- fail_unless(inst3 != NULL, "srd_inst_new() 3 failed.");
-
- /* The returned instance pointers must not be the same. */
- fail_unless(inst1 != inst2);
- fail_unless(inst1 != inst3);
- fail_unless(inst2 != inst3);
-
- /* Each instance must have another py_inst than any of the others. */
- fail_unless(inst1->py_inst != inst2->py_inst);
- fail_unless(inst1->py_inst != inst3->py_inst);
- fail_unless(inst2->py_inst != inst3->py_inst);
-
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_inst_option_set() works for an empty options hash.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_inst_option_set_empty)
-{
- int ret;
- struct srd_session *sess;
- struct srd_decoder_inst *inst;
- GHashTable *options;
-
- srd_init(DECODERS_DIR);
- srd_decoder_load_all();
- srd_session_new(&sess);
- inst = srd_inst_new(sess, "uart", NULL);
- options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
- (GDestroyNotify)g_variant_unref);
- ret = srd_inst_option_set(inst, options);
- fail_unless(ret == SRD_OK, "srd_inst_option_set() with empty options "
- "hash failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_inst_option_set() works for bogus options.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_inst_option_set_bogus)
-{
- int ret;
- struct srd_session *sess;
- struct srd_decoder_inst *inst;
- GHashTable *options;
-
- srd_init(DECODERS_DIR);
- srd_decoder_load_all();
- srd_session_new(&sess);
- inst = srd_inst_new(sess, "uart", NULL);
-
- options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
- (GDestroyNotify)g_variant_unref);
-
- /* NULL instance. */
- ret = srd_inst_option_set(NULL, options);
- fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
- "instance failed: %d.", ret);
-
- /* NULL 'options' GHashTable. */
- ret = srd_inst_option_set(inst, NULL);
- fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
- "options hash failed: %d.", ret);
-
- /* NULL instance and NULL 'options' GHashTable. */
- ret = srd_inst_option_set(NULL, NULL);
- fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
- "instance and NULL options hash failed: %d.", ret);
-
- srd_exit();
-}
-END_TEST
-
-Suite *suite_inst(void)
-{
- Suite *s;
- TCase *tc;
-
- s = suite_create("inst");
-
- tc = tcase_create("new");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_inst_new);
- tcase_add_test(tc, test_inst_new_multiple);
- suite_add_tcase(s, tc);
-
- tc = tcase_create("option");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_inst_option_set_empty);
- tcase_add_test(tc, test_inst_option_set_bogus);
- suite_add_tcase(s, tc);
-
- return s;
-}
+++ /dev/null
-/*
- * This file is part of the libsigrokdecode project.
- *
- * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
-#include <stdlib.h>
-#include <check.h>
-#include "lib.h"
-
-void srdtest_setup(void)
-{
- /* Silence libsigrokdecode while the unit tests run. */
- srd_log_loglevel_set(SRD_LOG_NONE);
-}
-
-void srdtest_teardown(void)
-{
-}
-
-int main(void)
-{
- int ret;
- Suite *s;
- SRunner *srunner;
-
- s = suite_create("mastersuite");
- srunner = srunner_create(s);
-
- /* Add all testsuites to the master suite. */
- srunner_add_suite(srunner, suite_core());
- srunner_add_suite(srunner, suite_decoder());
- srunner_add_suite(srunner, suite_inst());
- srunner_add_suite(srunner, suite_session());
-
- srunner_run_all(srunner, CK_VERBOSE);
- ret = srunner_ntests_failed(srunner);
- srunner_free(srunner);
-
- return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
-}
+++ /dev/null
-/*
- * This file is part of the libsigrokdecode project.
- *
- * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
- *
- * This program is free software; you can redistribute it and/or modify
- * it under the terms of the GNU General Public License as published by
- * the Free Software Foundation; either version 2 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 General Public License for more details.
- *
- * You should have received a copy of the GNU General Public License
- * along with this program; if not, write to the Free Software
- * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
- */
-
-#include "../libsigrokdecode-internal.h" /* First, to avoid compiler warning. */
-#include "../libsigrokdecode.h"
-#include <stdint.h>
-#include <stdlib.h>
-#include <check.h>
-#include "lib.h"
-
-/*
- * Check whether srd_session_new() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_new)
-{
- int ret;
- struct srd_session *sess;
-
- srd_init(NULL);
- ret = srd_session_new(&sess);
- fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_session_new() fails for bogus parameters.
- * If it returns SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_new_bogus)
-{
- int ret;
-
- srd_init(NULL);
- ret = srd_session_new(NULL);
- fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether multiple srd_session_new() calls work.
- * If any call returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_new_multiple)
-{
- int ret;
- struct srd_session *sess1, *sess2, *sess3;
-
- sess1 = sess2 = sess3 = NULL;
-
- srd_init(NULL);
-
- /* Multiple srd_session_new() calls must work. */
- ret = srd_session_new(&sess1);
- fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
- ret = srd_session_new(&sess2);
- fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
- ret = srd_session_new(&sess3);
- fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
-
- /* The returned session pointers must all be non-NULL. */
- fail_unless(sess1 != NULL);
- fail_unless(sess2 != NULL);
- fail_unless(sess3 != NULL);
-
- /* The returned session pointers must not be the same. */
- fail_unless(sess1 != sess2);
- fail_unless(sess1 != sess3);
- fail_unless(sess2 != sess3);
-
- /* Each session must have another ID than any other session. */
- fail_unless(sess1->session_id != sess2->session_id);
- fail_unless(sess1->session_id != sess3->session_id);
- fail_unless(sess2->session_id != sess3->session_id);
-
- /* Destroying any of the sessions must work. */
- ret = srd_session_destroy(sess1);
- fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
- ret = srd_session_destroy(sess2);
- fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
- ret = srd_session_destroy(sess3);
- fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
-
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_session_destroy() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_destroy)
-{
- int ret;
- struct srd_session *sess;
-
- srd_init(NULL);
- srd_session_new(&sess);
- ret = srd_session_destroy(sess);
- fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_session_destroy() fails for bogus sessions.
- * If it returns SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_destroy_bogus)
-{
- int ret;
-
- srd_init(NULL);
- ret = srd_session_destroy(NULL);
- fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
- srd_exit();
-}
-END_TEST
-
-static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
-{
- int ret;
-
- ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
- fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
- PRIu64 ") failed: %d.", sess, key, x, ret);
-}
-
-static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
-{
- int ret;
-
- ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
- fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
- PRIu64 ") worked.", sess, key, x);
-}
-
-static void conf_check_fail_null(struct srd_session *sess, int key)
-{
- int ret;
-
- ret = srd_session_metadata_set(sess, key, NULL);
- fail_unless(ret != SRD_OK,
- "srd_session_metadata_set(NULL) for key %d worked.", key);
-}
-
-static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
-{
- int ret;
-
- ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
- fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
- "failed: %d.", key, ret);
-}
-
-/*
- * Check whether srd_session_metadata_set() works.
- * If it returns != SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_metadata_set)
-{
- uint64_t i;
- struct srd_session *sess;
-
- srd_init(NULL);
- srd_session_new(&sess);
- /* Try a bunch of values. */
- for (i = 0; i < 1000; i++)
- conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
- /* Try the max. possible value. */
- conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
- srd_session_destroy(sess);
- srd_exit();
-}
-END_TEST
-
-/*
- * Check whether srd_session_metadata_set() fails with invalid input.
- * If it returns SRD_OK (or segfaults) this test will fail.
- */
-START_TEST(test_session_metadata_set_bogus)
-{
- struct srd_session *sess;
-
- srd_init(NULL);
- srd_session_new(&sess);
-
- /* Incorrect gvariant type (currently only uint64 is used). */
- conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
- conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
-
- /* NULL data pointer. */
- conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
-
- /* NULL session. */
- conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
-
- /* Invalid keys. */
- conf_check_fail(sess, -1, 0);
- conf_check_fail(sess, 9, 0);
- conf_check_fail(sess, 123, 0);
-
- srd_session_destroy(sess);
- srd_exit();
-}
-END_TEST
-
-Suite *suite_session(void)
-{
- Suite *s;
- TCase *tc;
-
- s = suite_create("session");
-
- tc = tcase_create("new_destroy");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_session_new);
- tcase_add_test(tc, test_session_new_bogus);
- tcase_add_test(tc, test_session_new_multiple);
- tcase_add_test(tc, test_session_destroy);
- tcase_add_test(tc, test_session_destroy_bogus);
- suite_add_tcase(s, tc);
-
- tc = tcase_create("config");
- tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
- tcase_add_test(tc, test_session_metadata_set);
- tcase_add_test(tc, test_session_metadata_set_bogus);
- suite_add_tcase(s, tc);
-
- return s;
-}
--- /dev/null
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
+#include <stdlib.h>
+#include <check.h>
+#include "lib.h"
+
+/*
+ * Check various basic init related things.
+ *
+ * - Check whether an srd_init() call with path == NULL works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ *
+ * - Check whether a subsequent srd_exit() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_init_exit)
+{
+ int ret;
+
+ ret = srd_init(NULL);
+ fail_unless(ret == SRD_OK, "srd_init() failed: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() failed: %d.", ret);
+}
+END_TEST
+
+/*
+ * Check whether nested srd_init()/srd_exit() calls work/fail correctly.
+ * Two consecutive srd_init() calls without any srd_exit() inbetween are
+ * not allowed and should fail. However, two consecutive srd_exit() calls
+ * are currently allowed, the second one will just be a NOP basically.
+ */
+START_TEST(test_init_exit_2)
+{
+ int ret;
+
+ ret = srd_init(NULL);
+ fail_unless(ret == SRD_OK, "srd_init() 1 failed: %d.", ret);
+ ret = srd_init(NULL);
+ fail_unless(ret != SRD_OK, "srd_init() 2 didn't fail: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() 2 failed: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() 1 failed: %d.", ret);
+}
+END_TEST
+
+/*
+ * Check whether three nested srd_init()/srd_exit() calls work/fail correctly.
+ */
+START_TEST(test_init_exit_3)
+{
+ int ret;
+
+ ret = srd_init(NULL);
+ fail_unless(ret == SRD_OK, "srd_init() 1 failed: %d.", ret);
+ ret = srd_init(NULL);
+ fail_unless(ret != SRD_OK, "srd_init() 2 didn't fail: %d.", ret);
+ ret = srd_init(NULL);
+ fail_unless(ret != SRD_OK, "srd_init() 3 didn't fail: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() 3 failed: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() 2 failed: %d.", ret);
+ ret = srd_exit();
+ fail_unless(ret == SRD_OK, "srd_exit() 1 failed: %d.", ret);
+}
+END_TEST
+
+Suite *suite_core(void)
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("core");
+
+ tc = tcase_create("init_exit");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_init_exit);
+ tcase_add_test(tc, test_init_exit_2);
+ tcase_add_test(tc, test_init_exit_3);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
--- /dev/null
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
+#include <stdlib.h>
+#include <check.h>
+#include "lib.h"
+
+/*
+ * Check whether srd_decoder_load_all() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_load_all)
+{
+ int ret;
+
+ srd_init(DECODERS_DIR);
+ ret = srd_decoder_load_all();
+ fail_unless(ret == SRD_OK, "srd_decoder_load_all() failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load_all() fails without prior srd_init().
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
+ */
+START_TEST(test_load_all_no_init)
+{
+ int ret;
+
+ ret = srd_decoder_load_all();
+ fail_unless(ret != SRD_OK, "srd_decoder_load_all() didn't fail properly.");
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_load)
+{
+ int ret;
+
+ srd_init(DECODERS_DIR);
+ ret = srd_decoder_load("uart");
+ fail_unless(ret == SRD_OK, "srd_decoder_load(uart) failed: %d.", ret);
+ ret = srd_decoder_load("spi");
+ fail_unless(ret == SRD_OK, "srd_decoder_load(spi) failed: %d.", ret);
+ ret = srd_decoder_load("usb_signalling");
+ fail_unless(ret == SRD_OK, "srd_decoder_load(usb_signalling) failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load() fails for non-existing or bogus PDs.
+ * If it returns SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_load_bogus)
+{
+ srd_init(DECODERS_DIR);
+ /* http://sigrok.org/bugzilla/show_bug.cgi?id=176 */
+ fail_unless(srd_decoder_load(NULL) != SRD_OK);
+ fail_unless(srd_decoder_load("") != SRD_OK);
+ fail_unless(srd_decoder_load(" ") != SRD_OK);
+ fail_unless(srd_decoder_load("nonexisting") != SRD_OK);
+ fail_unless(srd_decoder_load("UART") != SRD_OK);
+ fail_unless(srd_decoder_load("UaRt") != SRD_OK);
+ fail_unless(srd_decoder_load("u a r t") != SRD_OK);
+ fail_unless(srd_decoder_load("uart ") != SRD_OK);
+ fail_unless(srd_decoder_load(" uart") != SRD_OK);
+ fail_unless(srd_decoder_load(" uart ") != SRD_OK);
+ fail_unless(srd_decoder_load("uart spi") != SRD_OK);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load() works/fails for valid/bogus PDs.
+ * If it returns incorrect values (or segfaults) this test will fail.
+ */
+START_TEST(test_load_valid_and_bogus)
+{
+ srd_init(DECODERS_DIR);
+ fail_unless(srd_decoder_load("") != SRD_OK);
+ fail_unless(srd_decoder_load("uart") == SRD_OK);
+ fail_unless(srd_decoder_load("") != SRD_OK);
+ fail_unless(srd_decoder_load("spi") == SRD_OK);
+ fail_unless(srd_decoder_load("") != SRD_OK);
+ fail_unless(srd_decoder_load("can") == SRD_OK);
+ fail_unless(srd_decoder_load("") != SRD_OK);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load() fails when run multiple times.
+ * If it returns a value != SRD_OK (or segfaults) this test will fail.
+ * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=177
+ */
+START_TEST(test_load_multiple)
+{
+ int ret;
+
+ srd_init(DECODERS_DIR);
+ ret = srd_decoder_load("uart");
+ fail_unless(ret == SRD_OK, "Loading uart PD 1x failed: %d", ret);
+ ret = srd_decoder_load("uart");
+ fail_unless(ret == SRD_OK, "Loading uart PD 2x failed: %d", ret);
+ ret = srd_decoder_load("uart");
+ fail_unless(ret == SRD_OK, "Loading uart PD 3x failed: %d", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_load() fails if a non-existing PD dir is used.
+ * If it returns SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_load_nonexisting_pd_dir)
+{
+#if 0
+ /* TODO: Build libsigrokdecode with no default PD dir. */
+ srd_init("/nonexisting_dir");
+ fail_unless(srd_decoder_load("spi") != SRD_OK);
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
+ srd_exit();
+#endif
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_list() returns a non-empty list.
+ * If it returns an empty list (or segfaults) this test will fail.
+ */
+START_TEST(test_decoder_list)
+{
+ srd_init(DECODERS_DIR);
+ srd_decoder_load_all();
+ fail_unless(srd_decoder_list() != NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_list() without prior srd_decoder_load_all()
+ * returns an empty list (return value != NULL).
+ * If it returns a non-empty list (or segfaults) this test will fail.
+ */
+START_TEST(test_decoder_list_no_load)
+{
+ srd_init(DECODERS_DIR);
+ fail_unless(srd_decoder_list() == NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_list() without prior srd_init()
+ * returns an empty list.
+ * If it returns a non-empty list (or segfaults) this test will fail.
+ * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=178
+ */
+START_TEST(test_decoder_list_no_init)
+{
+ srd_decoder_load_all();
+ fail_unless(srd_decoder_list() == NULL);
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_list() without prior srd_init() and without
+ * prior srd_decoder_load_all() returns an empty list.
+ * If it returns a non-empty list (or segfaults) this test will fail.
+ */
+START_TEST(test_decoder_list_no_init_no_load)
+{
+ fail_unless(srd_decoder_list() == NULL);
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_list() returns the correct number of PDs.
+ * If it returns a wrong number (or segfaults) this test will fail.
+ */
+START_TEST(test_decoder_list_correct_numbers)
+{
+ srd_init(DECODERS_DIR);
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 0);
+ srd_decoder_load("spi");
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 1);
+ srd_decoder_load("uart");
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 2);
+ srd_decoder_load("can");
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
+ srd_decoder_load("can"); /* Load same PD twice. */
+ fail_unless(g_slist_length((GSList *)srd_decoder_list()) == 3);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_get_by_id() works.
+ * If it returns NULL for valid PDs (or segfaults) this test will fail.
+ */
+START_TEST(test_get_by_id)
+{
+ srd_init(DECODERS_DIR);
+ srd_decoder_load("uart");
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ fail_unless(srd_decoder_get_by_id("can") == NULL);
+ srd_decoder_load("can");
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ fail_unless(srd_decoder_get_by_id("can") != NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_get_by_id() works multiple times in a row.
+ * If it returns NULL for valid PDs (or segfaults) this test will fail.
+ */
+START_TEST(test_get_by_id_multiple)
+{
+ srd_init(DECODERS_DIR);
+ srd_decoder_load("uart");
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ fail_unless(srd_decoder_get_by_id("uart") != NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_get_by_id() fails for bogus PDs.
+ * If it returns a value != NULL (or segfaults) this test will fail.
+ */
+START_TEST(test_get_by_id_bogus)
+{
+ srd_init(DECODERS_DIR);
+ fail_unless(srd_decoder_get_by_id(NULL) == NULL);
+ fail_unless(srd_decoder_get_by_id("") == NULL);
+ fail_unless(srd_decoder_get_by_id(" ") == NULL);
+ fail_unless(srd_decoder_get_by_id("nonexisting") == NULL);
+ fail_unless(srd_decoder_get_by_id("sPi") == NULL);
+ fail_unless(srd_decoder_get_by_id("SPI") == NULL);
+ fail_unless(srd_decoder_get_by_id("s p i") == NULL);
+ fail_unless(srd_decoder_get_by_id(" spi") == NULL);
+ fail_unless(srd_decoder_get_by_id("spi ") == NULL);
+ fail_unless(srd_decoder_get_by_id(" spi ") == NULL);
+ fail_unless(srd_decoder_get_by_id("spi uart") == NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_doc_get() works.
+ * If it returns NULL for valid PDs (or segfaults) this test will fail.
+ */
+START_TEST(test_doc_get)
+{
+ struct srd_decoder *dec;
+
+ srd_init(DECODERS_DIR);
+ srd_decoder_load("uart");
+ dec = srd_decoder_get_by_id("uart");
+ fail_unless(srd_decoder_doc_get(dec) != NULL);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_doc_get() fails with NULL as argument.
+ * If it returns a value != NULL (or segfaults) this test will fail.
+ * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=179
+ */
+START_TEST(test_doc_get_null)
+{
+ srd_init(DECODERS_DIR);
+ fail_unless(srd_decoder_doc_get(NULL) == NULL);
+ srd_exit();
+}
+END_TEST
+
+Suite *suite_decoder(void)
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("decoder");
+
+ tc = tcase_create("load");
+ tcase_set_timeout(tc, 0);
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_load_all);
+ tcase_add_test(tc, test_load_all_no_init);
+ tcase_add_test(tc, test_load);
+ tcase_add_test(tc, test_load_bogus);
+ tcase_add_test(tc, test_load_valid_and_bogus);
+ tcase_add_test(tc, test_load_multiple);
+ tcase_add_test(tc, test_load_nonexisting_pd_dir);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("list");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_decoder_list);
+ tcase_add_test(tc, test_decoder_list_no_load);
+ tcase_add_test(tc, test_decoder_list_no_init);
+ tcase_add_test(tc, test_decoder_list_no_init_no_load);
+ tcase_add_test(tc, test_decoder_list_correct_numbers);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("get_by_id");
+ tcase_add_test(tc, test_get_by_id);
+ tcase_add_test(tc, test_get_by_id_multiple);
+ tcase_add_test(tc, test_get_by_id_bogus);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("doc_get");
+ tcase_add_test(tc, test_doc_get);
+ tcase_add_test(tc, test_doc_get_null);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
--- /dev/null
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
+#include <stdlib.h>
+#include <check.h>
+#include "lib.h"
+
+/*
+ * Check whether srd_inst_new() works.
+ * If it returns NULL (or segfaults) this test will fail.
+ */
+START_TEST(test_inst_new)
+{
+ struct srd_session *sess;
+ struct srd_decoder_inst *inst;
+
+ srd_init(DECODERS_DIR);
+ srd_decoder_load("uart");
+ srd_session_new(&sess);
+ inst = srd_inst_new(sess, "uart", NULL);
+ fail_unless(inst != NULL, "srd_inst_new() failed.");
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether multiple srd_inst_new() calls work.
+ * If any of them returns NULL (or segfaults) this test will fail.
+ */
+START_TEST(test_inst_new_multiple)
+{
+ struct srd_session *sess;
+ struct srd_decoder_inst *inst1, *inst2, *inst3;
+
+ inst1 = inst2 = inst3 = NULL;
+
+ srd_init(DECODERS_DIR);
+ srd_decoder_load_all();
+ srd_session_new(&sess);
+
+ /* Multiple srd_inst_new() calls must work. */
+ inst1 = srd_inst_new(sess, "uart", NULL);
+ fail_unless(inst1 != NULL, "srd_inst_new() 1 failed.");
+ inst2 = srd_inst_new(sess, "spi", NULL);
+ fail_unless(inst2 != NULL, "srd_inst_new() 2 failed.");
+ inst3 = srd_inst_new(sess, "can", NULL);
+ fail_unless(inst3 != NULL, "srd_inst_new() 3 failed.");
+
+ /* The returned instance pointers must not be the same. */
+ fail_unless(inst1 != inst2);
+ fail_unless(inst1 != inst3);
+ fail_unless(inst2 != inst3);
+
+ /* Each instance must have another py_inst than any of the others. */
+ fail_unless(inst1->py_inst != inst2->py_inst);
+ fail_unless(inst1->py_inst != inst3->py_inst);
+ fail_unless(inst2->py_inst != inst3->py_inst);
+
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_inst_option_set() works for an empty options hash.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_inst_option_set_empty)
+{
+ int ret;
+ struct srd_session *sess;
+ struct srd_decoder_inst *inst;
+ GHashTable *options;
+
+ srd_init(DECODERS_DIR);
+ srd_decoder_load_all();
+ srd_session_new(&sess);
+ inst = srd_inst_new(sess, "uart", NULL);
+ options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+ (GDestroyNotify)g_variant_unref);
+ ret = srd_inst_option_set(inst, options);
+ fail_unless(ret == SRD_OK, "srd_inst_option_set() with empty options "
+ "hash failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_inst_option_set() works for bogus options.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_inst_option_set_bogus)
+{
+ int ret;
+ struct srd_session *sess;
+ struct srd_decoder_inst *inst;
+ GHashTable *options;
+
+ srd_init(DECODERS_DIR);
+ srd_decoder_load_all();
+ srd_session_new(&sess);
+ inst = srd_inst_new(sess, "uart", NULL);
+
+ options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free,
+ (GDestroyNotify)g_variant_unref);
+
+ /* NULL instance. */
+ ret = srd_inst_option_set(NULL, options);
+ fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
+ "instance failed: %d.", ret);
+
+ /* NULL 'options' GHashTable. */
+ ret = srd_inst_option_set(inst, NULL);
+ fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
+ "options hash failed: %d.", ret);
+
+ /* NULL instance and NULL 'options' GHashTable. */
+ ret = srd_inst_option_set(NULL, NULL);
+ fail_unless(ret != SRD_OK, "srd_inst_option_set() with NULL "
+ "instance and NULL options hash failed: %d.", ret);
+
+ srd_exit();
+}
+END_TEST
+
+Suite *suite_inst(void)
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("inst");
+
+ tc = tcase_create("new");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_inst_new);
+ tcase_add_test(tc, test_inst_new_multiple);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("option");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_inst_option_set_empty);
+ tcase_add_test(tc, test_inst_option_set_bogus);
+ suite_add_tcase(s, tc);
+
+ return s;
+}
--- /dev/null
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "../libsigrokdecode.h" /* First, to avoid compiler warning. */
+#include <stdlib.h>
+#include <check.h>
+#include "lib.h"
+
+void srdtest_setup(void)
+{
+ /* Silence libsigrokdecode while the unit tests run. */
+ srd_log_loglevel_set(SRD_LOG_NONE);
+}
+
+void srdtest_teardown(void)
+{
+}
+
+int main(void)
+{
+ int ret;
+ Suite *s;
+ SRunner *srunner;
+
+ s = suite_create("mastersuite");
+ srunner = srunner_create(s);
+
+ /* Add all testsuites to the master suite. */
+ srunner_add_suite(srunner, suite_core());
+ srunner_add_suite(srunner, suite_decoder());
+ srunner_add_suite(srunner, suite_inst());
+ srunner_add_suite(srunner, suite_session());
+
+ srunner_run_all(srunner, CK_VERBOSE);
+ ret = srunner_ntests_failed(srunner);
+ srunner_free(srunner);
+
+ return (ret == 0) ? EXIT_SUCCESS : EXIT_FAILURE;
+}
--- /dev/null
+/*
+ * This file is part of the libsigrokdecode project.
+ *
+ * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 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 General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
+ */
+
+#include "../libsigrokdecode-internal.h" /* First, to avoid compiler warning. */
+#include "../libsigrokdecode.h"
+#include <stdint.h>
+#include <stdlib.h>
+#include <check.h>
+#include "lib.h"
+
+/*
+ * Check whether srd_session_new() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_new)
+{
+ int ret;
+ struct srd_session *sess;
+
+ srd_init(NULL);
+ ret = srd_session_new(&sess);
+ fail_unless(ret == SRD_OK, "srd_session_new() failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_session_new() fails for bogus parameters.
+ * If it returns SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_new_bogus)
+{
+ int ret;
+
+ srd_init(NULL);
+ ret = srd_session_new(NULL);
+ fail_unless(ret != SRD_OK, "srd_session_new(NULL) worked.");
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether multiple srd_session_new() calls work.
+ * If any call returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_new_multiple)
+{
+ int ret;
+ struct srd_session *sess1, *sess2, *sess3;
+
+ sess1 = sess2 = sess3 = NULL;
+
+ srd_init(NULL);
+
+ /* Multiple srd_session_new() calls must work. */
+ ret = srd_session_new(&sess1);
+ fail_unless(ret == SRD_OK, "srd_session_new() 1 failed: %d.", ret);
+ ret = srd_session_new(&sess2);
+ fail_unless(ret == SRD_OK, "srd_session_new() 2 failed: %d.", ret);
+ ret = srd_session_new(&sess3);
+ fail_unless(ret == SRD_OK, "srd_session_new() 3 failed: %d.", ret);
+
+ /* The returned session pointers must all be non-NULL. */
+ fail_unless(sess1 != NULL);
+ fail_unless(sess2 != NULL);
+ fail_unless(sess3 != NULL);
+
+ /* The returned session pointers must not be the same. */
+ fail_unless(sess1 != sess2);
+ fail_unless(sess1 != sess3);
+ fail_unless(sess2 != sess3);
+
+ /* Each session must have another ID than any other session. */
+ fail_unless(sess1->session_id != sess2->session_id);
+ fail_unless(sess1->session_id != sess3->session_id);
+ fail_unless(sess2->session_id != sess3->session_id);
+
+ /* Destroying any of the sessions must work. */
+ ret = srd_session_destroy(sess1);
+ fail_unless(ret == SRD_OK, "srd_session_destroy() 1 failed: %d.", ret);
+ ret = srd_session_destroy(sess2);
+ fail_unless(ret == SRD_OK, "srd_session_destroy() 2 failed: %d.", ret);
+ ret = srd_session_destroy(sess3);
+ fail_unless(ret == SRD_OK, "srd_session_destroy() 3 failed: %d.", ret);
+
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_session_destroy() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_destroy)
+{
+ int ret;
+ struct srd_session *sess;
+
+ srd_init(NULL);
+ srd_session_new(&sess);
+ ret = srd_session_destroy(sess);
+ fail_unless(ret == SRD_OK, "srd_session_destroy() failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_session_destroy() fails for bogus sessions.
+ * If it returns SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_destroy_bogus)
+{
+ int ret;
+
+ srd_init(NULL);
+ ret = srd_session_destroy(NULL);
+ fail_unless(ret != SRD_OK, "srd_session_destroy() failed: %d.", ret);
+ srd_exit();
+}
+END_TEST
+
+static void conf_check_ok(struct srd_session *sess, int key, uint64_t x)
+{
+ int ret;
+
+ ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
+ fail_unless(ret == SRD_OK, "srd_session_metadata_set(%p, %d, %"
+ PRIu64 ") failed: %d.", sess, key, x, ret);
+}
+
+static void conf_check_fail(struct srd_session *sess, int key, uint64_t x)
+{
+ int ret;
+
+ ret = srd_session_metadata_set(sess, key, g_variant_new_uint64(x));
+ fail_unless(ret != SRD_OK, "srd_session_metadata_set(%p, %d, %"
+ PRIu64 ") worked.", sess, key, x);
+}
+
+static void conf_check_fail_null(struct srd_session *sess, int key)
+{
+ int ret;
+
+ ret = srd_session_metadata_set(sess, key, NULL);
+ fail_unless(ret != SRD_OK,
+ "srd_session_metadata_set(NULL) for key %d worked.", key);
+}
+
+static void conf_check_fail_str(struct srd_session *sess, int key, const char *s)
+{
+ int ret;
+
+ ret = srd_session_metadata_set(sess, key, g_variant_new_string(s));
+ fail_unless(ret != SRD_OK, "srd_session_metadata_set() for key %d "
+ "failed: %d.", key, ret);
+}
+
+/*
+ * Check whether srd_session_metadata_set() works.
+ * If it returns != SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_metadata_set)
+{
+ uint64_t i;
+ struct srd_session *sess;
+
+ srd_init(NULL);
+ srd_session_new(&sess);
+ /* Try a bunch of values. */
+ for (i = 0; i < 1000; i++)
+ conf_check_ok(sess, SRD_CONF_SAMPLERATE, i);
+ /* Try the max. possible value. */
+ conf_check_ok(sess, SRD_CONF_SAMPLERATE, UINT64_MAX);
+ srd_session_destroy(sess);
+ srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_session_metadata_set() fails with invalid input.
+ * If it returns SRD_OK (or segfaults) this test will fail.
+ */
+START_TEST(test_session_metadata_set_bogus)
+{
+ struct srd_session *sess;
+
+ srd_init(NULL);
+ srd_session_new(&sess);
+
+ /* Incorrect gvariant type (currently only uint64 is used). */
+ conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "");
+ conf_check_fail_str(sess, SRD_CONF_SAMPLERATE, "Foo");
+
+ /* NULL data pointer. */
+ conf_check_fail_null(sess, SRD_CONF_SAMPLERATE);
+
+ /* NULL session. */
+ conf_check_fail(NULL, SRD_CONF_SAMPLERATE, 0);
+
+ /* Invalid keys. */
+ conf_check_fail(sess, -1, 0);
+ conf_check_fail(sess, 9, 0);
+ conf_check_fail(sess, 123, 0);
+
+ srd_session_destroy(sess);
+ srd_exit();
+}
+END_TEST
+
+Suite *suite_session(void)
+{
+ Suite *s;
+ TCase *tc;
+
+ s = suite_create("session");
+
+ tc = tcase_create("new_destroy");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_session_new);
+ tcase_add_test(tc, test_session_new_bogus);
+ tcase_add_test(tc, test_session_new_multiple);
+ tcase_add_test(tc, test_session_destroy);
+ tcase_add_test(tc, test_session_destroy_bogus);
+ suite_add_tcase(s, tc);
+
+ tc = tcase_create("config");
+ tcase_add_checked_fixture(tc, srdtest_setup, srdtest_teardown);
+ tcase_add_test(tc, test_session_metadata_set);
+ tcase_add_test(tc, test_session_metadata_set_bogus);
+ suite_add_tcase(s, tc);
+
+ return s;
+}