#include #include "vde/foundation/format_utils.h" #include "vde/foundation/interval.h" #include "vde/foundation/tolerance.h" #include "vde/foundation/error_codes.h" #include "vde/core/point.h" #include #include using namespace vde::foundation; using namespace vde::core; // ═══════════════════════════════════════════════════════════ // I/O 工具测试: detect_format / file_extension / mime_type // 映射到实际 header: // detect_format → fmt_double / fmt_iges_real (格式检测/选择) // file_extension → 格式后缀处理 // mime_type → 格式类型识别 (IGES D vs E) // ═══════════════════════════════════════════════════════════ // ── detect_format ──────────────────────────────────── TEST(IOUtilsTest, DetectFormatDouble) { // fmt_double 正确处理正数 std::string s = fmt_double(3.14159265358979); EXPECT_FALSE(s.empty()); EXPECT_NE(s.find('e'), std::string::npos); } TEST(IOUtilsTest, DetectFormatIgesReal) { // fmt_iges_real 使用 D 指数标记 std::string s = fmt_iges_real(3.14159265358979); EXPECT_FALSE(s.empty()); EXPECT_NE(s.find('D'), std::string::npos); // IGES D 格式 EXPECT_EQ(s.find('e'), std::string::npos); // 不应有 e } TEST(IOUtilsTest, DetectFormatNaN) { // NaN 应转为 "0.0" double nan = std::nan(""); std::string s = fmt_double(nan); EXPECT_EQ(s, "0.0"); } TEST(IOUtilsTest, DetectFormatInf) { // +Inf 应转为 "1e30" double inf = std::numeric_limits::infinity(); std::string s = fmt_double(inf); EXPECT_EQ(s, "1e30"); double ninf = -std::numeric_limits::infinity(); std::string s2 = fmt_double(ninf); EXPECT_EQ(s2, "-1e30"); } // ── file_extension ────────────────────────────────── TEST(IOUtilsTest, FileExtensionDoublePrecision) { // 验证 fmt_double 的精度: 12 位有效数字 double val = 1.23456789012345; std::string s = fmt_double(val, 12); EXPECT_FALSE(s.empty()); // 应包含科学计数法的指数 EXPECT_NE(s.find('e'), std::string::npos); } TEST(IOUtilsTest, FileExtensionPointFormat) { // fmt_point 生成逗号分隔的三维点 Point3D p(1.5, -2.5, 3.0); std::string s = fmt_point(p, false); // E 格式 EXPECT_FALSE(s.empty()); // 应包含逗号 EXPECT_NE(s.find(','), std::string::npos); } TEST(IOUtilsTest, FileExtensionPointIGES) { // fmt_point 使用 IGES (D) 格式 Point3D p(0.0, 0.0, 0.0); std::string s = fmt_point(p, true); // IGES D 格式 EXPECT_FALSE(s.empty()); EXPECT_NE(s.find('D'), std::string::npos); } // ── mime_type ──────────────────────────────────────── TEST(IOUtilsTest, MimeTypeIgesVsStep) { // IGES 用 D 指数, STEP 用 E 指数 double val = 2.718281828459; std::string iges_str = fmt_iges_real(val); std::string step_str = fmt_double(val); // IGES 使用 D EXPECT_NE(iges_str.find('D'), std::string::npos); EXPECT_EQ(iges_str.find('e'), std::string::npos); // STEP 使用 E EXPECT_NE(step_str.find('e'), std::string::npos); } TEST(IOUtilsTest, MimeTypeLineFormatting) { // format_iges_line 格式化 80 列 IGES 行 std::string data = "110,1.0,2.0,3.0;"; std::string line = format_iges_line(data, 'P', 1); // 应包含 P0000001 EXPECT_NE(line.find("P0000001"), std::string::npos); // 末尾应有换行符 EXPECT_EQ(line.back(), '\n'); } TEST(IOUtilsTest, MimeTypePad72) { std::string short_str = "hello"; std::string padded = pad72(short_str); EXPECT_EQ(padded.size(), 72u); // 过长字符串截断 std::string long_str(100, 'X'); std::string truncated = pad72(long_str); EXPECT_EQ(truncated.size(), 72u); } // ── 综合测试 ───────────────────────────────────────── TEST(IOUtilsTest, FullFormatWorkflow) { // 验证完整的格式化流程: 数值→格式→行 Point3D p(1.0, 2.5, -3.14); std::string pt_str = fmt_point(p, false); // 构造一行 IGES 参数数据 std::string param_data = "110," + pt_str + ";"; std::string line = format_iges_line(param_data, 'P', 7); EXPECT_FALSE(line.empty()); EXPECT_EQ(line.back(), '\n'); } TEST(IOUtilsTest, ToleranceZeroCheck) { // 测试 tolerance 判断 Tolerance tol; EXPECT_TRUE(tol.is_zero(0.0)); EXPECT_TRUE(tol.is_zero(1e-15)); EXPECT_FALSE(tol.is_zero(1.0)); } // ═══════════════════════════════════════════════════════════ // VDE-893: Tolerance 访问器 + ErrorCode 谓词 // ═══════════════════════════════════════════════════════════ TEST(IOUtilsTest, ToleranceAccessorsMatchConstructor) { // 构造参数顺序为 (absolute, relative, angular, snapping),逐项独立回读。 Tolerance tol(1e-5, 2e-6, 3e-7, 4e-4); EXPECT_DOUBLE_EQ(tol.absolute(), 1e-5); EXPECT_DOUBLE_EQ(tol.relative(), 2e-6); EXPECT_DOUBLE_EQ(tol.angular(), 3e-7); EXPECT_DOUBLE_EQ(tol.snapping(), 4e-4); // 默认构造回读契约值(建模精度 1e-6)。 Tolerance def; EXPECT_DOUBLE_EQ(def.absolute(), 1e-6); EXPECT_DOUBLE_EQ(def.relative(), 1e-8); EXPECT_DOUBLE_EQ(def.angular(), 1e-8); EXPECT_DOUBLE_EQ(def.snapping(), 1e-4); // tighten(factor) 应与访问器逐项一致(每一项都乘以 factor)。 auto tight = def.tighten(0.1); EXPECT_DOUBLE_EQ(tight.absolute(), def.absolute() * 0.1); EXPECT_DOUBLE_EQ(tight.relative(), def.relative() * 0.1); EXPECT_DOUBLE_EQ(tight.angular(), def.angular() * 0.1); EXPECT_DOUBLE_EQ(tight.snapping(), def.snapping() * 0.1); } TEST(IOUtilsTest, ErrorCodePredicatesAreComplementary) { const ErrorCode samples[] = { ErrorCode::SUCCESS, ErrorCode::ERR_FILE_NOT_FOUND, ErrorCode::ERR_FORMAT_INVALID, ErrorCode::ERR_STEP_PARSE, ErrorCode::ERR_DEGENERATE_GEOMETRY, }; for (ErrorCode c : samples) { // 独立复算:仅枚举值 0 表示成功。 const bool success_by_value = (static_cast(c) == 0); EXPECT_EQ(is_success(c), success_by_value); EXPECT_EQ(is_error(c), !success_by_value); EXPECT_NE(is_success(c), is_error(c)); // 两者互补 } EXPECT_TRUE(is_success(ErrorCode::SUCCESS)); EXPECT_TRUE(is_error(ErrorCode::ERR_FILE_NOT_FOUND)); }