@@ -6,7 +6,8 @@
#include "gpiosim.h"
-#define UNUSED __attribute__((unused))
+#define UNUSED __attribute__((unused))
+#define NORETURN __attribute__((noreturn))
static const char *const line_names[] = {
"foo",
@@ -16,111 +17,89 @@ static const char *const line_names[] = {
"barfoo",
};
+static NORETURN void die(const char *msg)
+{
+ perror(msg);
+ exit(EXIT_FAILURE);
+}
+
+#define expect_or_die(_cond, _msg) \
+ do { \
+ if (!(_cond)) \
+ die(_msg); \
+ } while (0)
+
int main(int argc UNUSED, char **argv UNUSED)
{
struct gpiosim_bank *bank0, *bank1;
struct gpiosim_dev *dev;
struct gpiosim_ctx *ctx;
+ enum gpiosim_pull pull;
+ enum gpiosim_value val;
int ret, i;
printf("Creating gpiosim context\n");
ctx = gpiosim_ctx_new();
- if (!ctx) {
- perror("unable to create the gpios-sim context");
- return EXIT_FAILURE;
- }
+ expect_or_die(ctx, "unable to create the gpios-sim context");
printf("Creating a chip\n");
dev = gpiosim_dev_new(ctx);
- if (!dev) {
- perror("Unable to create a chip");
- return EXIT_FAILURE;
- }
+ expect_or_die(dev, "Unable to create a chip");
printf("Creating a bank\n");
bank0 = gpiosim_bank_new(dev);
- if (!bank0) {
- perror("Unable to create a bank");
- return EXIT_FAILURE;
- }
+ expect_or_die(bank0, "Unable to create a bank");
printf("Creating a second bank\n");
bank1 = gpiosim_bank_new(dev);
- if (!bank1) {
- perror("Unable to create a bank");
- return EXIT_FAILURE;
- }
+ expect_or_die(bank1, "Unable to create a bank");
printf("Setting the label of bank #2 to foobar\n");
ret = gpiosim_bank_set_label(bank1, "foobar");
- if (ret) {
- perror("Unable to set the label of bank #2");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to set the label of bank #2");
printf("Setting the number of lines in bank #1 to 16\n");
ret = gpiosim_bank_set_num_lines(bank0, 16);
- if (ret) {
- perror("Unable to set the number of lines");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to set the number of lines");
printf("Setting the number of lines in bank #2 to 8\n");
ret = gpiosim_bank_set_num_lines(bank1, 8);
- if (ret) {
- perror("Unable to set the number of lines");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to set the number of lines");
printf("Setting names for some lines in bank #1\n");
for (i = 0; i < 5; i++) {
ret = gpiosim_bank_set_line_name(bank0, i, line_names[i]);
- if (ret) {
- perror("Unable to set line names");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to set line names");
}
printf("Hog a line on bank #2\n");
ret = gpiosim_bank_hog_line(bank1, 3, "xyz",
GPIOSIM_DIRECTION_OUTPUT_HIGH);
- if (ret) {
- perror("Unable to hog a line");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to hog a line");
printf("Enabling the GPIO device\n");
ret = gpiosim_dev_enable(dev);
- if (ret) {
- perror("Unable to enable the device");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to enable the device");
printf("Setting the pull of a single line to pull-up\n");
ret = gpiosim_bank_set_pull(bank0, 6, GPIOSIM_PULL_UP);
- if (ret) {
- perror("Unable to set the pull");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to set the pull");
printf("Reading the pull back\n");
ret = gpiosim_bank_get_pull(bank0, 6);
- if (ret < 0) {
- perror("Unable to read the pull");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret >= 0, "Unable to read the pull");
if (ret != GPIOSIM_PULL_UP) {
fprintf(stderr, "Invalid pull value read\n");
@@ -129,13 +108,10 @@ int main(int argc UNUSED, char **argv UNUSED)
printf("Reading the value\n");
- ret = gpiosim_bank_get_value(bank0, 6);
- if (ret < 0) {
- perror("Unable to read the value");
- return EXIT_FAILURE;
- }
+ val = gpiosim_bank_get_value(bank0, 6);
+ expect_or_die(val != GPIOSIM_VALUE_ERROR, "Unable to read the value");
- if (ret != GPIOSIM_VALUE_ACTIVE) {
+ if (val != GPIOSIM_VALUE_ACTIVE) {
fprintf(stderr, "Invalid value read\n");
return EXIT_FAILURE;
}
@@ -143,28 +119,19 @@ int main(int argc UNUSED, char **argv UNUSED)
printf("Disabling the GPIO device\n");
ret = gpiosim_dev_disable(dev);
- if (ret) {
- perror("Error while disabling the device");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Error while disabling the device");
printf("Re-enabling the GPIO device\n");
ret = gpiosim_dev_enable(dev);
- if (ret) {
- perror("Unable to re-enable the device");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Unable to re-enable the device");
printf("Checking the pull has been reset\n");
- ret = gpiosim_bank_get_pull(bank0, 6);
- if (ret < 0) {
- perror("Unable to read the pull");
- return EXIT_FAILURE;
- }
+ pull = gpiosim_bank_get_pull(bank0, 6);
+ expect_or_die(pull != GPIOSIM_PULL_ERROR, "Unable to read the pull");
- if (ret != GPIOSIM_PULL_DOWN) {
+ if (pull != GPIOSIM_PULL_DOWN) {
fprintf(stderr, "Invalid pull value read\n");
return EXIT_FAILURE;
}
@@ -172,15 +139,14 @@ int main(int argc UNUSED, char **argv UNUSED)
printf("Re-disabling the device\n");
ret = gpiosim_dev_disable(dev);
- if (ret) {
- perror("Error while re-disabling the device");
- return EXIT_FAILURE;
- }
+ expect_or_die(ret == 0, "Error while re-disabling the device");
gpiosim_bank_unref(bank1);
gpiosim_bank_unref(bank0);
gpiosim_dev_unref(dev);
gpiosim_ctx_unref(ctx);
+ printf("ALL TESTS OK\n");
+
return EXIT_SUCCESS;
}