Bring local station and route editing/deletion, verified map saves, confirmed switches, direct map transport and manual velocity ramps onto latest main. Preserve upstream non-AGV implementations, interface definitions and startup behavior. Validated with the existing qmake Release build, AGV map/UI/manual-motion tests, loopback upload/deletion/SEER transport tests and function-level retention audits.
666 lines
42 KiB
C++
666 lines
42 KiB
C++
#include "agvmapdocument.h"
|
|
|
|
#include <QCoreApplication>
|
|
#include <QDebug>
|
|
#include <QFile>
|
|
#include <QJsonArray>
|
|
#include <QJsonDocument>
|
|
#include <QJsonObject>
|
|
|
|
#include <cmath>
|
|
#include <limits>
|
|
|
|
namespace {
|
|
|
|
void require(bool condition, const char *message)
|
|
{
|
|
if (!condition)
|
|
qFatal("AGV map test failed: %s", message);
|
|
}
|
|
|
|
const QString camelMap = QString::fromUtf8(R"JSON({
|
|
"header": {"mapType":"2D-Map","mapName":"warehouse","resolution":0.05},
|
|
"normalPosList": [{"x":1.2,"y":3.4}],
|
|
"unknown": {"largeInteger":9007199254740993,"quoted":"[ ] } \\\""},
|
|
"advancedPointList": [
|
|
{"className":"LandMark","instanceName":"A","pos":{"x":0,"y":0},"custom":9007199254740993},
|
|
{"className":"LandMark","instanceName":"B","pos":{"x":3,"y":6}}
|
|
],
|
|
"advancedCurveList": []
|
|
})JSON");
|
|
|
|
void testPreservationAndBidirectionalRoutes()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(document.load(camelMap, &error), "load confirmed camelCase format");
|
|
require(document.content() == camelMap && !document.isDirty(), "unmodified text is exact");
|
|
require(document.addStation("C", 4.0, 7.0, 0.7, &error), "append station");
|
|
require(document.addRoute("A-B", "A", "B", true, &error), "append two directed edges");
|
|
require(document.isDirty() && document.stationIds().size() == 3, "draft state and station IDs");
|
|
const QString saved = document.content();
|
|
require(saved.contains("\"largeInteger\":9007199254740993"), "preserve unknown integer precision");
|
|
require(saved.contains("\"custom\":9007199254740993"), "preserve existing station fields exactly");
|
|
const QJsonObject root = QJsonDocument::fromJson(saved.toUtf8()).object();
|
|
require(root.value("normalPosList").toArray().size() == 1, "preserve original map data");
|
|
const QJsonArray curves = root.value("advancedCurveList").toArray();
|
|
require(curves.size() == 2, "bidirectional creates two edges");
|
|
const QJsonObject forward = curves.at(0).toObject();
|
|
const QJsonObject reverse = curves.at(1).toObject();
|
|
require(forward.value("className") == QStringLiteral("BezierPath"), "use documented route class");
|
|
require(forward.value("startPos").toObject().value("instanceName") == QStringLiteral("A"), "forward source");
|
|
require(reverse.value("startPos").toObject().value("instanceName") == QStringLiteral("B"), "reverse source");
|
|
require(reverse.value("endPos").toObject().value("instanceName") == QStringLiteral("A"), "reverse target");
|
|
require(forward.value("controlPos1") == reverse.value("controlPos2"), "reverse control points");
|
|
const QVariantList overlays = document.routes();
|
|
require(overlays.size() == 2 && overlays.first().toMap().value("draft").toBool(), "draft route overlay");
|
|
const QVariantList geometry = overlays.first().toMap().value("points").toList();
|
|
require(geometry.size() == 33, "Bezier overlay is sampled");
|
|
require(std::abs(geometry.at(16).toMap().value("x").toDouble() - 1.5) < 1e-9,
|
|
"straight Bezier midpoint");
|
|
document.markSaved();
|
|
require(!document.isDirty() && document.content() == saved, "markSaved retains serialized text");
|
|
require(!document.routes().first().toMap().value("draft").toBool(), "saved overlay loses draft flag");
|
|
require(document.addStation("D", 8, 9, 0, &error), "append after markSaved");
|
|
require(QJsonDocument::fromJson(document.content().toUtf8()).isObject(), "second save remains valid JSON");
|
|
}
|
|
|
|
void testRejectionsAreAtomic()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(!document.addStation("A", 0, 0, 0, &error), "cannot edit unloaded map");
|
|
require(document.load(camelMap, &error), "load map for validation");
|
|
require(!document.addStation("A", 1, 2, 0, &error), "duplicate station rejected");
|
|
require(!document.addStation(" ", 1, 2, 0, &error), "empty station rejected");
|
|
require(!document.addStation("C", std::numeric_limits<double>::infinity(), 2, 0, &error), "infinite x rejected");
|
|
require(!document.addStation("C", 1, 2, std::numeric_limits<double>::quiet_NaN(), &error), "NaN heading rejected");
|
|
require(!document.addRoute("r", "A", "missing", false, &error), "unknown endpoint rejected");
|
|
require(!document.addRoute("r", "A", "A", false, &error), "same endpoint rejected");
|
|
require(!document.isDirty() && document.content() == camelMap, "failed edits leave map unchanged");
|
|
require(document.addStation("r_reverse", 7, 8, 0, &error), "prepare reverse ID collision");
|
|
const QString beforeRoute = document.content();
|
|
require(!document.addRoute("r", "A", "B", true, &error), "reverse ID collision rejected");
|
|
require(document.routes().isEmpty() && document.content() == beforeRoute, "reverse collision is atomic");
|
|
require(document.addStation("C", 0, 0, 0, &error), "coincident station is allowed");
|
|
require(!document.addRoute("same-position", "A", "C", false, &error), "zero-length route rejected");
|
|
}
|
|
|
|
void testSnakeCaseAndMissingRouteArray()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
const QString snakeMap = QStringLiteral(
|
|
"{\"header\":{\"map_type\":\"2D-Map\",\"resolution\":0.1},"
|
|
"\"advanced_point_list\":[],\"normal_pos_list\":[],\"keep\":\"unchanged\"}");
|
|
require(document.load(snakeMap, &error), "load snake_case format");
|
|
require(document.addStation("LM1", 1, 2, 0, &error), "first station in empty array");
|
|
require(document.addStation("LM2", 3, 4, 0, &error), "second station in empty array");
|
|
require(document.addRoute("LM1-LM2", "LM1", "LM2", false, &error), "create previously absent route array");
|
|
const QJsonObject root = QJsonDocument::fromJson(document.content().toUtf8()).object();
|
|
require(root.value("advanced_point_list").toArray().size() == 2, "snake_case stations serialized");
|
|
require(root.value("advanced_curve_list").toArray().size() == 1, "snake_case routes serialized");
|
|
require(!root.contains("advancedCurveList"), "original naming convention preserved");
|
|
require(root.value("advanced_curve_list").toArray().first().toObject().contains("start_pos"), "snake_case endpoint");
|
|
AgvMapDocument reread;
|
|
require(reread.load(document.content(), &error), "reload appended map");
|
|
require(reread.hasStation("LM1") && reread.routes().size() == 1, "reload overlays");
|
|
document.clear();
|
|
require(document.content().isEmpty() && document.stationIds().isEmpty() && !document.isDirty(), "clear all state");
|
|
}
|
|
|
|
void testUnsupportedFormats()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(!document.load("{}", &error), "unknown JSON rejected");
|
|
require(!document.load("PK\\x03\\x04", &error), "binary archive rejected");
|
|
require(!document.load("{\"header\":{\"mapType\":\"3D-Map\",\"resolution\":0.1},\"advancedPointList\":[]}", &error), "3D map rejected");
|
|
require(!document.load("{\"header\":{\"resolution\":0.1},\"advancedPointList\":[],\"advanced_point_list\":[]}", &error), "mixed field names rejected");
|
|
require(!document.load("{\"header\":{\"resolution\":0.1},\"advancedPointList\":[],\"advancedPointList\":[]}", &error), "duplicate top-level array rejected");
|
|
}
|
|
|
|
void testIdentifierCompatibility()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(document.load(camelMap, &error), "load identifier test map");
|
|
require(document.addStation(QStringLiteral("充电站"), 1, 2, 0, &error), "Chinese station ID is valid");
|
|
require(document.addRoute(QStringLiteral("回家路线"), QStringLiteral("充电站"), "A", false, &error),
|
|
"Chinese route and endpoint IDs are valid");
|
|
const QString before = document.content();
|
|
require(!document.addStation("A,B", 3, 4, 0, &error), "comma station ID rejected");
|
|
require(!document.addStation("new\nstation", 3, 4, 0, &error), "newline station ID rejected");
|
|
require(!document.addStation("new\tstation", 3, 4, 0, &error), "control character station ID rejected");
|
|
require(!document.addRoute("route,1", "A", "B", false, &error), "comma route ID rejected");
|
|
require(document.content() == before, "invalid IDs do not change content");
|
|
QString legacyMap = camelMap;
|
|
legacyMap.replace(QStringLiteral("\"instanceName\":\"A\""),
|
|
QStringLiteral("\"instanceName\":\"A,legacy\""));
|
|
require(document.load(legacyMap, &error), "legacy delimiter ID is preserved when loading");
|
|
require(!document.addRoute("legacy", "A,legacy", "B", false, &error),
|
|
"cannot create a new path using an unrepresentable endpoint ID");
|
|
}
|
|
|
|
void testStationNumericIdentifiers()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(document.nextAvailableStationId().isEmpty(), "unloaded document has no station suggestion");
|
|
QString source = camelMap;
|
|
source.replace(QStringLiteral("\"instanceName\":\"A\""), QStringLiteral("\"instanceName\":\"LM1\""));
|
|
source.replace(QStringLiteral("\"instanceName\":\"B\""), QStringLiteral("\"instanceName\":\"CP0002\""));
|
|
require(document.load(source, &error) && document.validateStationIds(&error),
|
|
"different numeric station IDs are valid");
|
|
require(document.nextAvailableStationId() == QStringLiteral("LM3"),
|
|
"default ID skips occupied numbers across all prefixes and leading zeroes");
|
|
for (const QString &id : {QStringLiteral("STATION_1"), QStringLiteral("LM01"),
|
|
QStringLiteral("0001"), QStringLiteral("LM2")}) {
|
|
require(!document.addStation(id, 4, 5, 0, &error), "numeric collision rejected before mutation");
|
|
require(error.contains(id) && error.contains(QStringLiteral("数字编号")),
|
|
"numeric rejection identifies the new ID and explains the conflict");
|
|
require(!document.isDirty() && document.content() == source,
|
|
"rejected numeric collision preserves clean content exactly");
|
|
}
|
|
require(document.addStation(document.nextAvailableStationId(), 4, 5, 0, &error,
|
|
QStringLiteral("装卸站一")), "suggested station ID is available");
|
|
const QString dirtySource = document.content();
|
|
require(!document.addStation("CUSTOM_0003", 5, 6, 0, &error)
|
|
&& document.isDirty() && document.content() == dirtySource,
|
|
"numeric collision also preserves an existing dirty draft");
|
|
require(document.addStation("zone1a", 5, 6, 0, &error),
|
|
"legacy nonnumeric IDs remain supported");
|
|
require(document.addRoute("CUSTOM_4", "LM1", "LM3", false, &error)
|
|
&& document.addStation("LM4", 6, 7, 0, &error),
|
|
"route numeric suffixes do not reserve station numbers");
|
|
require(document.addRoute("LM5", "LM1", "LM3", false, &error)
|
|
&& document.nextAvailableStationId() == QStringLiteral("LM6"),
|
|
"default station still avoids an exact route identifier collision");
|
|
require(document.validateStationIds(&error), "valid additions keep station numeric IDs unique");
|
|
|
|
AgvMapDocument latest;
|
|
require(latest.load(document.content(), &error)
|
|
&& !latest.addStation("STATION_3", 7, 8, 0, &error)
|
|
&& !latest.isDirty(),
|
|
"save merge rechecks a newly occupied numeric ID in the latest downloaded map");
|
|
|
|
QString badSource = source;
|
|
badSource.replace(QStringLiteral("CP0002"), QStringLiteral("STATION_1"));
|
|
require(document.load(badSource, &error), "existing numeric collision remains readable for diagnosis");
|
|
require(!document.validateStationIds(&error) && error.contains(QStringLiteral("LM1"))
|
|
&& error.contains(QStringLiteral("STATION_1"))
|
|
&& document.content() == badSource && !document.isDirty(),
|
|
"validation reports both existing colliding IDs without renaming or dirtying the map");
|
|
require(document.nextAvailableStationId() == QStringLiteral("LM2"),
|
|
"default allocation skips the shared number even in an existing bad map");
|
|
|
|
require(document.load(camelMap, &error), "load numeric overflow test map");
|
|
const QString huge = QStringLiteral("18446744073709551616000000000000000001");
|
|
require(document.addStation(QStringLiteral("LM") + huge, 1, 2, 0, &error),
|
|
"large numeric suffix is preserved without integer conversion");
|
|
const QString hugeSource = document.content();
|
|
require(!document.addStation(QStringLiteral("STATION_000") + huge, 2, 3, 0, &error)
|
|
&& document.content() == hugeSource,
|
|
"large numeric collision with leading zeroes is detected without overflow");
|
|
require(document.addStation(QStringLiteral("CP18446744073709551616000000000000000002"), 2, 3, 0, &error)
|
|
&& document.validateStationIds(&error), "different huge numbers are not conflated");
|
|
require(document.addStation("LM0", 3, 4, 0, &error), "zero numeric station ID remains compatible");
|
|
require(!document.addStation("CP000", 4, 5, 0, &error), "all-zero suffixes normalize to the same zero");
|
|
require(document.nextAvailableStationId() == QStringLiteral("LM1"),
|
|
"huge numeric IDs and zero do not overflow or block small available numbers");
|
|
|
|
require(document.load(camelMap, &error), "load reported map ID allocation reproduction");
|
|
for (int number = 1; number <= 11; ++number) {
|
|
const QString id = (number == 5 ? QStringLiteral("CP") : QStringLiteral("LM"))
|
|
+ QString::number(number);
|
|
require(document.addStation(id, number, 1, 0, &error), "prepare existing LM1-11 with CP5");
|
|
}
|
|
require(document.nextAvailableStationId() == QStringLiteral("LM12"),
|
|
"reported map reserves CP5 and offers LM12 rather than conflicting STATION_1");
|
|
require(!document.addStation("STATION_01", 12, 1, 0, &error),
|
|
"reported leading-zero alternative cannot reuse LM1's numeric ID");
|
|
}
|
|
|
|
QVariantMap stationOverlay(const AgvMapDocument &document, const QString &id)
|
|
{
|
|
for (const QVariant &value : document.stations()) {
|
|
if (value.toMap().value("id").toString() == id)
|
|
return value.toMap();
|
|
}
|
|
return {};
|
|
}
|
|
|
|
void testStationNames()
|
|
{
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(!document.setStationName("A", "name", &error), "unloaded rename rejected");
|
|
require(document.load(camelMap, &error), "load station names map");
|
|
const QVariantMap initial = stationOverlay(document, "A");
|
|
require(initial.value("name") == QStringLiteral("A")
|
|
&& initial.value("description").toString().isEmpty()
|
|
&& initial.value("originalName").toString().isEmpty()
|
|
&& !initial.value("nameChanged").toBool(), "missing description falls back only for display");
|
|
require(document.setStationName("A", QStringLiteral("装卸站一"), &error), "name existing station without description");
|
|
require(document.isDirty(), "rename alone is dirty");
|
|
require(stationOverlay(document, "A").value("nameChanged").toBool()
|
|
&& stationOverlay(document, "A").value("originalName").toString().isEmpty(),
|
|
"rename tracks empty original description");
|
|
QString expected = camelMap;
|
|
expected.replace(QStringLiteral("\"custom\":9007199254740993}"),
|
|
QStringLiteral("\"custom\":9007199254740993,\"description\":\"装卸站一\"}"));
|
|
require(document.content() == expected, "rename appends only description preserving complete original text");
|
|
require(document.setStationName("A", QStringLiteral("装卸站二"), &error), "repeat rename");
|
|
require(stationOverlay(document, "A").value("originalName").toString().isEmpty(),
|
|
"repeat rename retains first original name");
|
|
require(document.setStationName("A", QString(), &error), "restore absent description");
|
|
require(!document.isDirty() && document.content() == camelMap
|
|
&& !stationOverlay(document, "A").value("nameChanged").toBool(),
|
|
"rename back removes pending change and restores absent field exactly");
|
|
require(document.addStation("C", 4, 5, 0, &error, QStringLiteral("充电站, 一号")), "new Chinese display name with comma");
|
|
require(stationOverlay(document, "C").value("name") == QStringLiteral("充电站, 一号")
|
|
&& stationOverlay(document, "C").value("draft").toBool()
|
|
&& !stationOverlay(document, "C").value("nameChanged").toBool(),
|
|
"draft station name stays separate from identifier");
|
|
require(document.setStationName("C", QStringLiteral("充电站二"), &error), "rename draft station");
|
|
require(document.setStationName("A", QStringLiteral("装卸站一"), &error), "rename while additions pending");
|
|
require(document.addRoute("A-C", "A", "C", false, &error), "route endpoints continue using IDs");
|
|
const QString saved = document.content();
|
|
const QJsonObject savedRoot = QJsonDocument::fromJson(saved.toUtf8()).object();
|
|
const QJsonArray stations = savedRoot.value("advancedPointList").toArray();
|
|
require(stations.at(2).toObject().value("description") == QStringLiteral("充电站二"),
|
|
"draft rename persists final description");
|
|
require(savedRoot.value("advancedCurveList").toArray().first().toObject()
|
|
.value("startPos").toObject().value("instanceName") == QStringLiteral("A"),
|
|
"display name does not alter route identity");
|
|
require(saved.contains("\"custom\":9007199254740993")
|
|
&& saved.contains("\"largeInteger\":9007199254740993"),
|
|
"combined rename/additions preserve raw integers");
|
|
document.markSaved();
|
|
const QVariantMap savedA = stationOverlay(document, "A");
|
|
require(!document.isDirty() && document.content() == saved
|
|
&& savedA.value("originalName") == QStringLiteral("装卸站一")
|
|
&& !savedA.value("nameChanged").toBool()
|
|
&& !stationOverlay(document, "C").value("draft").toBool(),
|
|
"markSaved updates name base and clears draft flags");
|
|
require(document.setStationName("A", QStringLiteral("装卸站三"), &error), "rename after save");
|
|
require(stationOverlay(document, "A").value("originalName") == QStringLiteral("装卸站一"),
|
|
"subsequent rename compares saved base");
|
|
require(document.setStationName("A", QStringLiteral("装卸站一"), &error), "restore saved name");
|
|
require(!document.isDirty() && document.content() == saved, "restore saved name restores exact text");
|
|
require(!document.setStationName("missing", "name", &error) && !document.isDirty(),
|
|
"missing station rename is atomic");
|
|
document.clear();
|
|
require(document.stations().isEmpty() && !document.isDirty(), "clear removes names and changes");
|
|
}
|
|
|
|
void testStationNameTokenPreservation()
|
|
{
|
|
const QString source = QString::fromUtf8(R"JSON({
|
|
"header":{"resolution":0.05}, "advanced_point_list":[
|
|
{"instance_name":"A", "pos":{"x":1,"y":2}, "opaque":{"description":"nested","big":18446744073709551615}, "descr\u0069ption" : "旧名称", "tail":["} ]",9007199254740993]},
|
|
{"instance_name":"B", "description":"", "pos":{"x":3,"y":4}}
|
|
], "unknown":9007199254740993
|
|
})JSON");
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(document.load(source, &error), "load escaped description key and snake_case stations");
|
|
const QString name = QStringLiteral("新\"名称\\仓库\n二楼");
|
|
require(document.setStationName("A", name, &error), "rename escaped description field");
|
|
require(stationOverlay(document, "A").value("originalName") == QStringLiteral("旧名称"),
|
|
"existing string is rename comparison base");
|
|
const QByteArray encoded = QJsonDocument(QJsonArray{name}).toJson(QJsonDocument::Compact);
|
|
const QString encodedString = QString::fromUtf8(encoded.mid(1, encoded.size() - 2));
|
|
QString expected = source;
|
|
expected.replace(QStringLiteral("\"旧名称\""), encodedString);
|
|
require(document.content() == expected, "replace exactly one JSON value preserving key spelling whitespace and uint64");
|
|
AgvMapDocument reread;
|
|
require(reread.load(document.content(), &error)
|
|
&& stationOverlay(reread, "A").value("name") == name,
|
|
"escaped quotes slash newline and Chinese persist");
|
|
require(document.setStationName("A", QStringLiteral("再修改"), &error)
|
|
&& stationOverlay(document, "A").value("originalName") == QStringLiteral("旧名称"),
|
|
"existing name comparison base survives multiple changes");
|
|
require(document.setStationName("A", QStringLiteral("旧名称"), &error)
|
|
&& !document.isDirty() && document.content() == source,
|
|
"restore original string preserves original escape spelling");
|
|
require(document.setStationName("B", QStringLiteral("库房B"), &error), "rename explicit empty description");
|
|
require(document.setStationName("B", QString(), &error)
|
|
&& !document.isDirty() && document.content() == source,
|
|
"restore empty description keeps original field");
|
|
QString unsupported = source;
|
|
unsupported.replace(QStringLiteral("\"description\":\"\""), QStringLiteral("\"description\":{\"unknown\":9007199254740993}"));
|
|
require(document.load(unsupported, &error), "preserve station with unknown description type");
|
|
require(!document.setStationName("B", "new", &error)
|
|
&& !document.isDirty() && document.content() == unsupported,
|
|
"unknown description type cannot be silently replaced");
|
|
QString duplicate = source;
|
|
duplicate.replace(QStringLiteral("\"description\":\"\""), QStringLiteral("\"description\":\"first\",\"description\":\"last\""));
|
|
require(document.load(duplicate, &error), "preserve duplicate description map");
|
|
require(!document.setStationName("B", "new", &error)
|
|
&& !document.isDirty() && document.content() == duplicate,
|
|
"ambiguous duplicate descriptions reject rename without altering map");
|
|
}
|
|
|
|
void testDeleteArrayTokenPreservation()
|
|
{
|
|
const QStringList ids{QStringLiteral("A"), QStringLiteral("B"), QStringLiteral("C"), QStringLiteral("D")};
|
|
QStringList stationTokens;
|
|
QStringList routeTokens;
|
|
for (int index = 0; index < ids.size(); ++index) {
|
|
stationTokens.append(QStringLiteral("{\"instanceName\":\"%1\", \"pos\":{\"x\":%2,\"y\":2}, \"opaque\":18446744073709551615}")
|
|
.arg(ids.at(index)).arg(index));
|
|
routeTokens.append(QStringLiteral("{\"instanceName\":\"R%1\",\"unknown\":[9007199254740993,\"} ] \\\"\"]}")
|
|
.arg(index));
|
|
}
|
|
const QString source = QStringLiteral("{\n \"header\":{\"resolution\":0.05,\"opaque\":18446744073709551615},\n"
|
|
" \"advancedPointList\":[\n %1\n ],\n \"advancedCurveList\":[\n %2\n ],\n"
|
|
" \"normalPosList\":[{\"x\":9007199254740993}],\"unknown\":\"keep\\\\raw\"\n}")
|
|
.arg(stationTokens.join(QStringLiteral(",\n ")), routeTokens.join(QStringLiteral(",\n ")));
|
|
QString error;
|
|
for (int mask = 1; mask < 16; ++mask) {
|
|
AgvMapDocument document;
|
|
require(document.load(source, &error), "load raw array deletion fixture");
|
|
int survivors = 4;
|
|
for (int index = 0; index < 4; ++index) {
|
|
if (mask & (1 << index)) {
|
|
require(document.removeStation(ids.at(index), false, &error), "remove original station subset");
|
|
require(document.removeRoute(QStringLiteral("R%1").arg(index), &error), "remove original route subset");
|
|
--survivors;
|
|
}
|
|
}
|
|
const QString saved = document.content();
|
|
const QJsonDocument parsed = QJsonDocument::fromJson(saved.toUtf8());
|
|
require(parsed.isObject() && parsed.object().value("advancedPointList").toArray().size() == survivors
|
|
&& parsed.object().value("advancedCurveList").toArray().size() == survivors,
|
|
"first middle last contiguous disjoint and all deletions keep valid JSON");
|
|
require(saved.contains("\"header\":{\"resolution\":0.05,\"opaque\":18446744073709551615}")
|
|
&& saved.contains("\"normalPosList\":[{\"x\":9007199254740993}]")
|
|
&& saved.contains("\"unknown\":\"keep\\\\raw\""), "deletions preserve header scan and unknown raw tokens");
|
|
for (int index = 0; index < 4; ++index) {
|
|
require(saved.contains(stationTokens.at(index)) == !(mask & (1 << index))
|
|
&& saved.contains(routeTokens.at(index)) == !(mask & (1 << index)),
|
|
"every untouched object remains byte-for-byte and only deleted objects disappear");
|
|
}
|
|
require(document.addStation("NEW", 8, 9, 0, &error)
|
|
&& document.addRoute("NEW-R", "NEW", mask == 15 ? "missing" : document.stationIds().first(), false, &error) == (mask != 15),
|
|
"append after removals uses remaining station topology");
|
|
AgvMapDocument reread;
|
|
require(reread.load(document.content(), &error) && reread.hasStation("NEW"),
|
|
"append commas account for all original elements deleted");
|
|
}
|
|
}
|
|
|
|
void testDeleteDraftsAndIdentifierReservations()
|
|
{
|
|
QString error;
|
|
AgvMapDocument document;
|
|
require(document.load(camelMap, &error) && document.addStation("LM1", 8, 9, 0, &error)
|
|
&& document.addRoute("A-LM1", "A", "LM1", true, &error), "prepare draft station and bidirectional routes");
|
|
const AgvMapDocument undo = document;
|
|
const QString before = document.content();
|
|
require(!document.removeStation("LM1", false, &error) && document.content() == before,
|
|
"connected station rejection is atomic");
|
|
require(document.connectedRouteIds("LM1").size() == 2
|
|
&& document.removeStation("LM1", true, &error), "explicit cascade removes both directed draft routes");
|
|
require(!document.isDirty() && document.content() == camelMap
|
|
&& document.removedStationIds().isEmpty() && document.removedRouteIds().isEmpty(),
|
|
"new station then delete cancels additions and returns clean original text");
|
|
document = undo;
|
|
require(document.isDirty() && document.content() == before && document.hasStation("LM1")
|
|
&& document.routes().size() == 2, "document copy restores cascade in one undo step");
|
|
document.markSaved();
|
|
require(document.removeRoute("A-LM1", &error) && document.routes().size() == 1
|
|
&& document.removedRouteIds() == QStringList{QStringLiteral("A-LM1")},
|
|
"delete selected directed route leaves its reverse intact");
|
|
require(!document.addRoute("A-LM1", "A", "LM1", false, &error), "pending removed route ID cannot be reused");
|
|
require(document.removeStation("LM1", true, &error)
|
|
&& document.removedStationIds() == QStringList{QStringLiteral("LM1")}
|
|
&& !document.addStation("LM1", 1, 2, 0, &error)
|
|
&& !document.addStation("STATION_01", 1, 2, 0, &error)
|
|
&& document.nextAvailableStationId() == QStringLiteral("LM2"),
|
|
"pending removed station reserves textual ID and numeric suffix");
|
|
document.markSaved();
|
|
require(!document.isDirty() && document.removedStationIds().isEmpty() && document.removedRouteIds().isEmpty()
|
|
&& document.nextAvailableStationId() == QStringLiteral("LM1"), "confirmed save resets tombstones and ID reservation");
|
|
require(!document.removeRoute("missing", &error) && !document.removeStation("missing", true, &error)
|
|
&& !document.isDirty(), "missing delete leaves document clean");
|
|
require(document.load(camelMap, &error) && document.setStationName("A", "keep this rename", &error)
|
|
&& document.removeStation("B", false, &error), "combine surviving rename with trailing station deletion");
|
|
AgvMapDocument combined;
|
|
require(combined.load(document.content(), &error) && combined.stationIds().size() == 1
|
|
&& stationOverlay(combined, "A").value("description") == QStringLiteral("keep this rename")
|
|
&& document.content().contains("\"custom\":9007199254740993"),
|
|
"name insertion on preceding retained object does not overlap trailing deletion range");
|
|
require(document.removeStation("A", false, &error)
|
|
&& QJsonDocument::fromJson(document.content().toUtf8()).object().value("advancedPointList").toArray().isEmpty(),
|
|
"deleting locally renamed object removes its name edit and leaves valid empty array");
|
|
}
|
|
|
|
void testDeleteMergeConflictsAndConfirmation()
|
|
{
|
|
QString error;
|
|
AgvMapDocument base;
|
|
require(base.load(camelMap, &error) && base.addRoute("A-B", "A", "B", true, &error), "prepare persisted map with directed routes");
|
|
base.markSaved();
|
|
const QString original = base.content();
|
|
AgvMapDocument local = base;
|
|
require(local.setStationName("A", "local rename before delete", &error)
|
|
&& local.removeStation("A", true, &error), "rename then delete existing station");
|
|
AgvMapDocument latest;
|
|
require(latest.load(original, &error) && local.mergeRemovalsInto(&latest, &error)
|
|
&& !latest.hasStation("A") && latest.routes().isEmpty(),
|
|
"delete conflict comparison uses original station before local rename");
|
|
AgvMapDocument readback;
|
|
require(readback.load(latest.content(), &error) && local.verifyRemovals(readback, &error),
|
|
"readback confirms station routes and references absent");
|
|
require(local.mergeRemovalsInto(&readback, &error), "already absent objects allow idempotent retry");
|
|
require(!local.verifyRemovals(base, &error), "upload acknowledgement cannot confirm absent delete in stale map");
|
|
QString externallyChanged = original;
|
|
externallyChanged.replace("\"custom\":9007199254740993", "\"custom\":9007199254740992");
|
|
require(latest.load(externallyChanged, &error), "load externally changed uint64-like token");
|
|
const QString untouched = latest.content();
|
|
require(!local.mergeRemovalsInto(&latest, &error) && latest.content() == untouched && !latest.isDirty(),
|
|
"external change lost by double conversion blocks delete atomically");
|
|
require(latest.load(original, &error) && latest.addStation("C", 8, 9, 0, &error)
|
|
&& latest.addRoute("A-C", "A", "C", false, &error), "prepare external new reference");
|
|
latest.markSaved();
|
|
const QString withExternalRoute = latest.content();
|
|
require(!local.mergeRemovalsInto(&latest, &error) && latest.content() == withExternalRoute,
|
|
"new external connected route blocks cascade rather than being silently removed");
|
|
require(latest.load(original, &error) && latest.setStationName("B", "external unrelated edit", &error),
|
|
"prepare unrelated concurrent station rename");
|
|
latest.markSaved();
|
|
require(local.mergeRemovalsInto(&latest, &error)
|
|
&& stationOverlay(latest, "B").value("description") == QStringLiteral("external unrelated edit"),
|
|
"delete merge preserves unrelated concurrent changes");
|
|
QString reordered = original;
|
|
reordered.replace("{\"className\":\"LandMark\",\"instanceName\":\"A\",\"pos\":{\"x\":0,\"y\":0},\"custom\":9007199254740993}",
|
|
"{ \"custom\":9007199254740993,\"pos\":{\"y\":0,\"x\":0},\"instanceName\":\"A\",\"className\":\"LandMark\" }");
|
|
require(reordered != original && latest.load(reordered, &error) && local.mergeRemovalsInto(&latest, &error),
|
|
"complete original-object comparison accepts whitespace and object key reordering");
|
|
QString dangling = readback.content();
|
|
dangling.replace("\"advancedCurveList\":", "\"advancedCurveList\":");
|
|
QJsonObject danglingRoot = QJsonDocument::fromJson(dangling.toUtf8()).object();
|
|
danglingRoot.insert("advancedCurveList", QJsonArray{QJsonObject{{"instanceName", "new-dangling"},
|
|
{"startPos", QJsonObject{{"instanceName", "A"}}}, {"endPos", QJsonObject{{"instanceName", "B"}}}}});
|
|
require(latest.load(QString::fromUtf8(QJsonDocument(danglingRoot).toJson()), &error)
|
|
&& !local.verifyRemovals(latest, &error), "confirmation rejects new dangling reference to deleted station");
|
|
AgvMapDocument routeOnly = base;
|
|
require(routeOnly.removeRoute("A-B", &error), "prepare route-only deletion");
|
|
QString changedRoute = original;
|
|
changedRoute.replace("\"instanceName\":\"A-B\"", "\"changedExternal\":true,\"instanceName\":\"A-B\"");
|
|
require(latest.load(changedRoute, &error) && !routeOnly.mergeRemovalsInto(&latest, &error),
|
|
"external unknown route property change blocks deletion");
|
|
QString duplicateRoute = original;
|
|
const QJsonArray originalRoutes = QJsonDocument::fromJson(original.toUtf8()).object().value("advancedCurveList").toArray();
|
|
const QString duplicateToken = QString::fromUtf8(QJsonDocument(originalRoutes.first().toObject()).toJson(QJsonDocument::Compact));
|
|
const int curveClose = duplicateRoute.lastIndexOf(QLatin1Char(']'));
|
|
duplicateRoute.insert(curveClose, QLatin1Char(',') + duplicateToken);
|
|
require(latest.load(duplicateRoute, &error), "ambiguous legacy route identifiers remain readable");
|
|
const QString duplicateBefore = latest.content();
|
|
require(!latest.removeRoute("A-B", &error) && !latest.removeStation("A", true, &error)
|
|
&& latest.content() == duplicateBefore && !latest.isDirty(),
|
|
"ambiguous route deletion and cascades reject atomically");
|
|
AgvMapDocument empty;
|
|
require(empty.mergeRemovalsInto(nullptr, &error) && empty.verifyRemovals(empty, &error),
|
|
"no tombstones have zero-cost no-op merge and verify");
|
|
}
|
|
|
|
QString valueToken(const QString &source, const QString &key)
|
|
{
|
|
const QString encodedKey = QString::fromUtf8(QJsonDocument(QJsonArray{key})
|
|
.toJson(QJsonDocument::Compact));
|
|
const QString keyToken = encodedKey.mid(1, encodedKey.size() - 2);
|
|
int start = source.indexOf(keyToken + QLatin1Char(':'));
|
|
require(start >= 0, "native fixture contains requested root field token");
|
|
start += keyToken.size() + 1;
|
|
while (start < source.size() && source.at(start).isSpace())
|
|
++start;
|
|
require(source.at(start) == QLatin1Char('{') || source.at(start) == QLatin1Char('['),
|
|
"native root token is an object or array");
|
|
int depth = 0;
|
|
bool quoted = false;
|
|
for (int index = start; index < source.size(); ++index) {
|
|
const QChar character = source.at(index);
|
|
if (quoted) {
|
|
if (character == QLatin1Char('\\'))
|
|
++index;
|
|
else if (character == QLatin1Char('"'))
|
|
quoted = false;
|
|
} else if (character == QLatin1Char('"')) {
|
|
quoted = true;
|
|
} else if (character == QLatin1Char('{') || character == QLatin1Char('[')) {
|
|
++depth;
|
|
} else if (character == QLatin1Char('}') || character == QLatin1Char(']')) {
|
|
if (--depth == 0)
|
|
return source.mid(start, index - start + 1);
|
|
}
|
|
}
|
|
qFatal("AGV map test failed: native root token is not balanced");
|
|
return QString();
|
|
}
|
|
|
|
QString testNativeFullMapUploadContent(const QString &original)
|
|
{
|
|
// Add an unknown uint64 token to this LOCAL fixture to catch lossy full-root
|
|
// reserialization; no network client or vehicle write is used by this test.
|
|
QString source = original;
|
|
source.insert(source.lastIndexOf(QLatin1Char('}')),
|
|
QStringLiteral(",\"uploadUnknown\":{\"uint64\":18446744073709551615,\"opaque\":\"keep\\\\raw\\\"text\"}"));
|
|
const QJsonObject originalRoot = QJsonDocument::fromJson(source.toUtf8()).object();
|
|
require(originalRoot.value("header").isObject()
|
|
&& originalRoot.value("normalPosList").isArray()
|
|
&& !originalRoot.value("advancedPointList").toArray().isEmpty(),
|
|
"real-format fixture has root header, camel scan and stations");
|
|
const QString headerToken = valueToken(source, "header");
|
|
const QString scanToken = valueToken(source, "normalPosList");
|
|
const QString unknownToken = valueToken(source, "uploadUnknown");
|
|
const QString lineToken = valueToken(source, "advancedLineList");
|
|
const QString originalCurveToken = valueToken(source, "advancedCurveList");
|
|
const QJsonArray oldStations = originalRoot.value("advancedPointList").toArray();
|
|
const QJsonArray oldRoutes = originalRoot.value("advancedCurveList").toArray();
|
|
const QString originalId = oldStations.first().toObject().value("instanceName").toString();
|
|
const QString literalName = QStringLiteral("\"离线保存测试\"\\路径\n二楼");
|
|
AgvMapDocument document;
|
|
QString error;
|
|
require(document.load(source, &error) && document.content() == source,
|
|
"downloaded native map starts as exact original text");
|
|
require(document.setStationName(originalId, literalName, &error)
|
|
&& document.addStation("LOCAL_UPLOAD_TEST", 1.25, 0.5, 0.3, &error,
|
|
QStringLiteral("本地测试站"))
|
|
&& document.addRoute("LOCAL_UPLOAD_ROUTE", originalId, "LOCAL_UPLOAD_TEST", true, &error),
|
|
"native map supports combined existing-name edits, new station and bidirectional routes");
|
|
const QString content = document.content();
|
|
const QByteArray bytes = content.toUtf8();
|
|
const QJsonDocument upload = QJsonDocument::fromJson(bytes);
|
|
require(bytes.trimmed().startsWith('{') && bytes.trimmed().endsWith('}') && upload.isObject(),
|
|
"upload is one JSON object, never a quoted JSON string or wrapper");
|
|
const QJsonObject root = upload.object();
|
|
require(root.value("header") == originalRoot.value("header")
|
|
&& content.contains(headerToken),
|
|
"complete native header remains at root and is preserved byte-for-byte");
|
|
require(root.value("normalPosList") == originalRoot.value("normalPosList")
|
|
&& content.contains(scanToken), "all native scan samples and zero-value omissions retain exact text");
|
|
require(root.value("advancedLineList") == originalRoot.value("advancedLineList")
|
|
&& content.contains(lineToken) && content.contains(unknownToken)
|
|
&& content.contains("18446744073709551615"),
|
|
"advanced lines and unknown fields retain exact tokens including uint64 precision");
|
|
require(!root.contains("advanced_point_list") && !root.contains("advanced_curve_list")
|
|
&& !root.contains("content") && !root.contains("map")
|
|
&& root.value("advancedPointList").toArray().size() == oldStations.size() + 1
|
|
&& root.value("advancedCurveList").toArray().size() == oldRoutes.size() + 2,
|
|
"native camel arrays are extended without extra envelopes or snake fields");
|
|
const QJsonArray savedStations = root.value("advancedPointList").toArray();
|
|
for (int index = 0; index < oldStations.size(); ++index) {
|
|
QJsonObject expected = oldStations.at(index).toObject();
|
|
if (index == 0)
|
|
expected.insert("description", literalName);
|
|
require(savedStations.at(index).toObject() == expected,
|
|
"existing native station fields change only the explicitly edited description");
|
|
}
|
|
const QJsonObject added = savedStations.last().toObject();
|
|
require(added.value("className") == QStringLiteral("LandMark")
|
|
&& added.value("instanceName") == QStringLiteral("LOCAL_UPLOAD_TEST")
|
|
&& added.value("property").toArray().first().toObject().contains("boolValue"),
|
|
"new station follows the documented camel SMAP form");
|
|
const QJsonArray savedRoutes = root.value("advancedCurveList").toArray();
|
|
for (int index = 0; index < oldRoutes.size(); ++index)
|
|
require(savedRoutes.at(index) == oldRoutes.at(index), "all existing native routes remain unchanged");
|
|
require(valueToken(content, "advancedCurveList").startsWith(
|
|
originalCurveToken.left(originalCurveToken.size() - 1)),
|
|
"existing route tokens are retained verbatim before appended routes");
|
|
const QJsonObject newRoute = savedRoutes.at(oldRoutes.size()).toObject();
|
|
require(newRoute.value("className") == QStringLiteral("BezierPath")
|
|
&& newRoute.value("startPos").toObject().value("instanceName") == originalId
|
|
&& newRoute.value("endPos").toObject().value("instanceName") == QStringLiteral("LOCAL_UPLOAD_TEST")
|
|
&& newRoute.value("property").toArray().first().toObject().contains("int32Value"),
|
|
"new route uses documented camel references and property values");
|
|
AgvMapDocument reread;
|
|
require(reread.load(content, &error) && reread.hasStation("LOCAL_UPLOAD_TEST")
|
|
&& stationOverlay(reread, originalId).value("name") == literalName,
|
|
"complete upload text reloads with literal escaped name and additions");
|
|
document.markSaved();
|
|
require(!document.isDirty() && document.content() == content, "markSaved keeps exact complete native upload payload");
|
|
qInfo() << "PASS native SMAP local payload:" << bytes.size() << "bytes; root header and"
|
|
<< originalRoot.value("normalPosList").toArray().size() << "scan samples preserved";
|
|
return content;
|
|
}
|
|
|
|
void testNativeMapSerialization()
|
|
{
|
|
const QString source = QStringLiteral(R"JSON({"advancedCurveList":[{"className":"DegenerateBezier","instanceName":"LM1-LM2","startPos":{"instanceName":"LM1","pos":{"x":0.294,"y":-0.17}},"endPos":{"instanceName":"LM2","pos":{"x":5.201,"y":-0.054}},"controlPos1":{"x":1.93,"y":-0.131},"controlPos2":{"x":3.565,"y":-0.093}}],"advancedLineList":[{"className":"AdvancedLine","instanceName":"L1","startPos":{"x":1,"y":2},"endPos":{"x":3,"y":4}}],"advancedPointList":[{"className":"LocationMark","ignoreDir":true,"instanceName":"LM1","pos":{"x":0.294,"y":-0.17},"property":[{"boolValue":false,"key":"spin","type":"bool","value":"ZmFsc2U="}]},{"className":"LocationMark","ignoreDir":true,"instanceName":"LM2","pos":{"x":5.201,"y":-0.054}}],"header":{"mapName":"20260918102324025","mapType":"2D-Map","maxPos":{"x":8.206,"y":13.428},"minPos":{"x":-2.32,"y":-2.757},"resolution":0.02,"version":"1.0.6"},"normalPosList":[{"x":-2.32,"y":-2.757},{"y":0.5},{"x":8.206,"y":13.428}]})JSON");
|
|
testNativeFullMapUploadContent(source);
|
|
}
|
|
|
|
} // namespace
|
|
|
|
int main(int argc, char *argv[])
|
|
{
|
|
QCoreApplication application(argc, argv);
|
|
testPreservationAndBidirectionalRoutes();
|
|
testRejectionsAreAtomic();
|
|
testSnakeCaseAndMissingRouteArray();
|
|
testUnsupportedFormats();
|
|
testIdentifierCompatibility();
|
|
testStationNumericIdentifiers();
|
|
testStationNames();
|
|
testStationNameTokenPreservation();
|
|
testDeleteArrayTokenPreservation();
|
|
testDeleteDraftsAndIdentifierReservations();
|
|
testDeleteMergeConflictsAndConfirmation();
|
|
testNativeMapSerialization();
|
|
if (argc > 1) {
|
|
QFile fixture(QString::fromLocal8Bit(argv[1]));
|
|
require(fixture.open(QIODevice::ReadOnly), "local downloaded real SMAP fixture opens");
|
|
const QString content = testNativeFullMapUploadContent(QString::fromUtf8(fixture.readAll()));
|
|
if (argc > 2) {
|
|
QFile output(QString::fromLocal8Bit(argv[2]));
|
|
require(output.open(QIODevice::WriteOnly) && output.write(content.toUtf8()) == content.toUtf8().size(),
|
|
"optional local upload payload dump is written without network access");
|
|
}
|
|
}
|
|
qInfo() << "PASS: AGV map document offline tests";
|
|
return 0;
|
|
}
|