• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 #include <gtest/gtest.h>
2 #include "WelsList.h"
3 #include "WelsTaskThread.h"
4 
5 using namespace WelsCommon;
6 
TEST(CWelsList,CWelsListOne)7 TEST (CWelsList, CWelsListOne) {
8   CWelsList<int> cTestList;
9   int a = 0;
10 
11   for (int i = 0; i < 60; i++) {
12     ASSERT_TRUE (cTestList.push_back (&a));
13     EXPECT_TRUE (1 == cTestList.size()) << "after push size=" << cTestList.size() ;
14 
15     cTestList.pop_front();
16     EXPECT_TRUE (0 == cTestList.size()) << "after pop size=" << cTestList.size() ;
17   }
18 }
19 
TEST(CWelsList,CWelsListTen)20 TEST (CWelsList, CWelsListTen) {
21   CWelsList<int> cTestList;
22   int a = 0;
23   int* pPointer = &a;
24 
25   for (int j = 0; j < 10; j++) {
26 
27     for (int i = 0; i < 10; i++) {
28       EXPECT_TRUE (i == cTestList.size()) << "before push size=" << cTestList.size() ;
29       ASSERT_TRUE (cTestList.push_back (pPointer));
30     }
31     EXPECT_TRUE (10 == cTestList.size()) << "after push size=" << cTestList.size() ;
32 
33 
34     for (int i = 9; i >= 0; i--) {
35       cTestList.pop_front();
36       EXPECT_TRUE (i == cTestList.size()) << "after pop size=" << cTestList.size() ;
37     }
38   }
39 }
40 
41 
TEST(CWelsList,CWelsList_Null)42 TEST (CWelsList, CWelsList_Null) {
43   CWelsList<int> cTestList;
44   int a = 0;
45   int* pPointer = &a;
46 
47   ASSERT_TRUE (cTestList.push_back (pPointer));
48 
49   pPointer = NULL;
50   EXPECT_FALSE (cTestList.push_back (pPointer));
51 
52   EXPECT_FALSE (cTestList.findNode (pPointer));
53 
54   pPointer = &a;
55   EXPECT_TRUE (cTestList.findNode (pPointer));
56   ASSERT_TRUE (cTestList.push_back (pPointer));
57 }
58 
59 
TEST(CWelsList,CWelsListExpand)60 TEST (CWelsList, CWelsListExpand) {
61   CWelsList<int> cTestList;
62   int a = 0;
63   int* pPointer = &a;
64 
65   const int kiIncreaseNum = (rand() % 100) + 1;
66   const int kiDecreaseNum = rand() % kiIncreaseNum;
67 
68   for (int j = 0; j < 10; j++) {
69 
70     for (int i = 0; i < kiIncreaseNum; i++) {
71       ASSERT_TRUE (cTestList.push_back (pPointer));
72     }
73     EXPECT_TRUE (kiIncreaseNum + j * (kiIncreaseNum - kiDecreaseNum) == cTestList.size()) << "after push size=" <<
74         cTestList.size() ;
75 
76     for (int i = kiDecreaseNum; i > 0; i--) {
77       cTestList.pop_front();
78     }
79     EXPECT_TRUE ((j + 1) * (kiIncreaseNum - kiDecreaseNum) == cTestList.size()) << "after pop size=" << cTestList.size() ;
80   }
81 }
82 
TEST(CWelsList,CWelsListOverPop)83 TEST (CWelsList, CWelsListOverPop) {
84   CWelsList<int> cTestList;
85   int a = 0;
86   int* pPointer = &a;
87 
88   const int kiDecreaseNum = 30000;//(rand() % 65535) + 1;
89   const int kiIncreaseNum = rand() % kiDecreaseNum;
90 
91   EXPECT_TRUE (0 == cTestList.size());
92   cTestList.pop_front();
93   EXPECT_TRUE (0 == cTestList.size());
94 
95   for (int i = 0; i < kiIncreaseNum; i++) {
96     ASSERT_TRUE (cTestList.push_back (pPointer));
97   }
98 
99   for (int i = kiDecreaseNum; i > 0; i--) {
100     cTestList.pop_front();
101   }
102 
103   EXPECT_TRUE (0 == cTestList.size());
104 }
105 
106 
EraseOneInList(CWelsList<int> & cTestList,int * pPointer)107 void EraseOneInList (CWelsList<int>& cTestList, int* pPointer) {
108   int iPrevSize = cTestList.size();
109   EXPECT_TRUE (cTestList.erase (pPointer));
110   EXPECT_TRUE (cTestList.size() == (iPrevSize - 1));
111 }
112 
TEST(CWelsList,CWelsListEraseOne)113 TEST (CWelsList, CWelsListEraseOne) {
114 #define TEST_LEN (4)
115   CWelsList<int> cTestList;
116   int a[TEST_LEN];
117   int* pPointer;
118 
119   for (int i = 0; i < TEST_LEN; i++) {
120     a[i] = i;
121     ASSERT_TRUE (cTestList.push_back (&a[i]));
122   }
123 
124   EXPECT_TRUE (cTestList.size() == TEST_LEN);
125 
126   int iEraseIdx = rand() % TEST_LEN;
127   EraseOneInList (cTestList, &a[iEraseIdx]);
128   EXPECT_TRUE (cTestList.size() == (TEST_LEN - 1));
129 
130   for (int i = 0; i < TEST_LEN; i++) {
131     pPointer = cTestList.begin();
132     cTestList.pop_front();
133     if (!pPointer) {
134       EXPECT_TRUE (cTestList.size() == 0);
135       break;
136     }
137     if (i < iEraseIdx) {
138       EXPECT_TRUE (a[i] == (*pPointer));
139     } else {
140       EXPECT_TRUE (a[i + 1] == (*pPointer));
141     }
142   }
143 
144   EXPECT_TRUE (0 == cTestList.size());
145 }
146 
TEST(CWelsList,CWelsListEraseAll)147 TEST (CWelsList, CWelsListEraseAll) {
148 #define TEST_LEN (4)
149   CWelsList<int> cTestList;
150   int data[TEST_LEN];
151   int eraseidx[TEST_LEN] = {0};
152   int* pPointer;
153 
154   for (int i = 0; i < TEST_LEN; i++) {
155     data[i] = i;
156     ASSERT_TRUE (cTestList.push_back (&data[i]));
157   }
158   EXPECT_TRUE (cTestList.size() == TEST_LEN);
159 
160   int iCurrentLen = TEST_LEN;
161   do {
162     int iEraseIdx = rand() % TEST_LEN;
163     if (0 == eraseidx[iEraseIdx]) {
164       eraseidx[iEraseIdx] = 1;
165       EraseOneInList (cTestList, &data[iEraseIdx]);
166       EXPECT_TRUE (cTestList.size() == (--iCurrentLen));
167     }
168     EXPECT_FALSE (cTestList.erase (&data[iEraseIdx]));
169 
170     if (cTestList.size() == 0) {
171       break;
172     }
173 
174     pPointer = cTestList.begin();
175     for (int i = 0; i < TEST_LEN; i++) {
176       if ((*pPointer) == data[i]) {
177         EXPECT_TRUE (eraseidx[i] == 0);
178         break;
179       }
180     }
181   } while (cTestList.size());
182   EXPECT_TRUE (0 == cTestList.size());
183 }
184 
TEST(CWelsList,CWelsListEraseAndExpand)185 TEST (CWelsList, CWelsListEraseAndExpand) {
186 #define TEST_LEN_10 (10)
187   CWelsList<int> cTestList;
188   int data[TEST_LEN_10];
189   int eraseidx[TEST_LEN_10] = {0};
190   int* pPointer;
191 
192   for (int i = 0; i < TEST_LEN_10; i++) {
193     data[i] = i;
194     ASSERT_TRUE (cTestList.push_back (&data[i]));
195   }
196   EXPECT_TRUE (cTestList.size() == TEST_LEN_10);
197 
198   //erase some
199   int iCurrentLen = TEST_LEN_10;
200   do {
201     int iEraseIdx = rand() % TEST_LEN_10;
202     if (0 == eraseidx[iEraseIdx]) {
203       eraseidx[iEraseIdx] = 1;
204       EraseOneInList (cTestList, &data[iEraseIdx]);
205       EXPECT_TRUE (cTestList.size() == (--iCurrentLen));
206     }
207     EXPECT_FALSE (cTestList.erase (&data[iEraseIdx]));
208 
209     if (cTestList.size() == 0) {
210       break;
211     }
212 
213     pPointer = cTestList.begin();
214     for (int i = 0; i < TEST_LEN_10; i++) {
215       if ((*pPointer) == data[i]) {
216         EXPECT_TRUE (eraseidx[i] == 0);
217         break;
218       }
219     }
220   } while (iCurrentLen > (TEST_LEN_10 / 2));
221   EXPECT_TRUE (iCurrentLen == cTestList.size());
222 
223   //expand
224   int iAddLen = rand() % 65535;
225   for (int i = 0; i < iAddLen; i++) {
226     ASSERT_TRUE (cTestList.push_back (&data[0]));
227   }
228   EXPECT_TRUE ((iCurrentLen + iAddLen) == cTestList.size());
229   EraseOneInList (cTestList, &data[0]);
230   EXPECT_TRUE ((iCurrentLen + iAddLen - 1) == cTestList.size()) << (iCurrentLen + iAddLen - 1)  << "_" <<
231       cTestList.size();
232 
233   //clear up
234   do {
235     pPointer = cTestList.begin();
236     EXPECT_TRUE (NULL != pPointer);
237     cTestList.pop_front();
238   } while (cTestList.size());
239 
240   EXPECT_TRUE (0 == cTestList.size());
241 }
242 
TEST(CWelsNonDuplicatedList,CWelsNonDuplicatedList)243 TEST (CWelsNonDuplicatedList, CWelsNonDuplicatedList) {
244   int32_t a, b, c;
245   CWelsNonDuplicatedList<int32_t> cNonDuplicatedIntList;
246   int32_t* pObject1 = &a;
247   int32_t* pObject2 = &b;
248   int32_t* pObject3 = &c;
249 
250   //initial adding
251   ASSERT_TRUE (cNonDuplicatedIntList.push_back (pObject1));
252   ASSERT_TRUE (cNonDuplicatedIntList.push_back (pObject2));
253   ASSERT_TRUE (cNonDuplicatedIntList.push_back (pObject3));
254   EXPECT_TRUE (3 == cNonDuplicatedIntList.size());
255 
256   //try failed adding
257   EXPECT_FALSE (cNonDuplicatedIntList.push_back (pObject3));
258   EXPECT_TRUE (3 == cNonDuplicatedIntList.size());
259 
260   //try pop
261   EXPECT_TRUE (pObject1 == cNonDuplicatedIntList.begin());
262   cNonDuplicatedIntList.pop_front();
263   EXPECT_TRUE (2 == cNonDuplicatedIntList.size());
264 
265   //try what currently in
266   EXPECT_TRUE (cNonDuplicatedIntList.findNode (pObject2));
267   EXPECT_FALSE (cNonDuplicatedIntList.push_back (pObject2));
268   EXPECT_TRUE (cNonDuplicatedIntList.findNode (pObject3));
269   EXPECT_FALSE (cNonDuplicatedIntList.push_back (pObject3));
270   EXPECT_TRUE (2 == cNonDuplicatedIntList.size());
271 
272   //add back
273   ASSERT_TRUE (cNonDuplicatedIntList.push_back (pObject1));
274   EXPECT_TRUE (3 == cNonDuplicatedIntList.size());
275 
276   //another pop
277   EXPECT_TRUE (pObject2 == cNonDuplicatedIntList.begin());
278   cNonDuplicatedIntList.pop_front();
279   cNonDuplicatedIntList.pop_front();
280   EXPECT_TRUE (1 == cNonDuplicatedIntList.size());
281 
282   EXPECT_FALSE (cNonDuplicatedIntList.push_back (pObject1));
283   EXPECT_TRUE (1 == cNonDuplicatedIntList.size());
284 
285   ASSERT_TRUE (cNonDuplicatedIntList.push_back (pObject3));
286   EXPECT_TRUE (2 == cNonDuplicatedIntList.size());
287 
288   //clean-up
289   while (NULL != cNonDuplicatedIntList.begin()) {
290     cNonDuplicatedIntList.pop_front();
291   }
292   EXPECT_TRUE (0 == cNonDuplicatedIntList.size());
293 }
294 
295 #ifndef __APPLE__
TEST(CWelsNonDuplicatedList,CWelsNonDuplicatedListOnThread)296 TEST (CWelsNonDuplicatedList, CWelsNonDuplicatedListOnThread) {
297   CWelsNonDuplicatedList<CWelsTaskThread> cThreadList;
298   CWelsTaskThread* pTaskThread1 = new CWelsTaskThread (NULL); //this initialization seemed making prob on osx?
299   EXPECT_TRUE (NULL != pTaskThread1);
300   CWelsTaskThread* pTaskThread2 = new CWelsTaskThread (NULL);
301   EXPECT_TRUE (NULL != pTaskThread2);
302   CWelsTaskThread* pTaskThread3 = new CWelsTaskThread (NULL);
303   EXPECT_TRUE (NULL != pTaskThread3);
304 
305   //initial adding
306   ASSERT_TRUE (cThreadList.push_back (pTaskThread1));
307   ASSERT_TRUE (cThreadList.push_back (pTaskThread2));
308   ASSERT_TRUE (cThreadList.push_back (pTaskThread3));
309   EXPECT_TRUE (3 == cThreadList.size());
310 
311   //try failed adding
312   EXPECT_FALSE (cThreadList.push_back (pTaskThread3));
313   EXPECT_TRUE (3 == cThreadList.size());
314 
315   //try pop
316   EXPECT_TRUE (pTaskThread1 == cThreadList.begin());
317   cThreadList.pop_front();
318   EXPECT_TRUE (2 == cThreadList.size());
319 
320   //try what currently in
321   EXPECT_TRUE (cThreadList.findNode (pTaskThread2));
322   EXPECT_FALSE (cThreadList.push_back (pTaskThread2));
323   EXPECT_TRUE (cThreadList.findNode (pTaskThread3));
324   EXPECT_FALSE (cThreadList.push_back (pTaskThread3));
325   EXPECT_TRUE (2 == cThreadList.size());
326 
327   //add back
328   ASSERT_TRUE (cThreadList.push_back (pTaskThread1));
329   EXPECT_TRUE (3 == cThreadList.size());
330 
331   //another pop
332   EXPECT_TRUE (pTaskThread2 == cThreadList.begin());
333   cThreadList.pop_front();
334   cThreadList.pop_front();
335   EXPECT_TRUE (1 == cThreadList.size());
336 
337   EXPECT_FALSE (cThreadList.push_back (pTaskThread1));
338   EXPECT_TRUE (1 == cThreadList.size());
339 
340   ASSERT_TRUE (cThreadList.push_back (pTaskThread3));
341   EXPECT_TRUE (2 == cThreadList.size());
342 
343   //clean-up
344   while (NULL != cThreadList.begin()) {
345     cThreadList.pop_front();
346   }
347   EXPECT_TRUE (0 == cThreadList.size());
348 
349   delete pTaskThread1;
350   delete pTaskThread2;
351   delete pTaskThread3;
352 }
353 #endif
354 
355 
TEST(CWelsList,CWelsListReadWithIdx)356 TEST (CWelsList, CWelsListReadWithIdx) {
357   CWelsList<int32_t> cThreadList;
358   const int kiIncreaseNum = (rand() % 1000) + 1;
359   const int kiDecreaseNum = rand() % kiIncreaseNum;
360 
361   int32_t* pInput = static_cast<int32_t*> (malloc (kiIncreaseNum * 10 * sizeof (int32_t)));
362   if (!pInput) {
363     return;
364   }
365   for (int32_t i = 0; i < kiIncreaseNum * 10; i++) {
366     pInput[i] = i;
367   }
368 
369   for (int j = 0; j < 10; j++) {
370     const int iBias = j * (kiIncreaseNum - kiDecreaseNum);
371     for (int i = 0; i < kiIncreaseNum; i++) {
372       ASSERT_TRUE (cThreadList.push_back (&pInput[i + kiIncreaseNum * j]));
373     }
374     EXPECT_TRUE (kiIncreaseNum + iBias == cThreadList.size()) << "after push size=" <<
375         cThreadList.size() ;
376 
377     EXPECT_TRUE ((kiDecreaseNum * j) == * (cThreadList.begin()));
378 
379     for (int i = 0; i < kiIncreaseNum; i++) {
380       EXPECT_TRUE ((i + kiIncreaseNum * j) == * (cThreadList.getNode (i + iBias)));
381     }
382     for (int i = 0; i < cThreadList.size(); i++) {
383       EXPECT_TRUE ((i + kiDecreaseNum * j) == * (cThreadList.getNode (i)));
384     }
385 
386     for (int i = kiDecreaseNum; i > 0; i--) {
387       cThreadList.pop_front();
388     }
389     EXPECT_TRUE ((j + 1) * (kiIncreaseNum - kiDecreaseNum) == cThreadList.size()) << "after pop size=" <<
390         cThreadList.size() ;
391 
392     EXPECT_TRUE ((kiDecreaseNum * (j + 1)) == * (cThreadList.begin()));
393   }
394 
395   //clean-up
396   while (NULL != cThreadList.begin()) {
397     cThreadList.pop_front();
398   }
399   EXPECT_TRUE (0 == cThreadList.size());
400   free (pInput);
401 }
402