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
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
|
/*
* Copyright (C) 2010 Tobias Brunner
* Hochschule fuer Technik Rapperswil
*
* 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 2 of the License, or (at your
* option) any later version. See <http://www.fsf.org/copyleft/gpl.txt>.
*
* 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.
*/
#include <library.h>
#include <collections/hashtable.h>
static u_int hash(char *key)
{
return chunk_hash(chunk_create(key, strlen(key)));
}
static u_int equals(char *key1, char *key2)
{
return streq(key1, key2);
}
/**
* Test the remove_at method
*/
bool test_hashtable_remove_at()
{
char *k1 = "key1", *k2 = "key2", *k3 = "key3", *key;
char *v1 = "val1", *v2 = "val2", *v3 = "val3", *value;
enumerator_t *enumerator;
hashtable_t *ht = hashtable_create((hashtable_hash_t)hash,
(hashtable_equals_t)equals, 0);
ht->put(ht, k1, v1);
ht->put(ht, k2, v2);
ht->put(ht, k3, v3);
if (ht->get_count(ht) != 3)
{
return FALSE;
}
enumerator = ht->create_enumerator(ht);
while (enumerator->enumerate(enumerator, &key, &value))
{
if (streq(key, k2))
{
ht->remove_at(ht, enumerator);
}
}
enumerator->destroy(enumerator);
if (ht->get_count(ht) != 2)
{
return FALSE;
}
if (ht->get(ht, k1) == NULL ||
ht->get(ht, k3) == NULL)
{
return FALSE;
}
if (ht->get(ht, k2) != NULL)
{
return FALSE;
}
ht->put(ht, k2, v2);
if (ht->get_count(ht) != 3)
{
return FALSE;
}
if (ht->get(ht, k1) == NULL ||
ht->get(ht, k2) == NULL ||
ht->get(ht, k3) == NULL)
{
return FALSE;
}
enumerator = ht->create_enumerator(ht);
while (enumerator->enumerate(enumerator, &key, &value))
{
ht->remove_at(ht, enumerator);
}
enumerator->destroy(enumerator);
if (ht->get_count(ht) != 0)
{
return FALSE;
}
if (ht->get(ht, k1) != NULL ||
ht->get(ht, k2) != NULL ||
ht->get(ht, k3) != NULL)
{
return FALSE;
}
ht->destroy(ht);
return TRUE;
}
|