• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2023 Huawei Device Co., Ltd. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification,
5  * are permitted provided that the following conditions are met:
6  *
7  * 1. Redistributions of source code must retain the above copyright notice, this list of
8  *    conditions and the following disclaimer.
9  *
10  * 2. Redistributions in binary form must reproduce the above copyright notice, this list
11  *    of conditions and the following disclaimer in the documentation and/or other materials
12  *    provided with the distribution.
13  *
14  * 3. Neither the name of the copyright holder nor the names of its contributors may be used
15  *    to endorse or promote products derived from this software without specific prior written
16  *    permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
19  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO,
20  * THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
21  * PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR
22  * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
23  * EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
24  * PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
25  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY,
26  * WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR
27  * OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
28  * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
29  */
30 
31 #include "sched_api_test.h"
32 
33 LITE_TEST_SUIT(SCHED, PthreadSchedApi, PthreadSchedApiTestSuite);
34 
35 static int g_policy = 0;
36 static int g_prioriy = 0;
37 
PthreadSchedApiTestSuiteSetUp(void)38 static BOOL PthreadSchedApiTestSuiteSetUp(void)
39 {
40     return TRUE;
41 }
42 
PthreadSchedApiTestSuiteTearDown(void)43 static BOOL PthreadSchedApiTestSuiteTearDown(void)
44 {
45     return TRUE;
46 }
47 
48 /**
49  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0100
50  * @tc.name     test the default value of inheritsched.
51  * @tc.desc     [C- SOFTWARE -0200]
52  */
53 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrGetInheritsched, Function | MediumTest | Level1)
54 {
55     int ret;
56     pthread_attr_t attr;
57     ret = pthread_attr_init(&attr);
58     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
59 
60     int inheritsched = -1; /* -1, common data for test, no special meaning */
61     int rt = pthread_attr_getinheritsched(&attr, &inheritsched);
62     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
63     ICUNIT_ASSERT_EQUAL(inheritsched, PTHREAD_INHERIT_SCHED, inheritsched);
64     return 0;
65 }
66 
67 /**
68  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0200
69  * @tc.name     test set and get inheritsched.
70  * @tc.desc     [C- SOFTWARE -0200]
71  */
72 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrSetInheritsched, Function | MediumTest | Level1)
73 {
74     int ret;
75     pthread_attr_t attr;
76     ret = pthread_attr_init(&attr);
77     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
78 
79     int rt = pthread_attr_setinheritsched(&attr, PTHREAD_INHERIT_SCHED);
80     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
81     int inheritsched = -1; /* -1, common data for test, no special meaning */
82     rt = pthread_attr_getinheritsched(&attr, &inheritsched);
83     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
84     ICUNIT_ASSERT_EQUAL(inheritsched, PTHREAD_INHERIT_SCHED, inheritsched);
85 
86     rt = pthread_attr_setinheritsched(&attr, PTHREAD_EXPLICIT_SCHED);
87     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
88     inheritsched = -1; /* -1, common data for test, no special meaning */
89     rt = pthread_attr_getinheritsched(&attr, &inheritsched);
90     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
91     ICUNIT_ASSERT_EQUAL(inheritsched, PTHREAD_EXPLICIT_SCHED, inheritsched);
92     return 0;
93 }
94 
95 /**
96  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_INHERIT_0300
97  * @tc.name     pthread_attr_setinheritsched error test.
98  * @tc.desc     [C- SOFTWARE -0200]
99  */
100 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrSetInheritschedError, Function | MediumTest | Level1)
101 {
102     int ret;
103     pthread_attr_t attr;
104     ret = pthread_attr_init(&attr);
105     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
106 
107     int n = -GetRandom(100); /* 100, common data for test, no special meaning */
108     int rt = pthread_attr_setinheritsched(&attr, n);
109     ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
110     n = 2 + GetRandom(100); /* 2, 100, common data for test, no special meaning */
111     rt = pthread_attr_setinheritsched(&attr, n);
112     ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
113     return 0;
114 }
115 
116 /**
117  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPARAM_0200
118  * @tc.name     test set and get sched param.
119  * @tc.desc     [C- SOFTWARE -0200]
120  */
121 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrSetSchedParam, Function | MediumTest | Level1)
122 {
123     int ret;
124     pthread_attr_t attr;
125     ret = pthread_attr_init(&attr);
126     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
127 
128     struct sched_param param = {0};
129     int rt = pthread_attr_getschedparam(&attr, &param);
130     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
131 
132     param.sched_priority = 22; /* 22, common data for test, no special meaning */
133     rt = pthread_attr_setschedparam(&attr, &param);
134     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
135 
136     rt = pthread_attr_getschedparam(&attr, &param);
137     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
138     ICUNIT_ASSERT_EQUAL(param.sched_priority, 22, param.sched_priority); /* 22, common data for test, no special meaning */
139     return 0;
140 }
141 
142 /**
143  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPOLICY_0100
144  * @tc.name     test the default value of sched policy.
145  * @tc.desc     [C- SOFTWARE -0200]
146  */
147 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrGetSchedPolicy, Function | MediumTest | Level1)
148 {
149     int ret;
150     pthread_attr_t attr;
151     ret = pthread_attr_init(&attr);
152     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
153 
154     int policy = -1; /* -1, common data for test, no special meaning */
155     int rt = pthread_attr_getschedpolicy(&attr, &policy);
156     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
157     ICUNIT_ASSERT_EQUAL(policy, SCHED_RR, policy);
158     return 0;
159 }
160 
161 /**
162  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCHEDPOLICY_0300
163  * @tc.name     pthread_attr_setschedpolicy error test.
164  * @tc.desc     [C- SOFTWARE -0200]
165  */
166 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrSetSchedPolicyError, Function | MediumTest | Level1)
167 {
168     int ret;
169     pthread_attr_t attr;
170     ret = pthread_attr_init(&attr);
171     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
172     int rt;
173 
174     int invalidPolicy[7] = {SCHED_OTHER, SCHED_BATCH, SCHED_IDLE, SCHED_DEADLINE, SCHED_RESET_ON_FORK}; /* 7, common data for test, no special meaning */
175     invalidPolicy[5] = -GetRandom(10000); /* 5, 10000, common data for test, no special meaning */
176     invalidPolicy[6] = GetRandom(10000) + 6; /* 6, 10000, common data for test, no special meaning */
177     for (int i = 0; i < 7; i++) { /* 7, common data for test, no special meaning */
178         rt = pthread_attr_setschedpolicy(&attr, invalidPolicy[i]);
179         ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
180     }
181     return 0;
182 }
183 
184 /**
185  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCOPE_0100
186  * @tc.name     test the default value of sched scope.
187  * @tc.desc     [C- SOFTWARE -0200]
188  */
189 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrGetScope, Function | MediumTest | Level1)
190 {
191     int ret;
192     pthread_attr_t attr;
193     ret = pthread_attr_init(&attr);
194     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
195 
196     int scope = -1; /* -1, common data for test, no special meaning */
197     int rt = pthread_attr_getscope(&attr, &scope);
198     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
199     ICUNIT_ASSERT_EQUAL(scope, PTHREAD_SCOPE_SYSTEM, scope);
200     return 0;
201 }
202 
203 /**
204  * @tc.number   SUB_KERNEL_SCHED_API_PATTR_SCOPE_0200
205  * @tc.name     test set and get scope.
206  * @tc.desc     [C- SOFTWARE -0200]
207  */
208 LITE_TEST_CASE(PthreadSchedApiTestSuite, testAttrSetScope, Function | MediumTest | Level1)
209 {
210     int ret;
211     pthread_attr_t attr;
212     ret = pthread_attr_init(&attr);
213     ICUNIT_ASSERT_EQUAL(ret, 0, ret);
214 
215     int rt = pthread_attr_setscope(&attr, PTHREAD_SCOPE_PROCESS);
216     ICUNIT_ASSERT_EQUAL(rt, ENOTSUP, rt);
217 
218     rt = pthread_attr_setscope(&attr, -GetRandom(10000)); /* 10000, common data for test, no special meaning */
219     ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
220 
221     rt = pthread_attr_setscope(&attr, GetRandom(10000) + 2); /* 2, 10000, common data for test, no special meaning */
222     ICUNIT_ASSERT_EQUAL(rt, EINVAL, rt);
223 
224     int scope = -1; /* -1, common data for test, no special meaning */
225     rt = pthread_attr_getscope(&attr, &scope);
226     ICUNIT_ASSERT_EQUAL(rt, 0, rt);
227     ICUNIT_ASSERT_EQUAL(scope, PTHREAD_SCOPE_SYSTEM, scope);
228     return 0;
229 }
230 
231 RUN_TEST_SUITE(PthreadSchedApiTestSuite);
232 
PthreadSchedApiTest(void)233 void PthreadSchedApiTest(void)
234 {
235     RUN_ONE_TESTCASE(testAttrGetInheritsched);
236     RUN_ONE_TESTCASE(testAttrSetInheritsched);
237     RUN_ONE_TESTCASE(testAttrSetInheritschedError);
238     RUN_ONE_TESTCASE(testAttrSetSchedParam);
239     RUN_ONE_TESTCASE(testAttrGetSchedPolicy);
240     RUN_ONE_TESTCASE(testAttrSetSchedPolicyError);
241     RUN_ONE_TESTCASE(testAttrGetScope);
242     RUN_ONE_TESTCASE(testAttrSetScope);
243 }
244