1 //******************************************************************
3 // Copyright 2015 Samsung Electronics All Rights Reserved.
5 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
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
11 // http://www.apache.org/licenses/LICENSE-2.0
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.
19 //-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=-=
21 #include "gtest/gtest.h"
23 #include "uarraylist.h"
25 class UArrayListF : public testing::Test {
36 list = u_arraylist_create();
37 ASSERT_TRUE(list != NULL);
40 virtual void TearDown()
42 u_arraylist_free(&list);
43 ASSERT_EQ(NULL, list);
49 TEST(UArrayList, Base)
51 u_arraylist_t *list = u_arraylist_create();
52 ASSERT_TRUE(list != NULL);
54 u_arraylist_free(&list);
55 ASSERT_EQ(NULL, list);
58 TEST(UArrayList, CreateMany)
60 for (int i = 0; i < 100; ++i)
62 u_arraylist_t *list = u_arraylist_create();
63 ASSERT_TRUE(list != NULL);
65 u_arraylist_free(&list);
66 ASSERT_EQ(NULL, list);
70 TEST(UArrayList, FreeNull)
72 u_arraylist_free(NULL);
75 TEST_F(UArrayListF, Length)
77 ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
80 bool rc = u_arraylist_add(list, &dummy);
83 ASSERT_EQ(static_cast<uint32_t>(1), u_arraylist_length(list));
85 // Add a few times without checking, just in case checking has side-effects
86 rc = u_arraylist_add(list, &dummy);
88 rc = u_arraylist_add(list, &dummy);
90 rc = u_arraylist_add(list, &dummy);
93 ASSERT_EQ(static_cast<uint32_t>(4), u_arraylist_length(list));
96 TEST_F(UArrayListF, LengthMulti)
98 ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
101 for (int i = 0; i < 1000; ++i)
103 bool rc = u_arraylist_add(list, &dummy);
107 ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
110 TEST_F(UArrayListF, NoReserve)
112 static const int PAD_SIZE = 10000;
116 //u_arraylist_reserve(list, PAD_SIZE);
118 for (int i = 0; i < PAD_SIZE; ++i)
120 bool rc = u_arraylist_add(list, &dummy);
125 TEST_F(UArrayListF, Reserve)
127 static const int PAD_SIZE = 10000;
131 u_arraylist_reserve(list, PAD_SIZE);
133 for (int i = 0; i < PAD_SIZE; ++i)
135 bool rc = u_arraylist_add(list, &dummy);
141 TEST_F(UArrayListF, ShrinkToFit)
143 static const int PAD_SIZE = 100;
147 u_arraylist_reserve(list, PAD_SIZE);
149 for (int i = 0; i < PAD_SIZE; ++i)
151 bool rc = u_arraylist_add(list, &dummy);
155 for (int i = PAD_SIZE; i > 0; --i)
157 u_arraylist_remove(list, i);
160 EXPECT_GT(list->capacity, list->length);
161 u_arraylist_shrink_to_fit(list);
162 EXPECT_EQ(list->capacity, list->length);
165 TEST_F(UArrayListF, Get)
167 ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
169 int dummy[1000] = {0};
170 size_t cap = sizeof(dummy) / sizeof(dummy[0]);
172 for (size_t i = 0; i < cap; ++i)
174 bool rc = u_arraylist_add(list, &dummy[i]);
177 ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
179 for (size_t i = 0; i < cap; ++i)
181 void *value = u_arraylist_get(list, i);
182 ASSERT_TRUE(value != NULL);
183 ASSERT_EQ(&dummy[i], value);
187 TEST_F(UArrayListF, Remove)
189 ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
191 int dummy[1000] = {0};
192 size_t cap = sizeof(dummy) / sizeof(dummy[0]);
194 for (size_t i = 0; i < cap; ++i)
196 bool rc = u_arraylist_add(list, &dummy[i]);
199 ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
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);
206 void *value = u_arraylist_remove(list, idx);
207 ASSERT_TRUE(value != NULL);
208 ASSERT_EQ(value, &dummy[old]);
210 ASSERT_EQ(static_cast<uint32_t>(500), u_arraylist_length(list));
213 TEST_F(UArrayListF, Contains)
215 ASSERT_EQ(static_cast<uint32_t>(0), u_arraylist_length(list));
217 int dummy[1000] = {0};
218 size_t cap = sizeof(dummy) / sizeof(dummy[0]);
220 for (size_t i = 0; i < cap; ++i)
222 bool rc = u_arraylist_add(list, &dummy[i]);
225 ASSERT_EQ(static_cast<uint32_t>(1000), u_arraylist_length(list));
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);
232 void *value = u_arraylist_remove(list, idx);
233 ASSERT_TRUE(value != NULL);
234 ASSERT_EQ(value, &dummy[old]);
236 ASSERT_EQ(static_cast<uint32_t>(500), u_arraylist_length(list));
238 // Finally, check that the ones we expect are present, and others are not.
239 for (size_t i = 0; i < cap; ++i)
241 bool shouldBe = (i & 1) != 0;
242 bool found = u_arraylist_contains(list, &dummy[i]);
243 ASSERT_EQ(shouldBe, found) << " for entry " << i;
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)
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));
256 int dummy[PAD_SIZE] = {0};
257 size_t cap = sizeof(dummy) / sizeof(dummy[0]);
259 for (size_t i = 0; i < cap; ++i)
261 bool rc = u_arraylist_add(list, &dummy[i]);
264 ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE), u_arraylist_length(list));
266 // Finally add and remove a lot.
267 for (int i = 0; i < THRASH_COUNT; ++i)
269 bool rc = u_arraylist_add(list, &dummy2);
271 ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE + 1),
272 u_arraylist_length(list));
275 u_arraylist_remove(list, u_arraylist_length(list) - 1));
277 ASSERT_EQ(static_cast<uint32_t>(PAD_SIZE), u_arraylist_length(list));