1 /*
2  * Copyright (C) 2021-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 #include <gtest/gtest.h>
16 #include <cinttypes>
17 #include <climits>
18 #include <cstdio>
19 #include <unistd.h>
20 #include <string>
21 
22 #define SIZE_ALIGN (4 * sizeof(size_t))
23 #define THRESHOLD (0x1c00 * SIZE_ALIGN)
24 const int ITER_TIME = 20;
25 const int TIME_SPEC_LEN = 2;
26 
27 using namespace testing::ext;
28 using namespace std;
29 
30 class MallocPressure : public testing::Test {
31 public:
32 
33     static void SetUpTestCase();
34     static void TearDownTestCase();
35     void SetUp();
36     void TearDown();
37 private:
38 };
39 
SetUp()40 void MallocPressure::SetUp()
41 {
42 }
TearDown()43 void MallocPressure::TearDown()
44 {
45 }
SetUpTestCase()46 void MallocPressure::SetUpTestCase()
47 {
48 }
TearDownTestCase()49 void MallocPressure::TearDownTestCase()
50 {
51 }
52 
53 /*
54  * @tc.number CAM_FREE_FUN_0100
55  * @tc.name Apply part of the memory camfree reduction
56  * @tc.desc Test the basic features of CamFree
57 */
HWTEST_F(MallocPressure, pressureTest0100, Function | MediumTest | Level1)58 HWTEST_F(MallocPressure, pressureTest0100, Function | MediumTest | Level1)
59 {
60     struct timespec ts[TIME_SPEC_LEN];
61     clock_gettime(CLOCK_REALTIME, &ts[0]);
62     for (int i = 0; i < ITER_TIME; ++i) {
63         for (size_t size = 0; size < THRESHOLD; size += SIZE_ALIGN + 1) {
64             void *ptr = malloc(size);
65             if (!ptr) {
66                 printf("Malloc failed for size %zu\n", size);
67                 ASSERT_TRUE(true) << "malloc error = " << errno;
68             }
69             free(ptr);
70             ASSERT_TRUE(true) << "Abnormal operation of the system, errno" << errno;
71         }
72     }
73 }
74 
75