Skip to content

Commit

Permalink
test: Apply semantic patch file ztest_strcmp.cocci
Browse files Browse the repository at this point in the history
This patch file updates the use of assertion macros
comparing strings.

Command line used:
```
./scripts/coccicheck --mode=patch \
--cocci=scripts/coccinelle/ztest_strcmp.cocci tests/
```

Signed-off-by: Rubin Gerritsen <[email protected]>
  • Loading branch information
rugeGerritsen authored and aescolar committed Jun 14, 2024
1 parent 470a0fc commit 8799ab1
Show file tree
Hide file tree
Showing 33 changed files with 227 additions and 268 deletions.
2 changes: 1 addition & 1 deletion tests/drivers/sensor/generic/src/dummy_sensor.c
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ static int dummy_sensor_init(const struct device *dev)
const struct device *i2c = device_get_binding(config->i2c_name);

/* Bus and address should be configured. */
zassert_equal(strcmp(config->i2c_name, "dummy I2C"), 0);
zassert_str_equal(config->i2c_name, "dummy I2C");
zassert_equal(config->i2c_address, 123);

if (i2c != NULL) {
Expand Down
4 changes: 2 additions & 2 deletions tests/kernel/common/src/printk.c
Original file line number Diff line number Diff line change
Expand Up @@ -220,7 +220,7 @@ ZTEST(printk, test_printk)
pk_console[pos] = '\0';
__printk_hook_install(_old_char_out);
printk("expected '%s'\n", expected);
zassert_true((strcmp(pk_console, expected) == 0), "printk failed");
zassert_str_equal(pk_console, expected, "printk failed");

(void)memset(pk_console, 0, sizeof(pk_console));
count = 0;
Expand Down Expand Up @@ -251,7 +251,7 @@ ZTEST(printk, test_printk)
count += snprintk(pk_console + count, sizeof(pk_console) - count,
"0x%x %p %-2p\n", hex, ptr, (char *)42);
pk_console[count] = '\0';
zassert_true((strcmp(pk_console, expected) == 0), "snprintk failed");
zassert_str_equal(pk_console, expected, "snprintk failed");
}

extern void *common_setup(void);
Expand Down
22 changes: 11 additions & 11 deletions tests/kernel/threads/thread_apis/src/test_kthread_for_each.c
Original file line number Diff line number Diff line change
Expand Up @@ -228,44 +228,44 @@ ZTEST(threads_lifecycle_1cpu, test_k_thread_state_str)

tid->base.thread_state = 0;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "") == 0);
zassert_str_equal(str, "");

tid->base.thread_state = _THREAD_DUMMY;

str = k_thread_state_str(tid, NULL, sizeof(state_str));
zassert_true(strcmp(str, "") == 0);
zassert_str_equal(str, "");

str = k_thread_state_str(tid, state_str, 0);
zassert_true(strcmp(str, "") == 0);
zassert_str_equal(str, "");

str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "dummy") == 0);
zassert_str_equal(str, "dummy");

tid->base.thread_state = _THREAD_PENDING;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "pending") == 0);
zassert_str_equal(str, "pending");

tid->base.thread_state = _THREAD_PRESTART;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "prestart") == 0);
zassert_str_equal(str, "prestart");

tid->base.thread_state = _THREAD_DEAD;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "dead") == 0);
zassert_str_equal(str, "dead");

tid->base.thread_state = _THREAD_SUSPENDED;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "suspended") == 0);
zassert_str_equal(str, "suspended");

tid->base.thread_state = _THREAD_ABORTING;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "aborting") == 0);
zassert_str_equal(str, "aborting");

tid->base.thread_state = _THREAD_QUEUED;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "queued") == 0);
zassert_str_equal(str, "queued");

tid->base.thread_state = _THREAD_PENDING | _THREAD_SUSPENDED;
str = k_thread_state_str(tid, state_str, sizeof(state_str));
zassert_true(strcmp(str, "pending+suspended") == 0);
zassert_str_equal(str, "pending+suspended");
}
Original file line number Diff line number Diff line change
Expand Up @@ -165,19 +165,19 @@ ZTEST(threads_lifecycle, test_resume_unsuspend_thread)

/* Resume an unsuspend thread will not change the thread state. */
str = k_thread_state_str(tid, buffer, sizeof(buffer));
zassert_true(strcmp(str, "queued") == 0);
zassert_str_equal(str, "queued");
k_thread_resume(tid);
str = k_thread_state_str(tid, buffer, sizeof(buffer));
zassert_true(strcmp(str, "queued") == 0);
zassert_str_equal(str, "queued");

/* suspend created thread */
k_thread_suspend(tid);
str = k_thread_state_str(tid, buffer, sizeof(buffer));
zassert_true(strcmp(str, "suspended") == 0);
zassert_str_equal(str, "suspended");

/* Resume an suspend thread will make it to be next eligible.*/
k_thread_resume(tid);
str = k_thread_state_str(tid, buffer, sizeof(buffer));
zassert_true(strcmp(str, "queued") == 0);
zassert_str_equal(str, "queued");
k_thread_abort(tid);
}
4 changes: 2 additions & 2 deletions tests/kernel/workq/work/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -237,7 +237,7 @@ static void test_queue_start(void)

zassert_true(tn != cfg.name);
zassert_true(tn != NULL);
zassert_equal(strcmp(tn, cfg.name), 0);
zassert_str_equal(tn, cfg.name);
}

cfg.name = NULL;
Expand All @@ -251,7 +251,7 @@ static void test_queue_start(void)

zassert_true(tn != cfg.name);
zassert_true(tn != NULL);
zassert_equal(strcmp(tn, ""), 0);
zassert_str_equal(tn, "");
}

cfg.name = "wq.coophi";
Expand Down
51 changes: 24 additions & 27 deletions tests/lib/c_lib/common/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -230,7 +230,7 @@ ZTEST(libc_common, test_strcmp)
char test = 0;

zassert_true((strcmp(buffer, "fffff") < 0), "strcmp less ...");
zassert_true((strcmp(buffer, "eeeee") == 0), "strcmp equal ...");
zassert_str_equal(buffer, "eeeee", "strcmp equal ...");
zassert_true((strcmp(buffer, "ddddd") > 0), "strcmp greater ...");

zassert_true((strncasecmp(buffer, "FFFFF", 3) < 0), "strncasecmp less ...");
Expand Down Expand Up @@ -277,7 +277,7 @@ ZTEST(libc_common, test_strcpy)
(void)memset(buffer, '\0', BUFSIZE);
strcpy(buffer, "10 chars!\0");

zassert_true((strcmp(buffer, "10 chars!\0") == 0), "strcpy");
zassert_str_equal(buffer, "10 chars!\0", "strcpy");
}

/**
Expand Down Expand Up @@ -467,7 +467,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_alnum), 0, "isalnum error");
zassert_str_equal(buf, exp_alnum, "isalnum error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -476,7 +476,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_alpha), 0, "isalpha error");
zassert_str_equal(buf, exp_alpha, "isalpha error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -485,7 +485,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_digit), 0, "isdigit error");
zassert_str_equal(buf, exp_digit, "isdigit error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -494,7 +494,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_graph), 0, "isgraph error");
zassert_str_equal(buf, exp_graph, "isgraph error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -503,7 +503,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_print), 0, "isprint error");
zassert_str_equal(buf, exp_print, "isprint error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -512,7 +512,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_upper), 0, "isupper error");
zassert_str_equal(buf, exp_upper, "isupper error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -521,7 +521,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_space), 0, "isspace error");
zassert_str_equal(buf, exp_space, "isspace error");

ptr = buf;
for (int i = 0; i < 128; i++) {
Expand All @@ -530,7 +530,7 @@ ZTEST(libc_common, test_checktype)
}
}
*ptr = '\0';
zassert_equal(strcmp(buf, exp_xdigit), 0, "isxdigit error");
zassert_str_equal(buf, exp_xdigit, "isxdigit error");
}

/**
Expand Down Expand Up @@ -642,7 +642,7 @@ ZTEST(libc_common, test_str_operate)
char *ptr;

zassert_not_null(strcat(str1, str3), "strcat false");
zassert_equal(strcmp(str1, "aabbccd"), 0, "test strcat failed");
zassert_str_equal(str1, "aabbccd", "test strcat failed");

ret = strcspn(str1, str2);
zassert_equal(ret, 2, "strcspn failed");
Expand All @@ -659,12 +659,12 @@ ZTEST(libc_common, test_str_operate)
#if defined(__GNUC__) && __GNUC__ >= 7
#pragma GCC diagnostic pop
#endif
zassert_equal(strcmp(ncat, "ddeeaa"), 0, "strncat failed");
zassert_str_equal(ncat, "ddeeaa", "strncat failed");

zassert_is_null(strrchr(ncat, 'z'),
"strrchr not found this word. failed");
ptr = strrchr(ncat, 'e');
zassert_equal(strcmp(ptr, "eaa"), 0, "strrchr failed");
zassert_str_equal(ptr, "eaa", "strrchr failed");

zassert_is_null(strstr(str1, "ayz"), "strstr aabbccd with ayz failed");
zassert_not_null(strstr(str1, str2), "strstr aabbccd with b succeed");
Expand Down Expand Up @@ -728,13 +728,11 @@ ZTEST(libc_common, test_strtol)

ret = strtol(str_normal, &stop, 10);
zassert_equal(ret, -1011, "strtol base = 10 failed");
zassert_true((strcmp(stop, " This stopped it") == 0),
"strtol get stop failed");
zassert_str_equal(stop, " This stopped it", "strtol get stop failed");

ret = strtol(str_abnormal, &stop, 0);
zassert_equal(ret, 0, "strtol base = 0 failed");
zassert_true((strcmp(stop, "ABCDEFGH") == 0),
"strtol get stop failed");
zassert_str_equal(stop, "ABCDEFGH", "strtol get stop failed");

#if LONG_MAX > 2147483647
char border1[] = "-9223372036854775809";
Expand Down Expand Up @@ -817,13 +815,11 @@ ZTEST(libc_common, test_strtoul)

ret = strtoul(str_normal, &stop, 10);
zassert_equal(ret, -1011, "strtol base = 10 failed");
zassert_true((strcmp(stop, " This stopped it") == 0),
"strtol get stop failed");
zassert_str_equal(stop, " This stopped it", "strtol get stop failed");

ret = strtoul(str_abnormal, &stop, 0);
zassert_equal(ret, 0, "strtol base = 0 failed");
zassert_true((strcmp(stop, "ABCDEFGH") == 0),
"strtol get stop failed");
zassert_str_equal(stop, "ABCDEFGH", "strtol get stop failed");

#if LONG_MAX > 2147483647
char border1[] = "18446744073709551615";
Expand Down Expand Up @@ -901,11 +897,11 @@ void test_strtoll(void)

ret = strtoll(str_normal, &stop, 10);
zassert_equal(ret, -1011, "strtoll base = 10 failed");
zassert_true((strcmp(stop, " This stopped it") == 0), "strtoll get stop failed");
zassert_str_equal(stop, " This stopped it", "strtoll get stop failed");

ret = strtoll(str_abnormal, &stop, 0);
zassert_equal(ret, 0, "strtoll base = 0 failed");
zassert_true((strcmp(stop, "ABCDEFGH") == 0), "strtoll get stop failed");
zassert_str_equal(stop, "ABCDEFGH", "strtoll get stop failed");

char border1[] = "-9223372036854775808";
char border2[] = "+9223372036854775807";
Expand Down Expand Up @@ -981,11 +977,12 @@ void test_strtoull(void)

ret = strtoull(str_normal, &stop, 10);
zassert_equal(ret, -1011, "strtoull base = 10 failed");
zassert_true((strcmp(stop, " This stopped it") == 0), "strtoull get stop failed");
zassert_str_equal(stop, " This stopped it",
"strtoull get stop failed");

ret = strtoull(str_abnormal, &stop, 0);
zassert_equal(ret, 0, "strtoull base = 0 failed");
zassert_true((strcmp(stop, "ABCDEFGH") == 0), "strtoull get stop failed");
zassert_str_equal(stop, "ABCDEFGH", "strtoull get stop failed");

char border1[] = "+18446744073709551615";
char border2[] = "-18446744073709551615000";
Expand Down Expand Up @@ -1028,8 +1025,8 @@ ZTEST(libc_common, test_tolower_toupper)
}
lw[i] = up[i] = '\0';

zassert_equal(strcmp(up, toup), 0, "toupper error");
zassert_equal(strcmp(lw, tolw), 0, "tolower error");
zassert_str_equal(up, toup, "toupper error");
zassert_str_equal(lw, tolw, "tolower error");
}

void test_strtok_r_do(char *str, char *sep, int tlen,
Expand Down
4 changes: 2 additions & 2 deletions tests/lib/cbprintf_package/src/main.c
Original file line number Diff line number Diff line change
Expand Up @@ -289,11 +289,11 @@ ZTEST(cbprintf_package, test_cbprintf_fsc_package)
/* Get pointer to the first string in the package. */
addr = (char *)&fsc_package[desc->desc.len * sizeof(int) + 1];

zassert_equal(strcmp(test_str, addr), 0);
zassert_str_equal(test_str, addr);

/* Get address of the second string. */
addr += strlen(addr) + 2;
zassert_equal(strcmp(test_str1, addr), 0);
zassert_str_equal(test_str1, addr);
}

static void check_package(void *package, size_t len, const char *exp_str)
Expand Down
Loading

0 comments on commit 8799ab1

Please sign in to comment.