| 71 | } |
| 72 | |
| 73 | void test() |
| 74 | { |
| 75 | ConstObjectPtr res; |
| 76 | IntDataPtr v = new IntData(1); |
| 77 | |
| 78 | /// limit the pool to fit only one integer. |
| 79 | ObjectPoolPtr pool = new ObjectPool( v->Object::memoryUsage() ); |
| 80 | |
| 81 | Cache cache( get, hash, 1000, pool ); |
| 82 | |
| 83 | BOOST_CHECK_EQUAL( pool.get(), cache.objectPool() ); |
| 84 | |
| 85 | BOOST_CHECK_EQUAL( size_t(1000), cache.getMaxComputations() ); |
| 86 | cache.setMaxComputations( 100 ); |
| 87 | BOOST_CHECK_EQUAL( size_t(100), cache.getMaxComputations() ); |
| 88 | BOOST_CHECK_EQUAL( size_t(0), cache.cachedComputations() ); |
| 89 | |
| 90 | /// cache should return NULL on never computed values (hash is unknown) |
| 91 | res = cache.get( ComputationParams(2), Cache::NullIfMissing ); |
| 92 | BOOST_CHECK( !res ); |
| 93 | // this is weird, but because we are using LRUCache with a dummie and we don't |
| 94 | /// want to do two queries, it ends up registering that computation with the default hash... |
| 95 | BOOST_CHECK_EQUAL( size_t(1), cache.cachedComputations() ); |
| 96 | |
| 97 | /// computes a value (default value) |
| 98 | res = cache.get( ComputationParams(2) ); |
| 99 | BOOST_CHECK( res ); |
| 100 | BOOST_CHECK_EQUAL( size_t(1), cache.cachedComputations() ); |
| 101 | |
| 102 | /// computes a value (explicit) |
| 103 | res = cache.get( ComputationParams(3), Cache::ComputeIfMissing ); |
| 104 | BOOST_CHECK( res ); |
| 105 | BOOST_CHECK_EQUAL( size_t(2), cache.cachedComputations() ); |
| 106 | |
| 107 | /// Cache should have evicted one value due to the memory limit |
| 108 | /// of the ObjectPool. |
| 109 | ConstObjectPtr res2 = cache.get( ComputationParams( 2 ), Cache::NullIfMissing ); |
| 110 | ConstObjectPtr res3 = cache.get( ComputationParams( 3 ), Cache::NullIfMissing ); |
| 111 | BOOST_CHECK( !(res2 && res3) ); |
| 112 | BOOST_CHECK( res2 || res3 ); |
| 113 | |
| 114 | /// now increase memory limit to two IntData objects |
| 115 | pool->setMaxMemoryUsage( v->Object::memoryUsage() * 2 ); |
| 116 | |
| 117 | /// computes two new values |
| 118 | cache.get( ComputationParams(4) ); |
| 119 | cache.get( ComputationParams(5) ); |
| 120 | BOOST_CHECK_EQUAL( size_t(4), cache.cachedComputations() ); |
| 121 | res2 = cache.get( ComputationParams( 2 ), Cache::NullIfMissing ); |
| 122 | res3 = cache.get( ComputationParams( 3 ), Cache::NullIfMissing ); |
| 123 | ConstObjectPtr res4 = cache.get( ComputationParams( 4 ), Cache::NullIfMissing ); |
| 124 | ConstObjectPtr res5 = cache.get( ComputationParams( 5 ), Cache::NullIfMissing ); |
| 125 | /// Cache should have two values present, two missing |
| 126 | BOOST_CHECK_EQUAL( (int)!res2 + (int)!res3 + (int)!res4 + (int)!res5, 2 ); |
| 127 | |
| 128 | /// clears the all values |
| 129 | cache.clear(); |
| 130 | pool->clear(); |
nothing calls this directly
no test coverage detected