mirror of
https://github.com/RGBCube/serenity
synced 2025-07-25 17:57:35 +00:00
LibCore: Add unit test for File::read_line
This commit is contained in:
parent
714a96619f
commit
9ffb3e7e30
4 changed files with 66 additions and 0 deletions
46
Tests/LibCore/TestLibCoreIODevice.cpp
Normal file
46
Tests/LibCore/TestLibCoreIODevice.cpp
Normal file
|
@ -0,0 +1,46 @@
|
|||
/*
|
||||
* Copyright (c) 2021, the SerenityOS developers.
|
||||
*
|
||||
* SPDX-License-Identifier: BSD-2-Clause
|
||||
*/
|
||||
|
||||
#include <LibCore/File.h>
|
||||
#include <LibTest/TestCase.h>
|
||||
#include <unistd.h>
|
||||
|
||||
TEST_CASE(file_readline)
|
||||
{
|
||||
auto path = "long_lines.txt";
|
||||
auto file_or_error = Core::File::open(path, Core::OpenMode::ReadOnly);
|
||||
if (file_or_error.is_error()) {
|
||||
warnln("Failed to open {}: {}", path, strerror(errno));
|
||||
VERIFY_NOT_REACHED();
|
||||
}
|
||||
auto file = file_or_error.release_value();
|
||||
auto output_path = "/tmp/output.txt";
|
||||
auto outfile_or_error = Core::File::open(output_path, Core::OpenMode::WriteOnly);
|
||||
auto outputfile = outfile_or_error.release_value();
|
||||
while (file->can_read_line()) {
|
||||
outputfile->write(file->read_line());
|
||||
outputfile->write("\n");
|
||||
}
|
||||
file->close();
|
||||
outputfile->close();
|
||||
|
||||
// Open files again for comparison since otherwise read_all returns empty (even when not closing the file)
|
||||
file = Core::File::construct(path);
|
||||
if (!file->open(Core::OpenMode::ReadOnly))
|
||||
VERIFY_NOT_REACHED();
|
||||
outputfile = Core::File::construct(output_path);
|
||||
if (!outputfile->open(Core::OpenMode::ReadOnly))
|
||||
VERIFY_NOT_REACHED();
|
||||
auto inputData = file->read_all();
|
||||
auto outputData = outputfile->read_all();
|
||||
EXPECT(inputData.size() > 0);
|
||||
EXPECT_EQ(inputData.size(), outputData.size());
|
||||
|
||||
// Compare char by char
|
||||
for (size_t i = 0; i < inputData.size(); i++) {
|
||||
EXPECT_EQ(inputData[i], outputData[i]);
|
||||
}
|
||||
}
|
Loading…
Add table
Add a link
Reference in a new issue