X-Git-Url: https://sigrok.org/gitweb/?a=blobdiff_plain;f=tests%2Fcheck_input_binary.c;h=18567a0b613540c07a70372cf606bb93d03bb438;hb=25f94dfebced0db7b0c0c403a1c90c517017fca5;hp=aad2b04e5f9ddcad6e9a59bfc5e4dbed04669d15;hpb=6592c3699509213395fa80af5b37f2e4db25ac38;p=libsigrok.git diff --git a/tests/check_input_binary.c b/tests/check_input_binary.c index aad2b04e..18567a0b 100644 --- a/tests/check_input_binary.c +++ b/tests/check_input_binary.c @@ -1,7 +1,7 @@ /* * This file is part of the libsigrok project. * - * Copyright (C) 2013 Uwe Hermann + * Copyright (C) 2013-2014 Uwe Hermann * * 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 @@ -23,14 +23,13 @@ #include "../include/libsigrok/libsigrok.h" #include "lib.h" -#define FILENAME "foo.dat" -#define MAX_FILESIZE (1 * 1000000) +#define BUFSIZE 1000000 -#define CHECK_ALL_LOW 0 -#define CHECK_ALL_HIGH 1 -#define CHECK_HELLO_WORLD 2 - -static struct sr_context *sr_ctx; +enum { + CHECK_ALL_LOW, + CHECK_ALL_HIGH, + CHECK_HELLO_WORLD, +}; static uint64_t df_packet_counter = 0, sample_counter = 0; static gboolean have_seen_df_end = FALSE; @@ -39,22 +38,6 @@ static int check_to_perform; static uint64_t expected_samples; static uint64_t *expected_samplerate; -static void setup(void) -{ - int ret; - - ret = sr_init(&sr_ctx); - fail_unless(ret == SR_OK, "sr_init() failed: %d.", ret); -} - -static void teardown(void) -{ - int ret; - - ret = sr_exit(sr_ctx); - fail_unless(ret == SR_OK, "sr_exit() failed: %d.", ret); -} - static void check_all_low(const struct sr_datafeed_logic *logic) { uint64_t i; @@ -197,13 +180,15 @@ static void datafeed_in(const struct sr_dev_inst *sdi, } } -static void check_buf(const char *filename, GHashTable *param, - const uint8_t *buf, int check, uint64_t samples, - uint64_t *samplerate) +static void check_buf(GHashTable *options, const uint8_t *buf, int check, + uint64_t samples, uint64_t *samplerate) { int ret; struct sr_input *in; - struct sr_input_format *in_format; + const struct sr_input_module *imod; + struct sr_session *session; + struct sr_dev_inst *sdi; + GString *gbuf; /* Initialize global variables for this run. */ df_packet_counter = sample_counter = 0; @@ -213,52 +198,57 @@ static void check_buf(const char *filename, GHashTable *param, expected_samples = samples; expected_samplerate = samplerate; - in_format = srtest_input_get("binary"); + gbuf = g_string_new_len((gchar *)buf, (gssize)samples); + + imod = sr_input_find("binary"); + fail_unless(imod != NULL, "Failed to find input module."); - in = g_try_malloc0(sizeof(struct sr_input)); - fail_unless(in != NULL); + in = sr_input_new(imod, options); + fail_unless(in != NULL, "Failed to create input instance."); - in->format = in_format; - in->param = param; + sdi = sr_input_dev_inst_get(in); + fail_unless(sdi != NULL, "Failed to get device instance."); - srtest_buf_to_file(filename, buf, samples); /* Create a file. */ + sr_session_new(&session); + sr_session_datafeed_callback_add(session, datafeed_in, NULL); + sr_session_dev_add(session, sdi); - ret = in->format->init(in, filename); - fail_unless(ret == SR_OK, "Input format init error: %d", ret); - - sr_session_new(); - sr_session_datafeed_callback_add(datafeed_in, NULL); - sr_session_dev_add(in->sdi); - in_format->loadfile(in, filename); - sr_session_destroy(); + ret = sr_input_send(in, gbuf); + fail_unless(ret == SR_OK, "sr_input_send() error: %d", ret); - g_unlink(filename); /* Delete file again. */ + ret = sr_input_free(in); + fail_unless(ret == SR_OK, "Failed to free input instance: %d", ret); + + sr_session_destroy(session); + + g_string_free(gbuf, TRUE); } START_TEST(test_input_binary_all_low) { uint64_t i, samplerate; + GHashTable *options; uint8_t *buf; - GHashTable *param; + GVariant *gvar; - buf = g_try_malloc0(MAX_FILESIZE); - fail_unless(buf != NULL); + buf = g_malloc0(BUFSIZE); - param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - fail_unless(param != NULL); - g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250")); + gvar = g_variant_new_uint64(1250); + options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + (GDestroyNotify)g_variant_unref); + g_hash_table_insert(options, g_strdup("samplerate"), + g_variant_ref_sink(gvar)); samplerate = SR_HZ(1250); /* Check various filesizes, with/without specifying a samplerate. */ - check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL); - check_buf(FILENAME, param, buf, CHECK_ALL_LOW, 0, &samplerate); - for (i = 1; i < MAX_FILESIZE; i *= 3) { - check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, i, NULL); - check_buf(FILENAME, param, buf, CHECK_ALL_LOW, i, &samplerate); - + check_buf(NULL, buf, CHECK_ALL_LOW, 0, NULL); + check_buf(options, buf, CHECK_ALL_LOW, 0, &samplerate); + for (i = 1; i < BUFSIZE; i *= 3) { + check_buf(NULL, buf, CHECK_ALL_LOW, i, NULL); + check_buf(options, buf, CHECK_ALL_LOW, i, &samplerate); } - g_hash_table_destroy(param); + g_hash_table_destroy(options); g_free(buf); } END_TEST @@ -268,12 +258,12 @@ START_TEST(test_input_binary_all_high) uint64_t i; uint8_t *buf; - buf = g_try_malloc(MAX_FILESIZE); - memset(buf, 0xff, MAX_FILESIZE); + buf = g_malloc(BUFSIZE); + memset(buf, 0xff, BUFSIZE); - check_buf(FILENAME, NULL, buf, CHECK_ALL_LOW, 0, NULL); - for (i = 1; i < MAX_FILESIZE; i *= 3) - check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, i, NULL); + check_buf(NULL, buf, CHECK_ALL_HIGH, 0, NULL); + for (i = 1; i < BUFSIZE; i *= 3) + check_buf(NULL, buf, CHECK_ALL_HIGH, i, NULL); g_free(buf); } @@ -282,13 +272,15 @@ END_TEST START_TEST(test_input_binary_all_high_loop) { uint8_t *buf; + uint64_t bufsize; /* Note: _i is the loop variable from tcase_add_loop_test(). */ - buf = g_try_malloc((_i * 10) + 1); - memset(buf, 0xff, _i * 10); + bufsize = (_i * 10); + buf = g_malloc(BUFSIZE); + memset(buf, 0xff, BUFSIZE); - check_buf(FILENAME, NULL, buf, CHECK_ALL_HIGH, _i * 10, NULL); + check_buf(NULL, buf, CHECK_ALL_HIGH, bufsize, NULL); g_free(buf); } @@ -298,20 +290,23 @@ START_TEST(test_input_binary_hello_world) { uint64_t samplerate; uint8_t *buf; - GHashTable *param; + GHashTable *options; + GVariant *gvar; buf = (uint8_t *)g_strdup("Hello world"); - param = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, g_free); - fail_unless(param != NULL); - g_hash_table_insert(param, g_strdup("samplerate"), g_strdup("1250")); + gvar = g_variant_new_uint64(1250); + options = g_hash_table_new_full(g_str_hash, g_str_equal, g_free, + (GDestroyNotify)g_variant_unref); + g_hash_table_insert(options, g_strdup("samplerate"), + g_variant_ref_sink(gvar)); samplerate = SR_HZ(1250); /* Check with and without specifying a samplerate. */ - check_buf(FILENAME, NULL, buf, CHECK_HELLO_WORLD, 11, NULL); - check_buf(FILENAME, param, buf, CHECK_HELLO_WORLD, 11, &samplerate); + check_buf(NULL, buf, CHECK_HELLO_WORLD, 11, NULL); + check_buf(options, buf, CHECK_HELLO_WORLD, 11, &samplerate); - g_hash_table_destroy(param); + g_hash_table_destroy(options); g_free(buf); } END_TEST @@ -324,10 +319,10 @@ Suite *suite_input_binary(void) s = suite_create("input-binary"); tc = tcase_create("basic"); - tcase_add_checked_fixture(tc, setup, teardown); + tcase_add_checked_fixture(tc, srtest_setup, srtest_teardown); tcase_add_test(tc, test_input_binary_all_low); tcase_add_test(tc, test_input_binary_all_high); - tcase_add_loop_test(tc, test_input_binary_all_high_loop, 0, 10); + tcase_add_loop_test(tc, test_input_binary_all_high_loop, 1, 10); tcase_add_test(tc, test_input_binary_hello_world); suite_add_tcase(s, tc);