Files
hm bd4edde0cc fix(foundation): 补齐 R2010b+ TABLE/TABLECONTENT/TABLEGEOMETRY 比较签名
- TABLE:追加 R2010b+ 布局/未知字段/分段数据(r2010_layout、hor_dir、break_*)
  与内容段(content. 前缀)递归签名——单元格内容值段、属性引用、内容格式
  覆盖、单元格样式与几何缓存逐项展开;R2010b+ 的 pre-2010 cells 为空,此前
  只签名 pre-2010 字段,导致仅单元格文本不同的两个 AC1024+ 表格被误判
  identical。
- TABLECONTENT:改为逐列/逐行/逐单元格完整签名(名称、自定义数据、样式、
  内容值段、链接数据、行高与单元格几何缓存),修复内容不同却签名相同。
- TABLEGEOMETRY:在计数与标志之外展开每个单元格的嵌套几何缓存
  (dist_top_left/dist_center/content_width/content_height/width/height/flags)。
- 默认等价:R2007a+ TABLE 单元值段仅在非默认时追加键,R2000 位流无该段、
  解码后保持默认;R2010b+ 专有字段全部包在 r2010_layout 分支内;因此
  AC1015/AC1032 合成夹具签名黄金哈希(55412cac01f7f9f3 /
  47f27f6e42f426aa)与既有写侧黄金字节均不变。
- 测试:test_dwg_compare 新增 6 例——AC1032 生成往返/双跑幂等、AC1015/
  AC1032 夹具签名黄金哈希锁定、R2010/R2013/R2018 表格内容差异检测、
  TABLECONTENT/TABLEGEOMETRY 差异检测、真实 R2018 样本
  (VDE_DWG_AC1032_COMPARE_SAMPLE 环境变量,未设置则 GTEST_SKIP)自比较
  稳定;tests/foundation/CMakeLists.txt 为其注入 VDE_TEST_DATA_DIR。
- 验证:build/vde-r92cmp(MinGW/Ninja/Release)下 test_dwg_compare 12 项
  (11 过 1 跳过)、test_dwg_tables 76 项、test_dxf_tables 22 项、
  test_dwg_writer 39 项全绿(跳过项均为缺少真实样本的既有 env 门控);
  真实 AC1032 语料两份(R2018)自比较 identical、双跑摘要逐字节幂等。
2026-11-17 20:35:26 +08:00

767 lines
32 KiB
C++
Raw Permalink Blame History

This file contains ambiguous Unicode characters
This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.
/**
* @file test_dwg_compare.cpp
* @brief DWG 原生实体签名与两文档比较(D-20 尾项)验收测试
*
* 验收口径:
* 1. 同一文档两次写出/解码后比较必须逐句柄零差异(新增/删除/修改均为空);
* 2. 稳定签名只含类型名、句柄与字段摘要(固定精度),不含地址/指针信息,
* 同一对象重复生成签名字节相同;
* 3. LINE 端点/图层变化、实体新增/删除都能按句柄归并检出;实体增删同时
* 改变模型空间块记录实体链入口时,默认比较如实报告结构性 modified,
* 可用 compare_non_entities=false 只看实体并计入跳过计数;
* 4. AC1015(R2000)正常写出并经 `decode_dwg_objects()` 解码后比较为零差异;
* 5. 精度按选项截断(4e-7 在默认 6 位下不可见、8 位下可见),
* 不支持字段级比较的类型(PROXY_ENTITY 等)只签名、不比较,
* 计数进入 `unsupported_object_count`/`unsupported_types` 并给出告警,
* `fully_compared()` 诚实返回 false;
* 6. 非 R2000 目标:AC1032(R2018)生成文件两次解码后自比较零差异、幂等;
* R2010/R2013/R2018 的 R2010b+ TABLE 内容段(单元格文本)、独立
* TABLECONTENT 单元格内容与 TABLEGEOMETRY 逐单元格几何都必须进入签名,
* 构造对象只有这些字段不同时报告 modified(修复前为静默 identical 的
* 假阴性);pre-2010 布局不得出现 R2010b+ 键;
* 7. 入库 R2000/R2018 夹具的签名哈希作为黄金基线锁定既有签名字节(AC1015
* 扩展前后不变),缺失时 GTEST_SKIP;真实 R2018 样本经
* `VDE_DWG_AC1032_COMPARE_SAMPLE` 环境变量参与自比较/幂等验证。
*/
#include <gtest/gtest.h>
#include "vde/foundation/dwg_compare.h"
#include "vde/foundation/dwg_object.h"
#include "vde/foundation/dwg_writer.h"
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <string>
#include <utility>
#include <vector>
#ifndef VDE_TEST_TEMP_DIR
#define VDE_TEST_TEMP_DIR "."
#endif
#ifndef VDE_TEST_DATA_DIR
#define VDE_TEST_DATA_DIR ""
#endif
using vde::foundation::compare_dwg_documents;
using vde::foundation::compare_dwg_files;
using vde::foundation::decode_dwg_objects;
using vde::foundation::decode_dwg_objects_file;
using vde::foundation::dwg_object_compare_supported;
using vde::foundation::dwg_object_signature;
using vde::foundation::dwg_object_type_compare_supported;
using vde::foundation::dwg_version_name;
using vde::foundation::dwg_write_ac1015;
using vde::foundation::dwg_write_ac1015_file;
using vde::foundation::dwg_write_ac1032;
using vde::foundation::DwgCompareOptions;
using vde::foundation::DwgCompareResult;
using vde::foundation::DwgObject;
using vde::foundation::DwgObjectDocument;
using vde::foundation::DwgObjectSignature;
using vde::foundation::DwgObjectType;
using vde::foundation::DwgProxyEntity;
using vde::foundation::DwgTable;
using vde::foundation::DwgTableCellContent;
using vde::foundation::DwgTableCellGeometry;
using vde::foundation::DwgTableContent;
using vde::foundation::DwgTableContentCell;
using vde::foundation::DwgTableContentObject;
using vde::foundation::DwgTableContentRow;
using vde::foundation::DwgTableGeometry;
using vde::foundation::DwgTableGeometryCell;
using vde::foundation::DwgVersion;
using vde::foundation::DwgWriteDocument;
using vde::foundation::DwgWriteEntity;
using vde::foundation::DwgWriteEntityKind;
using vde::foundation::DwgWriteLayer;
using vde::foundation::DwgWritePoint;
using vde::foundation::DwgWriteResult;
namespace {
DwgWritePoint point(double x, double y, double z = 0.0) {
DwgWritePoint value;
value.x = x;
value.y = y;
value.z = z;
return value;
}
DwgWriteEntity line_entity(const DwgWritePoint& start, const DwgWritePoint& end,
const std::string& layer = "0") {
DwgWriteEntity entity;
entity.kind = DwgWriteEntityKind::Line;
entity.layer = layer;
entity.start = start;
entity.end = end;
return entity;
}
DwgWriteEntity circle_entity(const DwgWritePoint& center, double radius,
const std::string& layer = "0") {
DwgWriteEntity entity;
entity.kind = DwgWriteEntityKind::Circle;
entity.layer = layer;
entity.center = center;
entity.radius = radius;
return entity;
}
DwgObjectDocument roundtrip_ac1015(const DwgWriteDocument& document) {
const DwgWriteResult written = dwg_write_ac1015(document);
EXPECT_TRUE(written.ok) << written.error;
if (!written.ok) return {};
const DwgObjectDocument decoded =
decode_dwg_objects(written.bytes.data(), written.bytes.size());
EXPECT_TRUE(decoded.ok) << decoded.error;
return decoded;
}
std::vector<std::uint64_t> handles_of_type(const DwgObjectDocument& document,
DwgObjectType type) {
std::vector<std::uint64_t> handles;
for (const DwgObject& object : document.objects) {
if (object.type == type) handles.push_back(object.handle);
}
return handles;
}
bool contains_text(const std::string& haystack, const std::string& needle) {
return haystack.find(needle) != std::string::npos;
}
std::string temp_path(const std::string& name) {
return std::string(VDE_TEST_TEMP_DIR) + "/" + name;
}
/// 入库夹具路径(VDE_FIXTURE_ROOT 由 CMake 注入;缺失时由测试 GTEST_SKIP)。
std::string fixture_path(const std::string& name) {
return std::string(VDE_TEST_DATA_DIR) + "/real/" + name;
}
bool file_exists(const std::string& path) {
std::FILE* file = std::fopen(path.c_str(), "rb");
if (file == nullptr) return false;
std::fclose(file);
return true;
}
constexpr std::uint64_t kFnvOffset = 1469598103934665603ull;
std::uint64_t fnv1a64(const std::string& text,
std::uint64_t hash = kFnvOffset) {
for (const unsigned char byte : text) {
hash ^= byte;
hash *= 1099511628211ull;
}
return hash;
}
std::string hex64(std::uint64_t value) {
char buffer[32] = {};
std::snprintf(buffer, sizeof(buffer), "%016llx",
static_cast<unsigned long long>(value));
return buffer;
}
/// 与对象探针同口径:按文档对象顺序对参与字段级比较的签名文本做 FNV-1a
/// 64 位摘要。同一文档两次生成必须一致(无地址/指针/运行期状态)。
std::string signature_hash(const DwgObjectDocument& document) {
std::uint64_t hash = kFnvOffset;
for (const DwgObject& object : document.objects) {
if (!dwg_object_compare_supported(object)) continue;
hash = fnv1a64(dwg_object_signature(object).text, hash);
}
return hex64(hash);
}
/// R2010b+ 表格内容:1 列 × 1 行,单元格内容值段为字符串 `cell_text`。
DwgTableContent table_content_with_cell_text(const std::string& cell_text) {
DwgTableContent content;
content.columns.resize(1);
content.columns[0].name = "col0";
content.columns[0].width = 10.0;
content.rows.resize(1);
DwgTableContentRow& row = content.rows[0];
row.num_cells = 1;
row.height = 5.0;
DwgTableContentCell cell;
DwgTableCellContent entry;
entry.type = 1; // type==1 时带 TABLE_value_fields 值段
entry.value.data_type = 4; // 字符串
entry.value.data_text = cell_text;
cell.cell_contents.push_back(entry);
row.cells.push_back(cell);
return content;
}
/// R2010b+ TABLE 实体(r2010_layout=true 时内容在 content 中;false 时走
/// pre-2010 的 cells 布局,用于验证 AC1015 等旧版本签名不追加新键)。
DwgObject table_object(std::uint64_t handle, const std::string& cell_text,
bool r2010_layout = true) {
DwgObject object;
object.handle = handle;
object.type = DwgObjectType::Table;
object.type_name = "ACAD_TABLE";
object.decoded = true;
object.consistent = true;
object.is_entity = true;
DwgTable table;
table.r2010_layout = r2010_layout;
if (r2010_layout) {
table.content = table_content_with_cell_text(cell_text);
} else {
table.num_cols = 1;
table.num_rows = 1;
table.col_widths.push_back(10.0);
table.row_heights.push_back(5.0);
}
object.data = std::move(table);
return object;
}
DwgObject table_content_object(std::uint64_t handle,
const std::string& cell_text) {
DwgObject object;
object.handle = handle;
object.type = DwgObjectType::TableContent;
object.type_name = "TABLECONTENT";
object.decoded = true;
object.consistent = true;
DwgTableContentObject value;
value.content = table_content_with_cell_text(cell_text);
object.data = std::move(value);
return object;
}
DwgObject table_geometry_object(std::uint64_t handle, double content_width) {
DwgObject object;
object.handle = handle;
object.type = DwgObjectType::TableGeometry;
object.type_name = "TABLEGEOMETRY";
object.decoded = true;
object.consistent = true;
DwgTableGeometry geometry;
geometry.num_rows = 1;
geometry.num_cols = 1;
geometry.num_cells = 1;
DwgTableGeometryCell cell;
cell.geom_data_flag = 1;
cell.width_w_gap = content_width;
cell.height_w_gap = 3.0;
DwgTableCellGeometry detail;
detail.content_width = content_width;
detail.content_height = 2.0;
detail.width = content_width;
detail.height = 2.0;
cell.geometry.push_back(detail);
geometry.cells.push_back(cell);
object.data = std::move(geometry);
return object;
}
DwgObjectDocument document_with(std::vector<DwgObject> objects,
DwgVersion version = DwgVersion::R2018) {
DwgObjectDocument document;
document.ok = true;
document.version = version;
document.objects = std::move(objects);
return document;
}
} // namespace
TEST(DwgCompare, IdenticalDocumentsHaveNoDifferences) {
DwgWriteDocument document;
document.entities.push_back(line_entity(point(0.0, 0.0), point(10.0, 0.0)));
document.entities.push_back(circle_entity(point(5.0, 5.0), 2.5));
const DwgObjectDocument left = roundtrip_ac1015(document);
const DwgObjectDocument right = roundtrip_ac1015(document);
ASSERT_TRUE(left.ok);
ASSERT_TRUE(right.ok);
const DwgCompareResult result = compare_dwg_documents(left, right);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical());
EXPECT_TRUE(result.fully_compared());
EXPECT_EQ(result.difference_count(), 0u);
EXPECT_TRUE(result.added.empty());
EXPECT_TRUE(result.removed.empty());
EXPECT_TRUE(result.modified.empty());
EXPECT_TRUE(result.added_handles.empty());
EXPECT_TRUE(result.removed_handles.empty());
EXPECT_TRUE(result.modified_handles.empty());
EXPECT_GT(result.compared_object_count, 0u);
EXPECT_EQ(result.left_object_count, left.objects.size());
EXPECT_EQ(result.right_object_count, right.objects.size());
EXPECT_EQ(result.left_version, DwgVersion::R2000);
EXPECT_TRUE(result.warnings.empty());
EXPECT_TRUE(contains_text(result.summary(), "identical=1"));
EXPECT_TRUE(contains_text(result.summary(), "added=0"));
// 签名确定性:同一对象两次生成的文本完全一致,且不含地址/指针信息。
ASSERT_FALSE(left.objects.empty());
const DwgObjectSignature first = dwg_object_signature(left.objects.front());
const DwgObjectSignature second = dwg_object_signature(left.objects.front());
EXPECT_EQ(first.text, second.text);
EXPECT_EQ(first.handle, left.objects.front().handle);
EXPECT_FALSE(first.text.empty());
EXPECT_EQ(first.text.find("0x"), std::string::npos);
EXPECT_EQ(first.text.find("address"), std::string::npos);
EXPECT_EQ(first.text.find("pointer"), std::string::npos);
}
TEST(DwgCompare, ModifiedLineEndpointAndLayerAreDetected) {
DwgWriteDocument document_a;
DwgWriteLayer custom_layer;
custom_layer.name = "A";
custom_layer.color = 1;
document_a.layers.push_back(custom_layer);
document_a.entities.push_back(
line_entity(point(0.0, 0.0), point(3.0, 4.0)));
DwgWriteDocument document_b = document_a;
document_b.entities[0] =
line_entity(point(0.0, 0.0), point(5.0, 6.0), "A");
const DwgObjectDocument left = roundtrip_ac1015(document_a);
const DwgObjectDocument right = roundtrip_ac1015(document_b);
ASSERT_TRUE(left.ok);
ASSERT_TRUE(right.ok);
const std::vector<std::uint64_t> left_lines =
handles_of_type(left, DwgObjectType::Line);
ASSERT_EQ(left_lines.size(), 1u);
const DwgCompareResult result = compare_dwg_documents(left, right);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.identical());
EXPECT_TRUE(result.added.empty());
EXPECT_TRUE(result.removed.empty());
ASSERT_EQ(result.modified.size(), 1u);
ASSERT_EQ(result.modified_handles.size(), 1u);
EXPECT_EQ(result.modified[0].handle, left_lines[0]);
EXPECT_EQ(result.modified_handles[0], left_lines[0]);
EXPECT_EQ(result.modified[0].type, DwgObjectType::Line);
EXPECT_NE(result.modified[0].left_signature,
result.modified[0].right_signature);
EXPECT_TRUE(contains_text(result.modified[0].left_signature, "layer="));
EXPECT_TRUE(contains_text(result.modified[0].left_signature,
"(3.000000,4.000000,0.000000)"));
EXPECT_TRUE(contains_text(result.modified[0].right_signature,
"(5.000000,6.000000,0.000000)"));
EXPECT_TRUE(result.fully_compared());
}
TEST(DwgCompare, AddedAndRemovedObjectsAreDetected) {
const DwgWriteEntity line = line_entity(point(0.0, 0.0), point(1.0, 0.0));
DwgWriteDocument one_entity;
one_entity.entities.push_back(line);
DwgWriteDocument two_entities;
two_entities.entities.push_back(line);
two_entities.entities.push_back(circle_entity(point(2.0, 2.0), 1.0));
const DwgObjectDocument smaller = roundtrip_ac1015(one_entity);
const DwgObjectDocument larger = roundtrip_ac1015(two_entities);
ASSERT_TRUE(smaller.ok);
ASSERT_TRUE(larger.ok);
const std::vector<std::uint64_t> circles =
handles_of_type(larger, DwgObjectType::Circle);
ASSERT_EQ(circles.size(), 1u);
const DwgCompareResult added_result =
compare_dwg_documents(smaller, larger);
ASSERT_TRUE(added_result.ok) << added_result.error;
ASSERT_EQ(added_result.added.size(), 1u);
ASSERT_EQ(added_result.added_handles.size(), 1u);
EXPECT_EQ(added_result.added[0].handle, circles[0]);
EXPECT_EQ(added_result.added_handles[0], circles[0]);
EXPECT_EQ(added_result.added[0].type, DwgObjectType::Circle);
EXPECT_FALSE(added_result.added[0].right_signature.empty());
EXPECT_TRUE(added_result.removed.empty());
// 追加实体也会改变模型空间块记录的实体链入口(first/last entity),
// 因此默认比较如实报告 1 个结构性 modified(BLOCK_HEADER),而不是漏报。
ASSERT_EQ(added_result.modified.size(), 1u);
EXPECT_EQ(added_result.modified[0].type, DwgObjectType::BlockHeader);
EXPECT_EQ(added_result.difference_count(), 2u);
// compare_non_entities=false 时只看实体:新增的 CIRCLE 仍被检出,
// 结构性块记录修改被跳过并计数。
DwgCompareOptions entities_only;
entities_only.compare_non_entities = false;
const DwgCompareResult entities_only_result =
compare_dwg_documents(smaller, larger, entities_only);
ASSERT_TRUE(entities_only_result.ok) << entities_only_result.error;
ASSERT_EQ(entities_only_result.added.size(), 1u);
EXPECT_EQ(entities_only_result.added[0].handle, circles[0]);
EXPECT_TRUE(entities_only_result.removed.empty());
EXPECT_TRUE(entities_only_result.modified.empty());
EXPECT_GT(entities_only_result.skipped_non_entity_count, 0u);
ASSERT_FALSE(entities_only_result.warnings.empty());
EXPECT_TRUE(contains_text(entities_only_result.warnings[0],
"compare_non_entities=false"));
const DwgCompareResult removed_result =
compare_dwg_documents(larger, smaller);
ASSERT_TRUE(removed_result.ok) << removed_result.error;
ASSERT_EQ(removed_result.removed.size(), 1u);
ASSERT_EQ(removed_result.removed_handles.size(), 1u);
EXPECT_EQ(removed_result.removed[0].handle, circles[0]);
EXPECT_EQ(removed_result.removed[0].type, DwgObjectType::Circle);
EXPECT_FALSE(removed_result.removed[0].left_signature.empty());
EXPECT_TRUE(removed_result.added.empty());
// 反方向删除同理:模型空间块记录实体链入口变化被如实报告为结构性修改。
ASSERT_EQ(removed_result.modified.size(), 1u);
EXPECT_EQ(removed_result.modified[0].type, DwgObjectType::BlockHeader);
EXPECT_EQ(removed_result.difference_count(), 2u);
}
TEST(DwgCompare, Ac1015DocumentsCompareWithoutFalseDifferences) {
DwgWriteDocument document;
document.entities.push_back(line_entity(point(0.0, 0.0), point(10.0, 0.0)));
document.entities.push_back(circle_entity(point(3.0, 3.0), 1.5));
DwgWriteEntity arc;
arc.kind = DwgWriteEntityKind::Arc;
arc.center = point(0.0, 0.0);
arc.radius = 2.0;
arc.start_angle = 0.0;
arc.end_angle = 1.5707963267948966;
document.entities.push_back(arc);
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.insertion = point(1.0, 1.0);
text.height = 2.5;
text.text = "VDE";
document.entities.push_back(text);
DwgWriteEntity polyline;
polyline.kind = DwgWriteEntityKind::LwPolyline;
polyline.vertices = {point(0.0, 0.0), point(1.0, 0.0), point(1.0, 1.0)};
polyline.closed = true;
document.entities.push_back(polyline);
const DwgObjectDocument left = roundtrip_ac1015(document);
const DwgObjectDocument right = roundtrip_ac1015(document);
ASSERT_TRUE(left.ok);
ASSERT_TRUE(right.ok);
EXPECT_EQ(left.version, DwgVersion::R2000);
EXPECT_EQ(left.objects.size(), right.objects.size());
EXPECT_GT(left.entity_objects, 4u);
const DwgCompareResult result = compare_dwg_documents(left, right);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical());
EXPECT_TRUE(result.fully_compared());
EXPECT_TRUE(result.warnings.empty());
EXPECT_EQ(result.left_version, DwgVersion::R2000);
EXPECT_EQ(result.right_version, DwgVersion::R2000);
EXPECT_GT(result.compared_object_count, left.entity_objects);
}
TEST(DwgCompare, PrecisionAndUnsupportedTypesDegradeHonestly) {
DwgWriteDocument coarse_document;
coarse_document.entities.push_back(
line_entity(point(0.0, 0.0), point(3.0, 4.0)));
DwgWriteDocument fine_document;
fine_document.entities.push_back(
line_entity(point(0.0, 0.0), point(3.0000004, 4.0)));
const DwgObjectDocument coarse = roundtrip_ac1015(coarse_document);
const DwgObjectDocument fine = roundtrip_ac1015(fine_document);
ASSERT_TRUE(coarse.ok);
ASSERT_TRUE(fine.ok);
// 默认 6 位小数:4e-7 的差异被截断,视为相同。
const DwgCompareOptions default_options;
const DwgCompareResult rounded =
compare_dwg_documents(coarse, fine, default_options);
ASSERT_TRUE(rounded.ok) << rounded.error;
EXPECT_TRUE(rounded.identical()) << rounded.summary();
// 8 位小数:同一差异可见。
DwgCompareOptions precise_options;
precise_options.signature.precision = 8;
const DwgCompareResult precise =
compare_dwg_documents(coarse, fine, precise_options);
ASSERT_TRUE(precise.ok) << precise.error;
ASSERT_EQ(precise.modified.size(), 1u);
EXPECT_EQ(precise.modified[0].type, DwgObjectType::Line);
EXPECT_NE(precise.modified[0].left_signature,
precise.modified[0].right_signature);
// 不支持字段级比较的类型:只签名、不比较,计数 + 告警,fully_compared=false。
DwgObjectDocument left = roundtrip_ac1015(coarse_document);
DwgObjectDocument right = roundtrip_ac1015(coarse_document);
DwgObject proxy;
proxy.handle = 0xFFFFFFF0ull;
proxy.type = DwgObjectType::ProxyEntity;
proxy.type_name = "PROXY_ENTITY";
proxy.decoded = true;
proxy.consistent = true;
proxy.is_entity = true;
proxy.data = DwgProxyEntity{};
left.objects.push_back(proxy);
right.objects.push_back(proxy);
const DwgCompareResult degraded = compare_dwg_documents(left, right);
ASSERT_TRUE(degraded.ok) << degraded.error;
EXPECT_TRUE(degraded.identical());
EXPECT_FALSE(degraded.fully_compared());
EXPECT_EQ(degraded.unsupported_object_count, 2u);
EXPECT_TRUE(degraded.added.empty());
EXPECT_TRUE(degraded.removed.empty());
ASSERT_EQ(degraded.unsupported_types.size(), 1u);
EXPECT_EQ(degraded.unsupported_types[0].type, DwgObjectType::ProxyEntity);
EXPECT_EQ(degraded.unsupported_types[0].type_name, "PROXY_ENTITY");
EXPECT_EQ(degraded.unsupported_types[0].count, 2u);
ASSERT_FALSE(degraded.warnings.empty());
EXPECT_TRUE(contains_text(degraded.warnings.front(), "PROXY_ENTITY"));
EXPECT_TRUE(contains_text(degraded.summary(), "fully_compared=0"));
const DwgObjectSignature proxy_signature = dwg_object_signature(proxy);
EXPECT_FALSE(proxy_signature.compare_supported);
EXPECT_TRUE(contains_text(proxy_signature.text, "PROXY_ENTITY"));
EXPECT_FALSE(dwg_object_type_compare_supported(DwgObjectType::ProxyEntity));
EXPECT_TRUE(dwg_object_type_compare_supported(DwgObjectType::Line));
}
TEST(DwgCompare, FileComparisonUsesSamePipeline) {
DwgWriteDocument document;
document.entities.push_back(line_entity(point(0.0, 0.0), point(2.0, 0.0)));
document.entities.push_back(circle_entity(point(1.0, 1.0), 0.5));
const std::string left_path = temp_path("vde_dwg_compare_left.dwg");
const std::string right_path = temp_path("vde_dwg_compare_right.dwg");
const std::string added_path = temp_path("vde_dwg_compare_added.dwg");
std::string error;
ASSERT_TRUE(dwg_write_ac1015_file(left_path, document, &error)) << error;
ASSERT_TRUE(dwg_write_ac1015_file(right_path, document, &error)) << error;
const DwgCompareResult same = compare_dwg_files(left_path, right_path);
ASSERT_TRUE(same.ok) << same.error;
EXPECT_TRUE(same.identical());
EXPECT_EQ(same.left_version, DwgVersion::R2000);
EXPECT_EQ(same.right_version, DwgVersion::R2000);
EXPECT_GT(same.compared_object_count, 0u);
DwgWriteDocument wider = document;
wider.entities.push_back(circle_entity(point(4.0, 4.0), 1.0));
ASSERT_TRUE(dwg_write_ac1015_file(added_path, wider, &error)) << error;
const DwgCompareResult changed = compare_dwg_files(left_path, added_path);
ASSERT_TRUE(changed.ok) << changed.error;
ASSERT_EQ(changed.added.size(), 1u);
EXPECT_EQ(changed.added[0].type, DwgObjectType::Circle);
const DwgCompareResult missing =
compare_dwg_files(left_path, temp_path("vde_dwg_compare_missing.dwg"));
EXPECT_FALSE(missing.ok);
EXPECT_TRUE(contains_text(missing.error, "比较文件失败"));
std::remove(left_path.c_str());
std::remove(right_path.c_str());
std::remove(added_path.c_str());
}
TEST(DwgCompare, Ac1032GeneratedRoundtripIsIdenticalAndIdempotent) {
DwgWriteDocument document;
document.entities.push_back(line_entity(point(0.0, 0.0), point(10.0, 0.0)));
document.entities.push_back(circle_entity(point(5.0, 5.0), 2.5));
DwgWriteEntity text;
text.kind = DwgWriteEntityKind::Text;
text.insertion = point(1.0, 1.0);
text.height = 2.5;
text.text = "R2018";
document.entities.push_back(text);
const DwgWriteResult written = dwg_write_ac1032(document);
ASSERT_TRUE(written.ok) << written.error;
const DwgObjectDocument left =
decode_dwg_objects(written.bytes.data(), written.bytes.size());
const DwgObjectDocument right =
decode_dwg_objects(written.bytes.data(), written.bytes.size());
ASSERT_TRUE(left.ok) << left.error;
ASSERT_TRUE(right.ok) << right.error;
EXPECT_EQ(left.version, DwgVersion::R2018);
EXPECT_EQ(right.version, DwgVersion::R2018);
EXPECT_EQ(left.objects.size(), right.objects.size());
EXPECT_GT(left.entity_objects, 2u);
const DwgCompareResult result = compare_dwg_documents(left, right);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical()) << result.summary();
EXPECT_TRUE(result.fully_compared()) << result.summary();
EXPECT_EQ(result.difference_count(), 0u);
EXPECT_EQ(result.left_version, DwgVersion::R2018);
EXPECT_EQ(result.right_version, DwgVersion::R2018);
// 幂等:重复比较的摘要一致;两次独立解码的签名哈希一致。
const DwgCompareResult repeated = compare_dwg_documents(left, right);
EXPECT_EQ(result.summary(), repeated.summary());
EXPECT_EQ(signature_hash(left), signature_hash(right));
const DwgObjectDocument third =
decode_dwg_objects(written.bytes.data(), written.bytes.size());
ASSERT_TRUE(third.ok) << third.error;
EXPECT_EQ(signature_hash(left), signature_hash(third));
}
TEST(DwgCompare, Ac1015SignatureBytesStayStable) {
const std::string path = fixture_path("synth_ac1015.dwg");
if (!file_exists(path)) GTEST_SKIP() << "缺少入库夹具: " << path;
const DwgObjectDocument document = decode_dwg_objects_file(path);
ASSERT_TRUE(document.ok) << document.error;
EXPECT_EQ(document.version, DwgVersion::R2000);
// 修复前后探针实测同一哈希(55412cac01f7f9f3):R2010b+ 内容签名扩展
// 不得改变 AC1015 既有签名字节。
EXPECT_EQ(signature_hash(document), "55412cac01f7f9f3");
const DwgCompareResult result = compare_dwg_documents(document, document);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical());
EXPECT_TRUE(result.fully_compared());
}
TEST(DwgCompare, Ac1032FixtureSelfComparisonIsStable) {
const std::string path = fixture_path("synth_ac1032.dwg");
if (!file_exists(path)) GTEST_SKIP() << "缺少入库夹具: " << path;
const DwgObjectDocument first = decode_dwg_objects_file(path);
ASSERT_TRUE(first.ok) << first.error;
const DwgObjectDocument second = decode_dwg_objects_file(path);
ASSERT_TRUE(second.ok) << second.error;
EXPECT_EQ(first.version, DwgVersion::R2018);
EXPECT_EQ(second.version, DwgVersion::R2018);
EXPECT_GT(first.objects.size(), 0u);
// R2018 夹具同样锁住非表格对象的签名基线(47f27f6e42f426aa):内容段
// 扩展只应改变含 R2010b+ TABLE 族对象的输入,其它对象逐字节不变。
EXPECT_EQ(signature_hash(first), "47f27f6e42f426aa");
EXPECT_EQ(signature_hash(first), signature_hash(second));
const DwgCompareResult result = compare_dwg_documents(first, second);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical()) << result.summary();
EXPECT_TRUE(result.fully_compared()) << result.summary();
EXPECT_EQ(result.difference_count(), 0u);
EXPECT_EQ(result.left_object_count, first.objects.size());
EXPECT_EQ(result.compared_object_count, first.objects.size());
}
TEST(DwgCompare, R2010TableContentSignatureDetectsCellTextChanges) {
EXPECT_TRUE(dwg_object_type_compare_supported(DwgObjectType::Table));
const DwgObject left_object = table_object(0x10, "alpha");
const DwgObject right_object = table_object(0x10, "beta");
const DwgObjectSignature left_signature = dwg_object_signature(left_object);
const DwgObjectSignature right_signature = dwg_object_signature(right_object);
EXPECT_TRUE(left_signature.compare_supported);
EXPECT_TRUE(right_signature.compare_supported);
EXPECT_NE(left_signature.text, right_signature.text);
EXPECT_TRUE(contains_text(left_signature.text, "r2010_layout=1"));
EXPECT_TRUE(contains_text(left_signature.text, "content.row_count=1"));
EXPECT_TRUE(contains_text(left_signature.text, "alpha"));
EXPECT_TRUE(contains_text(right_signature.text, "beta"));
// 同一对象自比较零差异(签名确定性)。
const DwgObjectDocument same_document = document_with({left_object});
const DwgCompareResult same =
compare_dwg_documents(same_document, same_document);
ASSERT_TRUE(same.ok) << same.error;
EXPECT_TRUE(same.identical());
// 跨 R2010/R2013/R2018 三个非 R2000 版本:仅单元格文本不同也必须检出。
for (const DwgVersion version :
{DwgVersion::R2010, DwgVersion::R2013, DwgVersion::R2018}) {
const DwgCompareResult result =
compare_dwg_documents(document_with({left_object}, version),
document_with({right_object}, version));
ASSERT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.identical())
<< dwg_version_name(version) << " " << result.summary();
ASSERT_EQ(result.modified.size(), 1u) << dwg_version_name(version);
EXPECT_EQ(result.modified[0].handle, 0x10u);
EXPECT_EQ(result.modified[0].type, DwgObjectType::Table);
EXPECT_NE(result.modified[0].left_signature,
result.modified[0].right_signature);
EXPECT_TRUE(result.fully_compared());
}
// pre-2010 布局(AC1015 等)不得出现任何 R2010b+ / R2007a+ 新增键。
const DwgObjectSignature legacy_signature =
dwg_object_signature(table_object(0x11, "", false));
EXPECT_EQ(legacy_signature.text.find("r2010_layout"), std::string::npos);
EXPECT_EQ(legacy_signature.text.find("content."), std::string::npos);
EXPECT_EQ(legacy_signature.text.find("value."), std::string::npos);
}
TEST(DwgCompare, R2010TableContentAndGeometryObjectsDetectChanges) {
EXPECT_TRUE(
dwg_object_type_compare_supported(DwgObjectType::TableContent));
EXPECT_TRUE(
dwg_object_type_compare_supported(DwgObjectType::TableGeometry));
const DwgObjectDocument left = document_with(
{table_content_object(0x20, "alpha"), table_geometry_object(0x30, 1.0)});
const DwgObjectDocument right = document_with(
{table_content_object(0x20, "beta"), table_geometry_object(0x30, 2.0)});
const DwgCompareResult same = compare_dwg_documents(left, left);
ASSERT_TRUE(same.ok) << same.error;
EXPECT_TRUE(same.identical());
const DwgCompareResult result = compare_dwg_documents(left, right);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_FALSE(result.identical()) << result.summary();
ASSERT_EQ(result.modified.size(), 2u) << result.summary();
EXPECT_EQ(result.modified[0].handle, 0x20u);
EXPECT_EQ(result.modified[0].type, DwgObjectType::TableContent);
EXPECT_TRUE(contains_text(result.modified[0].right_signature, "beta"));
EXPECT_EQ(result.modified[1].handle, 0x30u);
EXPECT_EQ(result.modified[1].type, DwgObjectType::TableGeometry);
EXPECT_NE(result.modified[1].left_signature,
result.modified[1].right_signature);
EXPECT_TRUE(result.fully_compared());
EXPECT_EQ(result.difference_count(), 2u);
}
TEST(DwgCompare, RealAc1032SampleSelfComparisonIsStable) {
const char* sample = std::getenv("VDE_DWG_AC1032_COMPARE_SAMPLE");
if (sample == nullptr || sample[0] == '\0') {
GTEST_SKIP()
<< "未设置 VDE_DWG_AC1032_COMPARE_SAMPLE(真实 R2018 DWG 样本)";
}
const std::string path = sample;
if (!file_exists(path)) GTEST_SKIP() << "样本不存在: " << path;
const DwgObjectDocument first = decode_dwg_objects_file(path);
ASSERT_TRUE(first.ok) << first.error;
const DwgObjectDocument second = decode_dwg_objects_file(path);
ASSERT_TRUE(second.ok) << second.error;
EXPECT_EQ(first.version, DwgVersion::R2018);
EXPECT_GT(first.objects.size(), 0u);
EXPECT_GT(first.entity_objects, 0u);
EXPECT_GT(first.entity_decoded, 0u);
// 真实 R2018 样本的实体类型必须全部有解码路径(不含未实现类实体)。
EXPECT_EQ(first.entity_unsupported, 0u);
EXPECT_EQ(signature_hash(first), signature_hash(second));
const DwgCompareResult result = compare_dwg_documents(first, second);
ASSERT_TRUE(result.ok) << result.error;
EXPECT_TRUE(result.identical()) << result.summary();
EXPECT_EQ(result.difference_count(), 0u);
EXPECT_TRUE(result.added.empty());
EXPECT_TRUE(result.removed.empty());
EXPECT_TRUE(result.modified.empty());
EXPECT_EQ(result.left_object_count, first.objects.size());
EXPECT_EQ(result.right_object_count, second.objects.size());
EXPECT_GT(result.compared_object_count, 0u);
// 本模块按设计不比较的类(SPATIAL_INDEX/XRECORD/PLACEHOLDER 等)必须
// 诚实降级:计入 unsupported 并给出告警,而不是伪装成已比较。
if (!result.fully_compared()) {
EXPECT_FALSE(result.unsupported_types.empty());
EXPECT_FALSE(result.warnings.empty());
}
// 幂等:同一输入重复比较摘要稳定。
const DwgCompareResult repeated = compare_dwg_documents(first, second);
EXPECT_EQ(result.summary(), repeated.summary());
}