提交 486fd7f7 编写于 作者: M Michal Privoznik

internal: Simplify STREQ_NULLABLE

Our STREQ_NULLABLE and STRNEQ_NULLABLE macros are too
complicated. This was a result of some broken version of gcc.
However, that is long gone and therefore we can simplify the
macros.
Signed-off-by: NMichal Privoznik <mprivozn@redhat.com>
上级 6de3f116
......@@ -92,9 +92,9 @@
# define STRSKIP(a, b) (STRPREFIX(a, b) ? (a) + strlen(b) : NULL)
# define STREQ_NULLABLE(a, b) \
((a) ? (b) && STREQ((a) ? (a) : "", (b) ? (b) : "") : !(b))
((a) ? (b) && STREQ((a), (b)) : !(b))
# define STRNEQ_NULLABLE(a, b) \
((a) ? !(b) || STRNEQ((a) ? (a) : "", (b) ? (b) : "") : !!(b))
((a) ? !(b) || STRNEQ((a), (b)) : !!(b))
# define NUL_TERMINATE(buf) do { (buf)[sizeof(buf)-1] = '\0'; } while (0)
# define ARRAY_CARDINALITY(Array) (sizeof(Array) / sizeof(*(Array)))
......
......@@ -34,6 +34,53 @@
VIR_LOG_INIT("tests.stringtest");
struct testStreqData {
const char *a;
const char *b;
};
static int testStreq(const void *args)
{
const struct testStreqData *data = args;
int ret = -1;
bool equal = true;
bool streq_rv, strneq_rv;
size_t i;
if ((size_t) data->a ^ (size_t) data->b)
equal = false;
if (data->a && data->b) {
for (i = 0; data->a[i] != '\0'; i++) {
if (data->b[i] == '\0' ||
data->a[i] != data->b[i]) {
equal = false;
break;
}
}
}
streq_rv = STREQ_NULLABLE(data->a, data->b);
strneq_rv = STRNEQ_NULLABLE(data->a, data->b);
if (streq_rv != equal) {
virFilePrintf(stderr,
"STREQ not working correctly. Expected %d got %d",
(int) equal, (int) streq_rv);
goto cleanup;
}
if (strneq_rv == equal) {
virFilePrintf(stderr,
"STRNEQ not working correctly. Expected %d got %d",
(int) equal, (int) strneq_rv);
goto cleanup;
}
ret = 0;
cleanup:
return ret;
}
struct testSplitData {
const char *string;
const char *delim;
......@@ -651,6 +698,20 @@ mymain(void)
{
int ret = 0;
#define TEST_STREQ(aa, bb) \
do { \
struct testStreqData streqData = {.a = aa, .b = bb}; \
if (virTestRun("Streq", testStreq, &streqData) < 0) \
ret = -1; \
} while (0)
TEST_STREQ("hello", "world");
TEST_STREQ(NULL, NULL);
TEST_STREQ(NULL, "");
TEST_STREQ("", NULL);
TEST_STREQ("", "");
TEST_STREQ("hello", "hello");
#define TEST_SPLIT(str, del, max, toks) \
do { \
struct testSplitData splitData = { \
......
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册