1 /*
2  * Copyright 2021 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "test/common/mock_functions.h"
18 
19 #include <bluetooth/log.h>
20 
21 #include <map>
22 #include <mutex>
23 
24 std::mutex mutex_{};
25 
_get_func_call_count_map()26 static std::map<std::string, int>& _get_func_call_count_map() {
27   static std::map<std::string, int> mock_function_count_map;
28   return mock_function_count_map;
29 }
30 
get_func_call_count(const char * fn)31 int get_func_call_count(const char* fn) {
32   std::lock_guard<std::mutex> lock(mutex_);
33   return _get_func_call_count_map()[fn];
34 }
inc_func_call_count(const char * fn)35 void inc_func_call_count(const char* fn) {
36   std::lock_guard<std::mutex> lock(mutex_);
37   _get_func_call_count_map()[fn]++;
38 }
39 
reset_mock_function_count_map()40 void reset_mock_function_count_map() {
41   std::lock_guard<std::mutex> lock(mutex_);
42   _get_func_call_count_map().clear();
43 }
44 
get_func_call_size()45 int get_func_call_size() {
46   std::lock_guard<std::mutex> lock(mutex_);
47   return _get_func_call_count_map().size();
48 }
49 
dump_mock_function_count_map()50 void dump_mock_function_count_map() {
51   std::lock_guard<std::mutex> lock(mutex_);
52   bluetooth::log::info("Mock function count map size:{}",
53                        _get_func_call_count_map().size());
54 
55   for (const auto& it : _get_func_call_count_map()) {
56     bluetooth::log::info("function:{}: call_count:{}", it.first, it.second);
57   }
58 }
59