Testing Read()/Write() where length > maxlength To do this, the functions were modified to take maxlength as a parameter. In production code, the default maxlength is like 2GB, which is too large for fast tests.
| 623 | // the default maxlength is like 2GB, which is too |
| 624 | // large for fast tests. |
| 625 | int test6() { |
| 626 | const char *input1_contents = "diskfile_test test6 input1.txt"; |
| 627 | |
| 628 | { |
| 629 | DiskFile diskfile(std::cout, std::cerr); |
| 630 | if (!diskfile.Create("input1.txt", strlen(input1_contents))) { |
| 631 | std::cout << "Create failed!" << std::endl; |
| 632 | return 1; |
| 633 | } |
| 634 | |
| 635 | if (!diskfile.Write(0, input1_contents, strlen(input1_contents), 2)) { |
| 636 | std::cout << "Write failed 1" << std::endl; |
| 637 | return 1; |
| 638 | } |
| 639 | |
| 640 | diskfile.Close(); |
| 641 | } |
| 642 | |
| 643 | { |
| 644 | DiskFile diskfile(std::cout, std::cerr); |
| 645 | |
| 646 | if (!diskfile.Open("input1.txt")) { |
| 647 | std::cout << "Open failed" << std::endl; |
| 648 | return 1; |
| 649 | } |
| 650 | |
| 651 | const size_t buffer_len = strlen(input1_contents)+1; // for end-of-string |
| 652 | u8 *buffer = new u8[buffer_len]; |
| 653 | // put end-of-string in buffer. |
| 654 | buffer[buffer_len-1] = '\0'; |
| 655 | |
| 656 | if (!diskfile.Read(0, buffer, buffer_len - 1, 2)) { |
| 657 | std::cout << "Read whole file returned false" << std::endl; |
| 658 | return 1; |
| 659 | } |
| 660 | |
| 661 | if (std::string(input1_contents) != (char *) buffer) { |
| 662 | std::cout << "Read did not read contents correctly" << std::endl; |
| 663 | std::cout << "read \"" << buffer << "\"" << std::endl; |
| 664 | std::cout << "expected \"" << input1_contents << "\"" << std::endl; |
| 665 | return 1; |
| 666 | } |
| 667 | |
| 668 | diskfile.Close(); |
| 669 | |
| 670 | remove("input1.txt"); |
| 671 | } |
| 672 | |
| 673 | |
| 674 | const char *input2_contents = "diskfile_test test6 input2.txt is longer"; |
| 675 | |
| 676 | // try again, writing mid-file with different maxlength. |
| 677 | { |
| 678 | DiskFile diskfile(std::cout, std::cerr); |
| 679 | if (!diskfile.Create("input2.txt", strlen(input2_contents))) { |
| 680 | std::cout << "Create 2 failed." << std::endl; |
| 681 | return 1; |
| 682 | } |