9fde7c4a2d6a24a56494134fab109fa41440a787
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.cpp
1 /*
2  * Copyright (c) 2020 Samsung Electronics Co., Ltd.
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
18 #include "test-trace-call-stack.h"
19
20 #include <sstream>
21
22 namespace Dali
23 {
24 std::string ToString(int x)
25 {
26   std::stringstream out;
27   out << x;
28   return out.str();
29 }
30
31 std::string ToString(unsigned int x)
32 {
33   std::stringstream out;
34   out << x;
35   return out.str();
36 }
37
38 std::string ToString(float x)
39 {
40   std::stringstream out;
41   out << x;
42   return out.str();
43 }
44
45 /**
46  * Constructor
47  */
48 TraceCallStack::TraceCallStack()
49 : mTraceActive(false)
50 {
51 }
52
53 /**
54  * Destructor
55  */
56 TraceCallStack::~TraceCallStack()
57 {
58 }
59
60 /**
61  * Turn on / off tracing
62  */
63 void TraceCallStack::Enable(bool enable)
64 {
65   mTraceActive = enable;
66 }
67
68 bool TraceCallStack::IsEnabled()
69 {
70   return mTraceActive;
71 }
72
73 /**
74  * Push a call onto the stack if the trace is active
75  * @param[in] method The name of the method
76  * @param[in] params A comma separated list of parameter values
77  */
78 void TraceCallStack::PushCall(std::string method, std::string params)
79 {
80   if(mTraceActive)
81   {
82     FunctionCall stackFrame(method, params);
83     mCallStack.push_back(stackFrame);
84   }
85 }
86
87 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
88 {
89   if(mTraceActive)
90   {
91     FunctionCall stackFrame(method, params, altParams);
92     mCallStack.push_back(stackFrame);
93   }
94 }
95
96 /**
97  * Search for a method in the stack
98  * @param[in] method The name of the method
99  * @return true if the method was in the stack
100  */
101 bool TraceCallStack::FindMethod(std::string method) const
102 {
103   bool found = false;
104   for(size_t i = 0; i < mCallStack.size(); i++)
105   {
106     if(0 == mCallStack[i].method.compare(method))
107     {
108       found = true;
109       break;
110     }
111   }
112   return found;
113 }
114
115 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
116 {
117   bool found = false;
118   for(size_t i = 0; i < mCallStack.size(); i++)
119   {
120     if(0 == mCallStack[i].method.compare(method))
121     {
122       found  = true;
123       params = mCallStack[i].paramList;
124       break;
125     }
126   }
127   return found;
128 }
129
130 int TraceCallStack::CountMethod(std::string method) const
131 {
132   int numCalls = 0;
133   for(size_t i = 0; i < mCallStack.size(); i++)
134   {
135     if(0 == mCallStack[i].method.compare(method))
136     {
137       numCalls++;
138     }
139   }
140   return numCalls;
141 }
142
143 /**
144  * Search for a method in the stack with the given parameter list
145  * @param[in] method The name of the method
146  * @param[in] params A comma separated list of parameter values
147  * @return true if the method was in the stack
148  */
149 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
150 {
151   return FindIndexFromMethodAndParams(method, params) > -1;
152 }
153
154 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
155 {
156   return FindIndexFromMethodAndParams(method, params) > -1;
157 }
158
159 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
160 {
161   for(size_t i = startIndex; i < mCallStack.size(); ++i)
162   {
163     if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
164     {
165       startIndex = i;
166       return true;
167     }
168   }
169   return false;
170 }
171
172 /**
173  * Search for a method in the stack with the given parameter list
174  * @param[in] method The name of the method
175  * @param[in] params A comma separated list of parameter values
176  * @return index in the stack where the method was found or -1 otherwise
177  */
178 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
179 {
180   int32_t index = -1;
181   for(size_t i = 0; i < mCallStack.size(); i++)
182   {
183     if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
184     {
185       index = static_cast<int32_t>(i);
186       break;
187     }
188   }
189   return index;
190 }
191
192 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
193 {
194   int32_t index = -1;
195   for(size_t i = 0; i < mCallStack.size(); i++)
196   {
197     if(0 == mCallStack[i].method.compare(method))
198     {
199       // Test each of the passed in parameters:
200       bool match = true;
201       for(NamedParams::const_iterator iter = params.begin(); iter != params.end(); ++iter)
202       {
203         NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
204         if(paramIter == params.end() || paramIter->second.compare(iter->second) != 0)
205         {
206           match = false;
207           break;
208         }
209       }
210       if(match == true)
211       {
212         index = static_cast<int32_t>(i);
213         break;
214       }
215     }
216   }
217   return index;
218 }
219
220 /**
221  * Test if the given method and parameters are at a given index in the stack
222  * @param[in] index Index in the call stack
223  * @param[in] method Name of method to test
224  * @param[in] params A comma separated list of parameter values to test
225  */
226 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
227 {
228   return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
229 }
230
231 /**
232  * Reset the call stack
233  */
234 void TraceCallStack::Reset()
235 {
236   mCallStack.clear();
237 }
238
239 } // namespace Dali