Add transport provider status to message buffer
[ric-plt/lib/rmr.git] / test / symtab_static_test.c
1 /*
2 ==================================================================================
3             Copyright (c) 2019 Nokia
4             Copyright (c) 2018-2019 AT&T Intellectual Property.
5
6    Licensed under the Apache License, Version 2.0 (the "License");
7    you may not use this file except in compliance with the License.
8    You may obtain a copy of the License at
9
10            http://www.apache.org/licenses/LICENSE-2.0
11
12    Unless required by applicable law or agreed to in writing, software
13    distributed under the License is distributed on an "AS IS" BASIS,
14    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15    See the License for the specific language governing permissions and
16    limitations under the License.
17 ==================================================================================
18 */
19
20
21 /*
22         Mnemonic:       symtab_static_test.c
23         Abstract:       This is the static function that should be included by
24                                 any test that wants to test the symbol table. It must
25                                 be included in the compile, and not built to object.
26
27         Date:           1 April 2019
28         Author:         E. Scott Daniels
29 */
30
31 #include "rmr_symtab.h"
32 //  -- parent must include if needed #include "../src/common/src/symtab.c"
33
34 #include "test_support.c"
35
36 #ifndef GOOD
37 #define GOOD 0
38 #define BAD 1
39 #endif
40
41 int symtab_state = GOOD;                                                        // overall pass/fail state 0==fail
42 int symtab_counter = 0;                                                         // global counter for for-each tests
43
44 static void st_fetch( void* st, char* key, int class, int expected ) {
45         char* val;
46
47         val = rmr_sym_get( st, key, class );
48         if( val ) {
49                 fprintf( stderr, "<%s> get returns key=%s val=%s\n",  !expected ? "FAIL" : "OK", key, val );
50                 if( !expected ) {
51                         symtab_state = BAD;
52                 }
53
54         } else {
55                 fprintf( stderr, "<%s> string key st_fetch return nil\n", expected ? "FAIL" : "OK" );
56                 if( expected ) {
57                         symtab_state = BAD;
58                 }
59         }
60 }
61
62 static void st_nfetch( void* st, int key, int expected ) {
63         char* val;
64
65         val = rmr_sym_pull( st, key );
66         if( val ) {
67                 fprintf( stderr, "<%s> get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val );
68                 if( !expected )  {
69                         symtab_state = BAD;
70                 }
71         } else {
72                 fprintf( stderr, "<%s> get return nil for key=%d\n", expected ? "FAIL" : "OK", key );
73                 if( expected )  {
74                         symtab_state = BAD;
75                 }
76         }
77 }
78
79
80 /*
81         Driven by foreach class -- just incr the counter.
82 */
83 static void each_counter( void* a, void* b, const char* c, void* d, void* e ) {
84         symtab_counter++;
85 }
86
87 static int symtab_test( ) {
88         void*   st;
89         char*   foo = "foo";
90         char*   bar = "bar";
91         char*   goo = "goo";                            // name not in symtab
92         int             i;
93         int             class = 1;
94         int             s;
95         void*   p;
96         int             errors = 0;
97
98         st = rmr_sym_alloc( 10 );                                               // alloc with small value to force adjustment inside
99         errors += fail_if_nil( st, "symtab pointer" );
100
101         s = rmr_sym_put( st, foo, class, bar );                 // add entry with string key; returns 1 if it was inserted
102         errors += fail_if_false( s, "insert foo existed" );
103
104         s = rmr_sym_put( st, foo, class+1, bar );               // add to table with a different class
105         errors += fail_if_false( s, "insert foo existed" );
106
107         s = rmr_sym_put( st, foo, class, bar );                 // inserted above, should return not inserted (0)
108         errors += fail_if_true( s, "insert foo existed" );
109
110         st_fetch( st, foo, class, 1 );
111         st_fetch( st, goo, class, 0 );                                  // st_fetch non existant
112         rmr_sym_stats( st, 4 );                                                 // early stats at verbose level 4 so chatter is minimised
113         rmr_sym_dump( st );
114
115         for( i = 2000; i < 3000; i++ ) {                        // bunch of dummy things to force chains in the table
116                 rmr_sym_map( st, i, foo );                                      // add entry with unsigned integer key
117         }
118         rmr_sym_stats( st, 0 );                                                 // just the small facts to verify the 1000 we stuffed in
119         rmr_sym_ndel( st, 2001 );                                               // force a numeric key delete
120         rmr_sym_ndel( st, 12001 );                                              // delete numeric key not there
121
122         s = rmr_sym_map( st, 1234, foo );                                       // add known entries with unsigned integer key
123         errors += fail_if_false( s, "numeric add of key 1234 should not have existed" );
124         s = rmr_sym_map( st, 2345, bar );
125         errors += fail_if_true( s, "numeric add of key 2345 should have existed" );
126
127         symtab_counter = 0;
128         rmr_sym_foreach_class( st, 0, each_counter, NULL );
129         errors += fail_if_false( symtab_counter, "expected counter after foreach to be non-zero" );
130
131         st_nfetch( st, 1234, 1 );
132         st_nfetch( st, 2345, 1 );
133
134         rmr_sym_del( st, foo, 0 );              // drive for coverage
135         rmr_sym_stats( st, 0 );
136
137         rmr_sym_free( NULL );                   // ensure it doesn't barf when given a nil pointer
138         rmr_sym_free( st );
139
140         return !!( errors + symtab_state );
141 }
142