1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402
|
// © 2016 and later: Unicode, Inc. and others.
// License & terms of use: http://www.unicode.org/copyright.html
/*
*******************************************************************************
* Copyright (C) 2015, International Business Machines Corporation and *
* others. All Rights Reserved. *
*******************************************************************************
*
* File UNIFIEDCACHETEST.CPP
*
********************************************************************************
*/
#include "cstring.h"
#include "intltest.h"
#include "unifiedcache.h"
#include "unicode/datefmt.h"
class UCTItem : public SharedObject {
public:
char *value;
UCTItem(const char *x) : value(nullptr) {
value = uprv_strdup(x);
}
virtual ~UCTItem() {
uprv_free(value);
}
};
class UCTItem2 : public SharedObject {
};
U_NAMESPACE_BEGIN
template<> U_EXPORT
const UCTItem *LocaleCacheKey<UCTItem>::createObject(
const void *context, UErrorCode &status) const {
const UnifiedCache *cacheContext = static_cast<const UnifiedCache *>(context);
if (uprv_strcmp(fLoc.getName(), "zh") == 0) {
status = U_MISSING_RESOURCE_ERROR;
return nullptr;
}
if (uprv_strcmp(fLoc.getLanguage(), fLoc.getName()) != 0) {
const UCTItem *item = nullptr;
if (cacheContext == nullptr) {
UnifiedCache::getByLocale(fLoc.getLanguage(), item, status);
} else {
cacheContext->get(LocaleCacheKey<UCTItem>(fLoc.getLanguage()), item, status);
}
if (U_FAILURE(status)) {
return nullptr;
}
return item;
}
UCTItem *result = new UCTItem(fLoc.getName());
result->addRef();
return result;
}
template<> U_EXPORT
const UCTItem2 *LocaleCacheKey<UCTItem2>::createObject(
const void * /*unused*/, UErrorCode & /*status*/) const {
return nullptr;
}
U_NAMESPACE_END
class UnifiedCacheTest : public IntlTest {
public:
UnifiedCacheTest() {
}
void runIndexedTest(int32_t index, UBool exec, const char*& name, char* par = nullptr) override;
private:
void TestEvictionPolicy();
void TestBounded();
void TestBasic();
void TestError();
void TestHashEquals();
void TestEvictionUnderStress();
};
void UnifiedCacheTest::runIndexedTest(int32_t index, UBool exec, const char* &name, char* /*par*/) {
TESTCASE_AUTO_BEGIN;
TESTCASE_AUTO(TestEvictionPolicy);
TESTCASE_AUTO(TestBounded);
TESTCASE_AUTO(TestBasic);
TESTCASE_AUTO(TestError);
TESTCASE_AUTO(TestHashEquals);
TESTCASE_AUTO(TestEvictionUnderStress);
TESTCASE_AUTO_END;
}
void UnifiedCacheTest::TestEvictionUnderStress() {
#if !UCONFIG_NO_FORMATTING
int32_t localeCount;
const Locale *locales = DateFormat::getAvailableLocales(localeCount);
UErrorCode status = U_ZERO_ERROR;
const UnifiedCache *cache = UnifiedCache::getInstance(status);
int64_t evictedCountBefore = cache->autoEvictedCount();
for (int32_t i = 0; i < localeCount; ++i) {
LocalPointer<DateFormat> ptr(DateFormat::createInstanceForSkeleton("yMd", locales[i], status));
}
int64_t evictedCountAfter = cache->autoEvictedCount();
if (evictedCountBefore == evictedCountAfter) {
dataerrln("%s:%d Items should have been evicted from cache",
__FILE__, __LINE__);
}
#endif /* #if !UCONFIG_NO_FORMATTING */
}
void UnifiedCacheTest::TestEvictionPolicy() {
UErrorCode status = U_ZERO_ERROR;
// We have to call this first or else calling the UnifiedCache
// ctor will fail. This is by design to deter clients from using the
// cache API incorrectly by creating their own cache instances.
UnifiedCache::getInstance(status);
// We create our own local UnifiedCache instance to ensure we have
// complete control over it. Real clients should never ever create
// their own cache!
UnifiedCache cache(status);
assertSuccess("", status);
// Don't allow unused entries to exceed more than 100% of in use entries.
cache.setEvictionPolicy(0, 100, status);
static const char *locales[] = {
"1", "2", "3", "4", "5", "6", "7", "8", "9", "10",
"11", "12", "13", "14", "15", "16", "17", "18", "19", "20"};
const UCTItem *usedReferences[] = {nullptr, nullptr, nullptr, nullptr, nullptr};
const UCTItem *unusedReference = nullptr;
// Add 5 in-use entries
for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) {
cache.get(
LocaleCacheKey<UCTItem>(locales[i]),
&cache,
usedReferences[i],
status);
}
// Add 10 not in use entries.
for (int32_t i = 0; i < 10; ++i) {
cache.get(
LocaleCacheKey<UCTItem>(
locales[i + UPRV_LENGTHOF(usedReferences)]),
&cache,
unusedReference,
status);
}
unusedReference->removeRef();
// unused count not to exceed in use count
assertEquals("T1", UPRV_LENGTHOF(usedReferences), cache.unusedCount());
assertEquals("T2", 2*UPRV_LENGTHOF(usedReferences), cache.keyCount());
// Free up those used entries.
for (int32_t i = 0; i < UPRV_LENGTHOF(usedReferences); i++) {
usedReferences[i]->removeRef();
}
// This should free up all cache items
assertEquals("T3", 0, cache.keyCount());
assertSuccess("T4", status);
}
void UnifiedCacheTest::TestBounded() {
UErrorCode status = U_ZERO_ERROR;
// We have to call this first or else calling the UnifiedCache
// ctor will fail. This is by design to deter clients from using the
// cache API incorrectly by creating their own cache instances.
UnifiedCache::getInstance(status);
// We create our own local UnifiedCache instance to ensure we have
// complete control over it. Real clients should never ever create
// their own cache!
UnifiedCache cache(status);
assertSuccess("T0", status);
// Maximum unused count is 3.
cache.setEvictionPolicy(3, 0, status);
// Our cache will hold up to 3 unused key-value pairs
// We test the following invariants:
// 1. unusedCount <= 3
// 2. cache->get(X) always returns the same reference as long as caller
// already holds references to that same object.
// We first add 5 key-value pairs with two distinct values, "en" and "fr"
// keeping all those references.
const UCTItem *en = nullptr;
const UCTItem *enGb = nullptr;
const UCTItem *enUs = nullptr;
const UCTItem *fr = nullptr;
const UCTItem *frFr = nullptr;
cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, enUs, status);
cache.get(LocaleCacheKey<UCTItem>("en"), &cache, en, status);
assertEquals("T1", 1, cache.unusedCount());
cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, enGb, status);
cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, frFr, status);
cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, fr, status);
// Client holds two unique references, "en" and "fr" the other three
// entries are eligible for eviction.
assertEquals("T2", 3, cache.unusedCount());
assertEquals("T3", 5, cache.keyCount());
// Exercise cache more but don't hold the references except for
// the last one. At the end of this, we will hold references to one
// additional distinct value, so we will have references to 3 distinct
// values.
const UCTItem *throwAway = nullptr;
cache.get(LocaleCacheKey<UCTItem>("zn_AA"), &cache, throwAway, status);
cache.get(LocaleCacheKey<UCTItem>("sr_AA"), &cache, throwAway, status);
cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status);
const UCTItem *deAu(throwAway);
deAu->addRef();
// Client holds three unique references, "en", "fr", "de" although we
// could have a total of 8 entries in the cache maxUnusedCount == 3
// so we have only 6 entries.
assertEquals("T4", 3, cache.unusedCount());
assertEquals("T5", 6, cache.keyCount());
// For all the references we have, cache must continue to return
// those same references (#2)
cache.get(LocaleCacheKey<UCTItem>("en"), &cache, throwAway, status);
if (throwAway != en) {
errln("T6: Expected en to resolve to the same object.");
}
cache.get(LocaleCacheKey<UCTItem>("en_US"), &cache, throwAway, status);
if (throwAway != enUs) {
errln("T7: Expected enUs to resolve to the same object.");
}
cache.get(LocaleCacheKey<UCTItem>("en_GB"), &cache, throwAway, status);
if (throwAway != enGb) {
errln("T8: Expected enGb to resolve to the same object.");
}
cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status);
if (throwAway != frFr) {
errln("T9: Expected frFr to resolve to the same object.");
}
cache.get(LocaleCacheKey<UCTItem>("fr_FR"), &cache, throwAway, status);
cache.get(LocaleCacheKey<UCTItem>("fr"), &cache, throwAway, status);
if (throwAway != fr) {
errln("T10: Expected fr to resolve to the same object.");
}
cache.get(LocaleCacheKey<UCTItem>("de_AU"), &cache, throwAway, status);
if (throwAway != deAu) {
errln("T11: Expected deAu to resolve to the same object.");
}
assertEquals("T12", 3, cache.unusedCount());
assertEquals("T13", 6, cache.keyCount());
// Now we hold a references to two more distinct values. Cache size
// should grow to 8.
const UCTItem *es = nullptr;
const UCTItem *ru = nullptr;
cache.get(LocaleCacheKey<UCTItem>("es"), &cache, es, status);
cache.get(LocaleCacheKey<UCTItem>("ru"), &cache, ru, status);
assertEquals("T14", 3, cache.unusedCount());
assertEquals("T15", 8, cache.keyCount());
// Now release all the references we hold except for
// es, ru, and en
SharedObject::clearPtr(enGb);
SharedObject::clearPtr(enUs);
SharedObject::clearPtr(fr);
SharedObject::clearPtr(frFr);
SharedObject::clearPtr(deAu);
SharedObject::clearPtr(es);
SharedObject::clearPtr(ru);
SharedObject::clearPtr(en);
SharedObject::clearPtr(throwAway);
// Size of cache should magically drop to 3.
assertEquals("T16", 3, cache.unusedCount());
assertEquals("T17", 3, cache.keyCount());
// Be sure nothing happens setting the eviction policy in the middle of
// a run.
cache.setEvictionPolicy(3, 0, status);
assertSuccess("T18", status);
}
void UnifiedCacheTest::TestBasic() {
UErrorCode status = U_ZERO_ERROR;
const UnifiedCache *cache = UnifiedCache::getInstance(status);
assertSuccess("", status);
cache->flush();
int32_t baseCount = cache->keyCount();
const UCTItem *en = nullptr;
const UCTItem *enGb = nullptr;
const UCTItem *enGb2 = nullptr;
const UCTItem *enUs = nullptr;
const UCTItem *fr = nullptr;
const UCTItem *frFr = nullptr;
cache->get(LocaleCacheKey<UCTItem>("en"), en, status);
cache->get(LocaleCacheKey<UCTItem>("en_US"), enUs, status);
cache->get(LocaleCacheKey<UCTItem>("en_GB"), enGb, status);
cache->get(LocaleCacheKey<UCTItem>("fr_FR"), frFr, status);
cache->get(LocaleCacheKey<UCTItem>("fr"), fr, status);
cache->get(LocaleCacheKey<UCTItem>("en_GB"), enGb2, status);
SharedObject::clearPtr(enGb2);
if (enGb != enUs) {
errln("Expected en_GB and en_US to resolve to same object.");
}
if (fr != frFr) {
errln("Expected fr and fr_FR to resolve to same object.");
}
if (enGb == fr) {
errln("Expected en_GB and fr to return different objects.");
}
assertSuccess("T1", status);
// en_US, en_GB, en share one object; fr_FR and fr don't share.
// 5 keys in all.
assertEquals("T2", baseCount + 5, cache->keyCount());
SharedObject::clearPtr(enGb);
cache->flush();
// Only 2 unique values in the cache. flushing trims cache down
// to this minimum size.
assertEquals("T3", baseCount + 2, cache->keyCount());
SharedObject::clearPtr(enUs);
SharedObject::clearPtr(en);
cache->flush();
// With en_GB and en_US and en cleared there are no more hard references to
// the "en" object, so it gets flushed and the keys that refer to it
// get removed from the cache. Now we have just one unique value, fr, in
// the cache
assertEquals("T4", baseCount + 1, cache->keyCount());
SharedObject::clearPtr(fr);
cache->flush();
assertEquals("T5", baseCount + 1, cache->keyCount());
SharedObject::clearPtr(frFr);
cache->flush();
assertEquals("T6", baseCount + 0, cache->keyCount());
assertSuccess("T7", status);
}
void UnifiedCacheTest::TestError() {
UErrorCode status = U_ZERO_ERROR;
const UnifiedCache *cache = UnifiedCache::getInstance(status);
assertSuccess("", status);
cache->flush();
int32_t baseCount = cache->keyCount();
const UCTItem *zh = nullptr;
const UCTItem *zhTw = nullptr;
const UCTItem *zhHk = nullptr;
status = U_ZERO_ERROR;
cache->get(LocaleCacheKey<UCTItem>("zh"), zh, status);
if (status != U_MISSING_RESOURCE_ERROR) {
errln("Expected U_MISSING_RESOURCE_ERROR");
}
status = U_ZERO_ERROR;
cache->get(LocaleCacheKey<UCTItem>("zh_TW"), zhTw, status);
if (status != U_MISSING_RESOURCE_ERROR) {
errln("Expected U_MISSING_RESOURCE_ERROR");
}
status = U_ZERO_ERROR;
cache->get(LocaleCacheKey<UCTItem>("zh_HK"), zhHk, status);
if (status != U_MISSING_RESOURCE_ERROR) {
errln("Expected U_MISSING_RESOURCE_ERROR");
}
// 3 keys in cache zh, zhTW, zhHk all pointing to error placeholders
assertEquals("", baseCount + 3, cache->keyCount());
cache->flush();
// error placeholders have no hard references so they always get flushed.
assertEquals("", baseCount + 0, cache->keyCount());
}
void UnifiedCacheTest::TestHashEquals() {
LocaleCacheKey<UCTItem> key1("en_US");
LocaleCacheKey<UCTItem> key2("en_US");
LocaleCacheKey<UCTItem> diffKey1("en_UT");
LocaleCacheKey<UCTItem2> diffKey2("en_US");
assertTrue("", key1.hashCode() == key2.hashCode());
assertTrue("", key1.hashCode() != diffKey1.hashCode());
assertTrue("", key1.hashCode() != diffKey2.hashCode());
assertTrue("", diffKey1.hashCode() != diffKey2.hashCode());
assertTrue("", key1 == key2);
assertTrue("", key1 != diffKey1);
assertTrue("", key1 != diffKey2);
assertTrue("", diffKey1 != diffKey2);
}
extern IntlTest *createUnifiedCacheTest() {
return new UnifiedCacheTest();
}
|