]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libtest/src/libtest_tests/libtest_tests_hash_addonly_fail_and_overwrite.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_fail_and_overwrite.c
1 /***** includes *****/
2 #include "libtest_tests_internal.h"
3
4 /***** private prototypes *****/
5 static int key_compare_function( void const *new_value, void const *value_in_tree );
6 static void key_hash_function( void const *key, lfds710_pal_uint_t *hash );
7
8
9
10
11
12 /****************************************************************************/
13 #pragma warning( disable : 4100 )
14
15 void libtest_tests_hash_a_fail_and_overwrite_on_existing_key( struct lfds710_list_asu_state *list_of_logical_processors, struct libshared_memory_state *ms, enum lfds710_misc_validity *dvs )
16 {
17   enum lfds710_hash_a_insert_result
18     apr;
19
20   struct lfds710_hash_a_element
21     hae_one,
22     hae_two;
23
24   struct lfds710_hash_a_state
25     has;
26
27   struct lfds710_btree_au_state
28     *baus;
29
30   LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
31   LFDS710_PAL_ASSERT( ms != NULL );
32   LFDS710_PAL_ASSERT( dvs != NULL );
33
34   *dvs = LFDS710_MISC_VALIDITY_VALID;
35
36   baus = libshared_memory_alloc_from_unknown_node( ms, sizeof(struct lfds710_btree_au_state) * 10, LFDS710_PAL_ATOMIC_ISOLATION_IN_BYTES );
37
38   // TRD : fail on overwrite
39   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 );
40
41   LFDS710_HASH_A_SET_KEY_IN_ELEMENT( hae_one, 1 );
42   LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( hae_one, 0 );
43   apr = lfds710_hash_a_insert( &has, &hae_one, NULL );
44
45   if( apr != LFDS710_HASH_A_PUT_RESULT_SUCCESS )
46     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
47
48   LFDS710_HASH_A_SET_KEY_IN_ELEMENT( hae_two, 1 );
49   LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( hae_two, 1 );
50   apr = lfds710_hash_a_insert( &has, &hae_two, NULL );
51
52   if( apr != LFDS710_HASH_A_PUT_RESULT_FAILURE_EXISTING_KEY )
53     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
54
55   lfds710_hash_a_cleanup( &has, NULL );
56
57   // TRD : success on overwrite
58   lfds710_hash_a_init_valid_on_current_logical_core( &has, baus, 10, key_compare_function, key_hash_function, LFDS710_HASH_A_EXISTING_KEY_OVERWRITE, NULL );
59
60   LFDS710_HASH_A_SET_KEY_IN_ELEMENT( hae_one, 1 );
61   LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( hae_one, 1 );
62   apr = lfds710_hash_a_insert( &has, &hae_one, NULL );
63
64   if( apr != LFDS710_HASH_A_PUT_RESULT_SUCCESS )
65     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
66
67   LFDS710_HASH_A_SET_KEY_IN_ELEMENT( hae_two, 1 );
68   LFDS710_HASH_A_SET_VALUE_IN_ELEMENT( hae_two, 1 );
69   apr = lfds710_hash_a_insert( &has, &hae_two, NULL );
70
71   if( apr != LFDS710_HASH_A_PUT_RESULT_SUCCESS_OVERWRITE )
72     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
73
74   lfds710_hash_a_cleanup( &has, NULL );
75
76   return;
77 }
78
79 #pragma warning( default : 4100 )
80
81
82
83
84
85 /****************************************************************************/
86 static int key_compare_function( void const *new_key, void const *key_in_tree )
87 {
88   int
89     cr = 0;
90
91   // TRD : key_new can be any value in its range
92   // TRD : key_in_tree can be any value in its range
93
94   if( (lfds710_pal_uint_t) new_key < (lfds710_pal_uint_t) key_in_tree )
95     cr = -1;
96
97   if( (lfds710_pal_uint_t) new_key > (lfds710_pal_uint_t) key_in_tree )
98     cr = 1;
99
100   return cr;
101 }
102
103
104
105
106
107 /****************************************************************************/
108 static void key_hash_function( void const *key, lfds710_pal_uint_t *hash )
109 {
110   // TRD : key can be NULL
111   LFDS710_PAL_ASSERT( hash != NULL );
112
113   *hash = 0;
114
115   /* TRD : this function iterates over the user data
116            and we are using the void pointer *as* key data
117            so here we need to pass in the addy of key
118   */
119
120   LFDS710_HASH_A_HASH_FUNCTION( (void *) &key, sizeof(lfds710_pal_uint_t), *hash );
121
122   return;
123 }
124