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