From 07775772a7478c19121c7f68e4593d38f5f27993 Mon Sep 17 00:00:00 2001 From: Thomas Klaehn Date: Thu, 18 Jul 2019 12:08:25 +0200 Subject: [PATCH] Exchange NULL pointer checks with assert --- .vscode/c_cpp_properties.json | 17 +++++++++++++++++ .vscode/launch.json | 2 +- src/gpio.c | 24 +++++++----------------- test/unit/gpio_close.c | 4 ---- test/unit/gpio_open.c | 4 ---- test/unit/gpio_read.c | 5 ----- test/unit/gpio_toggle.c | 5 ----- test/unit/gpio_write.c | 5 ----- 8 files changed, 25 insertions(+), 41 deletions(-) create mode 100644 .vscode/c_cpp_properties.json diff --git a/.vscode/c_cpp_properties.json b/.vscode/c_cpp_properties.json new file mode 100644 index 0000000..5703028 --- /dev/null +++ b/.vscode/c_cpp_properties.json @@ -0,0 +1,17 @@ +{ + "configurations": [ + { + "name": "Linux", + "includePath": [ + "${workspaceFolder}/**", + "/usr/include/libftdi1" + ], + "defines": [], + "compilerPath": "/usr/bin/clang", + "cStandard": "c11", + "cppStandard": "c++17", + "intelliSenseMode": "clang-x64" + } + ], + "version": 4 +} \ No newline at end of file diff --git a/.vscode/launch.json b/.vscode/launch.json index 06a2d90..09caaf0 100644 --- a/.vscode/launch.json +++ b/.vscode/launch.json @@ -8,7 +8,7 @@ "name": "(gdb) Launch", "type": "cppdbg", "request": "launch", - "program": "${workspaceFolder}/bin/test", + "program": "${workspaceFolder}/bin/test/unit/gpio_ftdi", "args": [], "stopAtEntry": false, "cwd": "${workspaceFolder}", diff --git a/src/gpio.c b/src/gpio.c index 4659398..9bc93c4 100644 --- a/src/gpio.c +++ b/src/gpio.c @@ -1,3 +1,4 @@ +#include #include #include #include @@ -38,11 +39,7 @@ int gpio_open(const struct gpio *gpio) int res; unsigned char init_value = 0; - if(NULL == gpio) { - syslog(LOG_ERR, "No valid gpio object given.\n"); - return EXIT_FAILURE; - } - + assert(NULL != gpio); if(!gpio->ftdi_dev->is_open) { res = ftdi_open(gpio); if(res != EXIT_SUCCESS) { @@ -76,9 +73,7 @@ int gpio_open(const struct gpio *gpio) // cppcheck-suppress unusedFunction int gpio_close(const struct gpio *gpio) { - if(NULL == gpio) { - return EXIT_FAILURE; - } + assert(NULL != gpio); gpio_write(gpio, 0); @@ -95,9 +90,8 @@ int gpio_close(const struct gpio *gpio) // cppcheck-suppress unusedFunction int gpio_read(const struct gpio *gpio, unsigned int *value) { - if(NULL == gpio || NULL == value) { - return EXIT_FAILURE; - } + assert(NULL != gpio); + assert(NULL != value); if(gpio->ftdi_dev->status_mask & (unsigned char)(gpio->pin)) { *value = 1; @@ -113,9 +107,7 @@ int gpio_write(const struct gpio *gpio, unsigned int value) int res; unsigned char mask; - if(NULL == gpio) { - return EXIT_FAILURE; - } + assert(NULL != gpio); mask = gpio->ftdi_dev->status_mask; @@ -143,9 +135,7 @@ int gpio_toggle(const struct gpio *gpio) int res; unsigned char mask; - if(NULL == gpio) { - return EXIT_FAILURE; - } + assert(NULL != gpio); mask = (gpio->ftdi_dev->status_mask) ^ (unsigned char)(gpio->pin); diff --git a/test/unit/gpio_close.c b/test/unit/gpio_close.c index 457c864..595469e 100644 --- a/test/unit/gpio_close.c +++ b/test/unit/gpio_close.c @@ -9,10 +9,6 @@ #include #include -UTEST(gpio_close, invalid_gpio_obj) { - ASSERT_EQ(gpio_close(NULL), EXIT_FAILURE); -} - UTEST(gpio_close, empty_bitmask) { struct gpio gpio_1; struct ftdi_dev ftdi_obj; diff --git a/test/unit/gpio_open.c b/test/unit/gpio_open.c index c4297d3..3060761 100644 --- a/test/unit/gpio_open.c +++ b/test/unit/gpio_open.c @@ -89,10 +89,6 @@ UTEST(gpio_open, ftdi_open_success) { ASSERT_EQ(gpio_open(&gpio_1), EXIT_SUCCESS); } -UTEST(gpio_open, invalid_gpio_obj) { - ASSERT_EQ(gpio_open(NULL), EXIT_FAILURE); -} - UTEST(gpio_open, ftdi_set_bitmode_failed) { struct gpio gpio_1; struct ftdi_dev ftdi_obj; diff --git a/test/unit/gpio_read.c b/test/unit/gpio_read.c index a110f22..b8b5688 100644 --- a/test/unit/gpio_read.c +++ b/test/unit/gpio_read.c @@ -11,11 +11,6 @@ #include -UTEST(gpio_read, invalid_params) { - - ASSERT_EQ(gpio_read(NULL, NULL), EXIT_FAILURE); -} - UTEST(gpio_read, value_0) { unsigned int result; struct gpio gpio_1; diff --git a/test/unit/gpio_toggle.c b/test/unit/gpio_toggle.c index afd91aa..35531cc 100644 --- a/test/unit/gpio_toggle.c +++ b/test/unit/gpio_toggle.c @@ -11,11 +11,6 @@ #include -UTEST(gpio_toggle, invalid_params) { - - ASSERT_EQ(gpio_toggle(NULL), EXIT_FAILURE); -} - UTEST(gpio_toggle, ftdi_write_data_fails) { struct gpio gpio_1; struct ftdi_dev ftdi_obj; diff --git a/test/unit/gpio_write.c b/test/unit/gpio_write.c index ee0abac..95c80c3 100644 --- a/test/unit/gpio_write.c +++ b/test/unit/gpio_write.c @@ -9,11 +9,6 @@ #include #include -UTEST(gpio_write, invalid_params) { - - ASSERT_EQ(gpio_write(NULL, 0), EXIT_FAILURE); -} - UTEST(gpio_write, value_0) { unsigned int result; struct gpio gpio_1;