]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libtest/src/libtest_tests/libtest_tests_hash_addonly_iterate.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.1.0 / test_and_benchmark / libtest / src / libtest_tests / libtest_tests_hash_addonly_iterate.c
1 /***** includes *****/
2 #include "libtest_tests_internal.h"
3
4 /***** structs *****/
5 struct test_element
6 {
7   struct lfds710_btree_au_element
8     baue;
9
10   lfds710_pal_uint_t
11     datum;
12 };
13
14 /***** private prototypes *****/
15 static int key_compare_function( void const *new_key, void const *existing_key );
16 static void key_hash_function( void const *key, lfds710_pal_uint_t *hash );
17
18
19
20
21
22 /****************************************************************************/
23 #pragma warning( disable : 4100 )
24
25 void libtest_tests_hash_a_iterate( struct lfds710_list_asu_state *list_of_logical_processors, struct libshared_memory_state *ms, enum lfds710_misc_validity *dvs )
26 {
27   lfds710_pal_uint_t
28     *counter_array,
29     loop;
30
31   struct lfds710_hash_a_element
32     *hae;
33
34   struct lfds710_hash_a_iterate
35     hai;
36
37   struct lfds710_hash_a_state
38     has;
39
40   struct lfds710_hash_a_element
41     *element_array;
42
43   struct lfds710_btree_au_state
44     *baus,
45     *baus_thousand;
46
47   void
48     *value;
49
50   LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
51   LFDS710_PAL_ASSERT( ms != NULL );
52   LFDS710_PAL_ASSERT( dvs != NULL );
53
54   /* TRD : single-threaded test
55            we create a single hash_a
56            we populate with 1000 elements
57            where key and value is the number of the element (e.g. 0 to 999)
58            we then allocate 1000 counters, init to 0
59            we then iterate
60            we increment each element as we see it in the iterate
61            if any are missing or seen more than once, problemo!
62
63            we do this once with a table of 10, to ensure each table has (or almost certainly has) something in
64            and then a second tiem with a table of 10000, to ensure some empty tables exist
65   */
66
67   *dvs = LFDS710_MISC_VALIDITY_VALID;
68
69   counter_array = libshared_memory_alloc_from_unknown_node( ms, sizeof(lfds710_pal_uint_t) * 1000, sizeof(lfds710_pal_uint_t) );
70   element_array = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct lfds710_hash_a_element) * 1000, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
71   baus = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct lfds710_btree_au_state) * 10, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
72   baus_thousand = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct lfds710_btree_au_state) * 1000, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
73
74   // TRD : first time around
75   lfds710_hash_a_init_valid_on_current_logical_core( &has, baus, 10, key_compare_function, key_hash_function, LFDS710_HASH_A_EXISTING_KEY_FAIL, NULL );
76
77   for( loop = 0 ; loop < 1000 ; loop++ )
78   {
79     LFDS710_HASH_A_SET_KEY_IN_ELEMENT( *(element_array+loop), loop );
80     LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( *(element_array+loop), loop );
81     lfds710_hash_a_insert( &has, element_array+loop, NULL );
82   }
83
84   for( loop = 0 ; loop < 1000 ; loop++ )
85     *(counter_array+loop) = 0;
86
87   lfds710_hash_a_iterate_init( &has, &hai );
88
89   while( *dvs == LFDS710_MISC_VALIDITY_VALID and lfds710_hash_a_iterate(&hai, &hae) )
90   {
91     value = LFDS710_HASH_A_GET_VALUE_FROM_ELEMENT( *hae );
92     ( *(counter_array + (lfds710_pal_uint_t) value) )++;
93   }
94
95   if( *dvs == LFDS710_MISC_VALIDITY_VALID )
96     for( loop = 0 ; loop < 1000 ; loop++ )
97     {
98       if( *(counter_array+loop) > 1 )
99         *dvs = LFDS710_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
100
101       if( *(counter_array+loop) == 0 )
102         *dvs = LFDS710_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
103     }
104
105   lfds710_hash_a_cleanup( &has, NULL );
106
107   // TRD : second time around
108   if( *dvs == LFDS710_MISC_VALIDITY_VALID )
109   {
110     lfds710_hash_a_init_valid_on_current_logical_core( &has, baus_thousand, 10000, key_compare_function, key_hash_function, LFDS710_HASH_A_EXISTING_KEY_FAIL, NULL );
111
112     for( loop = 0 ; loop < 1000 ; loop++ )
113     {
114       LFDS710_HASH_A_SET_KEY_IN_ELEMENT( *(element_array+loop), loop );
115       LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( *(element_array+loop), loop );
116       lfds710_hash_a_insert( &has, element_array+loop, NULL );
117     }
118
119     for( loop = 0 ; loop < 1000 ; loop++ )
120       *(counter_array+loop) = 0;
121
122     lfds710_hash_a_iterate_init( &has, &hai );
123
124     while( *dvs == LFDS710_MISC_VALIDITY_VALID and lfds710_hash_a_iterate(&hai, &hae) )
125     {
126       value = LFDS710_HASH_A_GET_VALUE_FROM_ELEMENT( *hae );
127       ( *(counter_array + (lfds710_pal_uint_t) value ) )++;
128     }
129
130     if( *dvs == LFDS710_MISC_VALIDITY_VALID )
131       for( loop = 0 ; loop < 1000 ; loop++ )
132       {
133         if( *(counter_array+loop) > 1 )
134           *dvs = LFDS710_MISC_VALIDITY_INVALID_ADDITIONAL_ELEMENTS;
135
136         if( *(counter_array+loop) == 0 )
137           *dvs = LFDS710_MISC_VALIDITY_INVALID_MISSING_ELEMENTS;
138       }
139
140     lfds710_hash_a_cleanup( &has, NULL );
141   }
142
143   return;
144 }
145
146 #pragma warning( default : 4100 )
147
148
149
150
151
152 /****************************************************************************/
153 static int key_compare_function( void const *new_key, void const *existing_key )
154 {
155   int
156     cr = 0;
157
158   // TRD : new_key can be NULL (i.e. 0)
159   // TRD : existing_key can be NULL (i.e. 0)
160
161   if( (lfds710_pal_uint_t) new_key < (lfds710_pal_uint_t) existing_key )
162     cr = -1;
163
164   if( (lfds710_pal_uint_t) new_key > (lfds710_pal_uint_t) existing_key )
165     cr = 1;
166
167   return cr;
168 }
169
170
171
172
173
174 /****************************************************************************/
175 static void key_hash_function( void const *key, lfds710_pal_uint_t *hash )
176 {
177   // TRD : key can be NULL
178   LFDS710_PAL_ASSERT( hash != NULL );
179
180   *hash = 0;
181
182   /* TRD : this function iterates over the user data
183            and we are using the void pointer AS user data
184            so here we need to pass in the addy of value
185   */
186
187   LFDS710_HASH_A_HASH_FUNCTION( (void *) &key, sizeof(lfds710_pal_uint_t), *hash );
188
189   return;
190 }
191