c4e6c675a4d1f258529c3b7cfe21438e96473f61
[ircu2.10.12-pk.git] / libs / dbprim / tests / t_he_init.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
26 #define OBJECT  (void *)0x91827364
27 #define DEADINT 0xdeadbeef
28 #define DEADPTR (void *)0xdeadbeef
29
30 static void
31 check_init(hash_entry_t *entry, char *how)
32 {
33   if (entry->he_magic != HASH_ENTRY_MAGIC) /* Verify magic was set */
34     printf("FAIL/%s_magic:Initialization failed to set magic number\n", how);
35   else
36     printf("PASS/%s_magic:Initialization set magic number properly\n", how);
37
38   if (!le_verify(&entry->he_elem)) /* verify element was initialized */
39     printf("FAIL/%s_elem:Initialization failed to initialize linked list "
40            "element\n", how);
41   else
42     printf("PASS/%s_elem:Initialization initialized linked list element\n",
43            how);
44
45   if (entry->he_table != 0) /* verify table was cleared */
46     printf("FAIL/%s_table:Initialization failed to clear table\n", how);
47   else
48     printf("PASS/%s_table:Initialization set table to 0\n", how);
49
50   if (entry->he_hash != 0) /* verify hash value was cleared */
51     printf("FAIL/%s_hash:Initialization failed to clear hash value\n", how);
52   else
53     printf("PASS/%s_hash:Initialization set hash value to 0\n", how);
54
55   if (dk_key(&entry->he_key) != 0) /* verify key value was cleared */
56     printf("FAIL/%s_key:Initialization failed to clear database key\n", how);
57   else
58     printf("PASS/%s_key:Initialization set database key to 0\n", how);
59
60   if (dk_len(&entry->he_key) != 0) /* verify key length was cleared */
61     printf("FAIL/%s_keylen:Initialization failed to clear database key "
62            "length\n", how);
63   else
64     printf("PASS/%s_keylen:Initialization set database key length to 0\n",
65            how);
66
67   if (entry->he_value != OBJECT) /* verify value was set properly */
68     printf("FAIL/%s_value:Initialization failed to set value\n", how);
69   else
70     printf("PASS/%s_value:Initialization set value properly\n", how);
71 }
72
73 /* Check return value of operation and report PASS/FAIL */
74 static void
75 check_result(unsigned long result, unsigned long expected, char *test,
76              char *info, int die)
77 {
78   if (result != expected) {
79     printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
80            result, expected);
81     if (die)
82       exit(0);
83   } else
84     printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
85 }
86
87 int
88 main(int argc, char **argv)
89 {
90   hash_entry_t entry = HASH_ENTRY_INIT(OBJECT);
91
92   /* Check that the static initializer produces a passable structure */
93   check_init(&entry, "he_static");
94
95   /* now, check what he_init does with bad arguments */
96   check_result(he_init(0, 0), DB_ERR_BADARGS, "he_init_noargs",
97                "he_init() with no valid arguments", 0);
98
99   /* Scramble the structure */
100   entry.he_magic = DEADINT;
101   entry.he_elem.le_magic = DEADINT;
102   entry.he_table = DEADPTR;
103   entry.he_hash = DEADINT;
104   entry.he_key.dk_key = DEADPTR;
105   entry.he_key.dk_len = DEADINT;
106   entry.he_value = DEADPTR;
107
108   /* Now try to initialize our structure and see what happens */
109   check_result(he_init(&entry, OBJECT), 0, "he_dynamic",
110                "he_init() to dynamically initialize hash entry", 0);
111
112   /* Finally, verify initialization */
113   check_init(&entry, "he_dynamic");
114
115   return 0;
116 }