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