]> sigrok.org Git - libsigrok.git/commitdiff
Add a few unit tests for the analog conversion functions.
authorUwe Hermann <redacted>
Thu, 15 Oct 2015 10:13:43 +0000 (12:13 +0200)
committerUwe Hermann <redacted>
Tue, 20 Oct 2015 22:29:29 +0000 (00:29 +0200)
Makefile.am
tests/analog.c [new file with mode: 0644]
tests/lib.h
tests/main.c

index fe01e7969b168babcdac6275a1af5ab3c90e4678..fb925ce6a1a530ddcb063bc92830dd95cc450f1d 100644 (file)
@@ -524,7 +524,8 @@ tests_main_SOURCES = \
        tests/version.c \
        tests/driver_all.c \
        tests/device.c \
-       tests/trigger.c
+       tests/trigger.c \
+       tests/analog.c
 
 tests_main_LDADD = libsigrok.la $(SR_EXTRA_LIBS) $(TESTS_LIBS)
 
diff --git a/tests/analog.c b/tests/analog.c
new file mode 100644 (file)
index 0000000..c2c6555
--- /dev/null
@@ -0,0 +1,248 @@
+/*
+ * This file is part of the libsigrok project.
+ *
+ * Copyright (C) 2015 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 <config.h>
+#include <stdlib.h>
+#include <math.h>
+#include <check.h>
+#include <libsigrok/libsigrok.h>
+#include "lib.h"
+
+static int sr_analog_init_(struct sr_datafeed_analog *analog,
+               struct sr_analog_encoding *encoding,
+               struct sr_analog_meaning *meaning,
+               struct sr_analog_spec *spec,
+               int digits)
+{
+       memset(analog, 0, sizeof(*analog));
+       memset(encoding, 0, sizeof(*encoding));
+       memset(meaning, 0, sizeof(*meaning));
+       memset(spec, 0, sizeof(*spec));
+
+       analog->encoding = encoding;
+       analog->meaning = meaning;
+       analog->spec = spec;
+
+       encoding->unitsize = sizeof(float);
+       encoding->is_float = TRUE;
+#ifdef WORDS_BIGENDIAN
+       encoding->is_bigendian = TRUE;
+#else
+       encoding->is_bigendian = FALSE;
+#endif
+       encoding->digits = digits;
+       encoding->is_digits_decimal = TRUE;
+       encoding->scale.p = 1;
+       encoding->scale.q = 1;
+       encoding->offset.p = 0;
+       encoding->offset.q = 1;
+
+       spec->spec_digits = digits;
+
+       return SR_OK;
+}
+
+START_TEST(test_analog_to_float)
+{
+       int ret;
+       unsigned int i;
+       float f, fout;
+       struct sr_channel ch;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+       const float v[] = {-12.9, -333.999, 0, 3.1415, 29.7, 989898.121212};
+
+       sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
+       analog.num_samples = 1;
+       analog.data = &f;
+       meaning.channels = g_slist_append(NULL, &ch);
+
+       for (i = 0; i < ARRAY_SIZE(v); i++) {
+               fout = 19;
+               f = v[i];
+               ret = sr_analog_to_float(&analog, &fout);
+               fail_unless(ret == SR_OK, "sr_analog_to_float() failed: %d.", ret);
+               fail_unless(fabs(f - fout) <= 0.001, "%f != %f", f, fout);
+       }
+}
+END_TEST
+
+START_TEST(test_analog_to_float_null)
+{
+       int ret;
+       float f, fout;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+
+       f = G_PI;
+       sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
+       analog.num_samples = 1;
+       analog.data = &f;
+
+       ret = sr_analog_to_float(NULL, &fout);
+       fail_unless(ret == SR_ERR_ARG);
+       ret = sr_analog_to_float(&analog, NULL);
+       fail_unless(ret == SR_ERR_ARG);
+       ret = sr_analog_to_float(NULL, NULL);
+       fail_unless(ret == SR_ERR_ARG);
+
+       analog.data = NULL;
+       ret = sr_analog_to_float(&analog, &fout);
+       fail_unless(ret == SR_ERR_ARG);
+       analog.data = &f;
+
+       analog.meaning = NULL;
+       ret = sr_analog_to_float(&analog, &fout);
+       fail_unless(ret == SR_ERR_ARG);
+       analog.meaning = &meaning;
+
+       analog.encoding = NULL;
+       ret = sr_analog_to_float(&analog, &fout);
+       fail_unless(ret == SR_ERR_ARG);
+       analog.encoding = &encoding;
+}
+END_TEST
+
+#if 0
+START_TEST(test_analog_float_to_string)
+{
+       int ret;
+       unsigned int i;
+       char *result;
+       const char *r[] = {"3", "3.1", "3.14", "3.145", "3.1415", "3.15159"};
+
+       for (i = 0; i < ARRAY_SIZE(r); i++) {
+               ret = sr_analog_float_to_string(G_PI, i, &result);
+               fail_unless(ret == SR_OK);
+               fail_unless(result != NULL);
+               fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
+               g_free(result);
+       }
+}
+END_TEST
+#endif
+
+START_TEST(test_analog_float_to_string_null)
+{
+       int ret;
+
+       ret = sr_analog_float_to_string(0, 0, NULL);
+       fail_unless(ret == SR_ERR_ARG);
+}
+END_TEST
+
+START_TEST(test_analog_unit_to_string)
+{
+       int ret;
+       unsigned int i;
+       char *result;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+       const char *r[] = {" V RMS"};
+
+       sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
+
+       for (i = -1; i < ARRAY_SIZE(r); i++) {
+               meaning.unit = SR_UNIT_VOLT;
+               meaning.mqflags = SR_MQFLAG_RMS;
+               ret = sr_analog_unit_to_string(&analog, &result);
+               fail_unless(ret == SR_OK);
+               fail_unless(result != NULL);
+               fail_unless(!strcmp(result, r[i]), "%s != %s", result, r[i]);
+               g_free(result);
+       }
+}
+END_TEST
+
+START_TEST(test_analog_unit_to_string_null)
+{
+       int ret;
+       char *result;
+       struct sr_datafeed_analog analog;
+       struct sr_analog_encoding encoding;
+       struct sr_analog_meaning meaning;
+       struct sr_analog_spec spec;
+
+       sr_analog_init_(&analog, &encoding, &meaning, &spec, 3);
+
+       meaning.unit = SR_UNIT_VOLT;
+       meaning.mqflags = SR_MQFLAG_RMS;
+
+       ret = sr_analog_unit_to_string(NULL, &result);
+       fail_unless(ret == SR_ERR_ARG);
+       ret = sr_analog_unit_to_string(&analog, NULL);
+       fail_unless(ret == SR_ERR_ARG);
+       ret = sr_analog_unit_to_string(NULL, NULL);
+       fail_unless(ret == SR_ERR_ARG);
+
+       analog.meaning = NULL;
+       ret = sr_analog_unit_to_string(&analog, &result);
+       fail_unless(ret == SR_ERR_ARG);
+}
+END_TEST
+
+START_TEST(test_set_rational)
+{
+       unsigned int i;
+       struct sr_rational r;
+       const int64_t p[] = {0, 1, -5, INT64_MAX};
+       const uint64_t q[] = {0, 2, 7, UINT64_MAX};
+
+       for (i = 0; i < ARRAY_SIZE(p); i++) {
+               sr_rational_set(&r, p[i], q[i]);
+               fail_unless(r.p == p[i] && r.q == q[i]);
+       }
+}
+END_TEST
+
+START_TEST(test_set_rational_null)
+{
+       sr_rational_set(NULL, 5, 7);
+}
+END_TEST
+
+Suite *suite_analog(void)
+{
+       Suite *s;
+       TCase *tc;
+
+       s = suite_create("analog");
+
+       tc = tcase_create("analog_to_float");
+       tcase_add_test(tc, test_analog_to_float);
+       tcase_add_test(tc, test_analog_to_float_null);
+#if 0
+       tcase_add_test(tc, test_analog_float_to_string);
+#endif
+       tcase_add_test(tc, test_analog_float_to_string_null);
+       tcase_add_test(tc, test_analog_unit_to_string);
+       tcase_add_test(tc, test_analog_unit_to_string_null);
+       tcase_add_test(tc, test_set_rational);
+       tcase_add_test(tc, test_set_rational_null);
+       suite_add_tcase(s, tc);
+
+       return s;
+}
index cefe0438d7a4a201d73e600906e81dd76a33b629..576e06853105a7b53b2ed9dbfb039870be3b4a90 100644 (file)
@@ -23,6 +23,8 @@
 
 #include <libsigrok/libsigrok.h>
 
+#define ARRAY_SIZE(a) (sizeof(a) / sizeof((a)[0]))
+
 extern struct sr_context *srtest_ctx;
 
 void srtest_setup(void);
@@ -51,5 +53,6 @@ Suite *suite_strutil(void);
 Suite *suite_version(void);
 Suite *suite_device(void);
 Suite *suite_trigger(void);
+Suite *suite_analog(void);
 
 #endif
index e3ba55ae48241f81e9440b7b8aacd95ceae99510..cdd7b4fc391eed84625ddad7bd526396e207435a 100644 (file)
@@ -45,6 +45,7 @@ int main(void)
        srunner_add_suite(srunner, suite_version());
        srunner_add_suite(srunner, suite_device());
        srunner_add_suite(srunner, suite_trigger());
+       srunner_add_suite(srunner, suite_analog());
 
        srunner_run_all(srunner, CK_VERBOSE);
        ret = srunner_ntests_failed(srunner);