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