2 * Copyright (c) 2022 Samsung Electronics Co., Ltd.
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #include "test-trace-call-stack.h"
21 #include "dali-test-suite-utils.h"
25 std::string ToString(int x)
27 std::stringstream out;
32 std::string ToString(unsigned int x)
34 std::stringstream out;
39 std::string ToString(float x)
41 std::stringstream out;
49 TraceCallStack::TraceCallStack(bool logging, std::string prefix)
50 : mTraceActive(false),
59 TraceCallStack::~TraceCallStack()
64 * Turn on / off tracing
66 void TraceCallStack::Enable(bool enable)
68 mTraceActive = enable;
71 bool TraceCallStack::IsEnabled()
76 void TraceCallStack::EnableLogging(bool enablelogging)
78 mLogging = enablelogging;
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
86 void TraceCallStack::PushCall(std::string method, std::string params)
90 FunctionCall stackFrame(method, params);
91 mCallStack.push_back(stackFrame);
95 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
99 void TraceCallStack::PushCall(std::string method, std::string params, const TraceCallStack::NamedParams& altParams)
103 FunctionCall stackFrame(method, params, altParams);
104 mCallStack.push_back(stackFrame);
108 fprintf(stderr, "%s%s(%s)\n", mPrefix.c_str(), method.c_str(), params.c_str());
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
117 bool TraceCallStack::FindMethod(std::string method) const
120 for(size_t i = 0; i < mCallStack.size(); i++)
122 if(0 == mCallStack[i].method.compare(method))
130 fprintf(stderr, "Search for %s failed\n", method.c_str());
135 bool TraceCallStack::FindMethodAndGetParameters(std::string method, std::string& params) const
138 for(size_t i = 0; i < mCallStack.size(); i++)
140 if(0 == mCallStack[i].method.compare(method))
143 params = mCallStack[i].paramList;
149 fprintf(stderr, "Search for %s() failed\n", method.c_str());
154 bool TraceCallStack::FindMethodAndGetParameters(std::string method, NamedParams& params) const
157 for(size_t i = 0; i < mCallStack.size(); i++)
159 if(0 == mCallStack[i].method.compare(method))
162 params = mCallStack[i].namedParams;
168 fprintf(stderr, "Search for %s() failed\n", method.c_str());
173 int TraceCallStack::CountMethod(std::string method) const
176 for(size_t i = 0; i < mCallStack.size(); i++)
178 if(0 == mCallStack[i].method.compare(method))
187 * Search for a method in the stack with the given parameter list
188 * @param[in] method The name of the method
189 * @param[in] params A comma separated list of parameter values
190 * @return true if the method was in the stack
192 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
194 return FindIndexFromMethodAndParams(method, params) > -1;
197 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
199 return FindIndexFromMethodAndParams(method, params) > -1;
202 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
204 for(size_t i = startIndex; i < mCallStack.size(); ++i)
206 if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
216 * Search for a method in the stack with the given parameter list
217 * @param[in] method The name of the method
218 * @param[in] params A comma separated list of parameter values
219 * @return index in the stack where the method was found or -1 otherwise
221 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
224 for(size_t i = 0; i < mCallStack.size(); i++)
226 if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
228 index = static_cast<int32_t>(i);
234 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
239 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
242 for(size_t i = 0; i < mCallStack.size(); i++)
244 if(0 == mCallStack[i].method.compare(method))
246 // Test each of the passed in parameters:
249 for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
251 auto paramIter = mCallStack[i].namedParams.find(iter->parameterName);
252 std::string value = paramIter->value.str();
253 std::string iValue = iter->value.str();
255 if(paramIter == mCallStack[i].namedParams.end() || value.compare(iValue))
263 index = static_cast<int32_t>(i);
271 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.str().c_str());
277 const TraceCallStack::NamedParams* TraceCallStack::FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const
281 if(mCallStack.size() > 0)
283 for(index = static_cast<int>(mCallStack.size() - 1); index >= 0; --index)
285 if(0 == mCallStack[index].method.compare(method))
287 // Test each of the passed in parameters:
290 for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
292 auto paramIter = mCallStack[index].namedParams.find(iter->parameterName);
293 std::string value = paramIter->value.str();
294 std::string iValue = iter->value.str();
296 if(paramIter == mCallStack[index].namedParams.end() || value.compare(iValue))
311 return &mCallStack[index].namedParams;
317 * Test if the given method and parameters are at a given index in the stack
318 * @param[in] index Index in the call stack
319 * @param[in] method Name of method to test
320 * @param[in] params A comma separated list of parameter values to test
322 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
324 return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
328 * Reset the call stack
330 void TraceCallStack::Reset()
335 bool TraceCallStack::NamedParams::NameValue::operator==(int match) const
337 std::ostringstream matchStr;
339 std::string valueStr = value.str();
340 bool retval = !valueStr.compare(matchStr.str());
343 tet_printf("Comparing parameter \"%s\": %s with %s failed\n", parameterName.c_str(), value.str().c_str(), matchStr.str().c_str());