Merge branch 'devel/graphics' into devel/master
[platform/core/uifw/dali-toolkit.git] / automated-tests / src / dali-toolkit / dali-toolkit-test-utils / test-trace-call-stack.cpp
1 /*
2  * Copyright (c) 2021 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 #include <iostream>
20 #include <sstream>
21 #include "dali-test-suite-utils.h"
22
23 namespace Dali
24 {
25 std::string ToString(int x)
26 {
27   std::stringstream out;
28   out << x;
29   return out.str();
30 }
31
32 std::string ToString(unsigned int x)
33 {
34   std::stringstream out;
35   out << x;
36   return out.str();
37 }
38
39 std::string ToString(float x)
40 {
41   std::stringstream out;
42   out << x;
43   return out.str();
44 }
45
46 /**
47  * Constructor
48  */
49 TraceCallStack::TraceCallStack(bool logging, std::string prefix)
50 : mTraceActive(false),
51   mLogging(logging),
52   mPrefix(prefix)
53 {
54 }
55
56 /**
57  * Destructor
58  */
59 TraceCallStack::~TraceCallStack()
60 {
61 }
62
63 /**
64  * Turn on / off tracing
65  */
66 void TraceCallStack::Enable(bool enable)
67 {
68   mTraceActive = enable;
69 }
70
71 bool TraceCallStack::IsEnabled()
72 {
73   return mTraceActive;
74 }
75
76 void TraceCallStack::EnableLogging(bool enablelogging)
77 {
78   mLogging = enablelogging;
79 }
80
81 /**
82  * Push a call onto the stack if the trace is active
83  * @param[in] method The name of the method
84  * @param[in] params A comma separated list of parameter values
85  */
86 void TraceCallStack::PushCall(std::string method, std::string params)
87 {
88   if(mTraceActive)
89   {
90     FunctionCall stackFrame(method, params);
91     mCallStack.push_back(stackFrame);
92   }
93   if(mLogging)
94   {
95     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
96   }
97 }
98
99 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
100 {
101   if(mTraceActive)
102   {
103     FunctionCall stackFrame(method, params, altParams);
104     mCallStack.push_back(stackFrame);
105   }
106   if(mLogging)
107   {
108     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
109   }
110 }
111
112 /**
113  * Search for a method in the stack
114  * @param[in] method The name of the method
115  * @return true if the method was in the stack
116  */
117 bool TraceCallStack::FindMethod(std::string method) const
118 {
119   bool found = false;
120   for(size_t i = 0; i < mCallStack.size(); i++)
121   {
122     if(0 == mCallStack[i].method.compare(method))
123     {
124       found = true;
125       break;
126     }
127   }
128   if(!found)
129   {
130     fprintf(stderr, "Search for %s failed\n", method.c_str());
131   }
132   return found;
133 }
134
135 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
136 {
137   bool found = false;
138   for(size_t i = 0; i < mCallStack.size(); i++)
139   {
140     if(0 == mCallStack[i].method.compare(method))
141     {
142       found  = true;
143       params = mCallStack[i].paramList;
144       break;
145     }
146   }
147   if(!found)
148   {
149     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
150   }
151   return found;
152 }
153
154 int TraceCallStack::CountMethod(std::string method) const
155 {
156   int numCalls = 0;
157   for(size_t i = 0; i < mCallStack.size(); i++)
158   {
159     if(0 == mCallStack[i].method.compare(method))
160     {
161       numCalls++;
162     }
163   }
164   return numCalls;
165 }
166
167 /**
168  * Search for a method in the stack with the given parameter list
169  * @param[in] method The name of the method
170  * @param[in] params A comma separated list of parameter values
171  * @return true if the method was in the stack
172  */
173 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
174 {
175   return FindIndexFromMethodAndParams(method, params) > -1;
176 }
177
178 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
179 {
180   return FindIndexFromMethodAndParams(method, params) > -1;
181 }
182
183 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
184 {
185   for(size_t i = startIndex; i < mCallStack.size(); ++i)
186   {
187     if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
188     {
189       startIndex = i;
190       return true;
191     }
192   }
193   return false;
194 }
195
196 /**
197  * Search for a method in the stack with the given parameter list
198  * @param[in] method The name of the method
199  * @param[in] params A comma separated list of parameter values
200  * @return index in the stack where the method was found or -1 otherwise
201  */
202 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
203 {
204   int32_t index = -1;
205   for(size_t i = 0; i < mCallStack.size(); i++)
206   {
207     if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
208     {
209       index = static_cast<int32_t>(i);
210       break;
211     }
212   }
213   if(index == -1)
214   {
215     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
216   }
217   return index;
218 }
219
220 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
221 {
222   int32_t index = -1;
223   for(size_t i = 0; i < mCallStack.size(); i++)
224   {
225     if(0 == mCallStack[i].method.compare(method))
226     {
227       // Test each of the passed in parameters:
228       bool match = true;
229
230       for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
231       {
232         auto        paramIter = mCallStack[i].namedParams.find(iter->parameterName);
233         std::string value     = paramIter->value.str();
234         std::string iValue    = iter->value.str();
235
236         if(paramIter == mCallStack[i].namedParams.end() || value.compare(iValue))
237         {
238           match = false;
239           break;
240         }
241       }
242       if(match == true)
243       {
244         index = static_cast<int32_t>(i);
245         break;
246       }
247     }
248   }
249
250   if(index == -1)
251   {
252     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.str().c_str());
253   }
254
255   return index;
256 }
257
258 const TraceCallStack::NamedParams* TraceCallStack::FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const
259 {
260   int index = -1;
261
262   if(mCallStack.size() > 0)
263   {
264     for(index = static_cast<int>(mCallStack.size() - 1); index >= 0; --index)
265     {
266       if(0 == mCallStack[index].method.compare(method))
267       {
268         // Test each of the passed in parameters:
269         bool match = true;
270
271         for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
272         {
273           auto        paramIter = mCallStack[index].namedParams.find(iter->parameterName);
274           std::string value     = paramIter->value.str();
275           std::string iValue    = iter->value.str();
276
277           if(paramIter == mCallStack[index].namedParams.end() || value.compare(iValue))
278           {
279             match = false;
280             break;
281           }
282         }
283         if(match == true)
284         {
285           break;
286         }
287       }
288     }
289   }
290   if(index >= 0)
291   {
292     return &mCallStack[index].namedParams;
293   }
294   return nullptr;
295 }
296
297 /**
298  * Test if the given method and parameters are at a given index in the stack
299  * @param[in] index Index in the call stack
300  * @param[in] method Name of method to test
301  * @param[in] params A comma separated list of parameter values to test
302  */
303 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
304 {
305   return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
306 }
307
308 /**
309  * Reset the call stack
310  */
311 void TraceCallStack::Reset()
312 {
313   mCallStack.clear();
314 }
315
316 bool TraceCallStack::NamedParams::NameValue::operator==(int match) const
317 {
318   std::ostringstream matchStr;
319   matchStr << match;
320   std::string valueStr = value.str();
321   bool        retval   = !valueStr.compare(matchStr.str());
322   if(!retval)
323   {
324     tet_printf("Comparing parameter \"%s\": %s with %s failed\n", parameterName.c_str(), value.str().c_str(), matchStr.str().c_str());
325   }
326   return retval;
327 }
328
329 } // namespace Dali