int main(int argc, char* argv[]) { LRUCache<3> c; cout << "c.Insert(1, 1)" << endl; c.Insert(1, 1); cout << "c.Insert(1, 10)" << endl; c.Insert(1, 10); int val; cout << "c.Lookup(2, val)" << endl; assert(!c.Lookup(2, &val)); cout << "c.Lookup(1, val)" << endl; assert(c.Lookup(1, &val)); assert(val == 1); c.Erase(1); assert(!c.Lookup(1, &val)); c.Insert(2, 2); c.Insert(3, 3); c.Insert(4, 4); c.Insert(5, 5); assert(!c.Lookup(1, &val)); return 0; }
void testLRUCache() { int i; LRUCache c; int t[100]; for (i = 0; i < 100; ++i) t[i] = 1000+i; char* k[100]; for (i = 0; i < 100; ++i) { k[i] = new char[5]; sprintf(k[i], "k%03d", i); } bool r; void* p; const char* k2; r = c.Lookup("k050", p); assert(!r); c.SetAt("k050", &t[50]); r = c.Lookup("k050", p); assert(r); assert(p == &t[50]); for (i = 0; i < 100; ++i) c.SetAt(k[i], &t[i]); r = c.getLRU(0, k2, p); assert(r); assert(strcmp(k2, "k000") == 0); assert(p == &t[0]); c.Touch("k000"); r = c.getLRU(0, k2, p); assert(r); assert(strcmp(k2, "k001") == 0); assert(p == &t[1]); r = c.getLRU(1, k2, p); assert(r); assert(strcmp(k2, "k002") == 0); assert(p == &t[2]); c.Remove("k001"); r = c.getLRU(0, k2, p); assert(r); assert(strcmp(k2, "k002") == 0); assert(p == &t[2]); for (i = 0; i < 98; ++i) { r = c.getLRU(0, k2, p); assert(r); assert(strcmp(k2, k[2+i]) == 0); assert(p == &t[2+i]); c.Remove(k2); } assert(c.GetCount() == 1); r = c.getLRU(0, k2, p); assert(r); assert(strcmp(k2, "k000") == 0); assert(p == &t[0]); assert(!c.getLRU(1, k2, p)); }