/ tests / lib / hash.c
hash.c
 1  /*
 2   * Copyright (c) 2024 Baptiste Daroussin <bapt@FreeBSD.org>
 3   *
 4   * SPDX-License-Identifier: BSD-2-Clause
 5   */
 6  
 7  #include <atf-c.h>
 8  #include <pkghash.h>
 9  #include <xmalloc.h>
10  
11  ATF_TC_WITHOUT_HEAD(hash);
12  
13  ATF_TC_BODY(hash, tc)
14  {
15  	struct pkghash *h = pkghash_new();
16  	ATF_REQUIRE_EQ(pkghash_count(h), 0);
17  	ATF_REQUIRE(pkghash_add(h, "key", "value", NULL));
18  	ATF_REQUIRE_EQ(pkghash_count(h), 1);
19  	ATF_REQUIRE(!pkghash_del(h, "plop"));
20  	ATF_REQUIRE_EQ(pkghash_count(h), 1);
21  	ATF_REQUIRE(pkghash_del(h, "key"));
22  	ATF_REQUIRE_EQ(pkghash_count(h), 0);
23  	char *val = xstrdup("value");
24  	ATF_REQUIRE(pkghash_add(h, "key", val, free));
25  	ATF_REQUIRE_EQ(pkghash_count(h), 1);
26  	ATF_REQUIRE_STREQ((char *)pkghash_delete(h, "key"), "value");
27  	ATF_REQUIRE_STREQ(val, "value");
28  	ATF_REQUIRE_EQ(pkghash_count(h), 0);
29  	ATF_REQUIRE(pkghash_add(h, "key", val, free));
30  	ATF_REQUIRE_EQ(pkghash_count(h), 1);
31  	ATF_REQUIRE(pkghash_del(h, "key"));
32  	ATF_REQUIRE_EQ(pkghash_count(h), 0);
33  	val = xstrdup("value");
34  	ATF_REQUIRE(pkghash_add(h, "key", val, free));
35  	ATF_REQUIRE_EQ(pkghash_delete(h, "bla"), NULL);
36  	pkghash_destroy(h);
37  }
38  
39  ATF_TP_ADD_TCS(tp)
40  {
41  	ATF_TP_ADD_TC(tp, hash);
42  
43  	return (atf_no_error());
44  }