1 |
/* |
2 |
** Copyright (C) 2002 by Kevin L. Mitchell <klmitch@mit.edu> |
3 |
** |
4 |
** This library is free software; you can redistribute it and/or |
5 |
** modify it under the terms of the GNU Library General Public |
6 |
** License as published by the Free Software Foundation; either |
7 |
** version 2 of the License, or (at your option) any later version. |
8 |
** |
9 |
** This library is distributed in the hope that it will be useful, |
10 |
** but WITHOUT ANY WARRANTY; without even the implied warranty of |
11 |
** MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU |
12 |
** Library General Public License for more details. |
13 |
** |
14 |
** You should have received a copy of the GNU Library General Public |
15 |
** License along with this library; if not, write to the Free |
16 |
** Software Foundation, Inc., 59 Temple Place - Suite 330, Boston, |
17 |
** MA 02111-1307, USA |
18 |
** |
19 |
** @(#)$Id: t_ht_free.c,v 1.2 2003/06/12 01:10:11 klmitch Exp $ |
20 |
*/ |
21 |
#include <stdio.h> |
22 |
#include <stdlib.h> |
23 |
|
24 |
#include "dbprim.h" |
25 |
#include "dbprim_int.h" |
26 |
|
27 |
#define TABLE0 (void *)0x76543210 |
28 |
#define TABLE1 (void *)0x87654321 |
29 |
|
30 |
#define OBJECT0 (void *)0x01234567 |
31 |
#define OBJECT1 (void *)0x12345678 |
32 |
#define OBJECT2 (void *)0x23456789 |
33 |
#define OBJECT3 (void *)0x3456789a |
34 |
#define OBJECT4 (void *)0x456789ab |
35 |
|
36 |
#define DEADINT 0xdeadbeef |
37 |
#define DEADPTR (void *)0xdeadbeef |
38 |
|
39 |
/* Check return value of add operation and report PASS/FAIL */ |
40 |
static void |
41 |
check_result(unsigned long result, unsigned long expected, char *test, |
42 |
char *info, int die) |
43 |
{ |
44 |
if (result != expected) { |
45 |
printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info, |
46 |
result, expected); |
47 |
if (die) |
48 |
exit(0); |
49 |
} else |
50 |
printf("PASS/%s:%s correctly returned %lu\n", test, info, result); |
51 |
} |
52 |
|
53 |
static unsigned long |
54 |
check_func(hash_table_t *table, db_key_t *key) |
55 |
{ |
56 |
return dk_len(key); |
57 |
} |
58 |
|
59 |
static unsigned long |
60 |
check_comp(hash_table_t *table, db_key_t *key1, db_key_t *key2) |
61 |
{ |
62 |
return (!(dk_len(key1) == dk_len(key2) && dk_key(key1) == dk_key(key2))); |
63 |
} |
64 |
|
65 |
int |
66 |
main(int argc, char **argv) |
67 |
{ |
68 |
int i; |
69 |
hash_table_t table[] = { /* some tables to operate on */ |
70 |
HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE0), |
71 |
HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE1), |
72 |
{ DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADPTR, |
73 |
(hash_func_t)DEADPTR, (hash_comp_t)DEADPTR, (hash_resize_t)DEADPTR, |
74 |
DEADPTR } /* table[2] */ |
75 |
}; |
76 |
hash_entry_t entry[] = { /* some entries to operate on */ |
77 |
HASH_ENTRY_INIT(OBJECT0), |
78 |
HASH_ENTRY_INIT(OBJECT1), |
79 |
HASH_ENTRY_INIT(OBJECT2), |
80 |
HASH_ENTRY_INIT(OBJECT3), |
81 |
HASH_ENTRY_INIT(OBJECT4), |
82 |
}; |
83 |
db_key_t key[] = { /* some keys... */ |
84 |
DB_KEY_INIT("obj0", 0), |
85 |
DB_KEY_INIT("obj1", 1), |
86 |
DB_KEY_INIT("obj2", 2), |
87 |
DB_KEY_INIT("obj3", 3), |
88 |
DB_KEY_INIT("obj4", 4), |
89 |
}; |
90 |
|
91 |
/* initialize the tables with a size */ |
92 |
if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) || |
93 |
ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7) || |
94 |
!table[0].ht_table || !table[1].ht_table) |
95 |
return -1; /* failed to initialize test */ |
96 |
|
97 |
/* Add some entries to various hash tables */ |
98 |
for (i = 0; i < 5; i++) |
99 |
if (ht_add(&table[0], &entry[i], &key[i])) |
100 |
return -1; /* failed to initialize test */ |
101 |
|
102 |
/* Check handling of bad arguments */ |
103 |
check_result(ht_free(0), DB_ERR_BADARGS, "ht_free_noargs", |
104 |
"ht_free() with no valid arguments", 0); |
105 |
check_result(ht_free(&table[2]), DB_ERR_BADARGS, "ht_free_badtable", |
106 |
"ht_free() with bad table", 0); |
107 |
|
108 |
/* Freeze the table temporarily */ |
109 |
ht_flags(&table[1]) |= HASH_FLAG_FREEZE; |
110 |
/* check if frozen tables are excluded */ |
111 |
check_result(ht_free(&table[1]), DB_ERR_FROZEN, "ht_free_frozen", |
112 |
"ht_free() on frozen table", 1); |
113 |
/* Unfreeze the table */ |
114 |
ht_flags(&table[1]) &= ~HASH_FLAG_FREEZE; |
115 |
|
116 |
/* Check if non-empty tables are excluded */ |
117 |
check_result(ht_free(&table[0]), DB_ERR_NOTEMPTY, "ht_free_nonempty", |
118 |
"ht_free() on non-empty table", 0); |
119 |
|
120 |
/* OK, now try to free the table */ |
121 |
check_result(ht_free(&table[1]), 0, "ht_free_t0", "ht_free() on empty table", |
122 |
1); |
123 |
|
124 |
/* Verify that table pointer is now 0 */ |
125 |
if (table[1].ht_table != 0) |
126 |
printf("FAIL/ht_free_t0_table:Table not cleared properly\n"); |
127 |
else |
128 |
printf("PASS/ht_free_t0_table:Table properly cleared\n"); |
129 |
|
130 |
return 0; |
131 |
} |