Imported Upstream version 1.0.0
[platform/upstream/iotivity.git] / resource / csdk / connectivity / test / uarraylist_test.cpp
1 //******************************************************************
2 //
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
4 //
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
6 //
7 // Licensed under the Apache License, Version 2.0 (the "License");
8 // you may not use this file except in compliance with the License.
9 // You may obtain a copy of the License at
10 //
11 //      http://www.apache.org/licenses/LICENSE-2.0
12 //
13 // Unless required by applicable law or agreed to in writing, software
14 // distributed under the License is distributed on an "AS IS" BASIS,
15 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16 // See the License for the specific language governing permissions and
17 // limitations under the License.
18 //
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
20
21 #include "gtest/gtest.h"
22
23 #include "uarraylist.h"
24
25 class UArrayListF : public testing::Test {
26 public:
27   UArrayListF() :
28       testing::Test(),
29       list(NULL)
30   {
31   }
32
33 protected:
34     virtual void SetUp()
35     {
36         list = u_arraylist_create();
37         ASSERT_TRUE(list != NULL);
38     }
39
40     virtual void TearDown()
41     {
42         u_arraylist_free(&list);
43         ASSERT_EQ(NULL, list);
44     }
45
46     u_arraylist_t *list;
47 };
48
49 TEST(UArrayList, Base)
50 {
51     u_arraylist_t *list = u_arraylist_create();
52     ASSERT_TRUE(list != NULL);
53
54     u_arraylist_free(&list);
55     ASSERT_EQ(NULL, list);
56 }
57
58 TEST(UArrayList, CreateMany)
59 {
60     for (int i = 0; i < 100; ++i)
61     {
62         u_arraylist_t *list = u_arraylist_create();
63         ASSERT_TRUE(list != NULL);
64
65         u_arraylist_free(&list);
66         ASSERT_EQ(NULL, list);
67     }
68 }
69
70 TEST(UArrayList, FreeNull)
71 {
72     u_arraylist_free(NULL);
73 }
74
75 TEST_F(UArrayListF, Length)
76 {
77     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
78
79     int dummy = 0;
80     bool rc = u_arraylist_add(list, &dummy);
81     ASSERT_TRUE(rc);
82
83     ASSERT_EQ(static_cast<uint32_t>(1), u_arraylist_length(list));
84
85     // Add a few times without checking, just in case checking has side-effects
86     rc = u_arraylist_add(list, &dummy);
87     ASSERT_TRUE(rc);
88     rc = u_arraylist_add(list, &dummy);
89     ASSERT_TRUE(rc);
90     rc = u_arraylist_add(list, &dummy);
91     ASSERT_TRUE(rc);
92
93     ASSERT_EQ(static_cast<uint32_t>(4), u_arraylist_length(list));
94 }
95
96 TEST_F(UArrayListF, LengthMulti)
97 {
98     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
99
100     int dummy = 0;
101     for (int i = 0; i < 1000; ++i)
102     {
103         bool rc = u_arraylist_add(list, &dummy);
104         ASSERT_TRUE(rc);
105     }
106
107     ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
108 }
109
110 TEST_F(UArrayListF, NoReserve)
111 {
112     static const int PAD_SIZE = 10000;
113
114     int dummy = 0;
115
116     //u_arraylist_reserve(list, PAD_SIZE);
117
118     for (int i = 0; i < PAD_SIZE; ++i)
119     {
120         bool rc = u_arraylist_add(list, &dummy);
121         ASSERT_TRUE(rc);
122     }
123 }
124
125 TEST_F(UArrayListF, Reserve)
126 {
127     static const int PAD_SIZE = 10000;
128
129     int dummy = 0;
130
131     u_arraylist_reserve(list, PAD_SIZE);
132
133     for (int i = 0; i < PAD_SIZE; ++i)
134     {
135         bool rc = u_arraylist_add(list, &dummy);
136         ASSERT_TRUE(rc);
137     }
138 }
139
140
141 TEST_F(UArrayListF, ShrinkToFit)
142 {
143     static const int PAD_SIZE = 100;
144
145     int dummy = 0;
146
147     u_arraylist_reserve(list, PAD_SIZE);
148
149     for (int i = 0; i < PAD_SIZE; ++i)
150     {
151         bool rc = u_arraylist_add(list, &dummy);
152         ASSERT_TRUE(rc);
153     }
154
155     for (int i = PAD_SIZE; i > 0; --i)
156     {
157         u_arraylist_remove(list, i);
158     }
159
160     EXPECT_GT(list->capacity, list->length);
161     u_arraylist_shrink_to_fit(list);
162     EXPECT_EQ(list->capacity, list->length);
163 }
164
165 TEST_F(UArrayListF, Get)
166 {
167     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
168
169     int dummy[1000] = {0};
170     size_t cap = sizeof(dummy) / sizeof(dummy[0]);
171
172     for (size_t i = 0; i < cap; ++i)
173     {
174         bool rc = u_arraylist_add(list, &dummy[i]);
175         ASSERT_TRUE(rc);
176     }
177     ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
178
179     for (size_t i = 0; i < cap; ++i)
180     {
181         void *value = u_arraylist_get(list, i);
182         ASSERT_TRUE(value != NULL);
183         ASSERT_EQ(&dummy[i], value);
184     }
185 }
186
187 TEST_F(UArrayListF, Remove)
188 {
189     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
190
191     int dummy[1000] = {0};
192     size_t cap = sizeof(dummy) / sizeof(dummy[0]);
193
194     for (size_t i = 0; i < cap; ++i)
195     {
196         bool rc = u_arraylist_add(list, &dummy[i]);
197         ASSERT_TRUE(rc);
198     }
199     ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
200
201     // Remove walking forward so as to have a non-trivial case.
202     for (uint32_t idx = 0, old = 0;
203          idx < u_arraylist_length(list);
204          ++idx, old += 2)
205     {
206         void *value = u_arraylist_remove(list, idx);
207         ASSERT_TRUE(value != NULL);
208         ASSERT_EQ(value, &dummy[old]);
209     }
210     ASSERT_EQ(static_cast<uint32_t>(500), u_arraylist_length(list));
211 }
212
213 TEST_F(UArrayListF, Contains)
214 {
215     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
216
217     int dummy[1000] = {0};
218     size_t cap = sizeof(dummy) / sizeof(dummy[0]);
219
220     for (size_t i = 0; i < cap; ++i)
221     {
222         bool rc = u_arraylist_add(list, &dummy[i]);
223         ASSERT_TRUE(rc);
224     }
225     ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
226
227     // Remove walking forward so as to have a non-trivial case.
228     for (uint32_t idx = 0, old = 0;
229          idx < u_arraylist_length(list);
230          ++idx, old += 2)
231     {
232         void *value = u_arraylist_remove(list, idx);
233         ASSERT_TRUE(value != NULL);
234         ASSERT_EQ(value, &dummy[old]);
235     }
236     ASSERT_EQ(static_cast<uint32_t>(500), u_arraylist_length(list));
237
238     // Finally, check that the ones we expect are present, and others are not.
239     for (size_t i = 0; i < cap; ++i)
240     {
241         bool shouldBe = (i & 1) != 0;
242         bool found = u_arraylist_contains(list, &dummy[i]);
243         ASSERT_EQ(shouldBe, found) << " for entry " << i;
244     }
245 }
246
247 // Test to repeatedly add and remove with some contents present so that
248 // a poor implmentation will thrash memory.
249 TEST_F(UArrayListF, Thrash)
250 {
251     static const int PAD_SIZE = 1000;
252     static const int THRASH_COUNT = 1500;
253     ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
254
255     int dummy2 = 0;
256     int dummy[PAD_SIZE] = {0};
257     size_t cap = sizeof(dummy) / sizeof(dummy[0]);
258
259     for (size_t i = 0; i < cap; ++i)
260     {
261         bool rc = u_arraylist_add(list, &dummy[i]);
262         ASSERT_TRUE(rc);
263     }
264     ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE), u_arraylist_length(list));
265
266     // Finally add and remove a lot.
267     for (int i = 0; i < THRASH_COUNT; ++i)
268     {
269         bool rc = u_arraylist_add(list, &dummy2);
270         ASSERT_TRUE(rc);
271         ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE + 1),
272                   u_arraylist_length(list));
273
274         ASSERT_EQ(&dummy2,
275                   u_arraylist_remove(list, u_arraylist_length(list) - 1));
276
277         ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE), u_arraylist_length(list));
278     }
279 }