@@ -7,7 +7,7 @@ using namespace kv_cache_manager;
77
88class ChecksumVerifyUtilTest : public TESTBASE {};
99
10- // Fast path: 所有 block 一致 -> mismatch=false, faulty_indices 空。
10+ // 所有 block 一致 -> mismatch=false, faulty_indices 空。
1111TEST_F (ChecksumVerifyUtilTest, FastPathAllMatch) {
1212 std::vector<int64_t > expected = {0x1111 , 0x2222 , 0x3333 };
1313 std::vector<int64_t > actual = expected;
@@ -16,7 +16,7 @@ TEST_F(ChecksumVerifyUtilTest, FastPathAllMatch) {
1616 EXPECT_TRUE (r.faulty_indices .empty ());
1717}
1818
19- // Fast path: 检测到不匹配后应该回填 faulty_indices 让上层逐块打日志。
19+ // 检测到不匹配后应该回填 faulty_indices 让上层逐块打日志。
2020TEST_F (ChecksumVerifyUtilTest, FastPathDetectsMismatchAndLocatesIndex) {
2121 std::vector<int64_t > expected = {0x1111 , 0x2222 , 0x3333 };
2222 std::vector<int64_t > actual = {0x1111 , 0xFFFF , 0x3333 }; // block #1 错
@@ -26,7 +26,7 @@ TEST_F(ChecksumVerifyUtilTest, FastPathDetectsMismatchAndLocatesIndex) {
2626 EXPECT_EQ (r.faulty_indices [0 ], 1u );
2727}
2828
29- // 多个错位 block:fallback 阶段把所有错的都列出来 。
29+ // 多个错位 block:把所有错的都列出来 。
3030TEST_F (ChecksumVerifyUtilTest, FastPathListsAllFaultyBlocks) {
3131 std::vector<int64_t > expected = {0x1111 , 0x2222 , 0x3333 , 0x4444 };
3232 std::vector<int64_t > actual = {0xAAAA , 0x2222 , 0xBBBB , 0x4444 }; // #0, #2 错
@@ -45,7 +45,7 @@ TEST_F(ChecksumVerifyUtilTest, FastPathSentinelZeroIsSkipped) {
4545 EXPECT_FALSE (r.mismatch );
4646}
4747
48- // 全 sentinel:fast 路径没有可比较的项 ,等同于全 match。
48+ // 全 sentinel:没有可比较的项 ,等同于全 match。
4949TEST_F (ChecksumVerifyUtilTest, FastPathAllSentinelsTreatedAsMatch) {
5050 std::vector<int64_t > expected = {0 , 0 , 0 };
5151 std::vector<int64_t > actual = {0xAA , 0xBB , 0xCC };
@@ -62,7 +62,7 @@ TEST_F(ChecksumVerifyUtilTest, SizeMismatchReturnsMismatchWithoutIndices) {
6262 EXPECT_TRUE (r.faulty_indices .empty ());
6363}
6464
65- // Strict mode:行为跟 fast fallback 一致 (per-block 比对),但跳过 fast 聚合阶段 。
65+ // strict_mode 参数保留兼容;当前实现始终逐块比对 。
6666TEST_F (ChecksumVerifyUtilTest, StrictModeMatchesFastFallback) {
6767 std::vector<int64_t > expected = {0x1111 , 0 , 0x3333 , 0x4444 };
6868 std::vector<int64_t > actual = {0xAAAA , 0x2222 , 0x3333 , 0xBBBB }; // #0, #3 错;#1 是 sentinel
@@ -81,8 +81,7 @@ TEST_F(ChecksumVerifyUtilTest, StrictModeAllMatch) {
8181 EXPECT_FALSE (r.mismatch );
8282}
8383
84- // Block swap (读串): expected=[A,B], actual=[B,A]. 老 XOR 聚合无序会漏,
85- // 加了 position-dependent 奇数乘子后 fast path 也能识别并回填两个 faulty index。
84+ // Block swap (读串): expected=[A,B], actual=[B,A]. 必须识别并回填两个 faulty index。
8685TEST_F (ChecksumVerifyUtilTest, FastPathCatchesBlockSwap) {
8786 std::vector<int64_t > expected = {0xAAAA , 0xBBBB };
8887 std::vector<int64_t > actual = {0xBBBB , 0xAAAA };
@@ -98,8 +97,7 @@ TEST_F(ChecksumVerifyUtilTest, FastPathCatchesBlockSwap) {
9897}
9998
10099// Same-delta 成对突变:每个 block 都被同一 delta 改写 (expected=[A,B],
101- // actual=[A^X, B^X])。老 XOR fast 会 delta 对消而漏;新的乘法聚合不再有 GF(2)-
102- // 线性,所以两条路径都能识别。这里同时断言,防止将来实现回退成纯 XOR 时漏检回归。
100+ // actual=[A^X, B^X])。老 XOR fast 会 delta 对消而漏;逐块比对必须识别。
103101TEST_F (ChecksumVerifyUtilTest, DetectsSameDeltaPairedMutation) {
104102 constexpr int64_t kDelta = 0x0F0F0F0F0F0F0F0FLL ;
105103 std::vector<int64_t > expected = {0xAAAA , 0xBBBB };
@@ -111,3 +109,17 @@ TEST_F(ChecksumVerifyUtilTest, DetectsSameDeltaPairedMutation) {
111109 ASSERT_TRUE (r_strict.mismatch );
112110 EXPECT_EQ (r_strict.faulty_indices .size (), 2u );
113111}
112+
113+ // High-bit 成对突变:奇数乘法聚合也会让最高位 delta 在偶数个 block 中抵消。
114+ // 逐块比对不能接受这种 batch。
115+ TEST_F (ChecksumVerifyUtilTest, DetectsHighBitPairedMutation) {
116+ constexpr int64_t kHighBit = static_cast <int64_t >(0x8000000000000000ULL );
117+ std::vector<int64_t > expected = {0x1111 , 0x2222 , 0x3333 };
118+ std::vector<int64_t > actual = {0x1111 ^ kHighBit , 0x2222 ^ kHighBit , 0x3333 };
119+
120+ auto r = VerifyBatchChecksums (expected, actual, /* strict_mode=*/ false );
121+ ASSERT_TRUE (r.mismatch );
122+ ASSERT_EQ (r.faulty_indices .size (), 2u );
123+ EXPECT_EQ (r.faulty_indices [0 ], 0u );
124+ EXPECT_EQ (r.faulty_indices [1 ], 1u );
125+ }
0 commit comments