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.h
1 #ifndef TEST_TRACE_CALL_STACK_H
2 #define TEST_TRACE_CALL_STACK_H
3
4 /*
5  * Copyright (c) 2021 Samsung Electronics Co., Ltd.
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  *
19  */
20
21 #include <map>
22 #include <sstream>
23 #include <string>
24 #include <vector>
25
26 namespace Dali
27 {
28 template<typename T>
29 std::string ToString(const T& x)
30 {
31   return "undefined";
32 }
33
34 std::string ToString(int x);
35 std::string ToString(unsigned int x);
36 std::string ToString(float x);
37
38 /**
39  * Helper class to track method calls in the abstraction and search for them in test cases
40  */
41 class TraceCallStack
42 {
43 public:
44   /// Typedef for passing and storing named parameters
45   class NamedParams
46   {
47   public:
48     struct NameValue
49     {
50       std::string        parameterName;
51       std::ostringstream value;
52       NameValue(std::string pname, std::string aValue)
53       : parameterName(pname),
54         value(aValue)
55       {
56       }
57       NameValue(const NameValue& rhs)
58       : parameterName(rhs.parameterName),
59         value(rhs.value.str())
60       {
61       }
62       NameValue& operator=(const NameValue& rhs)
63       {
64         if(this != &rhs)
65         {
66           this->parameterName = rhs.parameterName;
67           this->value.str(rhs.value.str());
68         }
69         return *this;
70       }
71
72       bool operator==(const NameValue& rhs)
73       {
74         return !parameterName.compare(rhs.parameterName) && !value.str().compare(rhs.value.str());
75       }
76     };
77
78     auto find(const std::string& param) const
79     {
80       auto iter = mParams.begin();
81       for(; iter != mParams.end(); ++iter)
82       {
83         if(!iter->parameterName.compare(param))
84         {
85           break;
86         }
87       }
88       return iter;
89     }
90
91     auto begin() const
92     {
93       return mParams.begin();
94     }
95     auto end() const
96     {
97       return mParams.end();
98     }
99
100     std::ostringstream& operator[](std::string name)
101     {
102       auto iter = mParams.begin();
103       for(; iter != mParams.end(); ++iter)
104       {
105         if(!iter->parameterName.compare(name))
106         {
107           break;
108         }
109       }
110
111       if(iter != mParams.end())
112       {
113         return iter->value;
114       }
115       else
116       {
117         mParams.push_back(NameValue(name, ""));
118         return mParams.back().value;
119       }
120     }
121
122     std::string str() const
123     {
124       std::ostringstream out;
125       bool               first = true;
126       for(auto& elem : mParams)
127       {
128         out << (first ? "" : " ") << elem.parameterName << ": " << elem.value.str();
129         first = false;
130       }
131       return out.str();
132     }
133     std::vector<NameValue> mParams{};
134   };
135
136   /**
137    * Constructor
138    */
139   TraceCallStack(std::string prefix = "gl");
140   TraceCallStack(bool logging, std::string prefix = "gl");
141
142   /**
143    * Destructor
144    */
145   ~TraceCallStack();
146
147   /**
148    * Turn on / off tracing
149    */
150   void Enable(bool enable);
151
152   bool IsEnabled();
153
154   void EnableLogging(bool enable);
155
156   /**
157    * Push a call onto the stack if the trace is active
158    * @param[in] method The name of the method
159    * @param[in] params A comma separated list of parameter values
160    */
161   void PushCall(std::string method, std::string params);
162
163   /**
164    * Push a call onto the stack if the trace is active
165    * @param[in] method The name of the method
166    * @param[in] params A comma separated list of parameter values
167    * @param[in] altParams A map of named parameter values
168    */
169   void PushCall(std::string method, std::string params, const NamedParams& altParams);
170
171   /**
172    * Search for a method in the stack
173    * @param[in] method The name of the method
174    * @return true if the method was in the stack
175    */
176   bool FindMethod(std::string method) const;
177
178   /**
179    * Search for a method in the stack and return its parameters if found
180    * @param[in] method The name of the method
181    * @param[out] params of the method
182    * @return true if the method was in the stack
183    */
184   bool FindMethodAndGetParameters(std::string method, std::string& params) const;
185
186   /**
187    * Count how many times a method was called
188    * @param[in] method The name of the method
189    * @return The number of times it was called
190    */
191   int CountMethod(std::string method) const;
192
193   /**
194    * Search for a method in the stack with the given parameter list
195    * @param[in] method The name of the method
196    * @param[in] params A comma separated list of parameter values
197    * @return true if the method was in the stack
198    */
199   bool FindMethodAndParams(std::string method, std::string params) const;
200
201   /**
202    * Search for a method in the stack with the given parameter list
203    * @param[in] method The name of the method
204    * @param[in] params A map of named parameters to test for
205    * @return true if the method was in the stack
206    */
207   bool FindMethodAndParams(std::string method, const NamedParams& params) const;
208
209   /**
210    * Search for a method in the stack with the given parameter list.
211    * The search is done from a given index.
212    * This allows the order of methods and parameters to be checked.
213    * @param[in] method The name of the method
214    * @param[in] params A comma separated list of parameter values
215    * @param[in/out] startIndex The method index to start looking from.
216    *                This is updated if a method is found so subsequent
217    *                calls can search for methods occuring after this one.
218    * @return True if the method was in the stack
219    */
220   bool FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const;
221
222   /**
223    * Search for a method in the stack with the given parameter list
224    * @param[in] method The name of the method
225    * @param[in] params A comma separated list of parameter values
226    * @return index in the stack where the method was found or -1 otherwise
227    */
228   int FindIndexFromMethodAndParams(std::string method, std::string params) const;
229
230   /**
231    * Search for a method in the stack with the given parameter list
232    * @param[in] method The name of the method
233    * @param[in] params A map of named parameter values to match
234    * @return index in the stack where the method was found or -1 otherwise
235    */
236   int FindIndexFromMethodAndParams(std::string method, const NamedParams& params) const;
237
238   /**
239    * Test if the given method and parameters are at a given index in the stack
240    * @param[in] index Index in the call stack
241    * @param[in] method Name of method to test
242    * @param[in] params A comma separated list of parameter values to test
243    */
244   bool TestMethodAndParams(int index, std::string method, std::string params) const;
245
246   /**
247    * Reset the call stack
248    */
249   void Reset();
250
251   /**
252    * Method to display contents of the TraceCallStack.
253    * @return A string containing a list of function calls and parameters (may contain newline characters)
254    */
255   std::string GetTraceString()
256   {
257     std::stringstream traceStream;
258     std::size_t       functionCount = mCallStack.size();
259     for(std::size_t i = 0; i < functionCount; ++i)
260     {
261       Dali::TraceCallStack::FunctionCall functionCall = mCallStack[i];
262       traceStream << "StackTrace: Index:" << i << ",  Function:" << functionCall.method << ",  ParamList:" << functionCall.paramList << std::endl;
263     }
264
265     return traceStream.str();
266   }
267
268 private:
269   bool        mTraceActive{false}; ///< True if the trace is active
270   bool        mLogging{false};     ///< True if the trace is logged to stdout
271   std::string mPrefix;
272
273   struct FunctionCall
274   {
275     std::string method;
276     std::string paramList;
277     NamedParams namedParams;
278     FunctionCall(const std::string& aMethod, const std::string& aParamList)
279     : method(aMethod),
280       paramList(aParamList)
281     {
282     }
283     FunctionCall(const std::string& aMethod, const std::string& aParamList, const NamedParams& altParams)
284     : method(aMethod),
285       paramList(aParamList),
286       namedParams(altParams)
287     {
288     }
289   };
290
291   std::vector<FunctionCall> mCallStack; ///< The call stack
292 };
293
294 } // namespace Dali
295
296 #endif // TEST_TRACE_CALL_STACK_H