t-strvec: use if_test
The macro TEST takes a single expression. If a test requires multiple statements then they need to be placed in a function that's called in the TEST expression. Remove the cognitive overhead of defining and calling single-use functions by using if_test instead. Signed-off-by: René Scharfe <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>
This commit is contained in:

committed by
Junio C Hamano

parent
2c4a6a8d9c
commit
9ddec6b79a
@ -36,16 +36,16 @@ static void check_strvec_loc(const char *loc, struct strvec *vec, ...)
|
|||||||
check_pointer_eq(vec->v[nr], NULL);
|
check_pointer_eq(vec->v[nr], NULL);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_static_init(void)
|
int cmd_main(int argc, const char **argv)
|
||||||
{
|
{
|
||||||
|
if_test ("static initialization") {
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
check_pointer_eq(vec.v, empty_strvec);
|
check_pointer_eq(vec.v, empty_strvec);
|
||||||
check_uint(vec.nr, ==, 0);
|
check_uint(vec.nr, ==, 0);
|
||||||
check_uint(vec.alloc, ==, 0);
|
check_uint(vec.alloc, ==, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_dynamic_init(void)
|
if_test ("dynamic initialization") {
|
||||||
{
|
|
||||||
struct strvec vec;
|
struct strvec vec;
|
||||||
strvec_init(&vec);
|
strvec_init(&vec);
|
||||||
check_pointer_eq(vec.v, empty_strvec);
|
check_pointer_eq(vec.v, empty_strvec);
|
||||||
@ -53,8 +53,7 @@ static void t_dynamic_init(void)
|
|||||||
check_uint(vec.alloc, ==, 0);
|
check_uint(vec.alloc, ==, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_clear(void)
|
if_test ("clear") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_push(&vec, "foo");
|
strvec_push(&vec, "foo");
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
@ -63,8 +62,7 @@ static void t_clear(void)
|
|||||||
check_uint(vec.alloc, ==, 0);
|
check_uint(vec.alloc, ==, 0);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_push(void)
|
if_test ("push") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
|
|
||||||
strvec_push(&vec, "foo");
|
strvec_push(&vec, "foo");
|
||||||
@ -76,24 +74,21 @@ static void t_push(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_pushf(void)
|
if_test ("pushf") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushf(&vec, "foo: %d", 1);
|
strvec_pushf(&vec, "foo: %d", 1);
|
||||||
check_strvec(&vec, "foo: 1", NULL);
|
check_strvec(&vec, "foo: 1", NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_pushl(void)
|
if_test ("pushl") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
check_strvec(&vec, "foo", "bar", "baz", NULL);
|
check_strvec(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_pushv(void)
|
if_test ("pushv") {
|
||||||
{
|
|
||||||
const char *strings[] = {
|
const char *strings[] = {
|
||||||
"foo", "bar", "baz", NULL,
|
"foo", "bar", "baz", NULL,
|
||||||
};
|
};
|
||||||
@ -105,8 +100,7 @@ static void t_pushv(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_replace_at_head(void)
|
if_test ("replace at head") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_replace(&vec, 0, "replaced");
|
strvec_replace(&vec, 0, "replaced");
|
||||||
@ -114,8 +108,7 @@ static void t_replace_at_head(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_replace_at_tail(void)
|
if_test ("replace at tail") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_replace(&vec, 2, "replaced");
|
strvec_replace(&vec, 2, "replaced");
|
||||||
@ -123,8 +116,7 @@ static void t_replace_at_tail(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_replace_in_between(void)
|
if_test ("replace in between") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_replace(&vec, 1, "replaced");
|
strvec_replace(&vec, 1, "replaced");
|
||||||
@ -132,8 +124,7 @@ static void t_replace_in_between(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_replace_with_substring(void)
|
if_test ("replace with substring") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", NULL);
|
strvec_pushl(&vec, "foo", NULL);
|
||||||
strvec_replace(&vec, 0, vec.v[0] + 1);
|
strvec_replace(&vec, 0, vec.v[0] + 1);
|
||||||
@ -141,8 +132,7 @@ static void t_replace_with_substring(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_remove_at_head(void)
|
if_test ("remove at head") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_remove(&vec, 0);
|
strvec_remove(&vec, 0);
|
||||||
@ -150,8 +140,7 @@ static void t_remove_at_head(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_remove_at_tail(void)
|
if_test ("remove at tail") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_remove(&vec, 2);
|
strvec_remove(&vec, 2);
|
||||||
@ -159,8 +148,7 @@ static void t_remove_at_tail(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_remove_in_between(void)
|
if_test ("remove in between") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_remove(&vec, 1);
|
strvec_remove(&vec, 1);
|
||||||
@ -168,16 +156,14 @@ static void t_remove_in_between(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_pop_empty_array(void)
|
if_test ("pop with empty array") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pop(&vec);
|
strvec_pop(&vec);
|
||||||
check_strvec(&vec, NULL);
|
check_strvec(&vec, NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_pop_non_empty_array(void)
|
if_test ("pop with non-empty array") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
strvec_pushl(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_pop(&vec);
|
strvec_pop(&vec);
|
||||||
@ -185,48 +171,42 @@ static void t_pop_non_empty_array(void)
|
|||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_split_empty_string(void)
|
if_test ("split empty string") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_split(&vec, "");
|
strvec_split(&vec, "");
|
||||||
check_strvec(&vec, NULL);
|
check_strvec(&vec, NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_split_single_item(void)
|
if_test ("split single item") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_split(&vec, "foo");
|
strvec_split(&vec, "foo");
|
||||||
check_strvec(&vec, "foo", NULL);
|
check_strvec(&vec, "foo", NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_split_multiple_items(void)
|
if_test ("split multiple items") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_split(&vec, "foo bar baz");
|
strvec_split(&vec, "foo bar baz");
|
||||||
check_strvec(&vec, "foo", "bar", "baz", NULL);
|
check_strvec(&vec, "foo", "bar", "baz", NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_split_whitespace_only(void)
|
if_test ("split whitespace only") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_split(&vec, " \t\n");
|
strvec_split(&vec, " \t\n");
|
||||||
check_strvec(&vec, NULL);
|
check_strvec(&vec, NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_split_multiple_consecutive_whitespaces(void)
|
if_test ("split multiple consecutive whitespaces") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
strvec_split(&vec, "foo\n\t bar");
|
strvec_split(&vec, "foo\n\t bar");
|
||||||
check_strvec(&vec, "foo", "bar", NULL);
|
check_strvec(&vec, "foo", "bar", NULL);
|
||||||
strvec_clear(&vec);
|
strvec_clear(&vec);
|
||||||
}
|
}
|
||||||
|
|
||||||
static void t_detach(void)
|
if_test ("detach") {
|
||||||
{
|
|
||||||
struct strvec vec = STRVEC_INIT;
|
struct strvec vec = STRVEC_INIT;
|
||||||
const char **detached;
|
const char **detached;
|
||||||
|
|
||||||
@ -244,29 +224,5 @@ static void t_detach(void)
|
|||||||
free(detached);
|
free(detached);
|
||||||
}
|
}
|
||||||
|
|
||||||
int cmd_main(int argc, const char **argv)
|
|
||||||
{
|
|
||||||
TEST(t_static_init(), "static initialization");
|
|
||||||
TEST(t_dynamic_init(), "dynamic initialization");
|
|
||||||
TEST(t_clear(), "clear");
|
|
||||||
TEST(t_push(), "push");
|
|
||||||
TEST(t_pushf(), "pushf");
|
|
||||||
TEST(t_pushl(), "pushl");
|
|
||||||
TEST(t_pushv(), "pushv");
|
|
||||||
TEST(t_replace_at_head(), "replace at head");
|
|
||||||
TEST(t_replace_in_between(), "replace in between");
|
|
||||||
TEST(t_replace_at_tail(), "replace at tail");
|
|
||||||
TEST(t_replace_with_substring(), "replace with substring");
|
|
||||||
TEST(t_remove_at_head(), "remove at head");
|
|
||||||
TEST(t_remove_in_between(), "remove in between");
|
|
||||||
TEST(t_remove_at_tail(), "remove at tail");
|
|
||||||
TEST(t_pop_empty_array(), "pop with empty array");
|
|
||||||
TEST(t_pop_non_empty_array(), "pop with non-empty array");
|
|
||||||
TEST(t_split_empty_string(), "split empty string");
|
|
||||||
TEST(t_split_single_item(), "split single item");
|
|
||||||
TEST(t_split_multiple_items(), "split multiple items");
|
|
||||||
TEST(t_split_whitespace_only(), "split whitespace only");
|
|
||||||
TEST(t_split_multiple_consecutive_whitespaces(), "split multiple consecutive whitespaces");
|
|
||||||
TEST(t_detach(), "detach");
|
|
||||||
return test_done();
|
return test_done();
|
||||||
}
|
}
|
||||||
|
Reference in New Issue
Block a user