153bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes/*
253bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * Copyright (C) 2013 The Android Open Source Project
353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes *
453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * Licensed under the Apache License, Version 2.0 (the "License");
553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * you may not use this file except in compliance with the License.
653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * You may obtain a copy of the License at
753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes *
853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes *      http://www.apache.org/licenses/LICENSE-2.0
953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes *
1053bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * Unless required by applicable law or agreed to in writing, software
1153bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * distributed under the License is distributed on an "AS IS" BASIS,
1253bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
1353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * See the License for the specific language governing permissions and
1453bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes * limitations under the License.
1553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes */
1653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
1753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes#include <gtest/gtest.h>
1853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
1953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes#include <errno.h>
2053bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes#include <sched.h>
2153bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes#include <sys/types.h>
2253bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes#include <sys/wait.h>
2353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
24063525c61d24776094d76971f33920e2a2079530Elliott Hughes#if defined(__BIONIC__)
2553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughesstatic int child_fn(void* i_ptr) {
2653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  *reinterpret_cast<int*>(i_ptr) = 42;
2753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  return 123;
2853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes}
2953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
3053bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott HughesTEST(sched, clone) {
3153bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  void* child_stack[1024];
3253bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
3353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  int i = 0;
3413613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris  pid_t tid = clone(child_fn, &child_stack[1024], CLONE_VM, &i);
3553bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
3653bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  int status;
3753bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  ASSERT_EQ(tid, TEMP_FAILURE_RETRY(waitpid(tid, &status, __WCLONE)));
3853bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
3953bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  ASSERT_EQ(42, i);
4053bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes
4153bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  ASSERT_TRUE(WIFEXITED(status));
4253bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes  ASSERT_EQ(123, WEXITSTATUS(status));
4353bfdae4ffdbd43d0c019d1a35af1f8477a272c9Elliott Hughes}
4413613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris#else
4513613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris// For glibc, any call to clone with CLONE_VM set will cause later pthread
4613613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris// calls in the same process to misbehave.
4713613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris// See https://sourceware.org/bugzilla/show_bug.cgi?id=10311 for more details.
4813613137bc4266656bffce464e525eb9ae6371f0Christopher FerrisTEST(sched, clone) {
4913613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris  // In order to enumerate all possible tests for CTS, create an empty test.
5013613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris  GTEST_LOG_(INFO) << "This test does nothing.\n";
5113613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris}
5213613137bc4266656bffce464e525eb9ae6371f0Christopher Ferris#endif
53b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
54954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott HughesTEST(sched, clone_errno) {
55954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes  // Check that our hand-written clone assembler sets errno correctly on failure.
56954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes  uintptr_t fake_child_stack[16];
57954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes  errno = 0;
58954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes  ASSERT_EQ(-1, clone(NULL, &fake_child_stack[16], CLONE_THREAD, NULL));
59954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes  ASSERT_EQ(EINVAL, errno);
60954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes}
61954cf0d4e2669f91194b45f484152e47efa4f6c7Elliott Hughes
62b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_set) {
63b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set;
64b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
65b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set);
66b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(0, &set);
67b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(17, &set);
68b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
69b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0 || i == 17, CPU_ISSET(i, &set));
70b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
71b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
72b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  // We should fail silently if we try to set/test outside the range.
73b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(CPU_SETSIZE, &set);
74b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
75b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
76b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
77b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_count) {
78b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set;
79b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
80b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set);
81b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(0, CPU_COUNT(&set));
82b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(2, &set);
83b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(10, &set);
84b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(2, CPU_COUNT(&set));
85b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_CLR(10, &set);
86b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(1, CPU_COUNT(&set));
87b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
88b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
89b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_zero) {
90b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set;
91b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
92b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set);
93b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(0, CPU_COUNT(&set));
94b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
95b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_FALSE(CPU_ISSET(i, &set));
96b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
97b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
98b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
99b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_clr) {
100b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set;
101b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
102b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set);
103b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(0, &set);
104b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(1, &set);
105b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
106b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set));
107b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
108b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_CLR(1, &set);
109b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
110b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0, CPU_ISSET(i, &set));
111b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
112b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
113b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  // We should fail silently if we try to clear/test outside the range.
114b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_CLR(CPU_SETSIZE, &set);
115b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_FALSE(CPU_ISSET(CPU_SETSIZE, &set));
116b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
117b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
118b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_equal) {
119b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set1;
120b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set2;
121b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
122b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set1);
123b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set2);
124b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(1, &set1);
125b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_FALSE(CPU_EQUAL(&set1, &set2));
126b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(1, &set2);
127b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_TRUE(CPU_EQUAL(&set1, &set2));
128b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
129b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
130b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_op) {
131b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set1;
132b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set2;
133b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t set3;
134b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
135b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set1);
136b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set2);
137b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO(&set3);
138b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(0, &set1);
139b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(0, &set2);
140b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET(1, &set2);
141b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
142b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_AND(&set3, &set1, &set2);
143b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
144b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0, CPU_ISSET(i, &set3));
145b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
146b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
147b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_XOR(&set3, &set1, &set2);
148b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
149b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 1, CPU_ISSET(i, &set3));
150b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
151b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
152b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_OR(&set3, &set1, &set2);
153b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < CPU_SETSIZE; i++) {
154b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0 || i == 1, CPU_ISSET(i, &set3));
155b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
156b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
157b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
158b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
159b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_alloc_small) {
160b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t* set = CPU_ALLOC(17);
161b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size = CPU_ALLOC_SIZE(17);
162b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
163b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size, set);
164b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(0, CPU_COUNT_S(size, set));
165b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(16, size, set);
166b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_TRUE(CPU_ISSET_S(16, size, set));
167b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
168b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set);
169b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
170b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
171b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_alloc_big) {
172b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t* set = CPU_ALLOC(10 * CPU_SETSIZE);
173b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size = CPU_ALLOC_SIZE(10 * CPU_SETSIZE);
174b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
175b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size, set);
176b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_EQ(0, CPU_COUNT_S(size, set));
177b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(CPU_SETSIZE, size, set);
178b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_TRUE(CPU_ISSET_S(CPU_SETSIZE, size, set));
179b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
180b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set);
181b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
182b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
183b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_s_macros) {
184b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size = 64;
185b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size = CPU_ALLOC_SIZE(set_size);
186b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  cpu_set_t* set = CPU_ALLOC(set_size);
187b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
188b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size, set);
189b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < set_size; i++) {
190b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_FALSE(CPU_ISSET_S(i, size, set));
191b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    CPU_SET_S(i, size, set);
192b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_TRUE(CPU_ISSET_S(i, size, set));
193b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i + 1, CPU_COUNT_S(size, set));
194b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
195b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
196b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < set_size; i++) {
197b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    CPU_CLR_S(i, size, set);
198b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_FALSE(CPU_ISSET_S(i, size, set));
199b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(set_size - i - 1, CPU_COUNT_S(size, set));
200b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
201b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
202b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set);
203b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
204b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
205b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_op_s_macros) {
206b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size1 = 64;
207b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size2 = set_size1 * 2;
208b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size3 = set_size1 * 3;
209b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size1 = CPU_ALLOC_SIZE(set_size1);
210b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size2 = CPU_ALLOC_SIZE(set_size2);
211b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size3 = CPU_ALLOC_SIZE(set_size3);
212b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
2132d36750c3ad8fd91cf52cb0ed8f38ecfd88b0f13Elliott Hughes  cpu_set_t* set1 = CPU_ALLOC(set_size1);
2142d36750c3ad8fd91cf52cb0ed8f38ecfd88b0f13Elliott Hughes  cpu_set_t* set2 = CPU_ALLOC(set_size2);
2152d36750c3ad8fd91cf52cb0ed8f38ecfd88b0f13Elliott Hughes  cpu_set_t* set3 = CPU_ALLOC(set_size3);
216b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size1, set1);
217b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size2, set2);
218b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size3, set3);
219b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
220b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(0, size1, set1);
221b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(0, size2, set2);
222b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(1, size3, set2);
223b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
224b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_AND_S(size1, set3, set1, set2);
225b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < set_size3; i++) {
226b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0, CPU_ISSET_S(i, size3, set3));
227b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
228b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
229b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_OR_S(size1, set3, set1, set2);
230b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < set_size3; i++) {
231b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 0 || i == 1, CPU_ISSET_S(i, size3, set3));
232b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
233b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
234b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_XOR_S(size1, set3, set1, set2);
235b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  for (int i = 0; i < set_size3; i++) {
236b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle    ASSERT_EQ(i == 1, CPU_ISSET_S(i, size3, set3));
237b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  }
238b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
239b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set1);
240b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set2);
241b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set3);
242b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
243b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
244b743790ccabd9b0b93355ff693066478d10dae0dCalin JuravleTEST(sched, cpu_equal_s) {
245b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size1 = 64;
246b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  int set_size2 = set_size1 * 2;
247b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size1 = CPU_ALLOC_SIZE(set_size1);
248b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  size_t size2 = CPU_ALLOC_SIZE(set_size2);
249b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
2502d36750c3ad8fd91cf52cb0ed8f38ecfd88b0f13Elliott Hughes  cpu_set_t* set1 = CPU_ALLOC(set_size1);
2512d36750c3ad8fd91cf52cb0ed8f38ecfd88b0f13Elliott Hughes  cpu_set_t* set2 = CPU_ALLOC(set_size2);
252b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
253b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size1, set1);
254b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_ZERO_S(size2, set2);
255b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
256b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(0, size1, set1);
257b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set1));
258b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_FALSE(CPU_EQUAL_S(size1, set1, set2));
259b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_SET_S(0, size2, set2);
260b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  ASSERT_TRUE(CPU_EQUAL_S(size1, set1, set2));
261b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle
262b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set1);
263b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle  CPU_FREE(set2);
264b743790ccabd9b0b93355ff693066478d10dae0dCalin Juravle}
265