git/list[1] front-page[2] threads[3] people[4] search[5] about
 

Re: [PATCH 6/6] t-strbuf: use TEST_RUN

From
Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Date
Jul 15, 2024, 14:46 UTC
Message-ID
<D2Q70KGM98S7.3FMTY19CAFTEM@gmail.com>
In-Reply-To
<35f828d3-6d3d-4b86-b6bb-a35753d91b9b@gmail.com>
Phillip Wood <phillip.wood123@gmail.com> wrote:
Show 15 quoted lines
> On 02/07/2024 16:14, phillip.wood123@gmail.com wrote:
> > Getting rid of the untyped test arguments is 
> > definitely a benefit of this approach.
>
> That got me thinking how we might make type-safe setup()
> functions. The diff below shows how we could define a macro to
> generate the functions. DEFINE_SETUP_FN(char, ch) defines setup_ch()
> that takes a test function and a char that is passed to the test with
> the initialized strbuf. I'm not sure that's the way we want to go for
> this test file but I thought I'd post it in case it is useful for
> future tests.
>
> Best Wishes
>
> Phillip

This seems interesting if we want keep using setup() architecture. But a bit too much if we have to keep doing this in every test where we need setup(). Maybe some of it can be abstracted away in test-lib? Anyway, I was a bit surprised that we didn't just use 'const char *' instead of 'const void *', as we pass a string to all of them. So, I don't see the value of using 'void *' in the first place in this test.

Thanks.
Show 133 quoted lines
> ---- >8 ----
> diff --git a/t/unit-tests/t-strbuf.c b/t/unit-tests/t-strbuf.c
> index 6027dafef70..8fc9a8b38df 100644
> --- a/t/unit-tests/t-strbuf.c
> +++ b/t/unit-tests/t-strbuf.c
> @@ -1,27 +1,60 @@
> #include "test-lib.h"
> #include "strbuf.h"
>   
> -/* wrapper that supplies tests with an empty, initialized strbuf */
> -static void setup(void (*f)(struct strbuf*, const void*),
> - const void *data)
> -{
> - struct strbuf buf = STRBUF_INIT;
> -
> - f(&buf, data);
> - strbuf_release(&buf);
> - check_uint(buf.len, ==, 0);
> - check_uint(buf.alloc, ==, 0);
> -}
> -
> -/* wrapper that supplies tests with a populated, initialized strbuf */
> -static void setup_populated(void (*f)(struct strbuf*, const void*),
> - const char *init_str, const void *data)
> -{
> - struct strbuf buf = STRBUF_INIT;
> -
> - strbuf_addstr(&buf, init_str);
> - check_uint(buf.len, ==, strlen(init_str));
> - f(&buf, data);
> +/*
> + * Define a type safe wrapper function that supplies test functions
> + * with an initialized strbuf populated with an optional string and
> + * some data and then frees the strbuf when the test function
> + * returns. For example given the test function
> + *
> + * t_foo(struct strbuf *buf, struct foo *data)
> + *
> + * the type safe wrapper function
> + *
> + * setup_foo(void(*)(struct strbuf*, const struct foo*),
> + * const char *init_str, const struct foo*)
> + *
> + * can be defined with
> + *
> + * DEFINE_SETUP_FN(const struct foo*, foo)
> + *
> + * and used to run t_foo() with
> + *
> + * TEST(setup_foo(t_foo, "initial string", &my_foo), "test foo");
> + */
> +#define DEFINE_SETUP_FN(type, suffix) \
> + static void marshal_##suffix(void(*test_fn)(void), \
> + struct strbuf *buf, const void *ctx) \
> + { \
> + type data = *(type *)ctx; \
> + ((void(*)(struct strbuf*, type)) test_fn)(buf, data); \
> + } \
> + \
> + static void setup_##suffix(void(*test_fn)(struct strbuf*, type), \
> + const char *init_str, type data) \
> + { \
> + void *ctx = &data; \
> + do_setup(init_str, (void(*)(void)) test_fn, ctx, \
> + marshal_##suffix); \
> + }
> +
> +/*
> + * Helper function for DEFINE_SETUP_FN() that initializes the strbuf,
> + * calls the test function and releases the strbuf
> + */
> +static void do_setup(const char* init_str, void(*f)(void), const void
> *ctx,
> + void(*marshal)(void(*)(void), struct strbuf*, const void*))
> +{
> + struct strbuf buf = STRBUF_INIT;
> +
> + if (init_str) {
> + strbuf_addstr(&buf, init_str);
> + if (!check_uint(buf.len, ==, strlen(init_str))) {
> + strbuf_release(&buf);
> + return;
> + }
> + }
> + marshal(f, &buf, ctx);
> strbuf_release(&buf);
> check_uint(buf.len, ==, 0);
> check_uint(buf.alloc, ==, 0);
> @@ -66,10 +99,9 @@ static void t_dynamic_init(void)
> strbuf_release(&buf);
> }
>   
> -static void t_addch(struct strbuf *buf, const void *data)
> +DEFINE_SETUP_FN(char, ch)
> +static void t_addch(struct strbuf *buf, char ch)
> {
> - const char *p_ch = data;
> - const char ch = *p_ch;
> size_t orig_alloc = buf->alloc;
> size_t orig_len = buf->len;
>   
> @@ -85,9 +117,9 @@ static void t_addch(struct strbuf *buf, const void
> *data)
> check_char(buf->buf[buf->len], ==, '\0');
> }
>   
> -static void t_addstr(struct strbuf *buf, const void *data)
> +DEFINE_SETUP_FN(const char*, str)
> +static void t_addstr(struct strbuf *buf, const char *text)
> {
> - const char *text = data;
> size_t len = strlen(text);
> size_t orig_alloc = buf->alloc;
> size_t orig_len = buf->len;
> @@ -110,12 +142,12 @@ int cmd_main(int argc, const char **argv)
> if (!TEST(t_static_init(), "static initialization works"))
> test_skip_all("STRBUF_INIT is broken");
> TEST(t_dynamic_init(), "dynamic initialization works");
> - TEST(setup(t_addch, "a"), "strbuf_addch adds char");
> - TEST(setup(t_addch, ""), "strbuf_addch adds NUL char");
> - TEST(setup_populated(t_addch, "initial value", "a"),
> + TEST(setup_ch(t_addch, NULL, 'a'), "strbuf_addch adds char");
> + TEST(setup_ch(t_addch, NULL, '\0'), "strbuf_addch adds NUL char");
> + TEST(setup_ch(t_addch, "initial value", 'a'),
> "strbuf_addch appends to initial value");
> - TEST(setup(t_addstr, "hello there"), "strbuf_addstr adds string");
> - TEST(setup_populated(t_addstr, "initial value", "hello there"),
> + TEST(setup_str(t_addstr, NULL, "hello there"), "strbuf_addstr adds
> string");
> + TEST(setup_str(t_addstr, "initial value", "hello there"),
> "strbuf_addstr appends string to initial value");
>   
> return test_done();
Previous: René ScharfeNext: Ghanshyam Thakkar
Message 41 of 115 in “unit-tests: add and use TEST_RUN to simplify tests”
  1. 0/6 unit-tests: add and use TEST_RUN to simplify testsRené Scharfe, Jun 29, 2024
  2. 1/6 t0080: move expected output to a fileRené Scharfe, Jun 29, 2024
  3. Jeff KingJul 1, 2024
  4. Junio C HamanoJul 1, 2024
  5. Jeff KingJul 1, 2024
  6. Junio C HamanoJul 1, 2024
  7. Jeff KingJul 2, 2024
  8. René ScharfeJul 1, 2024
  9. Jeff KingJul 1, 2024
  10. 2/6 unit-tests: add TEST_RUNRené Scharfe, Jun 29, 2024
  11. phillip.wood123@gmail.comJul 2, 2024
  12. Junio C HamanoJul 2, 2024
  13. René ScharfeJul 2, 2024
  14. René ScharfeJul 2, 2024
  15. phillip.wood123@gmail.comJul 5, 2024
  16. René ScharfeJul 5, 2024
  17. René ScharfeJul 7, 2024
  18. phillip.wood123@gmail.comJul 8, 2024
  19. Junio C HamanoJul 8, 2024
  20. Junio C HamanoJul 11, 2024
  21. Phillip WoodJul 13, 2024
  22. Junio C HamanoJul 13, 2024
  23. phillip.wood123@gmail.comJul 8, 2024
  24. 3/6 t-ctype: use TEST_RUNRené Scharfe, Jun 29, 2024
  25. Josh SteadmonJul 1, 2024
  26. René ScharfeJul 1, 2024
  27. phillip.wood123@gmail.comJul 2, 2024
  28. René ScharfeJul 2, 2024
  29. 4/6 t-reftable-basics: use TEST_RUNRené Scharfe, Jun 29, 2024
  30. 5/6 t-strvec: use TEST_RUNRené Scharfe, Jun 29, 2024
  31. phillip.wood123@gmail.comJul 2, 2024
  32. René ScharfeJul 2, 2024
  33. 6/6 t-strbuf: use TEST_RUNRené Scharfe, Jun 29, 2024
  34. Josh SteadmonJul 1, 2024
  35. René ScharfeJul 1, 2024
  36. phillip.wood123@gmail.comJul 2, 2024
  37. René ScharfeJul 2, 2024
  38. phillip.wood123@gmail.comJul 4, 2024
  39. Phillip WoodJul 10, 2024
  40. René ScharfeJul 14, 2024
  41. Ghanshyam ThakkarJul 15, 2024
  42. Ghanshyam ThakkarJul 2, 2024
  43. René ScharfeJul 2, 2024
  44. Ghanshyam ThakkarJul 3, 2024
  45. Josh SteadmonJul 8, 2024
  46. Ghanshyam ThakkarJul 8, 2024
  47. Josh SteadmonJul 1, 2024
  48. Junio C HamanoJul 10, 2024
  49. Phillip WoodJul 11, 2024
  50. Junio C HamanoJul 11, 2024
  51. René ScharfeJul 14, 2024
  52. 0/6 unit-tests: add and use for_test to simplify testsRené Scharfe, Jul 21, 2024
  53. 1/6 t0080: move expected output to a fileRené Scharfe, Jul 21, 2024
  54. Jeff KingJul 23, 2024
  55. 2/6 unit-tests: add for_testRené Scharfe, Jul 21, 2024
  56. Kyle LippincottJul 22, 2024
  57. Junio C HamanoJul 22, 2024
  58. René ScharfeJul 22, 2024
  59. Junio C HamanoJul 22, 2024
  60. Kyle LippincottJul 22, 2024
  61. René ScharfeJul 23, 2024
  62. unit-tests: show location of checks outside of testsRené Scharfe, Jul 23, 2024
  63. Phillip WoodJul 23, 2024
  64. Kyle LippincottJul 22, 2024
  65. René ScharfeJul 23, 2024
  66. Patrick SteinhardtJul 23, 2024
  67. René ScharfeJul 23, 2024
  68. Patrick SteinhardtJul 23, 2024
  69. René ScharfeJul 23, 2024
  70. Patrick SteinhardtJul 23, 2024
  71. Phillip WoodJul 23, 2024
  72. René ScharfeJul 23, 2024
  73. Phillip WoodJul 23, 2024
  74. Phillip WoodJul 25, 2024
  75. René ScharfeJul 30, 2024
  76. 3/6 t-ctype: use for_testRené Scharfe, Jul 21, 2024
  77. 4/6 t-reftable-basics: use for_testRené Scharfe, Jul 21, 2024
  78. 5/6 t-strvec: use for_testRené Scharfe, Jul 21, 2024
  79. 6/6 t-strbuf: use for_testRené Scharfe, Jul 21, 2024
  80. Phillip WoodJul 23, 2024
  81. 0/7 add and use for_test to simplify testsRené Scharfe, Jul 24, 2024
  82. 1/7 t0080: use here-doc test bodyRené Scharfe, Jul 24, 2024
  83. 2/7 unit-tests: show location of checks outside of testsRené Scharfe, Jul 24, 2024
  84. 3/7 unit-tests: add for_testRené Scharfe, Jul 24, 2024
  85. Kyle LippincottJul 24, 2024
  86. Phillip WoodJul 25, 2024
  87. Junio C HamanoJul 25, 2024
  88. Kyle LippincottJul 25, 2024
  89. Junio C HamanoJul 26, 2024
  90. Patrick SteinhardtJul 26, 2024
  91. Junio C HamanoJul 26, 2024
  92. Patrick SteinhardtJul 29, 2024
  93. Junio C HamanoJul 29, 2024
  94. Patrick SteinhardtJul 30, 2024
  95. René ScharfeJul 30, 2024
  96. Patrick SteinhardtJul 31, 2024
  97. René ScharfeJul 31, 2024
  98. Patrick SteinhardtAug 1, 2024
  99. 4/7 t-ctype: use for_testRené Scharfe, Jul 24, 2024
  100. 5/7 t-reftable-basics: use for_testRené Scharfe, Jul 24, 2024
  101. 6/7 t-strvec: use for_testRené Scharfe, Jul 24, 2024
  102. 7/7 t-strbuf: use for_testRené Scharfe, Jul 24, 2024
  103. 0/6 add and use if_test to simplify testsRené Scharfe, Jul 30, 2024
  104. 1/6 t0080: use here-doc test bodyRené Scharfe, Jul 30, 2024
  105. Kyle LippincottJul 31, 2024
  106. 2/6 unit-tests: show location of checks outside of testsRené Scharfe, Jul 30, 2024
  107. Kyle LippincottJul 31, 2024
  108. René ScharfeAug 1, 2024
  109. 3/6 unit-tests: add if_testRené Scharfe, Jul 30, 2024
  110. Kyle LippincottJul 31, 2024
  111. René ScharfeAug 1, 2024
  112. Kyle LippincottAug 2, 2024
  113. 4/6 t-ctype: use if_testRené Scharfe, Jul 30, 2024
  114. 5/6 t-reftable-basics: use if_testRené Scharfe, Jul 30, 2024
  115. 6/6 t-strvec: use if_testRené Scharfe, Jul 30, 2024

Read the whole thread, see it on lore, or plain text.

$ cat FOOTERMessages come from the public archive at lore.kernel.org/git, fetched every hour. The front page is chosen and written each morning by an AI editor and can be wrong; the threads themselves are the record. About and API. For agents: an MCP server at https://gitlist.dev/mcp, and any thread, story or person page as Markdown by adding .md to its URL (or sending Accept: text/markdown). Details in /llms.txt.