Updated test files to match dali-core
[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
20 #include <iostream>
21 #include <sstream>
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(std::string prefix)
50 : mTraceActive(false),
51   mPrefix(prefix)
52 {
53 }
54
55 /**
56  * Destructor
57  */
58 TraceCallStack::~TraceCallStack()
59 {
60 }
61
62 /**
63  * Turn on / off tracing
64  */
65 void TraceCallStack::Enable(bool enable)
66 {
67   mTraceActive = enable;
68 }
69
70 bool TraceCallStack::IsEnabled()
71 {
72   return mTraceActive;
73 }
74
75 void TraceCallStack::EnableLogging(bool enablelogging)
76 {
77   mLogging = enablelogging;
78 }
79
80 /**
81  * Push a call onto the stack if the trace is active
82  * @param[in] method The name of the method
83  * @param[in] params A comma separated list of parameter values
84  */
85 void TraceCallStack::PushCall(std::string method, std::string params)
86 {
87   if(mTraceActive)
88   {
89     FunctionCall stackFrame(method, params);
90     mCallStack.push_back(stackFrame);
91   }
92   if(mLogging)
93   {
94     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
95   }
96 }
97
98 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
99 {
100   if(mTraceActive)
101   {
102     FunctionCall stackFrame(method, params, altParams);
103     mCallStack.push_back(stackFrame);
104   }
105   if(mLogging)
106   {
107     fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
108   }
109 }
110
111 /**
112  * Search for a method in the stack
113  * @param[in] method The name of the method
114  * @return true if the method was in the stack
115  */
116 bool TraceCallStack::FindMethod(std::string method) const
117 {
118   bool found = false;
119   for(size_t i = 0; i < mCallStack.size(); i++)
120   {
121     if(0 == mCallStack[i].method.compare(method))
122     {
123       found = true;
124       break;
125     }
126   }
127   if(!found)
128   {
129     fprintf(stderr, "Search for %s failed\n", method.c_str());
130   }
131   return found;
132 }
133
134 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
135 {
136   bool found = false;
137   for(size_t i = 0; i < mCallStack.size(); i++)
138   {
139     if(0 == mCallStack[i].method.compare(method))
140     {
141       found  = true;
142       params = mCallStack[i].paramList;
143       break;
144     }
145   }
146   if(!found)
147   {
148     fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
149   }
150   return found;
151 }
152
153 int TraceCallStack::CountMethod(std::string method) const
154 {
155   int numCalls = 0;
156   for(size_t i = 0; i < mCallStack.size(); i++)
157   {
158     if(0 == mCallStack[i].method.compare(method))
159     {
160       numCalls++;
161     }
162   }
163   return numCalls;
164 }
165
166 /**
167  * Search for a method in the stack with the given parameter list
168  * @param[in] method The name of the method
169  * @param[in] params A comma separated list of parameter values
170  * @return true if the method was in the stack
171  */
172 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
173 {
174   return FindIndexFromMethodAndParams(method, params) > -1;
175 }
176
177 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
178 {
179   return FindIndexFromMethodAndParams(method, params) > -1;
180 }
181
182 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
183 {
184   for(size_t i = startIndex; i < mCallStack.size(); ++i)
185   {
186     if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
187     {
188       startIndex = i;
189       return true;
190     }
191   }
192   return false;
193 }
194
195 /**
196  * Search for a method in the stack with the given parameter list
197  * @param[in] method The name of the method
198  * @param[in] params A comma separated list of parameter values
199  * @return index in the stack where the method was found or -1 otherwise
200  */
201 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
202 {
203   int32_t index = -1;
204   for(size_t i = 0; i < mCallStack.size(); i++)
205   {
206     if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
207     {
208       index = static_cast<int32_t>(i);
209       break;
210     }
211   }
212   return index;
213 }
214
215 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
216 {
217   int32_t index = -1;
218   for(size_t i = 0; i < mCallStack.size(); i++)
219   {
220     if(0 == mCallStack[i].method.compare(method))
221     {
222       // Test each of the passed in parameters:
223       bool match = true;
224       for(NamedParams::const_iterator iter = params.begin(); iter != params.end(); ++iter)
225       {
226         NamedParams::const_iterator paramIter = mCallStack[i].namedParams.find(iter->first);
227         if(paramIter == params.end() || paramIter->second.compare(iter->second) != 0)
228         {
229           match = false;
230           break;
231         }
232       }
233       if(match == true)
234       {
235         index = static_cast<int32_t>(i);
236         break;
237       }
238     }
239   }
240   return index;
241 }
242
243 /**
244  * Test if the given method and parameters are at a given index in the stack
245  * @param[in] index Index in the call stack
246  * @param[in] method Name of method to test
247  * @param[in] params A comma separated list of parameter values to test
248  */
249 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
250 {
251   return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
252 }
253
254 /**
255  * Reset the call stack
256  */
257 void TraceCallStack::Reset()
258 {
259   mCallStack.clear();
260 }
261
262 } // namespace Dali