Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 1 | /* |
| 2 | ================================================================================== |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 3 | Copyright (c) 2019 Nokia |
| 4 | Copyright (c) 2018-2019 AT&T Intellectual Property. |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [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 |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [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_test.c |
| 23 | Abstract: This is the unit test module that will drive tests against |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 24 | the symbol table portion of RMr. Run with: |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 25 | ksh unit_test.ksh symtab_test.c |
| 26 | Date: 1 April 2019 |
| 27 | Author: E. Scott Daniels |
| 28 | */ |
| 29 | |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 30 | #define NO_DUMMY_RMR 1 // no dummy rmr functions; we don't pull in rmr.h or agnostic.h |
| 31 | |
E. Scott Daniels | 412d53d | 2019-05-20 20:00:52 +0000 | [diff] [blame] | 32 | #include "rmr_symtab.h" |
| 33 | #include "symtab.c" |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 34 | |
| 35 | #include "test_support.c" |
| 36 | |
| 37 | int state = GOOD; // overall pass/fail state 0==fail |
| 38 | int counter; // global counter for for-each tests |
| 39 | |
| 40 | |
| 41 | |
| 42 | static void fetch( void* st, char* key, int class, int expected ) { |
| 43 | char* val; |
| 44 | |
| 45 | val = rmr_sym_get( st, key, class ); |
| 46 | if( val ) { |
| 47 | fprintf( stderr, "[%s] get returns key=%s val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 48 | if( !expected ) { |
| 49 | state = BAD; |
| 50 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 51 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 52 | } else { |
| 53 | fprintf( stderr, "[%s] string key fetch return nil\n", expected ? "FAIL" : "OK" ); |
| 54 | if( expected ) { |
| 55 | state = BAD; |
| 56 | } |
| 57 | } |
| 58 | } |
| 59 | |
| 60 | static void nfetch( void* st, int key, int expected ) { |
| 61 | char* val; |
| 62 | |
| 63 | val = rmr_sym_pull( st, key ); |
| 64 | if( val ) { |
| 65 | fprintf( stderr, "[%s] get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 66 | if( !expected ) { |
| 67 | state = BAD; |
| 68 | } |
| 69 | } else { |
| 70 | fprintf( stderr, "[%s] get return nil for key=%d\n", expected ? "FAIL" : "OK", key ); |
| 71 | if( expected ) { |
| 72 | state = BAD; |
| 73 | } |
| 74 | } |
| 75 | } |
| 76 | |
| 77 | |
| 78 | /* |
| 79 | Driven by foreach class -- just incr the counter. |
| 80 | */ |
| 81 | static void each_counter( void* a, void* b, const char* c, void* d, void* e ) { |
| 82 | counter++; |
| 83 | } |
| 84 | |
| 85 | int main( ) { |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 86 | void* st; |
| 87 | char* foo = "foo"; |
| 88 | char* bar = "bar"; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 89 | char* goo = "goo"; // name not in symtab |
| 90 | int i; |
| 91 | int class = 1; |
| 92 | int s; |
| 93 | void* p; |
| 94 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 95 | st = rmr_sym_alloc( 10 ); // alloc with small value to force adjustment inside |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 96 | fail_if_nil( st, "symtab pointer" ); |
| 97 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 98 | s = rmr_sym_put( st, foo, class, bar ); // add entry with string key; returns 1 if it was inserted |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 99 | fail_if_false( s, "insert foo existed" ); |
| 100 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 101 | s = rmr_sym_put( st, foo, class+1, bar ); // add to table with a different class |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 102 | fail_if_false( s, "insert foo existed" ); |
| 103 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 104 | s = rmr_sym_put( st, foo, class, bar ); // inserted above, should return not inserted (0) |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 105 | fail_if_true( s, "insert foo existed" ); |
| 106 | |
| 107 | fetch( st, foo, class, 1 ); |
| 108 | fetch( st, goo, class, 0 ); // fetch non existant |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 109 | rmr_sym_stats( st, 4 ); // early stats at verbose level 4 so chatter is minimised |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 110 | rmr_sym_dump( st ); |
| 111 | |
| 112 | for( i = 2000; i < 3000; i++ ) { // bunch of dummy things to force chains in the table |
| 113 | rmr_sym_map( st, i, foo ); // add entry with unsigned integer key |
| 114 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 115 | rmr_sym_stats( st, 0 ); // just the small facts to verify the 1000 we stuffed in |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 116 | rmr_sym_ndel( st, 2001 ); // force a numeric key delete |
| 117 | rmr_sym_ndel( st, 12001 ); // delete numeric key not there |
| 118 | |
| 119 | s = rmr_sym_map( st, 1234, foo ); // add known entries with unsigned integer key |
| 120 | fail_if_false( s, "numeric add of key 1234 should not have existed" ); |
| 121 | s = rmr_sym_map( st, 2345, bar ); |
| 122 | fail_if_true( s, "numeric add of key 2345 should have existed" ); |
| 123 | |
| 124 | counter = 0; |
| 125 | rmr_sym_foreach_class( st, 0, each_counter, NULL ); |
| 126 | fail_if_false( counter, "expected counter after foreach to be non-zero" ); |
| 127 | |
| 128 | nfetch( st, 1234, 1 ); |
| 129 | nfetch( st, 2345, 1 ); |
| 130 | |
| 131 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 132 | rmr_sym_del( st, foo, 0 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 133 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 134 | rmr_sym_stats( st, 0 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [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 | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 139 | return state; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 140 | } |
| 141 | |