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 <stdlib.h>
17 #include <signal.h>
18 #include <pthread.h>
19 #include <sys/wait.h>
20 #include "functionalext.h"
21
22 #define NUM 8
compare(const void * p,const void * q)23 int compare(const void *p, const void *q)
24 {
25 return (*(int *)p - *(int *)q);
26 }
27
28 /**
29 * @tc.name : bsearch_0100
30 * @tc.desc : Verify that binary search can be achieved (each parameter is valid)
31 * @tc.level : Level 0
32 */
bsearch_0100(void)33 void bsearch_0100(void)
34 {
35 int array[NUM] = {9, 2, 7, 11, 3, 87, 34, 6};
36 int key = 3;
37 int *p;
38 qsort(array, NUM, sizeof(int), compare);
39 p = (int *)bsearch(&key, array, NUM, sizeof(int), compare);
40 EXPECT_EQ("bsearch_0100", (*p), key);
41 }
42
43 /**
44 * @tc.name : bsearch_0200
45 * @tc.desc : Binary lookup cannot be implemented in validation (invalid parameters)
46 * @tc.level : Level 2
47 */
bsearch_0200(void)48 void bsearch_0200(void)
49 {
50 int array[NUM] = {9, 2, 7, 11, 3, 87, 34, 6};
51 int key = 3;
52 int *p;
53 qsort(array, NUM, sizeof(int), compare);
54 p = (int *)bsearch(&key, array, 0, sizeof(int), compare);
55 EXPECT_TRUE("bsearch_0200", p == NULL);
56 }
57
main(int argc,char * argv[])58 int main(int argc, char *argv[])
59 {
60 bsearch_0100();
61 bsearch_0200();
62 return t_status;
63 }