From 8690b76568a8771921ed3ad183afa60c2de85603 Mon Sep 17 00:00:00 2001 From: Daniel Wagner Date: Thu, 8 Aug 2013 10:42:42 +0200 Subject: unit: Do not compare expression against NULL This patch generate via coccinelle with: @ disable is_null,isnt_null1 @ expression E; @@ ( - E == NULL + !E | - E != NULL + E ) --- unit/test-pacrunner.c | 100 +++++++++++++++++++++++++------------------------- 1 file changed, 50 insertions(+), 50 deletions(-) diff --git a/unit/test-pacrunner.c b/unit/test-pacrunner.c index 3279fb3..70e2dbe 100644 --- a/unit/test-pacrunner.c +++ b/unit/test-pacrunner.c @@ -72,7 +72,7 @@ static bool test_config; static void free_pacrunner_test_suite(struct pacrunner_test_suite *suite) { - if (suite == NULL) + if (!suite) return; g_free(suite->title); @@ -90,23 +90,23 @@ static gchar **_g_strappendv(gchar **str_array, const gchar *str) gchar **result; gchar *copy; - if (str == NULL) + if (!str) return NULL; - if (str_array != NULL) + if (str_array) length = g_strv_length(str_array); result = g_try_malloc0(sizeof(gchar *) * (length + 2)); - if (result == NULL) + if (!result) return NULL; copy = g_strdup(str); - if (copy == NULL) { + if (!copy) { g_free(result); return NULL; } - if (str_array != NULL) { + if (str_array) { g_memmove(result, str_array, length * sizeof(gchar *)); memset(str_array, 0, length * sizeof(gchar *)); } @@ -120,7 +120,7 @@ static void print_test_suite(struct pacrunner_test_suite *suite) { gchar **line; - if (suite == NULL) + if (!suite) return; printf("\nSuite: %s\n", suite->title); @@ -128,16 +128,16 @@ static void print_test_suite(struct pacrunner_test_suite *suite) printf("\nPAC:\n%s\n", suite->pac); printf("\nServers:\n"); - if (suite->servers != NULL) { - for (line = suite->servers; *line != NULL; line++) + if (suite->servers) { + for (line = suite->servers; *line; line++) printf("%s\n", *line); } else printf("(none)\n"); printf("\nExcludes:\n"); - if (suite->excludes != NULL) { - for (line = suite->excludes; *line != NULL; line++) + if (suite->excludes) { + for (line = suite->excludes; *line; line++) printf("%s\n", *line); } else printf("(none)\n"); @@ -146,10 +146,10 @@ static void print_test_suite(struct pacrunner_test_suite *suite) suite->config_result ? "Valid" : "Invalid"); printf("\nTests:\n"); - if (suite->tests != NULL) { + if (suite->tests) { short test = 0; - for (line = suite->tests; *line != NULL; line++) { + for (line = suite->tests; *line; line++) { if (test == 0) { printf("%s --> ", *line); test++; @@ -174,7 +174,7 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) gchar **line; suite = g_try_malloc0(sizeof(struct pacrunner_test_suite)); - if (suite == NULL) + if (!suite) goto error; if (!g_file_get_contents(path, &content, NULL, NULL)) @@ -184,10 +184,10 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) goto error; lines = g_strsplit(content, "\n", 0); - if (lines == NULL) + if (!lines) goto error; - for (line = lines; *line != NULL; line++) { + for (line = lines; *line; line++) { if (strlen(*line) == 0) continue; @@ -197,17 +197,17 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) if (*line[0] != '[') { switch (part) { case SUITE_TITLE: - if (suite->title != NULL) + if (suite->title) goto error; suite->title = g_strdup(*line); - if (suite->title == NULL) + if (!suite->title) goto error; break; case SUITE_PAC: - if (suite->pac == NULL) + if (!suite->pac) suite->pac = g_strdup_printf("%s\n", *line); else { @@ -218,13 +218,13 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) g_free(oldpac); } - if (suite->pac == NULL) + if (!suite->pac) goto error; break; case SUITE_SERVERS: array = _g_strappendv(suite->servers, *line); - if (array == NULL) + if (!array) goto error; g_free(suite->servers); @@ -233,7 +233,7 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) break; case SUITE_EXCLUDES: array = _g_strappendv(suite->excludes, *line); - if (array == NULL) + if (!array) goto error; g_free(suite->excludes); @@ -249,7 +249,7 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) break; case SUITE_TESTS: array = _g_strappendv(suite->tests, *line); - if (array == NULL) + if (!array) goto error; g_free(suite->tests); @@ -281,9 +281,9 @@ static struct pacrunner_test_suite *read_test_suite(const char *path) if (verbose) print_test_suite(suite); - if (suite->title == NULL || (suite->tests != NULL + if (!suite->title || (suite->tests && g_strv_length(suite->tests) % 2 != 0) - || (suite->servers == NULL && suite->pac == NULL)) + || (!suite->servers && !suite->pac)) goto error; g_free(content); @@ -303,7 +303,7 @@ error: static int test_suite_init(void) { proxy = pacrunner_proxy_create("eth0"); - if (proxy == NULL) + if (!proxy) return -1; return 0; @@ -352,11 +352,11 @@ static void test_proxy_requests(void) if (verbose) printf("\n"); - for (test = test_suite->tests; *test != NULL; test = test + 2) { + for (test = test_suite->tests; *test; test = test + 2) { gchar *test_result = *(test+1); test_strings = g_strsplit(*test, " ", 2); - if (test_strings == NULL || g_strv_length(test_strings) != 2) { + if (!test_strings || g_strv_length(test_strings) != 2) { g_strfreev(test_strings); continue; } @@ -368,7 +368,7 @@ static void test_proxy_requests(void) verify = false; if (strncmp(test_result, "DIRECT", 6) == 0) { - if (result == NULL || + if (!result || strncmp(result, "DIRECT", 6) == 0) verify = true; } else { @@ -385,13 +385,13 @@ static void test_proxy_requests(void) msg = g_strdup_printf( "\tTEST: %s -> %s FAILED (%s)", *test, test_result, - result == NULL ? "DIRECT" : result); + !result ? "DIRECT" : result); printf("%s\n", msg); g_free(msg); } - if (result != NULL) + if (result) g_free(result); CU_ASSERT_TRUE(verify); @@ -403,11 +403,11 @@ static void run_test_suite(const char *test_file_path, enum cu_test_mode mode) CU_pTestRegistry cu_registry; CU_pSuite cu_suite; - if (test_file_path == NULL) + if (!test_file_path) return; test_suite = read_test_suite(test_file_path); - if (test_suite == NULL) { + if (!test_suite) { if (verbose) printf("Invalid suite\n"); return; @@ -424,13 +424,13 @@ static void run_test_suite(const char *test_file_path, enum cu_test_mode mode) cu_suite = CU_add_suite(test_suite->title, test_suite_init, test_suite_cleanup); - if (test_suite->pac != NULL) + if (test_suite->pac) CU_add_test(cu_suite, "PAC config test", test_pac_config); else CU_add_test(cu_suite, "Manual config test", test_manual_config); - if (test_suite->config_result && test_suite->tests != NULL) + if (test_suite->config_result && test_suite->tests) CU_add_test(cu_suite, "Proxy requests test", test_proxy_requests); @@ -470,7 +470,7 @@ static void find_and_run_test_suite(GDir *test_dir, const gchar *test_file; gchar *test_file_path; - if (test_dir == NULL && test_path == NULL) + if (!test_dir && !test_path) return; if (CU_initialize_registry() != CUE_SUCCESS) { @@ -478,12 +478,12 @@ static void find_and_run_test_suite(GDir *test_dir, return; } - if (test_dir != NULL) { - for (test_file = g_dir_read_name(test_dir); test_file != NULL; + if (test_dir) { + for (test_file = g_dir_read_name(test_dir); test_file; test_file = g_dir_read_name(test_dir)) { test_file_path = g_strdup_printf("%s/%s", test_path, test_file); - if (test_file_path == NULL) + if (!test_file_path) return; run_test_suite(test_file_path, mode); @@ -491,7 +491,7 @@ static void find_and_run_test_suite(GDir *test_dir, g_free(test_file_path); } } else { - if (test_path != NULL) + if (test_path) test_file_path = g_strdup_printf("%s/%s", test_path, file_path); else @@ -509,11 +509,11 @@ static GDir *open_test_dir(gchar **test_path) { GDir *test_dir; - if (test_path == NULL || *test_path == NULL) + if (!test_path || !*test_path) return NULL; test_dir = g_dir_open(*test_path, 0, NULL); - if (test_dir == NULL && !g_path_is_absolute(*test_path)) { + if (!test_dir && !g_path_is_absolute(*test_path)) { gchar *current, *path; current = g_get_current_dir(); @@ -525,7 +525,7 @@ static GDir *open_test_dir(gchar **test_path) *test_path = path; test_dir = g_dir_open(*test_path, 0, NULL); - if (test_dir == NULL) + if (!test_dir) return NULL; } @@ -568,14 +568,14 @@ int main(int argc, char *argv[]) test_options, &opt_index)) != -1) { switch (c) { case 'd': - if (file_path != NULL) + if (file_path) goto error; test_path = g_strdup(optarg); break; case 'f': - if (test_path != NULL) + if (test_path) goto error; file_path = g_strdup(optarg); @@ -606,18 +606,18 @@ int main(int argc, char *argv[]) } } - if (test_path == NULL && file_path == NULL) + if (!test_path && !file_path) goto error; - if (test_path != NULL) { + if (test_path) { test_dir = open_test_dir(&test_path); - if (test_dir == NULL) { + if (!test_dir) { g_free(test_path); return EXIT_FAILURE; } } - if (file_path != NULL) + if (file_path) test_path = g_get_current_dir(); __pacrunner_proxy_init(); @@ -634,7 +634,7 @@ int main(int argc, char *argv[]) g_free(test_path); - if (test_dir != NULL) + if (test_dir) g_dir_close(test_dir); return EXIT_SUCCESS; -- cgit 1.2.3-korg