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