blob: 19713178c450df09acaf64f62e972c7d1e4dcd59 [file] [log] [blame]
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04001/*
2==================================================================================
E. Scott Daniels8790bf02019-04-23 12:59:28 +00003 Copyright (c) 2019 Nokia
4 Copyright (c) 2018-2019 AT&T Intellectual Property.
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -04005
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
E. Scott Daniels8790bf02019-04-23 12:59:28 +000010 http://www.apache.org/licenses/LICENSE-2.0
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040011
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_test.c
23 Abstract: This is the unit test module that will drive tests against
E. Scott Daniels8790bf02019-04-23 12:59:28 +000024 the symbol table portion of RMr. Run with:
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040025 ksh unit_test.ksh symtab_test.c
26 Date: 1 April 2019
27 Author: E. Scott Daniels
28*/
29
E. Scott Danielsd7109572019-04-18 14:01:16 +000030#define NO_DUMMY_RMR 1 // no dummy rmr functions; we don't pull in rmr.h or agnostic.h
E. Scott Danielsfc5c77b2020-02-21 13:24:29 -050031#define NO_EMULATION
32#define NO_PRIVATE_HEADERS
E. Scott Danielsd7109572019-04-18 14:01:16 +000033
E. Scott Danielsfc5c77b2020-02-21 13:24:29 -050034#include <rmr.h>
35#include <rmr_agnostic.h>
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040036#include "test_support.c"
E. Scott Danielsfc5c77b2020-02-21 13:24:29 -050037#include "rmr_symtab.h"
38
39#include "symtab.c" // module under test
40
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040041
42int state = GOOD; // overall pass/fail state 0==fail
43int counter; // global counter for for-each tests
44
45
46
47static void fetch( void* st, char* key, int class, int expected ) {
48 char* val;
49
50 val = rmr_sym_get( st, key, class );
51 if( val ) {
52 fprintf( stderr, "[%s] get returns key=%s val=%s\n", !expected ? "FAIL" : "OK", key, val );
53 if( !expected ) {
54 state = BAD;
55 }
E. Scott Daniels8790bf02019-04-23 12:59:28 +000056
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040057 } else {
58 fprintf( stderr, "[%s] string key fetch return nil\n", expected ? "FAIL" : "OK" );
59 if( expected ) {
60 state = BAD;
61 }
62 }
63}
64
65static void nfetch( void* st, int key, int expected ) {
66 char* val;
67
68 val = rmr_sym_pull( st, key );
69 if( val ) {
70 fprintf( stderr, "[%s] get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val );
71 if( !expected ) {
72 state = BAD;
73 }
74 } else {
75 fprintf( stderr, "[%s] get return nil for key=%d\n", expected ? "FAIL" : "OK", key );
76 if( expected ) {
77 state = BAD;
78 }
79 }
80}
81
82
83/*
84 Driven by foreach class -- just incr the counter.
85*/
86static void each_counter( void* a, void* b, const char* c, void* d, void* e ) {
87 counter++;
88}
89
90int main( ) {
E. Scott Daniels8790bf02019-04-23 12:59:28 +000091 void* st;
92 char* foo = "foo";
93 char* bar = "bar";
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -040094 char* goo = "goo"; // name not in symtab
95 int i;
96 int class = 1;
97 int s;
98 void* p;
99
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000100 st = rmr_sym_alloc( 10 ); // alloc with small value to force adjustment inside
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400101 fail_if_nil( st, "symtab pointer" );
102
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000103 s = rmr_sym_put( st, foo, class, bar ); // add entry with string key; returns 1 if it was inserted
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400104 fail_if_false( s, "insert foo existed" );
105
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000106 s = rmr_sym_put( st, foo, class+1, bar ); // add to table with a different class
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400107 fail_if_false( s, "insert foo existed" );
108
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000109 s = rmr_sym_put( st, foo, class, bar ); // inserted above, should return not inserted (0)
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400110 fail_if_true( s, "insert foo existed" );
111
112 fetch( st, foo, class, 1 );
113 fetch( st, goo, class, 0 ); // fetch non existant
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000114 rmr_sym_stats( st, 4 ); // early stats at verbose level 4 so chatter is minimised
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400115 rmr_sym_dump( st );
116
117 for( i = 2000; i < 3000; i++ ) { // bunch of dummy things to force chains in the table
118 rmr_sym_map( st, i, foo ); // add entry with unsigned integer key
119 }
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000120 rmr_sym_stats( st, 0 ); // just the small facts to verify the 1000 we stuffed in
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400121 rmr_sym_ndel( st, 2001 ); // force a numeric key delete
122 rmr_sym_ndel( st, 12001 ); // delete numeric key not there
123
124 s = rmr_sym_map( st, 1234, foo ); // add known entries with unsigned integer key
125 fail_if_false( s, "numeric add of key 1234 should not have existed" );
126 s = rmr_sym_map( st, 2345, bar );
127 fail_if_true( s, "numeric add of key 2345 should have existed" );
128
129 counter = 0;
130 rmr_sym_foreach_class( st, 0, each_counter, NULL );
131 fail_if_false( counter, "expected counter after foreach to be non-zero" );
132
133 nfetch( st, 1234, 1 );
134 nfetch( st, 2345, 1 );
135
136
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000137 rmr_sym_del( st, foo, 0 );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400138
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000139 rmr_sym_stats( st, 0 );
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400140
141 rmr_sym_free( NULL ); // ensure it doesn't barf when given a nil pointer
142 rmr_sym_free( st );
143
E. Scott Daniels8790bf02019-04-23 12:59:28 +0000144 return state;
Ashwin Sridharanfd9cc7a2019-04-03 16:47:02 -0400145}
146