1 /*
2 * Copyright (C) 2018 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 "types.h"
18
19 #include <gtest/gtest.h>
20
21 #include <memory>
22 #include <utility>
23
24 #include "fdevent/fdevent_test.h"
25
create_block(const std::string & string)26 static IOVector::block_type create_block(const std::string& string) {
27 return IOVector::block_type(string.begin(), string.end());
28 }
29
create_block(char value,size_t len)30 static IOVector::block_type create_block(char value, size_t len) {
31 auto block = IOVector::block_type();
32 block.resize(len);
33 memset(&(block)[0], value, len);
34 return block;
35 }
36
37 template <typename T>
copy_block(const T & block)38 static IOVector::block_type copy_block(const T& block) {
39 auto copy = IOVector::block_type();
40 copy.assign(block.begin(), block.end());
41 return copy;
42 }
43
TEST(IOVector,empty)44 TEST(IOVector, empty) {
45 // Empty IOVector.
46 IOVector bc;
47 CHECK_EQ(0ULL, bc.coalesce().size());
48 }
49
TEST(IOVector,single_block)50 TEST(IOVector, single_block) {
51 // A single block.
52 auto block = create_block('x', 100);
53 IOVector bc;
54 bc.append(copy_block(block));
55 ASSERT_EQ(100ULL, bc.size());
56 auto coalesced = bc.coalesce();
57 ASSERT_EQ(block, coalesced);
58 }
59
TEST(IOVector,single_block_split)60 TEST(IOVector, single_block_split) {
61 // One block split.
62 IOVector bc;
63 bc.append(create_block("foobar"));
64 IOVector foo = bc.take_front(3);
65 ASSERT_EQ(3ULL, foo.size());
66 ASSERT_EQ(3ULL, bc.size());
67 ASSERT_EQ(create_block("foo"), foo.coalesce());
68 ASSERT_EQ(create_block("bar"), bc.coalesce());
69 }
70
TEST(IOVector,aligned_split)71 TEST(IOVector, aligned_split) {
72 IOVector bc;
73 bc.append(create_block("foo"));
74 bc.append(create_block("bar"));
75 bc.append(create_block("baz"));
76 ASSERT_EQ(9ULL, bc.size());
77
78 IOVector foo = bc.take_front(3);
79 ASSERT_EQ(3ULL, foo.size());
80 ASSERT_EQ(create_block("foo"), foo.coalesce());
81
82 IOVector bar = bc.take_front(3);
83 ASSERT_EQ(3ULL, bar.size());
84 ASSERT_EQ(create_block("bar"), bar.coalesce());
85
86 IOVector baz = bc.take_front(3);
87 ASSERT_EQ(3ULL, baz.size());
88 ASSERT_EQ(create_block("baz"), baz.coalesce());
89
90 ASSERT_EQ(0ULL, bc.size());
91 }
92
TEST(IOVector,misaligned_split)93 TEST(IOVector, misaligned_split) {
94 IOVector bc;
95 bc.append(create_block("foo"));
96 bc.append(create_block("bar"));
97 bc.append(create_block("baz"));
98 bc.append(create_block("qux"));
99 bc.append(create_block("quux"));
100
101 // Aligned left, misaligned right, across multiple blocks.
102 IOVector foob = bc.take_front(4);
103 ASSERT_EQ(4ULL, foob.size());
104 ASSERT_EQ(create_block("foob"), foob.coalesce());
105
106 // Misaligned left, misaligned right, in one block.
107 IOVector a = bc.take_front(1);
108 ASSERT_EQ(1ULL, a.size());
109 ASSERT_EQ(create_block("a"), a.coalesce());
110
111 // Misaligned left, misaligned right, across two blocks.
112 IOVector rba = bc.take_front(3);
113 ASSERT_EQ(3ULL, rba.size());
114 ASSERT_EQ(create_block("rba"), rba.coalesce());
115
116 // Misaligned left, misaligned right, across three blocks.
117 IOVector zquxquu = bc.take_front(7);
118 ASSERT_EQ(7ULL, zquxquu.size());
119 ASSERT_EQ(create_block("zquxquu"), zquxquu.coalesce());
120
121 ASSERT_EQ(1ULL, bc.size());
122 ASSERT_EQ(create_block("x"), bc.coalesce());
123 }
124
TEST(IOVector,drop_front)125 TEST(IOVector, drop_front) {
126 IOVector vec;
127
128 vec.append(create_block('x', 2));
129 vec.append(create_block('y', 1000));
130 ASSERT_EQ(2U, vec.front_size());
131 ASSERT_EQ(1002U, vec.size());
132
133 vec.drop_front(1);
134 ASSERT_EQ(1U, vec.front_size());
135 ASSERT_EQ(1001U, vec.size());
136
137 vec.drop_front(1);
138 ASSERT_EQ(1000U, vec.front_size());
139 ASSERT_EQ(1000U, vec.size());
140 }
141
TEST(IOVector,take_front)142 TEST(IOVector, take_front) {
143 IOVector vec;
144 ASSERT_TRUE(vec.take_front(0).empty());
145
146 vec.append(create_block('x', 2));
147 ASSERT_EQ(2ULL, vec.size());
148
149 ASSERT_EQ(1ULL, vec.take_front(1).size());
150 ASSERT_EQ(1ULL, vec.size());
151
152 ASSERT_EQ(1ULL, vec.take_front(1).size());
153 ASSERT_EQ(0ULL, vec.size());
154 }
155
TEST(IOVector,trim_front)156 TEST(IOVector, trim_front) {
157 IOVector vec;
158 vec.append(create_block('x', 2));
159
160 ASSERT_EQ(1ULL, vec.take_front(1).size());
161 ASSERT_EQ(1ULL, vec.size());
162 vec.trim_front();
163 ASSERT_EQ(1ULL, vec.size());
164 }
165
166 class weak_ptr_test : public FdeventTest {};
167
168 struct Destructor : public enable_weak_from_this<Destructor> {
DestructorDestructor169 Destructor(bool* destroyed) : destroyed_(destroyed) {}
~DestructorDestructor170 ~Destructor() { *destroyed_ = true; }
171
172 bool* destroyed_;
173 };
174
TEST_F(weak_ptr_test,smoke)175 TEST_F(weak_ptr_test, smoke) {
176 PrepareThread();
177
178 Destructor* destructor = nullptr;
179 bool destroyed = false;
180 std::optional<weak_ptr<Destructor>> p;
181
182 fdevent_run_on_main_thread([&p, &destructor, &destroyed]() {
183 destructor = new Destructor(&destroyed);
184 p = destructor->weak();
185 ASSERT_TRUE(p->get());
186
187 p->reset();
188 ASSERT_FALSE(p->get());
189
190 p->reset(destructor);
191 ASSERT_TRUE(p->get());
192 });
193 WaitForFdeventLoop();
194 ASSERT_TRUE(destructor);
195 ASSERT_FALSE(destroyed);
196
197 destructor->schedule_deletion();
198 WaitForFdeventLoop();
199
200 ASSERT_TRUE(destroyed);
201 fdevent_run_on_main_thread([&p]() {
202 ASSERT_FALSE(p->get());
203 p.reset();
204 });
205
206 TerminateThread();
207 }
208