一、Tools 完善 * OffscreenChecks `edgestats`:改为自动挑"最有起伏"的信号。旧实现固定取 `signals[0]`,而 MF4 的第一条信号是常量(6000)⇒ 三个阈值的断言退化成 "0 == 0" 的空转。现在扫描前 200 条挑 `max-min` 最大者,SCATTER_TEST 上 三个阈值分别得到 (20,19)/(20,19)/(20,19),与独立参考实现逐位相同。 * UnitTests 新增 `stdio_mingw_vs_ucrt_formats`:把 MinGW ANSI stdio 与系统 UCRT 的 printf 在**本程序实际用到的全部格式**(`%.*f` / `%.6g` / `%g` / `%.4e` / `%.2f %ls` / `%llu` / `%lld` / `%02X` / `%.2f%%`)上做逐字符比对, 13.6 万次调用 **0 处差异**(覆盖 1e-40~1e40)。它是"把 swprintf 让给 UCRT" 这条体积优化的验收标准。 * Tools/README.md:同步新增用例与断言数量(219 项 / 18 用例)。 二、第 33 轮报告 §10 后续建议的结论(详细数字见 Progress 第 34 轮) 1. **`EdgeScanFor` 状态查找哈希化:实测后回退。** 实现方式为 `deque` + `unordered_multimap<keyHash, EdgeScanState*>`(桶内仍 逐字段比对,碰撞安全;新建/淘汰/定向作废/清空四处同步索引),功能与像素 验证全绿。但 198 个状态的整轮查询只从 0.097 降到 0.073 us/query(一轮省 ≈4.8 us),而产物体积 +5,632 B。⇒ 回退。 同时修正报告的量级:原估"288 信号 ≈0.3ms/帧",实测 ≈20us/帧。 2. **`EdgePanelLayoutFor` 表驱动化:不做。** 实测 1.9~2.5 us/次(每帧约 14 次 ≈30us),不是热点;第 33 轮已消除"按种类分派"的重复,剩余 957 行是几何算式 本身,无法表驱动。本轮改为交付"让这类重写可验证"的手段: `-run panels -mode record/compare` 逐面板比对几何 + 像素哈希。 3. **`wcstod` 自写替身:实测 0 字节收益。** 只加 `-D__USE_MINGW_ANSI_STDIO=0` 全量重编后产物仍是 788,480 B、`mingw_wpformat.o` 仍被链接。原因是预编译 libstdc++ 自己引用 `__mingw_swprintf`:只要还调用 `std::to_wstring` / `std::to_string`(全仓约 80 处,若干处实参是浮点), gdtoa 必然被链入。⇒ 回退,并在 Makefile 留下说明防止重复尝试。 三、文档 * `Documents/Code_Review_Plan.md` 新增 §六(第 34 轮节点与验收标准)。 * `Documents/Code_Review_Progress.md` 新增第 34 轮逐节点记录(含全部实测数字)。 * `Documents/Code_Review_Report.md` 新增 §11(结论表 + 两处量级修正 + 更新的后续建议)。 * `Documents/agent_mem.md` 新增第 34 轮规则:Tools 约定、两个构建陷阱、 两条"别再重复做"的实验结论。 验证: * `make release`(删 .o 全量)零 error / 零 warning,产物 **788,480 B**(与改动前一致)。 * `make -C Tools` 零 error / 零 warning。 * `UnitTests.exe` → checks=219 failed=0 cases=18。 * `OffscreenChecks.exe -all` → 5/5 检查 PASS,checks=71 failed=0。
437 lines
13 KiB
C++
437 lines
13 KiB
C++
/**
|
||
* @file Utf8UtilTests.cpp
|
||
* @brief `Utilities/Utf8Util` 的数值解析 / 文本格式化语义回归。
|
||
*
|
||
* 重点用例:
|
||
* * `parse.bit_exact_vs_strtod` —— **逐位**对比 `TryParseDouble` 与 libc `strtod`。
|
||
* 这是"自写 strtod 替身"类体积优化唯一可信的验收标准(见 Code Review 报告 §10.4):
|
||
* 常见写法(≤15 位有效数字、|十进制指数| ≤ 22)必须逐位相同;
|
||
* 17 位以上的长数字允许 ≤1 ULP 偏差(实现里已注释该取舍)。
|
||
* * `format.*` —— 只断言文档里给出的实例与"绝不出现科学计数法"这类硬约束,
|
||
* 避免把测试绑死在内部格式细节上。
|
||
*/
|
||
|
||
#include <cmath>
|
||
#include <cstdint>
|
||
#include <cstdio>
|
||
#include <cstdlib>
|
||
#include <cstring>
|
||
#include <cwchar>
|
||
#include <random>
|
||
#include <string>
|
||
#include <vector>
|
||
|
||
#include <stdio.h>
|
||
#include <wchar.h>
|
||
|
||
#include "UnitTestFramework.hpp"
|
||
#include "Utf8Util.hpp"
|
||
|
||
namespace
|
||
{
|
||
/** @brief 取 double 的位模式。 */
|
||
std::uint64_t BitsOf(const double value)
|
||
{
|
||
std::uint64_t bits = 0;
|
||
std::memcpy(&bits, &value, sizeof(bits));
|
||
return bits;
|
||
}
|
||
|
||
/** @brief 两个 double 之间相差多少个 ULP(异号时返回一个很大的数)。 */
|
||
long long UlpDifference(const double left, const double right)
|
||
{
|
||
if (left == right)
|
||
{
|
||
return 0;
|
||
}
|
||
const std::uint64_t a = BitsOf(left);
|
||
const std::uint64_t b = BitsOf(right);
|
||
if ((a >> 63) != (b >> 63))
|
||
{
|
||
return 1000000;
|
||
}
|
||
return (a > b) ? static_cast<long long>(a - b) : -static_cast<long long>(b - a);
|
||
}
|
||
|
||
/** @brief 用 `TryParseDouble` 解析整段文本。 */
|
||
bool Parse(const std::string& text, double& value)
|
||
{
|
||
return jdi::TryParseDouble(text.c_str(), text.c_str() + text.size(), value);
|
||
}
|
||
|
||
/** @brief 统计十进制有效数字位数(忽略符号/小数点/前导零/指数部分)。 */
|
||
int SignificantDigits(const std::string& text)
|
||
{
|
||
int digits = 0;
|
||
bool sawNonZero = false;
|
||
for (const char character : text)
|
||
{
|
||
if (character == 'e' || character == 'E')
|
||
{
|
||
break;
|
||
}
|
||
if (character < '0' || character > '9')
|
||
{
|
||
continue;
|
||
}
|
||
if (character != '0')
|
||
{
|
||
sawNonZero = true;
|
||
}
|
||
if (sawNonZero)
|
||
{
|
||
++digits;
|
||
}
|
||
}
|
||
return digits;
|
||
}
|
||
|
||
/** @brief 生成 `数字.数字e指数` 形式的随机十进制文本。 */
|
||
std::string MakeRandomNumber(std::mt19937& rng, const int maxDigits, const bool allowFraction,
|
||
const bool allowExponent, const int maxExponent)
|
||
{
|
||
std::string text;
|
||
if ((rng() & 1u) != 0u)
|
||
{
|
||
text.push_back('-');
|
||
}
|
||
const int integerDigits = 1 + static_cast<int>(rng() % static_cast<unsigned>(maxDigits));
|
||
for (int index = 0; index < integerDigits; ++index)
|
||
{
|
||
text.push_back(static_cast<char>('0' + (rng() % 10u)));
|
||
}
|
||
if (allowFraction && (rng() % 3u) != 0u)
|
||
{
|
||
text.push_back('.');
|
||
const int fractionDigits = 1 + static_cast<int>(rng() % static_cast<unsigned>(maxDigits));
|
||
for (int index = 0; index < fractionDigits; ++index)
|
||
{
|
||
text.push_back(static_cast<char>('0' + (rng() % 10u)));
|
||
}
|
||
}
|
||
if (allowExponent && (rng() % 2u) == 0u)
|
||
{
|
||
text.push_back((rng() & 1u) != 0u ? 'e' : 'E');
|
||
const bool negativeExponent = (rng() & 1u) != 0u;
|
||
text.push_back(negativeExponent ? '-' : '+');
|
||
const int exponent = static_cast<int>(rng() % static_cast<unsigned>(maxExponent + 1));
|
||
const std::string digits = std::to_string(exponent);
|
||
text += digits;
|
||
}
|
||
return text;
|
||
}
|
||
} // namespace
|
||
|
||
JDI_TEST(parse_fixed_corpus_matches_strtod)
|
||
{
|
||
static const char* const corpus[] = {
|
||
"0", "1", "-1", "+1", "0.1", "-0.1", "1.5",
|
||
"123.456", "-123.456", "1e10", "1e-10", "1E+5", "1e22", "1e-22",
|
||
"3.141592653589793", "2.718281828459045", "6.02214076e23", "1.602176634e-19", "0.000001",
|
||
"1000000", "-0", "0.0", "123456789012345",
|
||
};
|
||
|
||
for (const char* const text : corpus)
|
||
{
|
||
double ours = 0.0;
|
||
const bool accepted = Parse(text, ours);
|
||
|
||
char* stop = nullptr;
|
||
const double reference = std::strtod(text, &stop);
|
||
const bool referenceAccepted = (stop != text);
|
||
|
||
JDI_REQUIRE(text, accepted == referenceAccepted);
|
||
if (accepted && referenceAccepted)
|
||
{
|
||
JDI_CHECK_BIT_EQUAL(ours, reference);
|
||
}
|
||
}
|
||
}
|
||
|
||
JDI_TEST(parse_acceptance_semantics)
|
||
{
|
||
double value = 0.0;
|
||
JDI_CHECK(!Parse("", value));
|
||
JDI_CHECK(!Parse(" ", value));
|
||
JDI_CHECK(!Parse("abc", value));
|
||
JDI_CHECK(!Parse("e5", value));
|
||
|
||
// 尾部杂字符:与 `strtod` 一致,接受前缀数值(历史语义,不要改)
|
||
JDI_CHECK(Parse("12abc", value));
|
||
JDI_CHECK_NEAR(value, 12.0, 0.0);
|
||
JDI_CHECK(Parse("1,5", value));
|
||
JDI_CHECK_NEAR(value, 1.0, 0.0);
|
||
|
||
// 首尾空白应被忽略
|
||
JDI_CHECK(Parse(" -2.5\t\r\n", value));
|
||
JDI_CHECK_NEAR(value, -2.5, 0.0);
|
||
|
||
// 十六进制浮点 / inf / nan 走标准库回退路径,语义必须与 strtod 相同
|
||
static const char* const exotic[] = {"0x1p3", "inf", "-inf", "nan", "infinity", "1e9999", "1e-9999"};
|
||
for (const char* const text : exotic)
|
||
{
|
||
double ours = 0.0;
|
||
char* stop = nullptr;
|
||
const double reference = std::strtod(text, &stop);
|
||
const bool referenceAccepted = (stop != text);
|
||
JDI_REQUIRE(text, Parse(text, ours) == referenceAccepted);
|
||
if (referenceAccepted)
|
||
{
|
||
JDI_CHECK_BIT_EQUAL(ours, reference);
|
||
}
|
||
}
|
||
}
|
||
|
||
JDI_TEST(parse_random_corpus_bit_exact)
|
||
{
|
||
std::mt19937 rng(20261104u);
|
||
int mismatches = 0;
|
||
int beyondOneUlp = 0;
|
||
int cases = 0;
|
||
|
||
// 常见写法:≤15 位有效数字、|指数| ≤ 22 ⇒ 必须逐位相同。
|
||
for (int iteration = 0; iteration < 40000; ++iteration)
|
||
{
|
||
const std::string text = MakeRandomNumber(rng, 6, true, true, 22);
|
||
double ours = 0.0;
|
||
char* stop = nullptr;
|
||
const double reference = std::strtod(text.c_str(), &stop);
|
||
if (stop == text.c_str())
|
||
{
|
||
continue;
|
||
}
|
||
++cases;
|
||
if (!Parse(text, ours))
|
||
{
|
||
++mismatches;
|
||
continue;
|
||
}
|
||
if (BitsOf(ours) != BitsOf(reference))
|
||
{
|
||
++mismatches;
|
||
if (SignificantDigits(text) <= 15 && std::fabs(reference) > 1e-300 &&
|
||
std::fabs(reference) < 1e300)
|
||
{
|
||
std::printf(" [INFO] bit mismatch on short literal: %s ours=%.17g ref=%.17g\n", text.c_str(),
|
||
ours, reference);
|
||
}
|
||
}
|
||
}
|
||
|
||
JDI_CHECK_EQ(cases, 40000);
|
||
JDI_CHECK_EQ(mismatches, 0);
|
||
|
||
// 长数字(最多 19 位):允许 ≤1 ULP 偏差(实现里有明确注释与容差约定)。
|
||
for (int iteration = 0; iteration < 20000; ++iteration)
|
||
{
|
||
const std::string text = MakeRandomNumber(rng, 19, true, true, 22);
|
||
double ours = 0.0;
|
||
char* stop = nullptr;
|
||
const double reference = std::strtod(text.c_str(), &stop);
|
||
if (stop == text.c_str() || !Parse(text, ours))
|
||
{
|
||
continue;
|
||
}
|
||
if (UlpDifference(ours, reference) > 1)
|
||
{
|
||
++beyondOneUlp;
|
||
}
|
||
}
|
||
JDI_CHECK_EQ(beyondOneUlp, 0);
|
||
}
|
||
|
||
JDI_TEST(format_decimal_never_scientific)
|
||
{
|
||
JDI_CHECK_WIDE_EQ(jdi::FormatDecimal(1.0e-5), std::wstring(L"0.00001"));
|
||
JDI_CHECK_WIDE_EQ(jdi::FormatDecimal(0.0), std::wstring(L"0"));
|
||
JDI_CHECK_WIDE_EQ(jdi::FormatDecimalFixed(700.0, 3), std::wstring(L"700.000"));
|
||
|
||
static const double values[] = {1.0e-9, 1.0e-6, 1.0e-5, 0.000123456, 1.0, 1234.5678, 1.0e9};
|
||
for (const double value : values)
|
||
{
|
||
const std::wstring text = jdi::FormatDecimal(value);
|
||
JDI_REQUIRE("FormatDecimal must not use exponent", text.find(L'e') == std::wstring::npos &&
|
||
text.find(L'E') == std::wstring::npos);
|
||
}
|
||
|
||
// 文档实例:`FormatValue` = `%.6g`
|
||
JDI_CHECK_WIDE_EQ(jdi::FormatValue(123.456789), std::wstring(L"123.457"));
|
||
JDI_CHECK_WIDE_EQ(jdi::FormatValue(1.0), std::wstring(L"1"));
|
||
}
|
||
|
||
JDI_TEST(format_byte_size_and_frequency)
|
||
{
|
||
const std::wstring bytes = jdi::FormatByteSize(1024ull * 1024ull);
|
||
JDI_REQUIRE("FormatByteSize(1MB) contains MB", bytes.find(L"MB") != std::wstring::npos);
|
||
|
||
const std::wstring hertz = jdi::FormatFrequency(270.5166);
|
||
JDI_REQUIRE("FormatFrequency contains unit", hertz.find(L"Hz") != std::wstring::npos);
|
||
JDI_REQUIRE("FormatFrequency has no exponent", hertz.find(L'e') == std::wstring::npos);
|
||
|
||
const std::wstring duration = jdi::FormatDurationCompound(5.06);
|
||
JDI_REQUIRE("FormatDurationCompound has no exponent", duration.find(L'e') == std::wstring::npos);
|
||
JDI_REQUIRE("FormatDurationCompound ends with unit", duration.find(L's') != std::wstring::npos);
|
||
}
|
||
|
||
JDI_TEST(text_trim_and_utf8_conversion)
|
||
{
|
||
JDI_CHECK_WIDE_EQ(jdi::Trim(L" abc \t"), std::wstring(L"abc"));
|
||
JDI_CHECK_WIDE_EQ(jdi::Trim(L" \t\r\n "), std::wstring());
|
||
JDI_CHECK_WIDE_EQ(jdi::ToLowerAscii(L"AbC-129"), std::wstring(L"abc-129"));
|
||
|
||
const std::wstring wide = jdi::Utf8ToWide("abc");
|
||
JDI_CHECK_WIDE_EQ(wide, std::wstring(L"abc"));
|
||
|
||
JDI_CHECK(jdi::ContainsInsensitive(L"HelloWorld", L"world"));
|
||
JDI_CHECK(!jdi::ContainsInsensitive(L"HelloWorld", L"xyz"));
|
||
}
|
||
|
||
JDI_TEST(ticks_nice_step_and_build_ticks)
|
||
{
|
||
// `NiceStep` 必须落在 1/2/2.5/5 × 10^n 这类"好读"的格点上
|
||
static const double ranges[] = {1.0, 10.0, 123.0, 0.0003, 98765.0};
|
||
for (const double range : ranges)
|
||
{
|
||
const double step = jdi::NiceStep(range, 10);
|
||
JDI_REQUIRE("NiceStep > 0", step > 0.0);
|
||
const double ratio = step / std::pow(10.0, std::floor(std::log10(step)));
|
||
const bool nice = std::fabs(ratio - 1.0) < 1e-9 || std::fabs(ratio - 2.0) < 1e-9 ||
|
||
std::fabs(ratio - 2.5) < 1e-9 || std::fabs(ratio - 5.0) < 1e-9;
|
||
JDI_REQUIRE("NiceStep is a round number", nice);
|
||
}
|
||
|
||
std::vector<double> ticks;
|
||
jdi::BuildTicks(0.0, 10.0, 1.0, ticks);
|
||
JDI_CHECK_EQ(ticks.size(), 11);
|
||
if (ticks.size() == 11)
|
||
{
|
||
JDI_CHECK_NEAR(ticks.front(), 0.0, 0.0);
|
||
JDI_CHECK_NEAR(ticks.back(), 10.0, 0.0);
|
||
}
|
||
}
|
||
|
||
/**
|
||
* @brief MinGW ANSI stdio 与 UCRT printf 在**本程序实际使用的格式**上是否逐字符一致。
|
||
*
|
||
* 用途:评估"把 `swprintf` 让给系统 UCRT 实现(`__USE_MINGW_ANSI_STDIO=0`)"
|
||
* 这条体积优化是否可行 —— 若成立可以省掉整个 `mingw_wpformat` + `gdtoa` 链(≈33KB)。
|
||
* 判据极其严格:**逐字符相同**,一个字符的差异都不接受(显示文本是产品语义)。
|
||
*/
|
||
JDI_TEST(stdio_mingw_vs_ucrt_formats)
|
||
{
|
||
std::mt19937 rng(20261107u);
|
||
std::uniform_real_distribution<double> distribution(-1.0e7, 1.0e7);
|
||
std::uniform_real_distribution<double> magnitude(-40.0, 40.0);
|
||
|
||
int mismatches = 0;
|
||
int compared = 0;
|
||
static const int decimalsList[] = {0, 1, 2, 3, 4, 6, 8, 12};
|
||
|
||
for (int iteration = 0; iteration < 8000; ++iteration)
|
||
{
|
||
double value = distribution(rng);
|
||
// 覆盖极小 / 极大 / 整数量级,以及 FormatDecimal 会遇到的量级
|
||
if ((iteration % 7) == 0)
|
||
{
|
||
value = std::pow(10.0, magnitude(rng)) * ((iteration % 2 == 0) ? 1.0 : -1.0);
|
||
}
|
||
if ((iteration % 11) == 0)
|
||
{
|
||
value = static_cast<double>(iteration);
|
||
}
|
||
|
||
wchar_t ours[128] = {};
|
||
wchar_t theirs[128] = {};
|
||
|
||
for (const int decimals : decimalsList)
|
||
{
|
||
swprintf(ours, 128, L"%.*f", decimals, value);
|
||
_snwprintf(theirs, 128, L"%.*f", decimals, value);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
if (mismatches < 5)
|
||
{
|
||
std::printf(" [INFO] %%.%df value=%.17g ours=\"%ls\" ucrt=\"%ls\"\n", decimals, value, ours,
|
||
theirs);
|
||
}
|
||
++mismatches;
|
||
}
|
||
}
|
||
|
||
struct WideFormatProbe
|
||
{
|
||
const wchar_t* format;
|
||
};
|
||
static const WideFormatProbe probes[] = {{L"%.6g"}, {L"%.4e"}, {L"%.2f"}, {L"%g"}};
|
||
for (const WideFormatProbe& probe : probes)
|
||
{
|
||
swprintf(ours, 128, probe.format, value);
|
||
_snwprintf(theirs, 128, probe.format, value);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
if (mismatches < 5)
|
||
{
|
||
std::printf(" [INFO] \"%ls\" value=%.17g ours=\"%ls\" ucrt=\"%ls\"\n", probe.format, value,
|
||
ours, theirs);
|
||
}
|
||
++mismatches;
|
||
}
|
||
}
|
||
|
||
// 非浮点格式(颜色十六进制 / 计数 / 百分比尾缀)
|
||
const unsigned long long counter = static_cast<unsigned long long>(iteration) * 7919ull;
|
||
swprintf(ours, 128, L"%llu", counter);
|
||
_snwprintf(theirs, 128, L"%llu", counter);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
++mismatches;
|
||
}
|
||
|
||
swprintf(ours, 128, L"%02X%02X%02X", iteration & 0xFF, (iteration >> 3) & 0xFF, (iteration >> 6) & 0xFF);
|
||
_snwprintf(theirs, 128, L"%02X%02X%02X", iteration & 0xFF, (iteration >> 3) & 0xFF,
|
||
(iteration >> 6) & 0xFF);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
++mismatches;
|
||
}
|
||
|
||
swprintf(ours, 128, L"%.2f%%", value / 1000.0);
|
||
_snwprintf(theirs, 128, L"%.2f%%", value / 1000.0);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
++mismatches;
|
||
}
|
||
|
||
// `%ls`:`FormatByteSize` 的 `%.2f %ls` 依赖"宽格式串里的 %ls = 宽字符串"。
|
||
// ⚠️ MSVC/UCRT 的宽 printf 里 `%s` 也表示**宽**字符串,与 MinGW ANSI(`%s`=窄)相反,
|
||
// 因此全仓禁止在宽格式串里写 `%s`(已有注释约束),这里顺带把 `%ls` 也钉住。
|
||
swprintf(ours, 128, L"%.2f %ls", value / 1024.0, L"MB");
|
||
_snwprintf(theirs, 128, L"%.2f %ls", value / 1024.0, L"MB");
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
if (mismatches < 5)
|
||
{
|
||
std::printf(" [INFO] \"%%.2f %%ls\" ours=\"%ls\" ucrt=\"%ls\"\n", ours, theirs);
|
||
}
|
||
++mismatches;
|
||
}
|
||
|
||
swprintf(ours, 128, L"%lld", static_cast<long long>(iteration) * -7919ll);
|
||
_snwprintf(theirs, 128, L"%lld", static_cast<long long>(iteration) * -7919ll);
|
||
++compared;
|
||
if (wcscmp(ours, theirs) != 0)
|
||
{
|
||
++mismatches;
|
||
}
|
||
}
|
||
|
||
std::printf(" [INFO] compared %d format calls, %d mismatches\n", compared, mismatches);
|
||
JDI_CHECK_EQ(mismatches, 0);
|
||
}
|