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 <search.h>
17 #include "functionalext.h"
18
19 const int KEY = 10;
20 const int LENGTH1 = 10;
21 const int LENGTH2 = 5;
22
compare(int * x,int * y)23 int compare(int *x, int *y)
24 {
25 return (*x - *y);
26 }
27
28 /**
29 * @tc.name : lfind_0100
30 * @tc.desc : Verify lfind process success.
31 * @tc.level : level 0.
32 */
lfind_0100(void)33 void lfind_0100(void)
34 {
35 int array[10] = {1, 2, 3, 4, 5, 6, 7, 8, 9, 10};
36 size_t nlength = LENGTH1;
37 int key;
38 int *ret;
39 key = KEY;
40 ret = lfind(&key, array, &nlength, sizeof(int), (int (*)(const void *, const void *))compare);
41 EXPECT_NE("lfind_0100", *ret, 0);
42 }
43
44 /**
45 * @tc.name : lfind.
46 * @tc.desc : Verify lfind process fail because key not is array.
47 * @tc.level : level 2.
48 */
lfind_0200(void)49 void lfind_0200(void)
50 {
51 int array[5] = {1, 2, 3, 4, 5};
52 size_t nlength = LENGTH2;
53 int key;
54 int *ret;
55 key = 0;
56 ret = lfind(&key, array, &nlength, sizeof(int), (int (*)(const void *, const void *))compare);
57 EXPECT_PTREQ("lfind_0200", ret, NULL);
58 }
59
main(void)60 int main(void)
61 {
62 lfind_0100();
63 lfind_0200();
64 return t_status;
65 }
66