hash_table.cpp 3.1 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
/** Unit tests for hash table
 *
 * @author Steffen Vogel <stvogel@eonerc.rwth-aachen.de>
 * @copyright 2018, Institute for Automation of Complex Power Systems, EONERC
 * @license GNU General Public License (version 3)
 *
 * VILLAScommon
 *
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.
 *********************************************************************************/

#include <criterion/criterion.h>

#include <villas/utils.h>
#include <villas/hash_table.h>

Steffen Vogel's avatar
Steffen Vogel committed
28
29
30
static const char *keys[]   = { "able",   "achieve", "acoustics", "action", "activity", "aftermath", "afternoon", "afterthought", "apparel",  "appliance", "beginner", "believe",   "bomb",   "border",  "boundary",   "breakfast", "cabbage",  "cable",  "calculator", "calendar",  "caption", "carpenter", "cemetery", "channel", "circle", "creator", "creature", "education", "faucet", "feather", "friction", "fruit",     "fuel",    "galley", "guide",    "guitar", "health", "heart",   "idea",   "kitten",  "laborer",    "language" };
static const char *values[] = { "lawyer", "linen",   "locket",    "lumber", "magic",    "minister",  "mitten",    "money",        "mountain", "music",     "partner",  "passenger", "pickle", "picture", "plantation", "plastic",   "pleasure", "pocket", "police",     "pollution", "railway", "recess",    "reward",   "route",   "scene",  "scent",   "squirrel", "stranger",  "suit",   "sweater", "temper",   "territory", "texture", "thread", "treatment", "veil",  "vein",   "volcano", "wealth", "weather", "wilderness", "wren" };

31
TestSuite(hash_table, .description = "Hash table datastructure");
32
33
34
35

Test(hash_table, hash_table_lookup)
{
	int ret;
Steffen Vogel's avatar
Steffen Vogel committed
36
37
	struct hash_table ht;
	ht.state = STATE_DESTROYED;
38
39
40
41
42

	ret = hash_table_init(&ht, 20);
	cr_assert(!ret);

	/* Insert */
Steffen Vogel's avatar
Steffen Vogel committed
43
44
	for (unsigned i = 0; i < ARRAY_LEN(keys); i++) {
		ret = hash_table_insert(&ht, keys[i], (void *) values[i]);
45
46
47
48
		cr_assert(!ret);
	}

	/* Lookup */
Steffen Vogel's avatar
Steffen Vogel committed
49
50
	for (unsigned i = 0; i < ARRAY_LEN(keys); i++) {
		char *value = (char *) hash_table_lookup(&ht, keys[i]);
51
52
53
54
		cr_assert_eq(values[i], value);
	}

	/* Inserting the same key twice should fail */
Steffen Vogel's avatar
Steffen Vogel committed
55
	ret = hash_table_insert(&ht, keys[0], (void *) values[0]);
56
57
58
59
60
61
62
63
64
65
66
67
68
	cr_assert(ret);

	hash_table_dump(&ht);

	/* Removing an entry */
	ret = hash_table_delete(&ht, keys[0]);
	cr_assert(!ret);

	/* Removing the same entry twice should fail */
	ret = hash_table_delete(&ht, keys[0]);
	cr_assert(ret);

	/* After removing, we should be able to insert it again */
Steffen Vogel's avatar
Steffen Vogel committed
69
	ret = hash_table_insert(&ht, keys[0], (void *) values[0]);
70
71
	cr_assert(!ret);

Steffen Vogel's avatar
Steffen Vogel committed
72
	ret = hash_table_destroy(&ht, nullptr, false);
73
74
	cr_assert(!ret);
}