Merge "Add an API for 'create,window' event." into devel/master
[platform/core/uifw/dali-adaptor.git] / automated-tests / src / dali-adaptor-internal / utc-Dali-LRUCacheContainer.cpp
1 /*
2  * Copyright (c) 2014 Samsung Electronics Co., Ltd.
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  *
16  */
17
18 #include <iostream>
19 #include <string>
20
21 #include <dali-test-suite-utils.h>
22 #include <dali/internal/text/text-abstraction/plugin/lru-cache-container.h>
23
24 using namespace Dali::TextAbstraction::Internal;
25 using TestLRUCacheIntInt    = LRUCacheContainer<int, int>;
26 using TestLRUCacheIntString = LRUCacheContainer<int, std::string>;
27
28 namespace
29 {
30 template<typename K, typename E>
31 void TestLRUCacheExist(LRUCacheContainer<K, E>& cache, const K& key, bool expectExist, const char* location)
32 {
33   auto iter = cache.Find(key);
34   DALI_TEST_EQUALS((iter != cache.End()), expectExist, location);
35 }
36
37 template<typename K, typename E>
38 void TestLRUCachePop(LRUCacheContainer<K, E>& cache, const E& expectElement, const char* location)
39 {
40   auto popElement = cache.Pop();
41   DALI_TEST_EQUALS(popElement, expectElement, location);
42 }
43 } // namespace
44
45 void utc_dali_internal_lru_cache_container_startup(void)
46 {
47   test_return_value = TET_UNDEF;
48 }
49
50 void utc_dali_internal_lru_cache_container_cleanup(void)
51 {
52   test_return_value = TET_PASS;
53 }
54
55 int UtcDaliLRUCacheContainerPushPopTest(void)
56 {
57   TestLRUCacheIntInt cache(3);
58
59   tet_infoline("Test LRUCache Push and Pop");
60
61   DALI_TEST_EQUALS(cache.IsEmpty(), true, TEST_LOCATION);
62   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
63
64   cache.Push(1111, 111);
65   DALI_TEST_EQUALS(cache.IsEmpty(), false, TEST_LOCATION);
66
67   cache.Push(2222, 222);
68   cache.Push(3333, 333);
69   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
70
71   cache.Push(4444, 444);
72   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
73
74   TestLRUCacheExist(cache, 1111, false, TEST_LOCATION);
75   TestLRUCacheExist(cache, 2222, true, TEST_LOCATION);
76   TestLRUCacheExist(cache, 3333, true, TEST_LOCATION);
77   TestLRUCacheExist(cache, 4444, true, TEST_LOCATION);
78
79   TestLRUCachePop(cache, 222, TEST_LOCATION);
80   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
81
82   TestLRUCachePop(cache, 333, TEST_LOCATION);
83   DALI_TEST_EQUALS(cache.IsEmpty(), false, TEST_LOCATION);
84   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
85
86   cache.Push(5555, 555);
87   cache.Push(6666, 666);
88
89   // Replace exist key
90   cache.Push(5555, 777);
91   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
92
93   // Change element
94   DALI_TEST_EQUALS(cache.Get(5555), 777, TEST_LOCATION);
95   cache.Get(5555) = 888;
96   DALI_TEST_EQUALS(cache.Get(5555), 888, TEST_LOCATION);
97
98   TestLRUCachePop(cache, 444, TEST_LOCATION);
99
100   TestLRUCacheExist(cache, 2222, false, TEST_LOCATION);
101   TestLRUCacheExist(cache, 3333, false, TEST_LOCATION);
102   TestLRUCacheExist(cache, 4444, false, TEST_LOCATION);
103
104   TestLRUCachePop(cache, 666, TEST_LOCATION);
105   TestLRUCachePop(cache, 888, TEST_LOCATION);
106   DALI_TEST_EQUALS(cache.IsEmpty(), true, TEST_LOCATION);
107
108   END_TEST;
109 }
110
111 int UtcDaliLRUCacheContainerPushPopTest2(void)
112 {
113   TestLRUCacheIntString cache(3);
114
115   tet_infoline("Test LRUCache Push and Pop 2");
116
117   DALI_TEST_EQUALS(cache.IsEmpty(), true, TEST_LOCATION);
118   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
119
120   cache.Push(1111, "111");
121   DALI_TEST_EQUALS(cache.IsEmpty(), false, TEST_LOCATION);
122
123   cache.Push(2222, "222");
124   cache.Push(3333, "333");
125   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
126
127   cache.Push(4444, "444");
128   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
129
130   TestLRUCacheExist(cache, 1111, false, TEST_LOCATION);
131   TestLRUCacheExist(cache, 2222, true, TEST_LOCATION);
132   TestLRUCacheExist(cache, 3333, true, TEST_LOCATION);
133   TestLRUCacheExist(cache, 4444, true, TEST_LOCATION);
134
135   TestLRUCachePop(cache, std::string("222"), TEST_LOCATION);
136   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
137
138   TestLRUCachePop(cache, std::string("333"), TEST_LOCATION);
139   DALI_TEST_EQUALS(cache.IsEmpty(), false, TEST_LOCATION);
140   DALI_TEST_EQUALS(cache.IsFull(), false, TEST_LOCATION);
141
142   cache.Push(5555, "555");
143   cache.Push(6666, "666");
144
145   // Replace exist key
146   cache.Push(5555, "777");
147   DALI_TEST_EQUALS(cache.IsFull(), true, TEST_LOCATION);
148
149   // Change element
150   DALI_TEST_EQUALS(cache.Get(5555), "777", TEST_LOCATION);
151   cache.Get(5555) = "888";
152   DALI_TEST_EQUALS(cache.Get(5555), "888", TEST_LOCATION);
153
154   TestLRUCachePop(cache, std::string("444"), TEST_LOCATION);
155
156   TestLRUCacheExist(cache, 2222, false, TEST_LOCATION);
157   TestLRUCacheExist(cache, 3333, false, TEST_LOCATION);
158   TestLRUCacheExist(cache, 4444, false, TEST_LOCATION);
159
160   TestLRUCachePop(cache, std::string("666"), TEST_LOCATION);
161   TestLRUCachePop(cache, std::string("888"), TEST_LOCATION);
162   DALI_TEST_EQUALS(cache.IsEmpty(), true, TEST_LOCATION);
163
164   END_TEST;
165 }
166
167 int UtcDaliLRUCacheContainerPopEmptyNegative(void)
168 {
169   TestLRUCacheIntInt cache(3);
170
171   tet_infoline("Test LRUCache Pop empty");
172
173   try
174   {
175     cache.Pop();
176     DALI_TEST_CHECK(false); // Should not get here
177   }
178   catch(...)
179   {
180     DALI_TEST_CHECK(true); // Asserted
181   }
182
183   END_TEST;
184 }
185
186 int UtcDaliLRUCacheContainerGetInvalidNegative(void)
187 {
188   TestLRUCacheIntInt cache(3);
189
190   tet_infoline("Test LRUCache Get with invalid key");
191
192   cache.Push(111, 1);
193   cache.Push(222, 2);
194   cache.Push(333, 3);
195   cache.Push(444, 4);
196
197   try
198   {
199     cache.Get(111);
200     DALI_TEST_CHECK(false); // Should not get here
201   }
202   catch(...)
203   {
204     DALI_TEST_CHECK(true); // Asserted
205   }
206
207   END_TEST;
208 }