]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/test_lfds700_hash_addonly_iterate.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / test_lfds700_hash_addonly_iterate.c
1 /***** includes *****/
2 #include "internal.h"
3
4 /***** structs *****/
5 struct test_element
6 {
7   struct lfds700_btree_au_element
8     baue;
9
10   lfds700_pal_uint_t
11     datum;
12 };
13
14 struct test_state
15 {
16   enum lfds700_misc_flag
17     error_flag;
18
19   struct lfds700_hash_a_state
20     *has;
21
22   struct test_element
23     *element_array;
24 };
25
26 /***** private prototypes *****/
27 static int key_compare_function( void const *new_key, void const *existing_key );
28 static void key_hash_function( void const *key, lfds700_pal_uint_t *hash );
29
30
31
32
33
34 /****************************************************************************/
35 void test_lfds700_hash_a_iterate( void )
36 {
37   enum lfds700_misc_validity
38     dvs = LFDS700_MISC_VALIDITY_VALID;
39
40   lfds700_pal_uint_t
41     *counter_array,
42     loop;
43
44   struct lfds700_hash_a_element
45     *hae;
46
47   struct lfds700_hash_a_iterate
48     hai;
49
50   struct lfds700_hash_a_state
51     has;
52
53   struct lfds700_hash_a_element
54     *element_array;
55
56   struct lfds700_btree_au_state
57     *baus;
58
59   struct lfds700_misc_prng_state
60     ps;
61
62   void
63     *value;
64
65   /* TRD : single-threaded test
66            we create a single hash_a
67            we populate with 1000 elements
68            where key and value is the number of the element (e.g. 0 to 999)
69            we then allocate 1000 counters, init to 0
70            we then iterate
71            we increment each element as we see it in the iterate
72            if any are missing or seen more than once, problemo!
73
74            we do this once with a table of 10, to ensure each table has (or almost certainly has) something in
75            and then a second tiem with a table of 10000, to ensure some empty tables exist
76   */
77
78   internal_display_test_name( "Iterate" );
79
80   lfds700_misc_prng_init( &ps );
81
82   element_array = util_aligned_malloc( sizeof(struct lfds700_hash_a_element) * 1000, LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
83
84   counter_array = util_malloc_wrapper( sizeof(lfds700_pal_uint_t) * 1000 );
85
86   // TRD : first time around
87   baus = util_aligned_malloc( sizeof(struct lfds700_btree_au_state) * 10, LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
88
89   lfds700_hash_a_init_valid_on_current_logical_core( &has, baus, 10, key_compare_function, key_hash_function, LFDS700_HASH_A_EXISTING_KEY_FAIL, NULL );
90
91   for( loop = 0 ; loop < 1000 ; loop++ )
92   {
93     LFDS700_HASH_A_SET_KEY_IN_ELEMENT( *(element_array+loop), loop );
94     LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( *(element_array+loop), loop );
95     lfds700_hash_a_insert( &has, element_array+loop, NULL, &ps );
96   }
97
98   for( loop = 0 ; loop < 1000 ; loop++ )
99     *(counter_array+loop) = 0;
100
101   lfds700_hash_a_iterate_init( &has, &hai );
102
103   while( dvs == LFDS700_MISC_VALIDITY_VALID and lfds700_hash_a_iterate(&hai, &hae) )
104   {
105     value = LFDS700_HASH_A_GET_VALUE_FROM_ELEMENT( *hae );
106     ( *(counter_array + (lfds700_pal_uint_t) value) )++;
107   }
108
109   if( dvs == LFDS700_MISC_VALIDITY_VALID )
110     for( loop = 0 ; loop < 1000 ; loop++ )
111     {
112       if( *(counter_array+loop) > 1 )
113         dvs = LFDS700_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
114
115       if( *(counter_array+loop) == 0 )
116         dvs = LFDS700_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
117     }
118
119   lfds700_hash_a_cleanup( &has, NULL );
120
121   util_aligned_free( baus );
122
123   // TRD : second time around
124   if( dvs == LFDS700_MISC_VALIDITY_VALID )
125   {
126     baus = util_aligned_malloc( sizeof(struct lfds700_btree_au_state) * 10000, LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
127
128     lfds700_hash_a_init_valid_on_current_logical_core( &has, baus, 10000, key_compare_function, key_hash_function, LFDS700_HASH_A_EXISTING_KEY_FAIL, NULL );
129
130     for( loop = 0 ; loop < 1000 ; loop++ )
131     {
132       LFDS700_HASH_A_SET_KEY_IN_ELEMENT( *(element_array+loop), loop );
133       LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( *(element_array+loop), loop );
134       lfds700_hash_a_insert( &has, element_array+loop, NULL, &ps );
135     }
136
137     for( loop = 0 ; loop < 1000 ; loop++ )
138       *(counter_array+loop) = 0;
139
140     lfds700_hash_a_iterate_init( &has, &hai );
141
142     while( dvs == LFDS700_MISC_VALIDITY_VALID and lfds700_hash_a_iterate(&hai, &hae) )
143     {
144       value = LFDS700_HASH_A_GET_VALUE_FROM_ELEMENT( *hae );
145       ( *(counter_array + (lfds700_pal_uint_t) value ) )++;
146     }
147
148     if( dvs == LFDS700_MISC_VALIDITY_VALID )
149       for( loop = 0 ; loop < 1000 ; loop++ )
150       {
151         if( *(counter_array+loop) > 1 )
152           dvs = LFDS700_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
153
154         if( *(counter_array+loop) == 0 )
155           dvs = LFDS700_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
156       }
157
158     lfds700_hash_a_cleanup( &has, NULL );
159
160     util_aligned_free( baus );
161   }
162
163   // TRD : cleanup
164   util_aligned_free( element_array );
165   free( counter_array );
166
167   // TRD : print the test result
168   internal_display_test_result( 1, "hash_a", dvs );
169
170   return;
171 }
172
173
174
175
176
177 /****************************************************************************/
178 #pragma warning( disable : 4100 )
179
180 static int key_compare_function( void const *new_key, void const *existing_key )
181 {
182   int
183     cr = 0;
184
185   // TRD : new_key can be NULL (i.e. 0)
186   // TRD : existing_key can be NULL (i.e. 0)
187
188   if( (lfds700_pal_uint_t) new_key < (lfds700_pal_uint_t) existing_key )
189     cr = -1;
190
191   if( (lfds700_pal_uint_t) new_key > (lfds700_pal_uint_t) existing_key )
192     cr = 1;
193
194   return( cr );
195 }
196
197 #pragma warning( default : 4100 )
198
199
200
201
202
203 /****************************************************************************/
204 #pragma warning( disable : 4100 )
205
206 static void key_hash_function( void const *key, lfds700_pal_uint_t *hash )
207 {
208   // TRD : key can be NULL
209   assert( hash != NULL );
210
211   *hash = 0;
212
213   /* TRD : this function iterates over the user data
214            and we are using the void pointer AS user data
215            so here we need to pass in the addy of value
216   */
217
218   LFDS700_HASH_A_32BIT_HASH_FUNCTION( (void *) &key, sizeof(lfds700_pal_uint_t), *hash );
219
220   return;
221 }
222
223 #pragma warning( default : 4100 )
224