TODO: check that the memory was actually free-d better
| 129 | |
| 130 | // TODO: check that the memory was actually free-d better |
| 131 | int |
| 132 | testRefcounting() |
| 133 | { |
| 134 | int ret = 0; |
| 135 | |
| 136 | RefCountCache<ExampleStruct> *cache = new RefCountCache<ExampleStruct>(4); |
| 137 | |
| 138 | // Create and then immediately delete an item |
| 139 | ExampleStruct *to_delete = ExampleStruct::alloc(); |
| 140 | ret |= to_delete->refcount() != 0; |
| 141 | cache->put(1, to_delete); |
| 142 | ret |= to_delete->refcount() != 1; |
| 143 | cache->erase(1); |
| 144 | ret |= to_delete->refcount() != 0; |
| 145 | ret |= to_delete->idx != -1; |
| 146 | |
| 147 | // Set an item in the cache |
| 148 | ExampleStruct *tmp = ExampleStruct::alloc(); |
| 149 | ret |= tmp->refcount() != 0; |
| 150 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 151 | cache->put(static_cast<uint64_t>(1), tmp); |
| 152 | ret |= tmp->refcount() != 1; |
| 153 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 154 | tmp->idx = 1; |
| 155 | |
| 156 | // Grab a pointer to item 1 |
| 157 | Ptr<ExampleStruct> ccitem = cache->get(static_cast<uint64_t>(1)); |
| 158 | ret |= tmp->refcount() != 2; |
| 159 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 160 | |
| 161 | Ptr<ExampleStruct> tmpAfter = cache->get(static_cast<uint64_t>(1)); |
| 162 | ret |= tmp->refcount() != 3; |
| 163 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 164 | |
| 165 | // Delete a single item |
| 166 | cache->erase(1); |
| 167 | ret |= tmp->refcount() != 2; |
| 168 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 169 | // verify that it still isn't in there |
| 170 | ret |= cache->get(1).get() != nullptr; |
| 171 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 172 | ret |= tmpAfter.get()->idx != 1; |
| 173 | printf("ret=%d ref=%d\n", ret, tmp->refcount()); |
| 174 | |
| 175 | delete cache; |
| 176 | |
| 177 | return ret; |
| 178 | } |
| 179 | |
| 180 | int |
| 181 | testclear() |