Function to verify byte-for-byte matching with detailed error reporting
| 530 | |
| 531 | // Function to verify byte-for-byte matching with detailed error reporting |
| 532 | bool verify_match(const std::string& original, const std::string& generated) { |
| 533 | if (original.size() != generated.size()) { |
| 534 | cout << "Size mismatch: original=" << original.size() |
| 535 | << " bytes, generated=" << generated.size() << " bytes\n"; |
| 536 | return false; |
| 537 | } |
| 538 | |
| 539 | // Helper function to determine if a character is printable |
| 540 | auto is_printable = [](unsigned char c) { return c >= 32 && c < 127; }; |
| 541 | |
| 542 | // Helper function to format a byte as string (either character or hex) |
| 543 | auto format_byte = [&is_printable](unsigned char c) -> std::string { |
| 544 | if (is_printable(c)) { |
| 545 | return std::string(1, c); |
| 546 | } |
| 547 | else { |
| 548 | std::stringstream ss; |
| 549 | ss << "\\x" << std::hex << std::setw(2) << std::setfill('0') << static_cast<int>(c); |
| 550 | return ss.str(); |
| 551 | } |
| 552 | }; |
| 553 | |
| 554 | // Helper function to display context around a position |
| 555 | auto show_context = [&](size_t pos, size_t context_size) { |
| 556 | size_t start = (pos >= context_size) ? pos - context_size : 0; |
| 557 | size_t end = std::min(original.size(), pos + context_size + 1); |
| 558 | |
| 559 | std::string orig_context, gen_context; |
| 560 | std::string orig_highlight, gen_highlight; |
| 561 | |
| 562 | for (size_t i = start; i < end; ++i) { |
| 563 | unsigned char orig_c = static_cast<unsigned char>(original[i]); |
| 564 | unsigned char gen_c = static_cast<unsigned char>(generated[i]); |
| 565 | |
| 566 | orig_context += format_byte(orig_c); |
| 567 | gen_context += format_byte(gen_c); |
| 568 | |
| 569 | if (i == pos) { |
| 570 | orig_highlight = format_byte(orig_c); |
| 571 | gen_highlight = format_byte(gen_c); |
| 572 | } |
| 573 | } |
| 574 | |
| 575 | cout << "Context at position " << pos << ":\n"; |
| 576 | cout << "Original (" << (int)original[pos] << " = '" << orig_highlight |
| 577 | << "'): " << orig_context << "\n"; |
| 578 | cout << "Generated (" << (int)generated[pos] << " = '" << gen_highlight |
| 579 | << "'): " << gen_context << "\n"; |
| 580 | }; |
| 581 | |
| 582 | size_t mismatch_count = 0; |
| 583 | const size_t max_detailed_mismatches = 10; // Maximum number of detailed errors to display |
| 584 | const size_t context_size = 10; // Number of characters to show before/after error |
| 585 | |
| 586 | // Track error patterns |
| 587 | std::map<std::pair<char, char>, int> error_patterns; |
| 588 | |
| 589 | // Analyze consecutive error regions |