blob: 12348d9be4e9a83317da0d3f6a3036edfb1750ff [file] [log] [blame]
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001/*
2 * Copyright (C) 2013 The Android Open Source Project
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#include <stdlib.h>
18#include <string>
19#include <sstream>
20
21#include <gtest/gtest.h>
22
23#include "../linked_list.h"
24
25namespace {
26
27bool alloc_called = false;
28bool free_called = false;
29
30class LinkedListTestAllocator {
31 public:
32 typedef LinkedListEntry<const char> entry_t;
33
34 static entry_t* alloc() {
35 alloc_called = true;
36 return reinterpret_cast<entry_t*>(::malloc(sizeof(entry_t)));
37 }
38
39 static void free(entry_t* p) {
40 free_called = true;
41 ::free(p);
42 }
43 private:
44 DISALLOW_IMPLICIT_CONSTRUCTORS(LinkedListTestAllocator);
45};
46
47typedef LinkedList<const char, LinkedListTestAllocator> test_list_t;
48
49std::string test_list_to_string(test_list_t& list) {
50 std::stringstream ss;
51 list.for_each([&] (const char* c) {
52 ss << c;
53 });
54
55 return ss.str();
56}
57
58};
59
60TEST(linked_list, simple) {
61 alloc_called = free_called = false;
62 test_list_t list;
63 ASSERT_EQ("", test_list_to_string(list));
64 ASSERT_TRUE(!alloc_called);
65 ASSERT_TRUE(!free_called);
66 list.push_front("a");
67 ASSERT_TRUE(alloc_called);
68 ASSERT_TRUE(!free_called);
69 ASSERT_EQ("a", test_list_to_string(list));
70 list.push_front("b");
71 ASSERT_EQ("ba", test_list_to_string(list));
72 list.push_front("c");
73 list.push_front("d");
74 ASSERT_EQ("dcba", test_list_to_string(list));
75 ASSERT_TRUE(alloc_called);
76 ASSERT_TRUE(!free_called);
77 alloc_called = free_called = false;
78 list.remove_if([] (const char* c) {
79 return *c == 'c';
80 });
81
82 ASSERT_TRUE(!alloc_called);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -070083 ASSERT_TRUE(free_called);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -070084
85 ASSERT_EQ("dba", test_list_to_string(list));
86 alloc_called = free_called = false;
87 list.remove_if([] (const char* c) {
88 return *c == '2';
89 });
90 ASSERT_TRUE(!alloc_called);
91 ASSERT_TRUE(!free_called);
92 ASSERT_EQ("dba", test_list_to_string(list));
93 list.clear();
94 ASSERT_TRUE(!alloc_called);
95 ASSERT_TRUE(free_called);
96 ASSERT_EQ("", test_list_to_string(list));
97}
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -070098
99TEST(linked_list, push_pop) {
100 test_list_t list;
101 list.push_front("b");
102 list.push_front("a");
103 ASSERT_EQ("ab", test_list_to_string(list));
104 list.push_back("c");
105 ASSERT_EQ("abc", test_list_to_string(list));
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700106 ASSERT_STREQ("a", list.pop_front());
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700107 ASSERT_EQ("bc", test_list_to_string(list));
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700108 ASSERT_STREQ("b", list.pop_front());
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700109 ASSERT_EQ("c", test_list_to_string(list));
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700110 ASSERT_STREQ("c", list.pop_front());
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700111 ASSERT_EQ("", test_list_to_string(list));
112 ASSERT_TRUE(list.pop_front() == nullptr);
113 list.push_back("r");
114 ASSERT_EQ("r", test_list_to_string(list));
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700115 ASSERT_STREQ("r", list.pop_front());
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700116 ASSERT_TRUE(list.pop_front() == nullptr);
117}
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700118
119TEST(linked_list, remove_if_then_pop) {
120 test_list_t list;
121 list.push_back("a");
122 list.push_back("b");
123 list.push_back("c");
124 list.push_back("d");
125 list.remove_if([](const char* c) {
126 return *c == 'b' || *c == 'c';
127 });
128
129 ASSERT_EQ("ad", test_list_to_string(list));
130 ASSERT_STREQ("a", list.pop_front());
131 ASSERT_EQ("d", test_list_to_string(list));
132 ASSERT_STREQ("d", list.pop_front());
133 ASSERT_TRUE(list.pop_front() == nullptr);
134}
135
Dmitriy Ivanov7a9311f2015-11-05 17:41:05 -0800136TEST(linked_list, remove_if_last_then_push_back) {
137 test_list_t list;
138
139 list.push_back("a");
140 list.push_back("b");
141 list.push_back("c");
142 list.push_back("d");
143
144 list.remove_if([](const char* c) {
145 return *c == 'c' || *c == 'd';
146 });
147
148 ASSERT_EQ("ab", test_list_to_string(list));
149 list.push_back("d");
150 ASSERT_EQ("abd", test_list_to_string(list));
151}
152
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700153TEST(linked_list, copy_to_array) {
154 test_list_t list;
155 const size_t max_size = 128;
156 const char* buf[max_size];
157 memset(buf, 0, sizeof(buf));
158
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700159 ASSERT_EQ(0U, list.copy_to_array(buf, max_size));
160 ASSERT_EQ(nullptr, buf[0]);
161
162 list.push_back("a");
163 list.push_back("b");
164 list.push_back("c");
165 list.push_back("d");
166
167 memset(buf, 0, sizeof(buf));
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700168 ASSERT_EQ(2U, list.copy_to_array(buf, 2));
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700169 ASSERT_STREQ("a", buf[0]);
170 ASSERT_STREQ("b", buf[1]);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700171 ASSERT_EQ(nullptr, buf[2]);
172
173 ASSERT_EQ(4U, list.copy_to_array(buf, max_size));
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700174 ASSERT_STREQ("a", buf[0]);
175 ASSERT_STREQ("b", buf[1]);
176 ASSERT_STREQ("c", buf[2]);
177 ASSERT_STREQ("d", buf[3]);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700178 ASSERT_EQ(nullptr, buf[4]);
179
180 memset(buf, 0, sizeof(buf));
181 list.remove_if([](const char* c) {
182 return *c != 'c';
183 });
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700184 ASSERT_EQ(1U, list.copy_to_array(buf, max_size));
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700185 ASSERT_STREQ("c", buf[0]);
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700186 ASSERT_EQ(nullptr, buf[1]);
187
188 memset(buf, 0, sizeof(buf));
189
190 list.remove_if([](const char* c) {
191 return *c == 'c';
192 });
193
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700194 ASSERT_EQ(0U, list.copy_to_array(buf, max_size));
195 ASSERT_EQ(nullptr, buf[0]);
196}
197
Dmitriy Ivanova4926052014-09-02 09:45:40 -0700198TEST(linked_list, test_visit) {
199 test_list_t list;
200 list.push_back("a");
201 list.push_back("b");
202 list.push_back("c");
203 list.push_back("d");
204
205 int visits = 0;
206 std::stringstream ss;
207 bool result = list.visit([&](const char* c) {
208 ++visits;
209 ss << c;
210 return true;
211 });
212
213 ASSERT_TRUE(result);
214 ASSERT_EQ(4, visits);
215 ASSERT_EQ("abcd", ss.str());
216
217 visits = 0;
218 ss.str(std::string());
219
220 result = list.visit([&](const char* c) {
221 if (++visits == 3) {
222 return false;
223 }
224
225 ss << c;
226 return true;
227 });
228
229 ASSERT_TRUE(!result);
230 ASSERT_EQ(3, visits);
231 ASSERT_EQ("ab", ss.str());
232}
233