]> sigrok.org Git - libsigrok.git/blob - tests/lib.c
Add a testsuite for libsigrok.
[libsigrok.git] / tests / lib.c
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. */
27 struct 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. */
46 void 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. */
56 void 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. */
73 void srtest_set_samplerate(struct sr_dev_driver *driver, uint64_t samplerate)
74 {
75         int ret;
76         struct sr_dev_inst *sdi;
77
78         sdi = g_slist_nth_data(driver->priv, 0);
79
80         ret = driver->config_set(SR_CONF_SAMPLERATE, &samplerate, sdi);
81         fail_unless(ret == SR_OK, "%s: Failed to set SR_CONF_SAMPLERATE: %d.",
82                     driver->name, ret);
83 }
84
85 /* Get the respective driver's current samplerate. */
86 uint64_t srtest_get_samplerate(struct sr_dev_driver *driver)
87 {
88         int ret;
89         uint64_t *samplerate;
90         struct sr_dev_inst *sdi;
91
92         sdi = g_slist_nth_data(driver->priv, 0);
93
94         ret = driver->config_get(SR_CONF_SAMPLERATE,
95                                  (const void **)&samplerate, sdi);
96         fail_unless(ret == SR_OK, "%s: Failed to get SR_CONF_SAMPLERATE: %d.",
97                     driver->name, ret);
98         fail_unless(samplerate != NULL);
99
100         return *samplerate;
101 }
102
103 /* Check whether the respective driver can set/get the correct samplerate. */
104 void srtest_check_samplerate(struct sr_context *sr_ctx, const char *drivername,
105                              uint64_t samplerate)
106 {
107         struct sr_dev_driver *driver;
108         uint64_t s;
109
110         driver = srtest_driver_get(drivername);
111         srtest_driver_init(sr_ctx, driver);;
112         srtest_set_samplerate(driver, samplerate);
113         s = srtest_get_samplerate(driver);
114         fail_unless(s == samplerate, "%s: Incorrect samplerate: %" PRIu64 ".",
115                     drivername, s);
116 }