ViewVC Help
View File | Revision Log | Show Annotations | View Changeset | Root Listing
root/svn/vendor/pxys2-2.0.0/pxyservd/dbprim/tests/t_ht_move.c
Revision: 3252
Committed: Wed Apr 2 20:41:43 2014 UTC (11 years, 4 months ago) by michael
Content type: text/x-csrc
File size: 5786 byte(s)
Log Message:
- Imported pxys2-2.0.0

File Contents

# Content
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_move.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 #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 hash_entry_t *entry_p;
90 db_key_t key[] = { /* some keys... */
91 DB_KEY_INIT("obj0", 0),
92 DB_KEY_INIT("obj1", 1),
93 DB_KEY_INIT("obj2", 2),
94 DB_KEY_INIT("obj3", 3),
95 DB_KEY_INIT("obj4", 4),
96 DB_KEY_INIT("obj5", 5),
97 DB_KEY_INIT("obj6", 6)
98 };
99
100 /* initialize the tables with a size */
101 if (ht_init(&table[0], 0, check_func, check_comp, 0, TABLE0, 7) ||
102 ht_init(&table[1], 0, check_func, check_comp, 0, TABLE1, 7))
103 return -1; /* failed to initialize test */
104
105 /* Add some entries to various hash tables */
106 for (i = 0; i < 5; i++)
107 if (ht_add(&table[0], &entry[i], &key[i]))
108 return -1; /* failed to initialize test */
109 if (ht_add(&table[1], &entry[5], &key[5]))
110 return -1; /* failed to initialize test */
111
112 /* Check handling of bad arguments */
113 check_result(ht_move(0, 0, 0), DB_ERR_BADARGS, "ht_move_noargs",
114 "ht_move() with no valid arguments", 0);
115 check_result(ht_move(&table[2], &entry[0], &key[6]), DB_ERR_BADARGS,
116 "ht_move_badtable", "ht_move() with bad table", 1);
117 check_result(ht_move(&table[0], &entry[7], &key[6]), DB_ERR_BADARGS,
118 "ht_move_badentry", "ht_move() with bad entry", 1);
119 check_result(ht_move(&table[0], &entry[0], 0), DB_ERR_BADARGS,
120 "ht_move_badkey", "ht_move() with bad key", 1);
121
122 /* Check if unused entry is excluded */
123 check_result(ht_move(&table[0], &entry[6], &key[6]), DB_ERR_UNUSED,
124 "ht_move_unused", "ht_move() with unused entry", 1);
125 /* How about wrong table? */
126 check_result(ht_move(&table[0], &entry[5], &key[6]), DB_ERR_WRONGTABLE,
127 "ht_move_wrongtable", "ht_move() with entry in wrong table", 1);
128
129 /* Freeze the table temporarily */
130 ht_flags(&table[0]) |= HASH_FLAG_FREEZE;
131 /* check if frozen tables are excluded */
132 check_result(ht_move(&table[0], &entry[0], &key[6]), DB_ERR_FROZEN,
133 "ht_move_frozen", "ht_move() on frozen table", 1);
134 /* Unfreeze the table */
135 ht_flags(&table[0]) &= ~HASH_FLAG_FREEZE;
136
137 /* Are duplicate keys excluded? */
138 check_result(ht_move(&table[0], &entry[0], &key[4]), DB_ERR_DUPLICATE,
139 "ht_move_duplicate", "ht_move() with duplicate key", 1);
140
141 /* OK, try moving the entry */
142 check_result(ht_move(&table[0], &entry[0], &key[6]), 0, "ht_move_t0e0k6",
143 "Rekey entry 0 in hash table 1 to key 6", 1);
144
145 /* Look up the old entry... */
146 check_result(ht_find(&table[0], 0, &key[0]), DB_ERR_NOENTRY,
147 "ht_move_find_t0e0k0", "Attempt to find entry under old key",
148 0);
149
150 /* Now look up the new entry */
151 check_result(ht_find(&table[0], &entry_p, &key[6]), 0, "ht_move_find_t0e0k6",
152 "Locate entry under new key", 1);
153 if (entry_p != &entry[0])
154 printf("FAIL/ht_move_find_t0e0k6_entry:Attempt to look up entry 0 "
155 "retrieved %p (correct answer is %p)\n", (void *)entry_p,
156 (void *)&entry[0]);
157 else
158 printf("PASS/ht_move_find_t0e0k6_entry:Retrieved correct entry %p\n",
159 (void *)entry_p);
160
161 return 0;
162 }