2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 #include <gtest/gtest.h>
21 #include <sys/types.h>
24 #if defined(__BIONIC__)
25 static int child_fn(void* i_ptr) {
26 *reinterpret_cast<int*>(i_ptr) = 42;
31 void* child_stack[1024];
34 pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i);
37 ASSERT_EQ(tid, TEMP_FAILURE_RETRY(waitpid(tid, &status, __WCLONE)));
41 ASSERT_TRUE(WIFEXITED(status));
42 ASSERT_EQ(123, WEXITSTATUS(status));
45 // For glibc, any call to clone with CLONE_VM set will cause later pthread
46 // calls in the same process to misbehave.
47 // See https://sourceware.org/bugzilla/show_bug.cgi?id=10311 for more details.
49 // In order to enumerate all possible tests for CTS, create an empty test.
50 GTEST_LOG_(INFO) << "This test does nothing.\n";
54 TEST(sched, clone_errno) {
55 // Check that our hand-written clone assembler sets errno correctly on failure.
56 uintptr_t fake_child_stack[16];
58 ASSERT_EQ(-1, clone(NULL, &fake_child_stack[16], CLONE_THREAD, NULL));
59 ASSERT_EQ(EINVAL, errno);
62 TEST(sched, cpu_set) {
68 for (int i = 0; i < CPU_SETSIZE; i++) {
69 ASSERT_EQ(i == 0 || i == 17, CPU_ISSET(i, &set));
72 // We should fail silently if we try to set/test outside the range.
73 CPU_SET(CPU_SETSIZE, &set);
74 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
77 TEST(sched, cpu_count) {
81 ASSERT_EQ(0, CPU_COUNT(&set));
84 ASSERT_EQ(2, CPU_COUNT(&set));
86 ASSERT_EQ(1, CPU_COUNT(&set));
89 TEST(sched, cpu_zero) {
93 ASSERT_EQ(0, CPU_COUNT(&set));
94 for (int i = 0; i < CPU_SETSIZE; i++) {
95 ASSERT_FALSE(CPU_ISSET(i, &set));
99 TEST(sched, cpu_clr) {
105 for (int i = 0; i < CPU_SETSIZE; i++) {
106 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set));
109 for (int i = 0; i < CPU_SETSIZE; i++) {
110 ASSERT_EQ(i == 0, CPU_ISSET(i, &set));
113 // We should fail silently if we try to clear/test outside the range.
114 CPU_CLR(CPU_SETSIZE, &set);
115 ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
118 TEST(sched, cpu_equal) {
125 ASSERT_FALSE(CPU_EQUAL(&set1, &set2));
127 ASSERT_TRUE(CPU_EQUAL(&set1, &set2));
130 TEST(sched, cpu_op) {
142 CPU_AND(&set3, &set1, &set2);
143 for (int i = 0; i < CPU_SETSIZE; i++) {
144 ASSERT_EQ(i == 0, CPU_ISSET(i, &set3));
147 CPU_XOR(&set3, &set1, &set2);
148 for (int i = 0; i < CPU_SETSIZE; i++) {
149 ASSERT_EQ(i == 1, CPU_ISSET(i, &set3));
152 CPU_OR(&set3, &set1, &set2);
153 for (int i = 0; i < CPU_SETSIZE; i++) {
154 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set3));
159 TEST(sched, cpu_alloc_small) {
160 cpu_set_t* set = CPU_ALLOC(17);
161 size_t size = CPU_ALLOC_SIZE(17);
163 CPU_ZERO_S(size, set);
164 ASSERT_EQ(0, CPU_COUNT_S(size, set));
165 CPU_SET_S(16, size, set);
166 ASSERT_TRUE(CPU_ISSET_S(16, size, set));
171 TEST(sched, cpu_alloc_big) {
172 cpu_set_t* set = CPU_ALLOC(10 * CPU_SETSIZE);
173 size_t size = CPU_ALLOC_SIZE(10 * CPU_SETSIZE);
175 CPU_ZERO_S(size, set);
176 ASSERT_EQ(0, CPU_COUNT_S(size, set));
177 CPU_SET_S(CPU_SETSIZE, size, set);
178 ASSERT_TRUE(CPU_ISSET_S(CPU_SETSIZE, size, set));
183 TEST(sched, cpu_s_macros) {
185 size_t size = CPU_ALLOC_SIZE(set_size);
186 cpu_set_t* set = CPU_ALLOC(set_size);
188 CPU_ZERO_S(size, set);
189 for (int i = 0; i < set_size; i++) {
190 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
191 CPU_SET_S(i, size, set);
192 ASSERT_TRUE(CPU_ISSET_S(i, size, set));
193 ASSERT_EQ(i + 1, CPU_COUNT_S(size, set));
196 for (int i = 0; i < set_size; i++) {
197 CPU_CLR_S(i, size, set);
198 ASSERT_FALSE(CPU_ISSET_S(i, size, set));
199 ASSERT_EQ(set_size - i - 1, CPU_COUNT_S(size, set));
205 TEST(sched, cpu_op_s_macros) {
207 int set_size2 = set_size1 * 2;
208 int set_size3 = set_size1 * 3;
209 size_t size1 = CPU_ALLOC_SIZE(set_size1);
210 size_t size2 = CPU_ALLOC_SIZE(set_size2);
211 size_t size3 = CPU_ALLOC_SIZE(set_size3);
213 cpu_set_t* set1 = CPU_ALLOC(set_size1);
214 cpu_set_t* set2 = CPU_ALLOC(set_size2);
215 cpu_set_t* set3 = CPU_ALLOC(set_size3);
216 CPU_ZERO_S(size1, set1);
217 CPU_ZERO_S(size2, set2);
218 CPU_ZERO_S(size3, set3);
220 CPU_SET_S(0, size1, set1);
221 CPU_SET_S(0, size2, set2);
222 CPU_SET_S(1, size3, set2);
224 CPU_AND_S(size1, set3, set1, set2);
225 for (int i = 0; i < set_size3; i++) {
226 ASSERT_EQ(i == 0, CPU_ISSET_S(i, size3, set3));
229 CPU_OR_S(size1, set3, set1, set2);
230 for (int i = 0; i < set_size3; i++) {
231 ASSERT_EQ(i == 0 || i == 1, CPU_ISSET_S(i, size3, set3));
234 CPU_XOR_S(size1, set3, set1, set2);
235 for (int i = 0; i < set_size3; i++) {
236 ASSERT_EQ(i == 1, CPU_ISSET_S(i, size3, set3));
244 TEST(sched, cpu_equal_s) {
246 int set_size2 = set_size1 * 2;
247 size_t size1 = CPU_ALLOC_SIZE(set_size1);
248 size_t size2 = CPU_ALLOC_SIZE(set_size2);
250 cpu_set_t* set1 = CPU_ALLOC(set_size1);
251 cpu_set_t* set2 = CPU_ALLOC(set_size2);
253 CPU_ZERO_S(size1, set1);
254 CPU_ZERO_S(size2, set2);
256 CPU_SET_S(0, size1, set1);
257 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set1));
258 ASSERT_FALSE(CPU_EQUAL_S(size1, set1, set2));
259 CPU_SET_S(0, size2, set2);
260 ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set2));