The Android Open Source Project | 23580ca | 2008-10-21 07:00:00 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2007 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include <stdlib.h> |
| 18 | #undef NDEBUG |
| 19 | #include <assert.h> |
| 20 | #include "symtab.h" |
| 21 | |
| 22 | int |
| 23 | test_symtab() |
| 24 | { |
| 25 | SymbolTable *tab; |
| 26 | void *cookie; |
| 27 | int ret; |
| 28 | |
| 29 | /* Test creation */ |
| 30 | tab = createSymbolTable(); |
| 31 | assert(tab != NULL); |
| 32 | |
| 33 | /* Smoke-test deletion */ |
| 34 | deleteSymbolTable(tab); |
| 35 | |
| 36 | |
| 37 | tab = createSymbolTable(); |
| 38 | assert(tab != NULL); |
| 39 | |
| 40 | |
| 41 | /* table parameter must be non-NULL. */ |
| 42 | ret = addToSymbolTable(NULL, NULL, 0, NULL); |
| 43 | assert(ret < 0); |
| 44 | |
| 45 | /* symbol parameter must be non-NULL. */ |
| 46 | ret = addToSymbolTable(tab, NULL, 0, NULL); |
| 47 | assert(ret < 0); |
| 48 | |
| 49 | /* cookie parameter must be non-NULL. */ |
| 50 | ret = addToSymbolTable(tab, "null", 0, NULL); |
| 51 | assert(ret < 0); |
| 52 | |
| 53 | |
| 54 | /* table parameter must be non-NULL. */ |
| 55 | cookie = findInSymbolTable(NULL, NULL, 0); |
| 56 | assert(cookie == NULL); |
| 57 | |
| 58 | /* symbol parameter must be non-NULL. */ |
| 59 | cookie = findInSymbolTable(tab, NULL, 0); |
| 60 | assert(cookie == NULL); |
| 61 | |
| 62 | |
| 63 | /* Try some actual inserts. |
| 64 | */ |
| 65 | ret = addToSymbolTable(tab, "one", 0, (void *)1); |
| 66 | assert(ret == 0); |
| 67 | |
| 68 | ret = addToSymbolTable(tab, "two", 0, (void *)2); |
| 69 | assert(ret == 0); |
| 70 | |
| 71 | ret = addToSymbolTable(tab, "three", 0, (void *)3); |
| 72 | assert(ret == 0); |
| 73 | |
| 74 | /* Try some lookups. |
| 75 | */ |
| 76 | cookie = findInSymbolTable(tab, "one", 0); |
| 77 | assert((int)cookie == 1); |
| 78 | |
| 79 | cookie = findInSymbolTable(tab, "two", 0); |
| 80 | assert((int)cookie == 2); |
| 81 | |
| 82 | cookie = findInSymbolTable(tab, "three", 0); |
| 83 | assert((int)cookie == 3); |
| 84 | |
| 85 | /* Try to insert something that's already there. |
| 86 | */ |
| 87 | ret = addToSymbolTable(tab, "one", 0, (void *)1111); |
| 88 | assert(ret < 0); |
| 89 | |
| 90 | /* Make sure that the failed duplicate insert didn't |
| 91 | * clobber the original cookie value. |
| 92 | */ |
| 93 | cookie = findInSymbolTable(tab, "one", 0); |
| 94 | assert((int)cookie == 1); |
| 95 | |
| 96 | /* Try looking up something that isn't there. |
| 97 | */ |
| 98 | cookie = findInSymbolTable(tab, "FOUR", 0); |
| 99 | assert(cookie == NULL); |
| 100 | |
| 101 | /* Try looking up something that's similar to an existing entry. |
| 102 | */ |
| 103 | cookie = findInSymbolTable(tab, "on", 0); |
| 104 | assert(cookie == NULL); |
| 105 | |
| 106 | cookie = findInSymbolTable(tab, "onee", 0); |
| 107 | assert(cookie == NULL); |
| 108 | |
| 109 | /* Test flags. |
| 110 | * Try inserting something with a different flag. |
| 111 | */ |
| 112 | ret = addToSymbolTable(tab, "ten", 333, (void *)10); |
| 113 | assert(ret == 0); |
| 114 | |
| 115 | /* Make sure it's there. |
| 116 | */ |
| 117 | cookie = findInSymbolTable(tab, "ten", 333); |
| 118 | assert((int)cookie == 10); |
| 119 | |
| 120 | /* Make sure it's not there when looked up with a different flag. |
| 121 | */ |
| 122 | cookie = findInSymbolTable(tab, "ten", 0); |
| 123 | assert(cookie == NULL); |
| 124 | |
| 125 | /* Try inserting something that has the same name as something |
| 126 | * with a different flag. |
| 127 | */ |
| 128 | ret = addToSymbolTable(tab, "one", 333, (void *)11); |
| 129 | assert(ret == 0); |
| 130 | |
| 131 | /* Make sure the new entry exists. |
| 132 | */ |
| 133 | cookie = findInSymbolTable(tab, "one", 333); |
| 134 | assert((int)cookie == 11); |
| 135 | |
| 136 | /* Make sure the old entry still has the right value. |
| 137 | */ |
| 138 | cookie = findInSymbolTable(tab, "one", 0); |
| 139 | assert((int)cookie == 1); |
| 140 | |
| 141 | /* Try deleting again, now that there's stuff in the table. |
| 142 | */ |
| 143 | deleteSymbolTable(tab); |
| 144 | |
| 145 | return 0; |
| 146 | } |