Files
tai/taicpp/tests/capmcp_probe.cpp
T

73 lines
2.6 KiB
C++

// capmcp_probe: differential probe for the wire layer. Reads NUL separated
// cases from a file and prints one result line per case; tests/capmcp_diff.py
// runs the same cases through CPython and diffs the two outputs.
//
// capmcp_probe json CASES json.loads + json.dumps
// capmcp_probe toml CASES tomllib.loads + json.dumps
// capmcp_probe repr CASES json.loads + str() of the value
// capmcp_probe regex CASES PATTERN\x01TEXT -> re.search groups
// capmcp_probe float CASES text -> repr(float(text))
#include <cstdio>
#include <fstream>
#include <iostream>
#include <iterator>
#include <sstream>
#include "wire_json.h"
#include "wire_regex.h"
#include "wire_text.h"
#include "wire_toml.h"
using detz::wire::Json;
using detz::wire::WireText;
static std::vector<std::string> loadCases(const char* path) {
std::ifstream in(path, std::ios::binary);
std::string all((std::istreambuf_iterator<char>(in)), std::istreambuf_iterator<char>());
std::vector<std::string> out;
size_t pos = 0;
while (pos <= all.size()) {
size_t at = all.find('\0', pos);
if (at == std::string::npos) {
if (pos < all.size()) out.push_back(all.substr(pos));
break;
}
out.push_back(all.substr(pos, at - pos));
pos = at + 1;
}
return out;
}
int main(int argc, char** argv) {
if (argc < 3) return 2;
const std::string mode = argv[1];
const auto cases = loadCases(argv[2]);
for (size_t i = 0; i < cases.size(); ++i) {
try {
if (mode == "json") {
std::printf("%zu OK %s\n", i, Json::parse(cases[i]).dumps().c_str());
} else if (mode == "toml") {
std::printf("%zu OK %s\n", i, detz::wire::WireToml::loads(cases[i]).dumps().c_str());
} else if (mode == "repr") {
std::printf("%zu OK %s\n", i, Json::parse(cases[i]).pyRepr().c_str());
} else if (mode == "float") {
std::printf("%zu OK %s\n", i, WireText::reprFloat(std::strtod(cases[i].c_str(), nullptr)).c_str());
} else if (mode == "regex") {
const size_t sep = cases[i].find('\x01');
detz::wire::WireRegex re(cases[i].substr(0, sep));
auto m = re.search(cases[i].substr(sep + 1));
if (!m) {
std::printf("%zu NONE\n", i);
} else {
Json groups = Json::array();
for (size_t g = 0; g <= m.groupCount(); ++g) groups.push(m.hasGroup(g) ? Json(m.group(g)) : Json());
std::printf("%zu OK %zu %zu %s %s\n", i, m.start(), m.end(), groups.dumps().c_str(), m.groupdict().dumps().c_str());
}
}
} catch (const std::exception& exc) {
std::printf("%zu ERR %s\n", i, exc.what());
}
}
return 0;
}