]> sigrok.org Git - libsigrok.git/blame - tests/lib.c
Fix sr_si_string_u64() test cases.
[libsigrok.git] / tests / lib.c
CommitLineData
79bb0e97
UH
1/*
2 * This file is part of the libsigrok project.
3 *
4 * Copyright (C) 2013 Uwe Hermann <uwe@hermann-uwe.de>
5 *
6 * This program is free software; you can redistribute it and/or modify
7 * it under the terms of the GNU General Public License as published by
8 * the Free Software Foundation; either version 2 of the License, or
9 * (at your option) any later version.
10 *
11 * This program is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
14 * GNU General Public License for more details.
15 *
16 * You should have received a copy of the GNU General Public License
17 * along with this program; if not, write to the Free Software
18 * Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
19 */
20
21#include <stdio.h>
22#include <string.h>
23#include <check.h>
24#include "../libsigrok.h"
25
26/* Get a libsigrok driver by name. */
27struct sr_dev_driver *srtest_driver_get(const char *drivername)
28{
29 struct sr_dev_driver **drivers, *driver = NULL;
30 int i;
31
32 drivers = sr_driver_list();
33 fail_unless(drivers != NULL, "No drivers found.");
34
35 for (i = 0; drivers[i]; i++) {
36 if (strcmp(drivers[i]->name, drivername))
37 continue;
38 driver = drivers[i];
39 }
40 fail_unless(driver != NULL, "Driver '%s' not found.", drivername);
41
42 return driver;
43}
44
45/* Initialize a libsigrok driver. */
46void srtest_driver_init(struct sr_context *sr_ctx, struct sr_dev_driver *driver)
47{
48 int ret;
49
50 ret = sr_driver_init(sr_ctx, driver);
51 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
52 driver->name, ret);
53}
54
55/* Initialize all libsigrok drivers. */
56void srtest_driver_init_all(struct sr_context *sr_ctx)
57{
58 struct sr_dev_driver **drivers, *driver;
59 int i, ret;
60
61 drivers = sr_driver_list();
62 fail_unless(drivers != NULL, "No drivers found.");
63
64 for (i = 0; drivers[i]; i++) {
65 driver = drivers[i];
66 ret = sr_driver_init(sr_ctx, driver);
67 fail_unless(ret == SR_OK, "Failed to init '%s' driver: %d.",
68 driver->name, ret);
69 }
70}
71
72/* Set the samplerate for the respective driver to the specified value. */
73void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
74{
75 int ret;
76 struct sr_dev_inst *sdi;
34e4c273 77 GVariant *gvar;
79bb0e97
UH
78
79 sdi = g_slist_nth_data(driver->priv, 0);
80
34e4c273
UH
81 gvar = g_variant_new_uint64(samplerate);
82 ret = driver->config_set(SR_CONF_SAMPLERATE, gvar, sdi);
83 g_variant_unref(gvar);
84
79bb0e97
UH
85 fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
86 driver->name, ret);
87}
88
89/* Get the respective driver's current samplerate. */
90uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
91{
92 int ret;
34e4c273 93 uint64_t samplerate;
79bb0e97 94 struct sr_dev_inst *sdi;
34e4c273 95 GVariant *gvar;
79bb0e97
UH
96
97 sdi = g_slist_nth_data(driver->priv, 0);
98
34e4c273
UH
99 ret = driver->config_get(SR_CONF_SAMPLERATE, &gvar, sdi);
100 samplerate = g_variant_get_uint64(gvar);
101 g_variant_unref(gvar);
102
79bb0e97
UH
103 fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
104 driver->name, ret);
79bb0e97 105
34e4c273 106 return samplerate;
79bb0e97
UH
107}
108
109/* Check whether the respective driver can set/get the correct samplerate. */
110void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
111 uint64_t samplerate)
112{
113 struct sr_dev_driver *driver;
114 uint64_t s;
115
116 driver = srtest_driver_get(drivername);
117 srtest_driver_init(sr_ctx, driver);;
118 srtest_set_samplerate(driver, samplerate);
119 s = srtest_get_samplerate(driver);
120 fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
121 drivername, s);
122}