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 <malloc.h>
17 #include <unistd.h>
18 #include "functionalext.h"
19
20 /**
21 * @tc.name : memalign_0100
22 * @tc.desc : Each parameter is valid, and align is obtained by getpagesize(), which can allocate a memory block
23 * whose size is specified by len and whose address is a multiple of align.
24 * @tc.level : Level 0
25 */
memalign_0100(void)26 void memalign_0100(void)
27 {
28 int align = getpagesize();
29 EXPECT_NE("memalign_0100", align, -1);
30 void *buff = (void *)memalign(align, 8 * align);
31 EXPECT_TRUE("memalign_0100", buff != NULL);
32 free(buff);
33 buff = NULL;
34 }
35
36 /**
37 * @tc.name : memalign_0200
38 * @tc.desc : Each parameter is valid, and it can allocate a memory block with the size specified by
39 * len and the address is a multiple of align
40 * @tc.level : Level 1
41 */
memalign_0200(void)42 void memalign_0200(void)
43 {
44 int align = 16;
45 void *buff = NULL;
46 buff = (void *)memalign(align, 8 * align);
47 EXPECT_TRUE("memalign_0200", buff != NULL);
48 free(buff);
49 buff = NULL;
50 }
51
52 /**
53 * @tc.name : memalign_0300
54 * @tc.desc : The align parameter is invalid, and a memory block whose size is specified
55 * by len and whose address is a multiple of align cannot be allocated
56 * @tc.level : Level 2
57 */
memalign_0300(void)58 void memalign_0300(void)
59 {
60 int align = getpagesize() - 1;
61 EXPECT_NE("memalign_0300", align, -1);
62 void *buff = (void *)memalign(align, 128);
63 EXPECT_EQ("memalign_0300", buff, NULL);
64 free(buff);
65 buff = NULL;
66 }
67
68 /**
69 * @tc.name : memalign_0400
70 * @tc.desc : The len parameter is invalid, and a memory block whose size is specified by
71 * len and whose address is a multiple of align cannot be allocated
72 * @tc.level : Level 2
73 */
memalign_0400(void)74 void memalign_0400(void)
75 {
76 int align = getpagesize();
77 EXPECT_NE("memalign_0400", align, -1);
78 void *buff = (void *)memalign(align, SIZE_MAX - align + 1);
79 EXPECT_EQ("memalign_0400", buff, NULL);
80 free(buff);
81 buff = NULL;
82 }
83
84 /**
85 * @tc.name : memalign_0500
86 * @tc.desc : Insufficient memory space to allocate a memory block
87 * with a size specified by len and an address that is a multiple of align
88 * @tc.level : Level 2
89 */
memalign_0500(void)90 void memalign_0500(void)
91 {
92 int align = (4 * sizeof(size_t)) + 1;
93 void *buff = (void *)memalign(align, align * 1024);
94 EXPECT_EQ("memalign_0500", buff, NULL);
95 free(buff);
96 buff = NULL;
97 }
98
main(int argc,char * argv[])99 int main(int argc, char *argv[])
100 {
101 memalign_0100();
102 memalign_0200();
103 memalign_0300();
104 memalign_0400();
105 memalign_0500();
106
107 return t_status;
108 }
109