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 |
E. Scott Daniels | fc5c77b | 2020-02-21 13:24:29 -0500 | [diff] [blame] | 31 | #define NO_EMULATION |
| 32 | #define NO_PRIVATE_HEADERS |
E. Scott Daniels | d710957 | 2019-04-18 14:01:16 +0000 | [diff] [blame] | 33 | |
E. Scott Daniels | fc5c77b | 2020-02-21 13:24:29 -0500 | [diff] [blame] | 34 | #include <rmr.h> |
| 35 | #include <rmr_agnostic.h> |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 36 | #include "test_support.c" |
E. Scott Daniels | fc5c77b | 2020-02-21 13:24:29 -0500 | [diff] [blame] | 37 | #include "rmr_symtab.h" |
| 38 | |
| 39 | #include "symtab.c" // module under test |
| 40 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 41 | |
| 42 | int state = GOOD; // overall pass/fail state 0==fail |
| 43 | int counter; // global counter for for-each tests |
| 44 | |
| 45 | |
| 46 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 47 | static int fetch( void* st, char* key, int class, int expected ) { |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 48 | char* val; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 49 | int error = 0; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 50 | |
| 51 | val = rmr_sym_get( st, key, class ); |
| 52 | if( val ) { |
| 53 | fprintf( stderr, "[%s] get returns key=%s val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 54 | if( !expected ) { |
| 55 | state = BAD; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 56 | error = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 57 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 58 | |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 59 | } else { |
| 60 | fprintf( stderr, "[%s] string key fetch return nil\n", expected ? "FAIL" : "OK" ); |
| 61 | if( expected ) { |
| 62 | state = BAD; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 63 | error = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 64 | } |
| 65 | } |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 66 | |
| 67 | return error; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 68 | } |
| 69 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 70 | static int nfetch( void* st, int key, int expected ) { |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 71 | char* val; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 72 | int error = 0; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 73 | |
| 74 | val = rmr_sym_pull( st, key ); |
| 75 | if( val ) { |
| 76 | fprintf( stderr, "[%s] get returns key=%d val=%s\n", !expected ? "FAIL" : "OK", key, val ); |
| 77 | if( !expected ) { |
| 78 | state = BAD; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 79 | error = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 80 | } |
| 81 | } else { |
| 82 | fprintf( stderr, "[%s] get return nil for key=%d\n", expected ? "FAIL" : "OK", key ); |
| 83 | if( expected ) { |
| 84 | state = BAD; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 85 | error = 1; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 86 | } |
| 87 | } |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 88 | |
| 89 | return error; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 90 | } |
| 91 | |
| 92 | |
| 93 | /* |
| 94 | Driven by foreach class -- just incr the counter. |
| 95 | */ |
| 96 | static void each_counter( void* a, void* b, const char* c, void* d, void* e ) { |
| 97 | counter++; |
| 98 | } |
| 99 | |
| 100 | int main( ) { |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 101 | void* st; |
| 102 | char* foo = "foo"; |
| 103 | char* bar = "bar"; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 104 | char* goo = "goo"; // name not in symtab |
| 105 | int i; |
| 106 | int class = 1; |
| 107 | int s; |
| 108 | void* p; |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 109 | int errors = 0; |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 110 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 111 | st = rmr_sym_alloc( 10 ); // alloc with small value to force adjustment inside |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 112 | errors += fail_if_nil( st, "symtab pointer" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 113 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 114 | s = rmr_sym_put( st, foo, class, bar ); // add entry with string key; returns 1 if it was inserted |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 115 | errors += fail_if_false( s, "insert foo existed" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 116 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 117 | s = rmr_sym_put( st, foo, class+1, bar ); // add to table with a different class |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 118 | errors += fail_if_false( s, "insert foo existed" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 119 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 120 | s = rmr_sym_put( st, foo, class, bar ); // inserted above, should return not inserted (0) |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 121 | errors += fail_if_true( s, "insert foo existed" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 122 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 123 | errors += fetch( st, foo, class, 1 ); |
| 124 | errors += fetch( st, goo, class, 0 ); // fetch non existant |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 125 | 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] | 126 | rmr_sym_dump( st ); |
| 127 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 128 | for( i = 2000; i < 3000; i++ ) { // bunch of dummy things to force chains in the table |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 129 | rmr_sym_map( st, i, foo ); // add entry with unsigned integer key |
| 130 | } |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 131 | 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] | 132 | rmr_sym_ndel( st, 2001 ); // force a numeric key delete |
| 133 | rmr_sym_ndel( st, 12001 ); // delete numeric key not there |
| 134 | |
| 135 | s = rmr_sym_map( st, 1234, foo ); // add known entries with unsigned integer key |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 136 | errors += fail_if_false( s, "numeric add of key 1234 should not have existed" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 137 | s = rmr_sym_map( st, 2345, bar ); |
| 138 | fail_if_true( s, "numeric add of key 2345 should have existed" ); |
| 139 | |
| 140 | counter = 0; |
| 141 | rmr_sym_foreach_class( st, 0, each_counter, NULL ); |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 142 | errors += fail_if_false( counter, "expected counter after foreach to be non-zero" ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 143 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 144 | errors += nfetch( st, 1234, 1 ); |
| 145 | errors += nfetch( st, 2345, 1 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 146 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 147 | rmr_sym_del( st, foo, 0 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 148 | |
E. Scott Daniels | 8790bf0 | 2019-04-23 12:59:28 +0000 | [diff] [blame] | 149 | rmr_sym_stats( st, 0 ); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 150 | |
| 151 | rmr_sym_free( NULL ); // ensure it doesn't barf when given a nil pointer |
| 152 | rmr_sym_free( st ); |
| 153 | |
E. Scott Daniels | 77526eb | 2020-09-17 16:39:31 -0400 | [diff] [blame^] | 154 | test_summary( errors, "symtab tests" ); |
| 155 | if( state + errors == 0 ) { |
| 156 | fprintf( stderr, "<PASS> all symtab tests were OK\n\n" ); |
| 157 | } else { |
| 158 | fprintf( stderr, "<FAIL> %d errors in symtab code\n\n", errors ); |
| 159 | } |
| 160 | |
| 161 | return !!(state + errors); |
Ashwin Sridharan | fd9cc7a | 2019-04-03 16:47:02 -0400 | [diff] [blame] | 162 | } |
| 163 | |