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

Re: [GSoC][PATCH] t: migrate helper/test-oidmap.c to unit-tests/t-oidmap.c

From
PWPhillip Wood <phillip.wood123@gmail.com>
Date
Jun 25, 2024, 10:14 UTC
Message-ID
<827f6cea-2367-403f-ba8b-055c9c8a7259@gmail.com>
In-Reply-To
<ZnP6G6SSBynlBNUj@google.com>
Hi Ghanshyam
On 20/06/2024 10:45, Jonathan Nieder wrote:
Show 6 quoted lines
> Ghanshyam Thakkar wrote:
> 
>> helper/test-oidmap.c along with t0016-oidmap.sh test the oidmap.h
>> library which is built on top of hashmap.h to store arbitrary
>> datastructure (which must contain oidmap_entry, which is a wrapper
>> around object_id).

I'm not really sure what the sentence is trying to say. I think it would be helpful to start the commit message with an introductory sentence explaining that the oidmap is currently tested via `test-tool` and this commit converts those tests to unit tests.

  These entries can be accessed by querying their
Show 5 quoted lines
>> associated object_id.
>>
>> Migrate them to the unit testing framework for better performance,
>> concise code and better debugging. Along with the migration also plug
>> memory leaks and make the test logic independent for all the tests.
>> The migration removes 'put' tests from t0016, because it is used as
>> setup to all the other tests, so testing it separately does not yield
>> any benefit.
Thanks sounds sensible, thanks for explaining it in the commit message.
Overall the patch looks pretty good, I've left a couple of comments below.
>> Mentored-by: Christian Couder <chriscool@tuxfamily.org>
>> Mentored-by: Kaartic Sivaraam <kaartic.sivaraam@gmail.com>
>> Signed-off-by: Ghanshyam Thakkar <shyamthakkar001@gmail.com>
>> ---
Show 34 quoted lines
>> diff --git a/t/unit-tests/t-oidmap.c b/t/unit-tests/t-oidmap.c
>> new file mode 100644
>> index 0000000000..9b98a3ed09
>> --- /dev/null
>> +++ b/t/unit-tests/t-oidmap.c
>> @@ -0,0 +1,165 @@
>> +#include "test-lib.h"
>> +#include "lib-oid.h"
>> +#include "oidmap.h"
>> +#include "hash.h"
>> +#include "hex.h"
>> +
>> +/*
>> + * elements we will put in oidmap structs are made of a key: the entry.oid
>> + * field, which is of type struct object_id, and a value: the name field (could
>> + * be a refname for example)
>> + */
>> +struct test_entry {
>> +	struct oidmap_entry entry;
>> +	char name[FLEX_ARRAY];
>> +};
>> +
>> +static const char *key_val[][2] = { { "11", "one" },
>> +				    { "22", "two" },
>> +				    { "33", "three" } };
>> +
>> +static int put_and_check_null(struct oidmap *map, const char *hex,
>> +			      const char *entry_name)
>> +{
>> +	struct test_entry *entry;
>> +
>> +	FLEX_ALLOC_STR(entry, name, entry_name);
>> +	if (get_oid_arbitrary_hex(hex, &entry->entry.oid))
>> +		return -1;

When writing unit tests it is important to make sure that they fail, rather than just return early if there is an error. There are a number of places like this that return early without calling one of the check() macros to make the test fail.

Show 13 quoted lines
>> +	if (!check(oidmap_put(map, entry) == NULL))
>> +		return -1;
>> +	return 0;
>> +}
>> +
>> +static void setup(void (*f)(struct oidmap *map))
>> +{
>> +	struct oidmap map = OIDMAP_INIT;
>> +	int ret = 0;
>> +
>> +	for (size_t i = 0; i < ARRAY_SIZE(key_val); i++)
>> +		if ((ret = put_and_check_null(&map, key_val[i][0],
>> +					      key_val[i][1])))

Given there is only one caller I think it would be easier to see what is going on if the function body was just inlined into the loop here.

Show 6 quoted lines
>> +			break;
>> +
>> +	if (!ret)
>> +		f(&map);
>> +	oidmap_free(&map, 1);
>> +}

The tests for replace, get, remove all look like faithful translations of the old script and are fine apart from some missing check() calls when get_oid_arbitrary_hex() fails.

>> +static int key_val_contains(struct test_entry *entry)
>> +{
>> +	/* the test is small enough to be able to bear O(n) */

It is good to think about that but I'm not sure we need a comment about it in a small test like this.

Show 11 quoted lines
>> +	for (size_t i = 0; i < ARRAY_SIZE(key_val); i++) {
>> +		if (!strcmp(key_val[i][1], entry->name)) {
>> +			struct object_id oid;
>> +			if (get_oid_arbitrary_hex(key_val[i][0], &oid))
>> +				return -1;
>> +			if (oideq(&entry->entry.oid, &oid))
>> +				return 0;
>> +		}
>> +	}
>> +	return 1;
>> +}

So if we cannot construct the oid we return -1, if the oid matches we return 0 and if the oid does not match we return 1

Show 14 quoted lines
>> +static void t_iterate(struct oidmap *map)
>> +{
>> +	struct oidmap_iter iter;
>> +	struct test_entry *entry;
>> +	int ret;
>> +
>> +	oidmap_iter_init(map, &iter);
>> +	while ((entry = oidmap_iter_next(&iter))) {
>> +		if (!check_int((ret = key_val_contains(entry)), ==, 0)) {
>> +			if (ret == -1)
>> +				return;
>> +			test_msg("obtained entry was not given in the input\n"
>> +				 "  name: %s\n   oid: %s\n",
>> +				 entry->name, oid_to_hex(&entry->entry.oid));

This checks that all of the expect objects are present, but does not check for duplicate objects. An alternative would be to build an array of all the entries, then sort it by oid and compare that to a sorted version of `key_val`. That is what the scripted version does. We don't have any helpers for comparing arrays so you'd need to do that by comparing each element in a loop.

>> +		}
>> +	}
>> +	check_int(hashmap_get_size(&map->map), ==, ARRAY_SIZE(key_val));

One could argue that this helps guard against duplicate entries but that's only true if we trust hashmap_get_size() so I think keeping this to check that hashmap_get_size() gives the correct size and changing the loop above would be better.

Thanks
Phillip
Show 15 quoted lines
>> +}
>> +
>> +int cmd_main(int argc UNUSED, const char **argv UNUSED)
>> +{
>> +	TEST(setup(t_replace), "replace works");
>> +	TEST(setup(t_get), "get works");
>> +	TEST(setup(t_remove), "remove works");
>> +	TEST(setup(t_iterate), "iterate works");
>> +	return test_done();
>> +}
>> -- 
>> 2.45.2
>>
>>
> 
Previous: Ghanshyam ThakkarNext: Ghanshyam Thakkar
Message 4 of 16 in “t: migrate helper/test-oidmap.c to unit-tests/t-oidmap.c”
  1. Ghanshyam ThakkarJun 19, 2024
  2. Jonathan NiederJun 20, 2024
  3. Ghanshyam ThakkarJun 25, 2024
  4. Phillip WoodJun 25, 2024
  5. Ghanshyam ThakkarJun 25, 2024
  6. phillip.wood123@gmail.comJun 26, 2024
  7. [GSoC][PATCH v2] t: migrate helper/test-oidmap.c to unit-tests/t-oidmap.cGhanshyam Thakkar, Jun 28, 2024
  8. Josh SteadmonJul 1, 2024
  9. Junio C HamanoJul 1, 2024
  10. Junio C HamanoJul 1, 2024
  11. Junio C HamanoJul 1, 2024
  12. Ghanshyam ThakkarJul 2, 2024
  13. Phillip WoodJul 2, 2024
  14. Phillip WoodJul 2, 2024
  15. Ghanshyam ThakkarJul 2, 2024
  16. Junio C HamanoJul 2, 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.