]> sigrok.org Git - libsigrokdecode.git/commitdiff
testsuite: Add some decoder instance related tests.
authorUwe Hermann <redacted>
Wed, 23 Oct 2013 17:21:45 +0000 (19:21 +0200)
committerUwe Hermann <redacted>
Thu, 24 Oct 2013 13:22:16 +0000 (15:22 +0200)
tests/Makefile.am
tests/check_inst.c [new file with mode: 0644]
tests/check_main.c

index 39af793544b0960c0c49a785af978ba04595ef24..b1ac2bf58a8b986a0ed5a9d7448fad9378d7e4df 100644 (file)
@@ -29,6 +29,7 @@ check_main_SOURCES = \
        check_main.c \
        check_core.c \
        check_decoder.c \
+       check_inst.c \
        check_session.c
 
 check_main_CFLAGS = @check_CFLAGS@
diff --git a/tests/check_inst.c b/tests/check_inst.c
new file mode 100644 (file)
index 0000000..25adc4f
--- /dev/null
@@ -0,0 +1,172 @@
+/*
+ * 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>
+
+static void setup(void)
+{
+       /* Silence libsigrokdecode while the unit tests run. */
+       srd_log_loglevel_set(SRD_LOG_NONE);
+}
+
+static void teardown(void)
+{
+}
+
+/*
+ * 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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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, setup, 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, setup, 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;
+}
index 7391bb27b6746063b216a17806d6bbdb23dd199b..8c86532934e51f25b5eec69deaa6639558cbe9ba 100644 (file)
@@ -24,6 +24,7 @@
 
 Suite *suite_core(void);
 Suite *suite_decoder(void);
+Suite *suite_inst(void);
 Suite *suite_session(void);
 
 int main(void)
@@ -38,6 +39,7 @@ int main(void)
        /* 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);