Accept topic changes from servers that do not send topic-set timestamps (fixes SF...
[ircu2.10.12-pk.git] / libs / dbprim / tests / t_st_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 TABLE0  (void *)0x76543210
27
28 #define DEADINT 0xdeadbeef
29 #define DEADPTR (void *)0xdeadbeef
30
31 static void
32 check_init(smat_table_t *table, unsigned long flags, unsigned long mod,
33            smat_resize_t rsize, void *extra, char *how)
34 {
35   if (table->st_magic != SMAT_TABLE_MAGIC) /* Verify magic was set */
36     printf("FAIL/%s_magic:Initialization failed to set magic number\n", how);
37   else
38     printf("PASS/%s_magic:Initialization set magic number properly\n", how);
39
40   if (!ht_verify(&table->st_table)) /* Verify hash table initialized */
41     printf("FAIL/%s_htinit:Initialization failed to initialize hash table\n",
42            how);
43   else
44     printf("PASS/%s_htinit:Initialization initialized hash table properly\n",
45            how);
46
47   if (ht_flags(&table->st_table) != flags) /* verify flags were set */
48     printf("FAIL/%s_hflags:Initialization failed to set flags\n", how);
49   else
50     printf("PASS/%s_hflags:Initialization set flags properly\n", how);
51
52   if (ht_modulus(&table->st_table) != mod) /* verify modulus was set */
53     printf("FAIL/%s_hmodulus:Initialization failed to set modulus to %ld "
54            "(%ld instead)\n", how, mod, ht_modulus(&table->st_table));
55   else
56     printf("PASS/%s_hmodulus:Initialization set modulus to %ld\n", how, mod);
57
58   if (ht_func(&table->st_table) != _smat_hash) /* verify func was set */
59     printf("FAIL/%s_hfunc:Initialization failed to set hash func\n", how);
60   else
61     printf("PASS/%s_hfunc:Initialization set hash func properly\n", how);
62
63   if (ht_comp(&table->st_table) != _smat_comp) /* verify comp was set */
64     printf("FAIL/%s_hcomp:Initialization failed to set hash comp\n", how);
65   else
66     printf("PASS/%s_hcomp:Initialization set hash comp properly\n", how);
67
68   if (ht_rsize(&table->st_table) != _smat_resize) /* verify resize was set */
69     printf("FAIL/%s_hrsize:Initialization failed to set hash resize\n", how);
70   else
71     printf("PASS/%s_hrsize:Initialization set hash resize properly\n", how);
72
73   if (table->st_resize != rsize) /* verify resize was set */
74     printf("FAIL/%s_rsize:Initialization failed to set resize\n", how);
75   else
76     printf("PASS/%s_rsize:Initialization set resize properly\n", how);
77
78   if (ht_extra(&table->st_table) != extra) /* verify extra was set */
79     printf("FAIL/%s_hextra:Initialization failed to set extra\n", how);
80   else
81     printf("PASS/%s_hextra:Initialization set extra properly\n", how);
82 }
83
84 /* Check return value of operation and report PASS/FAIL */
85 static void
86 check_result(unsigned long result, unsigned long expected, char *test,
87              char *info, int die)
88 {
89   if (result != expected) {
90     printf("FAIL/%s:%s incorrectly returned %lu (expected %lu)\n", test, info,
91            result, expected);
92     if (die)
93       exit(0);
94   } else
95     printf("PASS/%s:%s correctly returned %lu\n", test, info, result);
96 }
97
98 /* Scramble the table */
99 static void
100 scramble(smat_table_t *table)
101 {
102   table->st_magic = DEADINT;
103   table->st_resize = (smat_resize_t)DEADPTR;
104   table->st_table.ht_magic = DEADINT;
105   ht_flags(&table->st_table) = DEADINT;
106   ht_modulus(&table->st_table) = DEADINT;
107   ht_func(&table->st_table) = (hash_func_t)DEADPTR;
108   ht_comp(&table->st_table) = (hash_comp_t)DEADPTR;
109   ht_extra(&table->st_table) = DEADPTR;
110 }
111
112 static unsigned long
113 check_rsize(smat_table_t *tab, unsigned long new_mod)
114 {
115   return 0;
116 }
117
118 int
119 main(int argc, char **argv)
120 {
121   smat_table_t table = SMAT_TABLE_INIT(0x80010000 | HASH_FLAG_AUTOGROW,
122                                        check_rsize, TABLE0);
123
124   /* Check that the static initializer produces a passable structure */
125   check_init(&table, HASH_FLAG_AUTOGROW, 0, check_rsize, TABLE0, "st_static");
126
127   /* now, check what ht_init does with bad arguments */
128   check_result(st_init(0, 0, 0, 0, 0), DB_ERR_BADARGS, "st_init_noargs",
129                "st_init() with no valid arguments", 0);
130
131   /* Scramble the structure */
132   scramble(&table);
133
134   /* Now try to initialize our structure with a 0 mod and see what happens */
135   check_result(st_init(&table, 0x80010000 | HASH_FLAG_AUTOGROW, check_rsize,
136                        TABLE0, 0), 0, "st_dynamic_nomod",
137                "st_init() with zero modulus", 0);
138   check_init(&table, HASH_FLAG_AUTOGROW, 0, check_rsize, TABLE0,
139              "st_dynamic_nomod");
140
141   /* Scramble the structure again */
142   scramble(&table);
143
144   /* Now try to initialize our structure with a non-0 mod and see what
145    * happens
146    */
147   check_result(st_init(&table, 0x80010000 | HASH_FLAG_AUTOGROW, check_rsize,
148                        TABLE0, 6), 0, "st_dynamic_mod6",
149                "st_init() with non-zero modulus", 0);
150   check_init(&table, HASH_FLAG_AUTOGROW, 7, check_rsize, TABLE0,
151              "st_dynamic_mod6");
152
153   return 0;
154 }