2 * Copyright (c) 2021 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(%s) failed\n", method.c_str(), params.c_str());
154 int TraceCallStack::CountMethod(std::string method) const
157 for(size_t i = 0; i < mCallStack.size(); i++)
159 if(0 == mCallStack[i].method.compare(method))
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
173 bool TraceCallStack::FindMethodAndParams(std::string method, std::string params) const
175 return FindIndexFromMethodAndParams(method, params) > -1;
178 bool TraceCallStack::FindMethodAndParams(std::string method, const NamedParams& params) const
180 return FindIndexFromMethodAndParams(method, params) > -1;
183 bool TraceCallStack::FindMethodAndParamsFromStartIndex(std::string method, std::string params, size_t& startIndex) const
185 for(size_t i = startIndex; i < mCallStack.size(); ++i)
187 if((mCallStack[i].method.compare(method) == 0) && (mCallStack[i].paramList.compare(params) == 0))
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
202 int32_t TraceCallStack::FindIndexFromMethodAndParams(std::string method, std::string params) const
205 for(size_t i = 0; i < mCallStack.size(); i++)
207 if(0 == mCallStack[i].method.compare(method) && 0 == mCallStack[i].paramList.compare(params))
209 index = static_cast<int32_t>(i);
215 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.c_str());
220 int TraceCallStack::FindIndexFromMethodAndParams(std::string method, const TraceCallStack::NamedParams& params) const
223 for(size_t i = 0; i < mCallStack.size(); i++)
225 if(0 == mCallStack[i].method.compare(method))
227 // Test each of the passed in parameters:
230 for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
232 auto paramIter = mCallStack[i].namedParams.find(iter->parameterName);
233 std::string value = paramIter->value.str();
234 std::string iValue = iter->value.str();
236 if(paramIter == mCallStack[i].namedParams.end() || value.compare(iValue))
244 index = static_cast<int32_t>(i);
252 fprintf(stderr, "Search for %s(%s) failed\n", method.c_str(), params.str().c_str());
258 const TraceCallStack::NamedParams* TraceCallStack::FindLastMatch(std::string method, const TraceCallStack::NamedParams& params) const
262 if(mCallStack.size() > 0)
264 for(index = static_cast<int>(mCallStack.size() - 1); index >= 0; --index)
266 if(0 == mCallStack[index].method.compare(method))
268 // Test each of the passed in parameters:
271 for(auto iter = params.mParams.begin(); iter != params.mParams.end(); ++iter)
273 auto paramIter = mCallStack[index].namedParams.find(iter->parameterName);
274 std::string value = paramIter->value.str();
275 std::string iValue = iter->value.str();
277 if(paramIter == mCallStack[index].namedParams.end() || value.compare(iValue))
292 return &mCallStack[index].namedParams;
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
303 bool TraceCallStack::TestMethodAndParams(int index, std::string method, std::string params) const
305 return (0 == mCallStack[index].method.compare(method) && 0 == mCallStack[index].paramList.compare(params));
309 * Reset the call stack
311 void TraceCallStack::Reset()
316 bool TraceCallStack::NamedParams::NameValue::operator==(int match) const
318 std::ostringstream matchStr;
320 std::string valueStr = value.str();
321 bool retval = !valueStr.compare(matchStr.str());
324 tet_printf("Comparing parameter \"%s\": %s with %s failed\n", parameterName.c_str(), value.str().c_str(), matchStr.str().c_str());