CoolFace
Apppublic

sanket3280/code-execution

sourceHugging Faceupdated 10mo agoView on Hugging Face
0likes
cppHarness.js44 linesDownload Raw Back to testHarness
1const { extractFunctionName } = require('./baseHarness');2 3const createCppTestHarness = (sourceCode, testCases) => {4  const functionName = extractFunctionName.cpp(sourceCode);5  6  let cleanCode = sourceCode;7  const mainIndex = cleanCode.indexOf('int main');8  if (mainIndex !== -1) {9    cleanCode = cleanCode.substring(0, mainIndex).trim();10  }11  12  let harness = '#include <iostream>\n#include <vector>\n#include <string>\nusing namespace std;\n\n';13  harness += cleanCode + '\n\n';14  harness += 'int main() {\n';15  harness += '    cout << "TEST_RESULTS_START" << endl;\n';16  17  testCases.forEach((tc, i) => {18    const input = JSON.parse(tc.input);19    const expected = tc.expected;20    21    harness += `    try {\n`;22    const args = Object.values(input).map(v =>23      Array.isArray(v) ? `{${v.join(',')}}` :24      typeof v === 'string' ? `"${v}"` : v25    ).join(', ');26    27    harness += `        auto result = ${functionName}(${args});\n`;28    harness += `        string output = to_string(result);\n`;29    harness += `        bool passed = output == "${expected}";\n`;30    harness += `        cout << "TEST_${i+1}:" << (passed ? "PASS" : "FAIL") << ":" << output << ":${expected}" << endl;\n`;31    harness += `    } catch (...) {\n`;32    harness += `        cout << "TEST_${i+1}:ERROR:Exception" << endl;\n`;33    harness += `    }\n`;34  });35  36  harness += '    cout << "TEST_RESULTS_END" << endl;\n';37  harness += '    return 0;\n';38  harness += '}';39  40  return harness;41};42 43module.exports = { createCppTestHarness };44