]> sigrok.org Git - libsigrokdecode.git/commitdiff
testsuite: Add a few more unit tests.
authorUwe Hermann <redacted>
Thu, 17 Oct 2013 20:44:29 +0000 (22:44 +0200)
committerUwe Hermann <redacted>
Thu, 17 Oct 2013 22:20:07 +0000 (00:20 +0200)
tests/check_decoder.c

index 1ad370a9210c77c5ea198b2a70a9697ceaf650b7..0b46348b70dc60419ebb50bea10d3a6469cf5876 100644 (file)
 #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_decoder_load_all() works.
  * If it returns != SRD_OK (or segfaults) this test will fail.
@@ -37,6 +47,20 @@ START_TEST(test_load_all)
 }
 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() failed: %d.", ret);
+}
+END_TEST
+
 /*
  * Check whether srd_decoder_load() works.
  * If it returns != SRD_OK (or segfaults) this test will fail.
@@ -63,6 +87,7 @@ END_TEST
 START_TEST(test_load_bogus)
 {
        srd_init(NULL);
+       /* 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);
@@ -78,6 +103,233 @@ START_TEST(test_load_bogus)
 }
 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(NULL);
+       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(NULL);
+       srd_log_loglevel_set(SRD_LOG_SPEW);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       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(NULL);
+       fail_unless(srd_decoder_doc_get(NULL) != NULL);
+       srd_exit();
+}
+END_TEST
+
+/*
+ * Check whether srd_decoder_doc_get() fails with an "empty" argument.
+ * If it returns a value != NULL (or segfaults) this test will fail.
+ * See also: http://sigrok.org/bugzilla/show_bug.cgi?id=180
+ */
+START_TEST(test_doc_get_empty)
+{
+       struct srd_decoder dec;
+
+       srd_init(NULL);
+
+       memset(&dec, 0, sizeof(struct srd_decoder));
+       fail_unless(srd_decoder_doc_get(&dec) == NULL);
+
+       srd_exit();
+}
+END_TEST
+
 Suite *suite_decoder(void)
 {
        Suite *s;
@@ -86,9 +338,35 @@ Suite *suite_decoder(void)
        s = suite_create("decoder");
 
        tc = tcase_create("load");
+       tcase_add_checked_fixture(tc, setup, 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, setup, 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);
+       tcase_add_test(tc, test_doc_get_empty);
        suite_add_tcase(s, tc);
 
        return s;