]> pd.if.org Git - liblfds/blob - liblfds/liblfds7.1.0/test_and_benchmark/libtest/src/libtest_tests/libtest_tests_btree_addonly_unbalanced_random_adds_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_btree_addonly_unbalanced_random_adds_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
7
8
9
10
11 /****************************************************************************/
12 #pragma warning( disable : 4100 )
13
14 void libtest_tests_btree_au_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 )
15 {
16   enum lfds710_btree_au_insert_result
17     alr;
18
19   struct lfds710_btree_au_element
20     baue_one,
21     baue_two,
22     *existing_baue;
23
24   struct lfds710_btree_au_state
25     baus;
26
27   void
28     *value;
29
30   LFDS710_PAL_ASSERT( list_of_logical_processors != NULL );
31   LFDS710_PAL_ASSERT( ms != NULL );
32   LFDS710_PAL_ASSERT( dvs != NULL );
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   *dvs = LFDS710_MISC_VALIDITY_VALID;
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   lfds710_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS710_BTREE_AU_EXISTING_KEY_FAIL, NULL );
57
58   LFDS710_BTREE_AU_SET_KEY_IN_ELEMENT( baue_one, 0 );
59   LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_one, 1 );
60   alr = lfds710_btree_au_insert( &baus, &baue_one, NULL );
61
62   if( alr != LFDS710_BTREE_AU_INSERT_RESULT_SUCCESS )
63     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
64
65   LFDS710_BTREE_AU_SET_KEY_IN_ELEMENT( baue_two, 0 );
66   LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_two, 2 );
67   alr = lfds710_btree_au_insert( &baus, &baue_two, &existing_baue );
68
69   if( alr != LFDS710_BTREE_AU_INSERT_RESULT_FAILURE_EXISTING_KEY )
70     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
71
72   if( existing_baue != &baue_one )
73     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
74
75   value = LFDS710_BTREE_AU_GET_VALUE_FROM_ELEMENT( *existing_baue );
76
77   if( (void *) (lfds710_pal_uint_t) value != (void *) (lfds710_pal_uint_t) 1 )
78     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
79
80   lfds710_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   lfds710_btree_au_init_valid_on_current_logical_core( &baus, key_compare_function, LFDS710_BTREE_AU_EXISTING_KEY_OVERWRITE, NULL );
92
93   LFDS710_BTREE_AU_SET_KEY_IN_ELEMENT( baue_one, 0 );
94   LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_one, 1 );
95   alr = lfds710_btree_au_insert( &baus, &baue_one, NULL );
96
97   if( alr != LFDS710_BTREE_AU_INSERT_RESULT_SUCCESS )
98     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
99
100   LFDS710_BTREE_AU_SET_KEY_IN_ELEMENT( baue_two, 0 );
101   LFDS710_BTREE_AU_SET_VALUE_IN_ELEMENT( baue_two, 2 );
102   alr = lfds710_btree_au_insert( &baus, &baue_two, NULL );
103
104   if( alr != LFDS710_BTREE_AU_INSERT_RESULT_SUCCESS_OVERWRITE )
105     *dvs = LFDS710_MISC_VALIDITY_INVALID_TEST_DATA;
106
107   lfds710_btree_au_cleanup( &baus, NULL );
108
109   return;
110 }
111
112 #pragma warning( default : 4100 )
113
114
115
116
117
118 /****************************************************************************/
119 static int key_compare_function( void const *new_key, void const *key_in_tree )
120 {
121   int
122     cr = 0;
123
124   // TRD : key_new can be any value in its range
125   // TRD : key_in_tree can be any value in its range
126
127   if( (lfds710_pal_uint_t) new_key < (lfds710_pal_uint_t) key_in_tree )
128     cr = -1;
129
130   if( (lfds710_pal_uint_t) new_key > (lfds710_pal_uint_t) key_in_tree )
131     cr = 1;
132
133   return cr;
134 }
135