diff mbox series

[libgpiod,3/7] tests: gpiosim: selftests: shrink the self-test code

Message ID 20251006-gpiosim-valid-lines-v1-3-b399373e90a9@linaro.org
State New
Headers show
Series tests: update libgpiosim tests and add a uAPI test case for valid lines | expand

Commit Message

Bartosz Golaszewski Oct. 6, 2025, 11:27 a.m. UTC
From: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>

Most tests failures end with a perror() print and an exit(1) so wrap it
in a helper function and a macro and shrink the code.

Signed-off-by: Bartosz Golaszewski <bartosz.golaszewski@linaro.org>
---
 tests/gpiosim/gpiosim-selftest.c | 112 ++++++++++++++-------------------------
 1 file changed, 39 insertions(+), 73 deletions(-)
diff mbox series

Patch

diff --git a/tests/gpiosim/gpiosim-selftest.c b/tests/gpiosim/gpiosim-selftest.c
index a8c460dd6abdd98e7558ef81a5a6828d02d1a410..749974b974a5b24205477bacee29e1b5df64668e 100644
--- a/tests/gpiosim/gpiosim-selftest.c
+++ b/tests/gpiosim/gpiosim-selftest.c
@@ -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;
 }