1 /* 2 * Copyright (c) 2023 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 #ifndef ECMASCRIPT_PGO_PROFILER_TESTS_PGO_CONTEXT_MOCK_H 17 #define ECMASCRIPT_PGO_PROFILER_TESTS_PGO_CONTEXT_MOCK_H 18 19 #include <memory> 20 21 #include "ecmascript/log_wrapper.h" 22 #include "ecmascript/pgo_profiler/pgo_context.h" 23 #include "ecmascript/base/file_header.h" 24 #include "ecmascript/pgo_profiler/pgo_profiler_info.h" 25 #include "ecmascript/pgo_profiler/pgo_utils.h" 26 27 namespace panda::ecmascript::pgo { 28 class PGOContextMock : public PGOContext { 29 public: PGOContextMock(base::FileHeaderBase::VersionType version)30 explicit PGOContextMock(base::FileHeaderBase::VersionType version) 31 { 32 profileTypePool_ = std::make_shared<PGOProfileTypePool>(); 33 PGOProfilerHeader::Build(&header_, sizeof(PGOProfilerHeader)); 34 header_->SetVersion(version); 35 } 36 ~PGOContextMock() override 37 { 38 delete header_; 39 header_ = nullptr; 40 }; 41 42 std::shared_ptr<PGOProfileTypePool> GetProfileTypePool() const override 43 { 44 return profileTypePool_; 45 } 46 uint32_t GetHotnessThreshold() const override 47 { 48 return threshold_; 49 } 50 PGOProfilerHeader *GetHeader() const override 51 { 52 return header_; 53 } 54 bool SupportElementsKind() const override 55 { 56 ASSERT(header_ != nullptr); 57 return header_->SupportElementsKind(); 58 } 59 60 bool SupportElementsTrackInfo() const override 61 { 62 ASSERT(header_ != nullptr); 63 return header_->SupportElementsTrackInfo(); 64 } 65 66 void ResetAbcIdRemap() const override 67 { 68 abcIdRemap_.clear(); 69 } 70 71 void AddAbcIdRemap(ApEntityId oldId, ApEntityId newId) const override 72 { 73 abcIdRemap_[oldId] = newId; 74 } 75 76 const std::map<ApEntityId, ApEntityId> &GetAbcIdRemap() const override 77 { 78 return abcIdRemap_; 79 } 80 81 private: 82 NO_COPY_SEMANTIC(PGOContextMock); 83 NO_MOVE_SEMANTIC(PGOContextMock); 84 std::shared_ptr<PGOProfileTypePool> profileTypePool_; 85 PGOProfilerHeader *header_ {}; 86 uint32_t threshold_ {}; 87 mutable std::map<ApEntityId, ApEntityId> abcIdRemap_; 88 }; 89 } // namespace panda::ecmascript::pgo 90 #endif