1 /* 2 * Copyright (c) 2021 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 "ecmascript/mem/assert_scope.h" 17 #include "ecmascript/tests/test_helper.h" 18 19 using namespace panda::ecmascript; 20 21 namespace panda::test { 22 class AssertScopeTest : public BaseTestWithOutScope { 23 }; 24 HWTEST_F_L0(AssertScopeTest,AssertScopeChecker)25HWTEST_F_L0(AssertScopeTest, AssertScopeChecker) 26 { 27 #if !defined(NDEBUG) 28 { 29 EXPECT_TRUE(AllowHeapAlloc::IsAllowed()); 30 EXPECT_TRUE(AllowGarbageCollection::IsAllowed()); 31 [[maybe_unused]] DisallowGarbageCollection noGc; // set dis-allow gc 32 { 33 [[maybe_unused]] AllowGarbageCollection allowGc; // set allow gc 34 EXPECT_TRUE(AllowGarbageCollection::IsAllowed()); 35 36 [[maybe_unused]] DisAllowHeapAlloc noHeapAlloc; // set dis-allow alloc 37 { 38 [[maybe_unused]] AllowHeapAlloc heapAlloc; // set allow alloc 39 EXPECT_TRUE(AllowHeapAlloc::IsAllowed()); 40 } 41 EXPECT_FALSE(AllowHeapAlloc::IsAllowed()); 42 } 43 // allow alloc 44 EXPECT_TRUE(AllowHeapAlloc::IsAllowed()); 45 // dis-allow gc 46 EXPECT_FALSE(AllowGarbageCollection::IsAllowed()); 47 } 48 // allow gc 49 EXPECT_TRUE(AllowGarbageCollection::IsAllowed()); 50 #endif 51 EXPECT_TRUE(AllowHeapAlloc::IsAllowed()); 52 EXPECT_TRUE(AllowGarbageCollection::IsAllowed()); 53 } 54 HWTEST_F_L0(AssertScopeTest,AssertScopeChecker_LocalToShareWeakRef)55HWTEST_F_L0(AssertScopeTest, AssertScopeChecker_LocalToShareWeakRef) 56 { 57 #ifndef NDEBUG 58 EXPECT_FALSE(AllowLocalToShareWeakRefHandle::IsAllowed()); 59 { 60 [[maybe_unused]] AllowLocalToShareWeakRefHandle weakRef; // set allow local-to-share weak ref 61 EXPECT_TRUE(AllowLocalToShareWeakRefHandle::IsAllowed()); 62 { 63 [[maybe_unused]] DisAllowLocalToShareWeakRefHandle noWeakRef; // set dis-allow local-to-share weak ref 64 EXPECT_FALSE(AllowLocalToShareWeakRefHandle::IsAllowed()); 65 } 66 EXPECT_TRUE(AllowLocalToShareWeakRefHandle::IsAllowed()); 67 } 68 EXPECT_FALSE(AllowLocalToShareWeakRefHandle::IsAllowed()); 69 #endif 70 } 71 } // namespace panda::test 72