E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
E. Scott Daniels | 11838bc | 2021-04-22 16:34:08 -0400 | [diff] [blame] | 3 | Copyright (c) 2019-2021 Nokia |
| 4 | Copyright (c) 2018-2021 AT&T Intellectual Property. |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 10 | http://www.apache.org/licenses/LICENSE-2.0 |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 23 | Abstract: This is the static function that should be included by |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 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 | |
E. Scott Daniels | 412d53d | 2019-05-20 20:00:52 +0000 | [diff] [blame] | 31 | #include "rmr_symtab.h" |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 32 | // -- parent must include if needed #include "../src/common/src/symtab.c" |
| 33 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 34 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 35 | #ifndef GOOD |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 36 | #define GOOD 0 |
| 37 | #define BAD 1 |
| 38 | #endif |
| 39 | |
| 40 | int symtab_state = GOOD; // overall pass/fail state 0==fail |
| 41 | int symtab_counter = 0; // global counter for for-each tests |
| 42 | |
| 43 | static void st_fetch( void* st, char* key, int class, int expected ) { |
| 44 | char* val; |
| 45 | |
| 46 | val = rmr_sym_get( st, key, class ); |
| 47 | if( val ) { |
| 48 | fprintf( stderr, "<%s> get returns key=%s val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 49 | if( !expected ) { |
| 50 | symtab_state = BAD; |
| 51 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 52 | |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 53 | } else { |
| 54 | fprintf( stderr, "<%s> string key st_fetch return nil\n", expected ? "FAIL" : "OK" ); |
| 55 | if( expected ) { |
| 56 | symtab_state = BAD; |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |
| 61 | static void st_nfetch( void* st, int key, int expected ) { |
| 62 | char* val; |
| 63 | |
| 64 | val = rmr_sym_pull( st, key ); |
| 65 | if( val ) { |
| 66 | fprintf( stderr, "<%s> get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 67 | if( !expected ) { |
| 68 | symtab_state = BAD; |
| 69 | } |
| 70 | } else { |
| 71 | fprintf( stderr, "<%s> get return nil for key=%d\n", expected ? "FAIL" : "OK", key ); |
| 72 | if( expected ) { |
| 73 | symtab_state = BAD; |
| 74 | } |
| 75 | } |
| 76 | } |
| 77 | |
| 78 | |
| 79 | /* |
| 80 | Driven by foreach class -- just incr the counter. |
| 81 | */ |
| 82 | static void each_counter( void* a, void* b, const char* c, void* d, void* e ) { |
| 83 | symtab_counter++; |
| 84 | } |
| 85 | |
| 86 | static int symtab_test( ) { |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 87 | void* st; |
| 88 | char* foo = "foo"; |
| 89 | char* bar = "bar"; |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 90 | char* goo = "goo"; // name not in symtab |
| 91 | int i; |
| 92 | int class = 1; |
| 93 | int s; |
| 94 | void* p; |
| 95 | int errors = 0; |
| 96 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 97 | st = rmr_sym_alloc( 10 ); // alloc with small value to force adjustment inside |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 98 | errors += fail_if_nil( st, "symtab pointer" ); |
| 99 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 100 | s = rmr_sym_put( st, foo, class, bar ); // add entry with string key; returns 1 if it was inserted |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 101 | errors += fail_if_false( s, "insert foo existed" ); |
| 102 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 103 | s = rmr_sym_put( st, foo, class+1, bar ); // add to table with a different class |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 104 | errors += fail_if_false( s, "insert foo existed" ); |
| 105 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 106 | s = rmr_sym_put( st, foo, class, bar ); // inserted above, should return not inserted (0) |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 107 | errors += fail_if_true( s, "insert foo existed" ); |
| 108 | |
| 109 | st_fetch( st, foo, class, 1 ); |
| 110 | st_fetch( st, goo, class, 0 ); // st_fetch non existant |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 111 | rmr_sym_stats( st, 4 ); // early stats at verbose level 4 so chatter is minimised |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 112 | rmr_sym_dump( st ); |
| 113 | |
| 114 | for( i = 2000; i < 3000; i++ ) { // bunch of dummy things to force chains in the table |
| 115 | rmr_sym_map( st, i, foo ); // add entry with unsigned integer key |
| 116 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 117 | rmr_sym_stats( st, 0 ); // just the small facts to verify the 1000 we stuffed in |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 118 | rmr_sym_ndel( st, 2001 ); // force a numeric key delete |
| 119 | rmr_sym_ndel( st, 12001 ); // delete numeric key not there |
| 120 | |
| 121 | s = rmr_sym_map( st, 1234, foo ); // add known entries with unsigned integer key |
| 122 | errors += fail_if_false( s, "numeric add of key 1234 should not have existed" ); |
| 123 | s = rmr_sym_map( st, 2345, bar ); |
| 124 | errors += fail_if_true( s, "numeric add of key 2345 should have existed" ); |
| 125 | |
| 126 | symtab_counter = 0; |
| 127 | rmr_sym_foreach_class( st, 0, each_counter, NULL ); |
| 128 | errors += fail_if_false( symtab_counter, "expected counter after foreach to be non-zero" ); |
| 129 | |
| 130 | st_nfetch( st, 1234, 1 ); |
| 131 | st_nfetch( st, 2345, 1 ); |
| 132 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 133 | rmr_sym_del( st, foo, 0 ); // drive for coverage |
| 134 | rmr_sym_stats( st, 0 ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 135 | |
| 136 | rmr_sym_free( NULL ); // ensure it doesn't barf when given a nil pointer |
| 137 | rmr_sym_free( st ); |
| 138 | |
E. Scott Daniels | 11838bc | 2021-04-22 16:34:08 -0400 | [diff] [blame] | 139 | return errors + (!!symtab_state ); |
E. Scott Daniels | 8dd4641 | 2019-04-16 20:47:54 +0000 | [diff] [blame] | 140 | } |
| 141 | |