diff options
author | 2023-01-11 18:22:01 -0800 | |
---|---|---|
committer | 2023-01-12 00:42:48 -0800 | |
commit | ded3d038547e20f83249f27e3ae122b749c5ea7c (patch) | |
tree | dc1657c8fa3feb5cd97787937b8c8b533bf616a4 | |
parent | aea515f10230e690821cf3aae44353c4e3b52d2d (diff) |
Mock static initialization order 01
cpp static order initialization is not guarantee.
In order to avoid the mess, we need to have a lazy init schema.
Bug: 265217208
Test: atest --host
Change-Id: Ia650949b54359b4e610d8dee760a3decf16ded75
-rw-r--r-- | system/test/common/mock_functions.cc | 18 | ||||
-rw-r--r-- | system/test/common/mock_functions.h | 7 |
2 files changed, 22 insertions, 3 deletions
diff --git a/system/test/common/mock_functions.cc b/system/test/common/mock_functions.cc index 3278b92470..85a7c646d4 100644 --- a/system/test/common/mock_functions.cc +++ b/system/test/common/mock_functions.cc @@ -21,12 +21,24 @@ std::map<std::string, int> mock_function_count_map; -void reset_mock_function_count_map() { mock_function_count_map.clear(); } +static std::map<std::string, int>& _get_func_call_count_map() { + // TODO(265217208) return singleton map instead + // static std::map<std::string, int> mock_function_count_map; + return mock_function_count_map; +} + +int get_func_call_count(const char* fn) { + return _get_func_call_count_map()[fn]; +} +void inc_func_call_count(const char* fn) { _get_func_call_count_map()[fn]++; } + +void reset_mock_function_count_map() { _get_func_call_count_map().clear(); } void dump_mock_function_count_map() { - LOG_INFO("Mock function count map size:%zu", mock_function_count_map.size()); + LOG_INFO("Mock function count map size:%zu", + _get_func_call_count_map().size()); - for (auto it : mock_function_count_map) { + for (const auto& it : _get_func_call_count_map()) { LOG_INFO("function:%s: call_count:%d", it.first.c_str(), it.second); } } diff --git a/system/test/common/mock_functions.h b/system/test/common/mock_functions.h index d8bf6552a0..04c1325f1d 100644 --- a/system/test/common/mock_functions.h +++ b/system/test/common/mock_functions.h @@ -17,7 +17,14 @@ #pragma once #include <map> +#include <string> +// TODO(265217208) Remove +// Usage is deprecated, use get_func_call_count / inc_func_call_count instead extern std::map<std::string, int> mock_function_count_map; + +int get_func_call_count(const char* fn); +void inc_func_call_count(const char* fn); + void dump_mock_function_count_map(); void reset_mock_function_count_map(); |