2 ==================================================================================
3 Copyright (c) 2019 Nokia
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
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
10 http://www.apache.org/licenses/LICENSE-2.0
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 ==================================================================================
22 Mnemonic: symtab_test.c
23 Abstract: This is the unit test module that will drive tests against
24 the symbol table portion of RMr. Run with:
25 ksh unit_test.ksh symtab_test.c
27 Author: E. Scott Daniels
30 #include "../src/common/include/rmr_symtab.h"
31 #include "../src/common/src/symtab.c"
33 #include "test_support.c"
35 int state = GOOD; // overall pass/fail state 0==fail
36 int counter; // global counter for for-each tests
40 static void fetch( void* st, char* key, int class, int expected ) {
43 val = rmr_sym_get( st, key, class );
45 fprintf( stderr, "[%s] get returns key=%s val=%s\n", !expected ? "FAIL" : "OK", key, val );
51 fprintf( stderr, "[%s] string key fetch return nil\n", expected ? "FAIL" : "OK" );
58 static void nfetch( void* st, int key, int expected ) {
61 val = rmr_sym_pull( st, key );
63 fprintf( stderr, "[%s] get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val );
68 fprintf( stderr, "[%s] get return nil for key=%d\n", expected ? "FAIL" : "OK", key );
77 Driven by foreach class -- just incr the counter.
79 static void each_counter( void* a, void* b, const char* c, void* d, void* e ) {
87 char* goo = "goo"; // name not in symtab
93 st = rmr_sym_alloc( 10 ); // alloc with small value to force adjustment inside
94 fail_if_nil( st, "symtab pointer" );
96 s = rmr_sym_put( st, foo, class, bar ); // add entry with string key; returns 1 if it was inserted
97 fail_if_false( s, "insert foo existed" );
99 s = rmr_sym_put( st, foo, class+1, bar ); // add to table with a different class
100 fail_if_false( s, "insert foo existed" );
102 s = rmr_sym_put( st, foo, class, bar ); // inserted above, should return not inserted (0)
103 fail_if_true( s, "insert foo existed" );
105 fetch( st, foo, class, 1 );
106 fetch( st, goo, class, 0 ); // fetch non existant
107 rmr_sym_stats( st, 4 ); // early stats at verbose level 4 so chatter is minimised
110 for( i = 2000; i < 3000; i++ ) { // bunch of dummy things to force chains in the table
111 rmr_sym_map( st, i, foo ); // add entry with unsigned integer key
113 rmr_sym_stats( st, 0 ); // just the small facts to verify the 1000 we stuffed in
114 rmr_sym_ndel( st, 2001 ); // force a numeric key delete
115 rmr_sym_ndel( st, 12001 ); // delete numeric key not there
117 s = rmr_sym_map( st, 1234, foo ); // add known entries with unsigned integer key
118 fail_if_false( s, "numeric add of key 1234 should not have existed" );
119 s = rmr_sym_map( st, 2345, bar );
120 fail_if_true( s, "numeric add of key 2345 should have existed" );
123 rmr_sym_foreach_class( st, 0, each_counter, NULL );
124 fail_if_false( counter, "expected counter after foreach to be non-zero" );
126 nfetch( st, 1234, 1 );
127 nfetch( st, 2345, 1 );
130 rmr_sym_del( st, foo, 0 );
132 rmr_sym_stats( st, 0 );
134 rmr_sym_free( NULL ); // ensure it doesn't barf when given a nil pointer