1/*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 *     http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16#include <stdio.h>
17#include <errno.h>
18#include <sched.h>
19#include <unistd.h>
20#include "test.h"
21
22/*
23 * @tc.name      : sched_setaffinity_0100
24 * @tc.desc      : Invalid cpu set size
25 * @tc.level     : Level 0
26 */
27void sched_setaffinity_0100(void)
28{
29    errno = 0;
30    int max = sysconf(_SC_NPROCESSORS_CONF) + 1;
31    cpu_set_t *set = CPU_ALLOC(max);
32    if (set == NULL) {
33        t_error("%s failed, set is NULL", __func__);
34    }
35
36    size_t set_size = CPU_ALLOC_SIZE(max);
37    CPU_ZERO_S(set_size, set);
38    CPU_SET_S(max, set_size, set);
39
40    int result = sched_setaffinity(0, set_size, set);
41    if (result != -1) {
42        t_error("%s affinity mask does not trigger an error", __func__);
43    }
44
45    if (errno != EINVAL) {
46        t_error("%s errno is %d", __func__, errno);
47    }
48}
49
50/*
51 * @tc.name      : sched_setaffinity_0100
52 * @tc.desc      : Set cpu affinity
53 * @tc.level     : Level 1
54 */
55void sched_setaffinity_0200(void)
56{
57    cpu_set_t mask;
58    cpu_set_t get;
59
60    CPU_ZERO(&mask);
61    CPU_SET(0, &mask);
62
63    int result = sched_setaffinity(0, sizeof(mask), &mask);
64    if (result == -1) {
65        t_error("%s failed to set cpu affinity\n", __func__);
66    }
67
68    CPU_ZERO(&get);
69    result = sched_getaffinity(0, sizeof(get), &get);
70    if (result == -1) {
71        t_error("%s sched_getaffinity failed\n", __func__);
72    }
73
74    if (!CPU_ISSET(0, &get)) {
75        t_error("%s getaffinity is not 0\n", __func__);
76    }
77}
78
79int main(int argc, char *argv[])
80{
81    sched_setaffinity_0100();
82    sched_setaffinity_0200();
83    return t_status;
84}