]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.0.0/test/src/test_lfds700_btree_addonly_unbalanced_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_btree_addonly_unbalanced_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
7
8
9
10
11 /****************************************************************************/
12 void test_lfds700_btree_au_fail_and_overwrite_on_existing_key()
13 {
14   enum lfds700_btree_au_insert_result
15     alr;
16
17   enum lfds700_misc_validity
18     dvs = LFDS700_MISC_VALIDITY_VALID;
19
20   struct lfds700_btree_au_element
21     baue_one,
22     baue_two,
23     *existing_baue;
24
25   struct lfds700_btree_au_state
26     baus;
27
28   struct lfds700_misc_prng_state
29     ps;
30
31   void
32     *value;
33
34   /* TRD : the random_adds tests with fail and overwrite don't (can't, not in a performant manner)
35            test that the fail and/or overwrite of user data has *actually* happened - they use the
36            return value from the link function call, rather than empirically observing the final
37            state of the tree
38
39            as such, we now have a couple of single threaded tests where we check that the user data
40            value really is being modified (or not modified, as the case may be)
41   */
42
43   internal_display_test_name( "Fail and overwrite on existing key" );
44
45   lfds700_misc_prng_init( &ps );
46
47   /* TRD : so, we make a tree which is fail on existing
48            add one element, with a known user data
49            we then try to add the same key again, with a different user data
50            the call should fail, and then we get the element by its key
51            and check its user data is unchanged
52            (and confirm the failed link returned the correct existing_baue)
53            that's the first test done
54   */
55
56   lfds700_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS700_BTREE_AU_EXISTING_KEY_FAIL, NULL );
57
58   LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( baue_one, 0 );
59   LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_one, 1 );
60   alr = lfds700_btree_au_insert( &baus, &baue_one, NULL, &ps );
61
62   if( alr != LFDS700_BTREE_AU_INSERT_RESULT_SUCCESS )
63     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
64
65   LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( baue_two, 0 );
66   LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_two, 2 );
67   alr = lfds700_btree_au_insert( &baus, &baue_two, &existing_baue, &ps );
68
69   if( alr != LFDS700_BTREE_AU_INSERT_RESULT_FAILURE_EXISTING_KEY )
70     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
71
72   if( existing_baue != &baue_one )
73     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
74
75   value = LFDS700_BTREE_AU_GET_VALUE_FROM_ELEMENT( *existing_baue );
76
77   if( (void *) (lfds700_pal_uint_t) value != (void *) (lfds700_pal_uint_t) 1 )
78     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
79
80   lfds700_btree_au_cleanup( &baus, NULL );
81
82   /* TRD : second test, make a tree which is overwrite on existing
83            add one element, with a known user data
84            we then try to add the same key again, with a different user data
85            the call should succeed, and then we get the element by its key
86            and check its user data is changed
87            (and confirm the failed link returned the correct existing_baue)
88            that's the secondtest done
89   */
90
91   lfds700_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS700_BTREE_AU_EXISTING_KEY_OVERWRITE, NULL );
92
93   LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( baue_one, 0 );
94   LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_one, 1 );
95   alr = lfds700_btree_au_insert( &baus, &baue_one, NULL, &ps );
96
97   if( alr != LFDS700_BTREE_AU_INSERT_RESULT_SUCCESS )
98     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
99
100   LFDS700_BTREE_AU_SET_KEY_IN_ELEMENT( baue_two, 0 );
101   LFDS700_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_two, 2 );
102   alr = lfds700_btree_au_insert( &baus, &baue_two, NULL, &ps );
103
104   if( alr != LFDS700_BTREE_AU_INSERT_RESULT_SUCCESS_OVERWRITE )
105     dvs = LFDS700_MISC_VALIDITY_INVALID_TEST_DATA;
106
107   lfds700_btree_au_cleanup( &baus, NULL );
108
109   // TRD : print the test result
110   internal_display_test_result( 1, "btree_au", dvs );
111
112   return;
113 }
114
115
116
117
118
119 /****************************************************************************/
120 #pragma warning( disable : 4100 )
121
122 static int key_compare_function( void const *new_key, void const *key_in_tree )
123 {
124   int
125     cr = 0;
126
127   // TRD : key_new can be any value in its range
128   // TRD : key_in_tree can be any value in its range
129
130   if( (lfds700_pal_uint_t) new_key < (lfds700_pal_uint_t) key_in_tree )
131     cr = -1;
132
133   if( (lfds700_pal_uint_t) new_key > (lfds700_pal_uint_t) key_in_tree )
134     cr = 1;
135
136   return( cr );
137 }
138
139 #pragma warning( default : 4100 )
140