]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/test_lfds700_hash_addonly_random_adds_fail_and_overwrite.c
Initial import (all versions, including the new 7.1.0)
[liblfds] / liblfds / liblfds7.0.0 / test / src / test_lfds700_hash_addonly_random_adds_fail_and_overwrite.c
1 /***** includes *****/
2 #include "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, lfds700_pal_uint_t *hash );
7
8
9
10
11
12 /****************************************************************************/
13 void test_lfds700_hash_a_fail_and_overwrite_on_existing_key()
14 {
15   enum lfds700_hash_a_insert_result
16     apr;
17
18   enum lfds700_misc_validity
19     dvs = LFDS700_MISC_VALIDITY_VALID;
20
21   struct lfds700_hash_a_element
22     hae_one,
23     hae_two;
24
25   struct lfds700_hash_a_state
26     has;
27
28   struct lfds700_btree_au_state
29     *baus;
30
31   struct lfds700_misc_prng_state
32     ps;
33
34   internal_display_test_name( "Fail and overwrite on existing key" );
35
36   lfds700_misc_prng_init( &ps );
37
38   baus = util_aligned_malloc( sizeof(struct lfds700_btree_au_state) * 10, LFDS700_PAL_ATOMIC_ISOLATION_IN_BYTES );
39
40   // TRD : fail on overwrite
41   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 );
42
43   LFDS700_HASH_A_SET_KEY_IN_ELEMENT( hae_one, 1 );
44   LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( hae_one, 0 );
45   apr = lfds700_hash_a_insert( &has, &hae_one, NULL, &ps );
46
47   if( apr != LFDS700_HASH_A_PUT_RESULT_SUCCESS )
48     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
49
50   LFDS700_HASH_A_SET_KEY_IN_ELEMENT( hae_two, 1 );
51   LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( hae_two, 1 );
52   apr = lfds700_hash_a_insert( &has, &hae_two, NULL, &ps );
53
54   if( apr != LFDS700_HASH_A_PUT_RESULT_FAILURE_EXISTING_KEY )
55     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
56
57   lfds700_hash_a_cleanup( &has, NULL );
58
59   // TRD : success on overwrite
60   lfds700_hash_a_init_valid_on_current_logical_core( &has, baus, 10, key_compare_function, key_hash_function, LFDS700_HASH_A_EXISTING_KEY_OVERWRITE, NULL );
61
62   LFDS700_HASH_A_SET_KEY_IN_ELEMENT( hae_one, 1 );
63   LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( hae_one, 1 );
64   apr = lfds700_hash_a_insert( &has, &hae_one, NULL, &ps );
65
66   if( apr != LFDS700_HASH_A_PUT_RESULT_SUCCESS )
67     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
68
69   LFDS700_HASH_A_SET_KEY_IN_ELEMENT( hae_two, 1 );
70   LFDS700_HASH_A_SET_VALUE_IN_ELEMENT( hae_two, 1 );
71   apr = lfds700_hash_a_insert( &has, &hae_two, NULL, &ps );
72
73   if( apr != LFDS700_HASH_A_PUT_RESULT_SUCCESS_OVERWRITE )
74     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
75
76   lfds700_hash_a_cleanup( &has, NULL );
77
78   util_aligned_free( baus );
79
80   // TRD : print the test result
81   internal_display_test_result( 1, "hash_a", dvs );
82
83   return;
84 }
85
86
87
88
89
90 /****************************************************************************/
91 #pragma warning( disable : 4100 )
92
93 static int key_compare_function( void const *new_key, void const *key_in_tree )
94 {
95   int
96     cr = 0;
97
98   // TRD : key_new can be any value in its range
99   // TRD : key_in_tree can be any value in its range
100
101   if( (lfds700_pal_uint_t) new_key < (lfds700_pal_uint_t) key_in_tree )
102     cr = -1;
103
104   if( (lfds700_pal_uint_t) new_key > (lfds700_pal_uint_t) key_in_tree )
105     cr = 1;
106
107   return( cr );
108 }
109
110 #pragma warning( default : 4100 )
111
112
113
114
115
116 /****************************************************************************/
117 #pragma warning( disable : 4100 )
118
119 static void key_hash_function( void const *key, lfds700_pal_uint_t *hash )
120 {
121   // TRD : key can be NULL
122   assert( hash != NULL );
123
124   *hash = 0;
125
126   /* TRD : this function iterates over the user data
127            and we are using the void pointer *as* key data
128            so here we need to pass in the addy of key
129   */
130
131   LFDS700_HASH_A_32BIT_HASH_FUNCTION( (void *) &key, sizeof(lfds700_pal_uint_t), *hash );
132
133   return;
134 }
135
136 #pragma warning( default : 4100 )
137