+std::string Join(const std::vector<std::string>& tokens, char delimiter)
+{
+ std::ostringstream oss;
+
+ unsigned int delimiterCount = 0;
+ for(auto& token : tokens)
+ {
+ oss << token;
+ if(delimiterCount < tokens.size() - 1)
+ {
+ oss << delimiter;
+ }
+ ++delimiterCount;
+ }
+ return oss.str();
+}
+
+std::string ChildOutputFilename(int pid)
+{
+ std::ostringstream os;
+ os << "/tmp/tct-child." << pid;
+ return os.str();
+}
+
+std::string TestModuleFilename(const char* processName)
+{
+ auto pathComponents = Split(processName, '/');
+ auto aModule = pathComponents.back();
+ aModule += "-tests.xml";
+ return aModule;
+}
+
+std::string TestModuleName(const char* processName)
+{
+ auto pathComponents = Split(processName, '/');
+ auto aModule = pathComponents.back();
+ auto moduleComponents = Split(aModule, '-');
+
+ moduleComponents[1][0] = std::toupper(moduleComponents[1][0]);
+ moduleComponents[2][0] = std::toupper(moduleComponents[2][0]);
+
+ std::ostringstream oss;
+ for(unsigned int i = 1; i < moduleComponents.size() - 1; ++i) // [0]=tct, [n-1]=core
+ {
+ oss << moduleComponents[i];
+
+ if(i > 1 && i < moduleComponents.size() - 2) // skip first and last delimiter
+ {
+ oss << '-';
+ }
+ }
+
+ return oss.str();
+}
+
+std::string ReadAndEscape(std::string filename)
+{
+ std::ostringstream os;
+ std::ifstream ifs;
+ ifs.open(filename, std::ifstream::in);
+ while(ifs.good())
+ {
+ std::string line;
+ std::getline(ifs, line);
+ for(auto c : line)
+ {
+ switch(c)
+ {
+ case '<':
+ os << "<";
+ break;
+ case '>':
+ os << ">";
+ break;
+ case '&':
+ os << "&";
+ break;
+ default:
+ os << c;
+ break;
+ }
+ }
+ os << "\\"
+ << "n";
+ }
+ ifs.close();
+ return os.str();
+}
+
+void OutputTestResult(
+ std::ofstream& ofs,
+ const char* pathToExecutable,
+ std::string testSuiteName,
+ TestCase& testCase,
+ std::string startTime,
+ std::string endTime)
+{
+ std::string outputFilename = ChildOutputFilename(testCase.childPid);
+ std::string testOutput = ReadAndEscape(outputFilename);
+
+ ofs << "<testcase component=\"CoreAPI/" << testSuiteName << "/default\" execution_type=\"auto\" id=\""
+ << testCase.name << "\" purpose=\"\" result=\"" << (testCase.result == 0 ? "PASS" : "FAIL") << "\">" << std::endl
+ << "<description><test_script_entry test_script_expected_result=\"0\">"
+ << pathToExecutable << testCase.name << "</test_script_entry>" << std::endl
+ << "</description>"
+ << "<result_info><actual_result>" << (testCase.result == 0 ? "PASS" : "FAIL") << "</actual_result>" << std::endl
+ << "<start>" << startTime << "</start>"
+ << "<end>" << endTime << "</end>"
+ << "<stdout><![CDATA[]]></stdout>"
+ << "<stderr><![CDATA[" << testOutput << "]]></stderr></result_info></testcase>" << std::endl;
+
+ unlink(outputFilename.c_str());
+}
+
+void OutputTestResults(const char* processName, RunningTestCases& children)
+{
+ std::ofstream ofs;
+ std::string filename = TestModuleFilename(processName);
+ std::string moduleName = TestModuleName(processName);
+ ofs.open(filename, std::ofstream::out | std::ofstream::app);
+
+ // Sort completed cases by original test case id
+ std::vector<TestCase> childTestCases;
+ childTestCases.reserve(children.size());
+ for(auto& element : children) childTestCases.push_back(element.second);
+ std::sort(childTestCases.begin(), childTestCases.end(), [](const TestCase& a, const TestCase& b) {
+ return a.testCase < b.testCase;
+ });
+
+ const int BUFSIZE = 256;
+ char buffer[BUFSIZE];
+ for(auto& testCase : childTestCases)
+ {
+ auto tt = system_clock::to_time_t(testCase.startSystemTime);
+ strftime(buffer, BUFSIZE, "%c", localtime(&tt));
+ std::string startTime(buffer);
+ OutputTestResult(ofs, processName, moduleName, testCase, startTime, startTime);
+ }
+
+ ofs.close();
+}
+
+void OutputStatistics(const char* processName, int32_t numPasses, int32_t numFailures)
+{
+ FILE* fp = fopen("summary.xml", "a");
+ if(fp != NULL)
+ {
+ fprintf(fp,
+ " <suite name=\"%s-tests\">\n"
+ " <total_case>%d</total_case>\n"
+ " <pass_case>%d</pass_case>\n"
+ " <pass_rate>%5.2f</pass_rate>\n"
+ " <fail_case>%d</fail_case>\n"
+ " <fail_rate>%5.2f</fail_rate>\n"
+ " <block_case>0</block_case>\n"
+ " <block_rate>0.00</block_rate>\n"
+ " <na_case>0</na_case>\n"
+ " <na_rate>0.00</na_rate>\n"
+ " </suite>\n",
+ basename(processName),
+ numPasses + numFailures,
+ numPasses,
+ (float)numPasses * 100.0f / (numPasses + numFailures),
+ numFailures,
+ (float)numFailures * 100.0f / (numPasses + numFailures));
+ fclose(fp);
+ }