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

Re: [GSoC][PATCH v2] t: port helper/test-hashmap.c to unit-tests/t-hashmap.c

From
Ghanshyam Thakkar <shyamthakkar001@gmail.com>
Date
Jul 10, 2024, 19:41 UTC
Message-ID
<D2M45OWGEGU8.2TWYW1FQ2X3I@gmail.com>
In-Reply-To
<xmqqcynmdyy2.fsf@gitster.g>
Junio C Hamano <gitster@pobox.com> wrote:
Show 33 quoted lines
> Josh Steadmon <steadmon@google.com> writes:
>
> >> +static void t_put(struct hashmap *map, int ignore_case)
> >> +{
> >> +	struct test_entry *entry;
> >> +	const char *key_val[][2] = { { "key1", "value1" },
> >> +				     { "key2", "value2" },
> >> +				     { "fooBarFrotz", "value3" } };
> >> +
> >> +	for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
> >> +		entry = alloc_test_entry(ignore_case, key_val[i][0], key_val[i][1]);
> >> +		check(hashmap_put_entry(map, entry, ent) == NULL);
> >> +	}
> >> +
> >> +	entry = alloc_test_entry(ignore_case, "foobarfrotz", "value4");
> >> +	entry = hashmap_put_entry(map, entry, ent);
> >> +	check(ignore_case ? entry != NULL : entry == NULL);
> >> +	free(entry);
> >> +
> >> +	check_int(map->tablesize, ==, 64);
> >> +	check_int(hashmap_get_size(map), ==,
> >> +		  ignore_case ? ARRAY_SIZE(key_val) : ARRAY_SIZE(key_val) + 1);
> >> +}
> >
> > Ahhh, so you're using the same function for both case-sensitive and
> > -insensitive tests. So I guess TEST_RUN isn't useful here after all.
> > Personally I'd still rather get rid of setup(), but I don't feel super
> > strongly about it.
>
> Consulting the table with "fooBarFrotz" and checking what gets
> returned (expect "value3" for !icase, or "value4" for icase) is one
> of the things that are missing. In fact, the values stored are not
> even checked with the above test at all.

Yeah, I tried to replicate the 'put' tests from the shellscript. However, t_get() does the same thing as you mentioned below, so I'll just remove it and rename t_get() to t_put_get(), and add some missing checks from t_put() to t_put_get().

Show 26 quoted lines
>
> >> +static void t_replace(struct hashmap *map, int ignore_case)
> >> +{
> >> +	struct test_entry *entry;
> >> +
> >> +	entry = alloc_test_entry(ignore_case, "key1", "value1");
> >> +	check(hashmap_put_entry(map, entry, ent) == NULL);
> >> +
> >> +	entry = alloc_test_entry(ignore_case, ignore_case ? "Key1" : "key1",
> >> +				 "value2");
> >> +	entry = hashmap_put_entry(map, entry, ent);
> >> +	if (check(entry != NULL))
> >> +		check_str(get_value(entry), "value1");
> >> +	free(entry);
> >> +
> >> +	entry = alloc_test_entry(ignore_case, "fooBarFrotz", "value3");
> >> +	check(hashmap_put_entry(map, entry, ent) == NULL);
> >> +
> >> +	entry = alloc_test_entry(ignore_case,
> >> +				 ignore_case ? "foobarfrotz" : "fooBarFrotz",
> >> +				 "value4");
>
> Curious. If the hashmap is set up for icase use, do callers still
> need to downcase the key? Shouldn't the library take care of that?
> After all, test_entry_cmp() when the hashmap is being used in icase
> mode does strcasecmp() anyway.

Of course we don't need to downcase. But the idea is to insert "fooBarFrotz" then insert "foobarfrotz" to check if it is considered the same or not. In a way, it is also testing test_entry_cmp() and alloc_test_entry(). If we pass "fooBarFrotz" both the times we can't be sure if it downcases or not, as both are same, we'd pass even if it didn't downcase. After all, we are testing the library rather than using it.

Thanks.
Show 44 quoted lines
>
> >> +	entry = hashmap_put_entry(map, entry, ent);
> >> +	if (check(entry != NULL))
> >> +		check_str(get_value(entry), "value3");
>
> Here the stored value is checked, which is good.
>
> >> +	free(entry);
> >> +}
> >> +
> >> +static void t_get(struct hashmap *map, int ignore_case)
> >> +{
> >> +	struct test_entry *entry;
> >> +	const char *key_val[][2] = { { "key1", "value1" },
> >> +				     { "key2", "value2" },
> >> +				     { "fooBarFrotz", "value3" },
> >> +				     { ignore_case ? "key4" : "foobarfrotz", "value4" } };
> >> +	const char *query[][2] = {
> >> +		{ ignore_case ? "Key1" : "key1", "value1" },
> >> +		{ ignore_case ? "keY2" : "key2", "value2" },
> >> +		{ ignore_case ? "foobarfrotz" : "fooBarFrotz", "value3" }
> >> +	};
> >> +
> >> +	for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
> >> +		entry = alloc_test_entry(ignore_case, key_val[i][0], key_val[i][1]);
> >> +		check(hashmap_put_entry(map, entry, ent) == NULL);
> >> +	}
> >> +
> >> +	for (size_t i = 0; i < ARRAY_SIZE(query); i++) {
> >> +		entry = get_test_entry(map, ignore_case, query[i][0]);
> >> +		if (check(entry != NULL))
> >> +			check_str(get_value(entry), query[i][1]);
> >> +	}
> >> +
> >> +	check(get_test_entry(map, ignore_case, "notInMap") == NULL);
> >> +}
>
> It is getting dubious if it is worth having t_put, when t_get does
> so similar things and more.
>
> Same comment applies wrt icase, and not limited to t_get() but all
> the remaining test functions (elided).
>
> Thanks.
Previous: Junio C HamanoNext: Ghanshyam Thakkar
Message 5 of 20 in “t: port helper/test-hashmap.c to unit-tests/t-hashmap.c”
  1. Ghanshyam ThakkarJun 28, 2024
  2. [GSoC][PATCH v2] t: port helper/test-hashmap.c to unit-tests/t-hashmap.cGhanshyam Thakkar, Jul 8, 2024
  3. Josh SteadmonJul 9, 2024
  4. Junio C HamanoJul 9, 2024
  5. Ghanshyam ThakkarJul 10, 2024
  6. Ghanshyam ThakkarJul 9, 2024
  7. Phillip WoodJul 10, 2024
  8. Ghanshyam ThakkarJul 11, 2024
  9. [GSoC][PATCH v3] t: port helper/test-hashmap.c to unit-tests/t-hashmap.cGhanshyam Thakkar, Jul 11, 2024
  10. Ghanshyam ThakkarJul 12, 2024
  11. Junio C HamanoJul 12, 2024
  12. Christian CouderJul 25, 2024
  13. t: port helper/test-hashmap.c to unit-tests/t-hashmap.cA U Thor, Jul 30, 2024
  14. Christian CouderJul 31, 2024
  15. Junio C HamanoJul 31, 2024
  16. Ghanshyam ThakkarAug 2, 2024
  17. [GSoC][PATCH v5] t: port helper/test-hashmap.c to unit-tests/t-hashmap.cGhanshyam Thakkar, Aug 3, 2024
  18. Christian CouderAug 7, 2024
  19. Ghanshyam ThakkarAug 7, 2024
  20. Junio C HamanoAug 7, 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.