Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / tests / t_ht_remove.c
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$
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 #define OBJECT5 (void *)0x56789abc
36 #define OBJECT6 (void *)0x6789abcd
37
38 #define DEADINT 0xdeadbeef
39 #define DEADPTR (void *)0xdeadbeef
40
41 /* Check return value of add operation and report PASS/FAIL */
42 static void
43 check_result(unsigned long result, unsigned long expected, char *test,
44              char *info, int die)
45 {
46   if (result != expected) {
47     printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
48            result, expected);
49     if (die)
50       exit(0);
51   } else
52     printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
53 }
54
55 static unsigned long
56 check_func(hash_table_t *table, db_key_t *key)
57 {
58   return dk_len(key);
59 }
60
61 static unsigned long
62 check_comp(hash_table_t *table, db_key_t *key1, db_key_t *key2)
63 {
64   return (!(dk_len(key1) == dk_len(key2) && dk_key(key1) == dk_key(key2)));
65 }
66
67 int
68 main(int argc, char **argv)
69 {
70   int i;
71   hash_table_t table[] = { /* some tables to operate on */
72     HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE0),
73     HASH_TABLE_INIT(0, check_func, check_comp, 0, TABLE1),
74     { DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADINT, DEADPTR,
75       (hash_func_t)DEADPTR, (hash_comp_t)DEADPTR, (hash_resize_t)DEADPTR,
76       DEADPTR } /* table[2] */
77   };
78   hash_entry_t entry[] = { /* some entries to operate on */
79     HASH_ENTRY_INIT(OBJECT0),
80     HASH_ENTRY_INIT(OBJECT1),
81     HASH_ENTRY_INIT(OBJECT2),
82     HASH_ENTRY_INIT(OBJECT3),
83     HASH_ENTRY_INIT(OBJECT4),
84     HASH_ENTRY_INIT(OBJECT5),
85     HASH_ENTRY_INIT(OBJECT6),
86     { DEADINT, { DEADINT, DEADPTR, DEADPTR, DEADPTR, DEADPTR, DEADINT },
87       DEADPTR, DEADINT, { DEADPTR, DEADINT }, DEADPTR } /* entry[7] */
88   };
89   db_key_t key[] = { /* some keys... */
90     DB_KEY_INIT("obj0", 0),
91     DB_KEY_INIT("obj1", 1),
92     DB_KEY_INIT("obj2", 2),
93     DB_KEY_INIT("obj3", 3),
94     DB_KEY_INIT("obj4", 4),
95     DB_KEY_INIT("obj5", 5),
96     DB_KEY_INIT("obj6", 6)
97   };
98
99   /* initialize the tables with a size */
100   if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) ||
101       ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7))
102     return -1; /* failed to initialize test */
103
104   /* Add some entries to various hash tables */
105   for (i = 0; i < 5; i++)
106     if (ht_add(&table[0], &entry[i], &key[i]))
107       return -1; /* failed to initialize test */
108   if (ht_add(&table[1], &entry[5], &key[5]))
109     return -1; /* failed to initialize test */
110
111   /* Check handling of bad arguments */
112   check_result(ht_remove(0, 0), DB_ERR_BADARGS, "ht_remove_noargs",
113                "ht_remove() with no valid arguments", 0);
114   check_result(ht_remove(&table[2], &entry[0]), DB_ERR_BADARGS,
115                "ht_remove_badtable", "ht_remove() with bad table", 1);
116   check_result(ht_remove(&table[0], &entry[7]), DB_ERR_BADARGS,
117                "ht_remove_badentry", "ht_remove() with bad entry", 1);
118
119   /* Check if unused entry is excluded */
120   check_result(ht_remove(&table[0], &entry[6]), DB_ERR_UNUSED,
121                "ht_remove_unused", "ht_remove() with unused entry", 1);
122   /* How about wrong table? */
123   check_result(ht_remove(&table[0], &entry[5]), DB_ERR_WRONGTABLE,
124                "ht_remove_wrongtable", "ht_remove() with entry in wrong table",
125                1);
126
127   /* Freeze the table temporarily */
128   ht_flags(&table[0]) |= HASH_FLAG_FREEZE;
129   /* check if frozen tables are excluded */
130   check_result(ht_remove(&table[0], &entry[0]), DB_ERR_FROZEN,
131                "ht_remove_frozen", "ht_remove() on frozen table", 1);
132   /* Unfreeze the table */
133   ht_flags(&table[0]) &= ~HASH_FLAG_FREEZE;
134
135   /* OK, try removing an entry */
136   check_result(ht_remove(&table[0], &entry[0]), 0, "ht_remove_t0e0",
137                "Remove entry 0 in table 0", 1);
138   /* check to see if we can find it */
139   check_result(ht_find(&table[0], 0, &key[0]), DB_ERR_NOENTRY,
140                "ht_remove_find_t0e0", "Attempt to look up removed entry", 0);
141
142   /* Check to see if the removed entry's table pointer is 0 */
143   if (he_table(&entry[0]) != 0)
144     printf("FAIL/ht_remove_t0e0_table:Table pointer not reset\n");
145   else
146     printf("PASS/ht_remove_t0e0_table:Table pointer properly reset\n");
147
148   /* Finally, check to see if the count is correct */
149   if (ht_count(&table[0]) != 4)
150     printf("FAIL/ht_remove_t0e0_count:Table count not adjusted; is %ld, "
151            "should be 4\n", ht_count(&table[0]));
152   else
153     printf("PASS/ht_remove_t0e0_count:Table count correctly adjusted\n");
154
155   return 0;
156 }