Skip to content

Commit

Permalink
Add test for FileWriter through LocalFileWriterTest (#139)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #139

This diff does the same as what the previous diff does, but for FileWriter

Reviewed By: elliottlawrence

Differential Revision: D34978545

fbshipit-source-id: 70fd698b637531b1ffa205d241eebdfbe0a113ce
  • Loading branch information
adshastri authored and facebook-github-bot committed Mar 23, 2022
1 parent 27b0484 commit b47a72e
Showing 1 changed file with 40 additions and 16 deletions.
56 changes: 40 additions & 16 deletions fbpcf/io/api/test/LocalFileWriterTest.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -12,21 +12,16 @@
#include <string>
#include "folly/logging/xlog.h"

#include "fbpcf/io/api/FileWriter.h"
#include "fbpcf/io/api/LocalFileWriter.h"
#include "fbpcf/io/api/test/utils/IOTestHelper.h"

namespace fbpcf::io {

TEST(LocalFileWriterTest, testWritingToFile) {
std::string baseDir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::random_device rd;
std::default_random_engine defEngine(rd());
std::uniform_int_distribution<int> intDistro(1, 25000);
auto randint = intDistro(defEngine);
std::string fileToWriteTo = baseDir + "data/local_file_writer_test_file" +
std::to_string(randint) + ".txt";
auto writer = std::make_unique<fbpcf::io::LocalFileWriter>(fileToWriteTo);

inline void runBaseWriterTests(
IWriterCloser& writer,
std::string fileToWriteTo,
std::string expectedFile) {
/*
CASE 1
Write simple string to file
Expand All @@ -35,7 +30,7 @@ TEST(LocalFileWriterTest, testWritingToFile) {
"this file contains the expected text in local_file_writer_test_file.text";
auto buf =
std::vector<char>(toWrite.c_str(), toWrite.c_str() + toWrite.size());
auto nBytes = writer->write(buf);
auto nBytes = writer.write(buf);
EXPECT_EQ(nBytes, toWrite.size());

/*
Expand All @@ -45,7 +40,7 @@ TEST(LocalFileWriterTest, testWritingToFile) {
std::vector<char> arbitraryBytes{'\n', '\n', 'L', 'o', 'c', 'a', 'l', 'F',
'i', 'l', 'e', 'W', 'r', 'i', 't', 'e',
'r', 'T', 'e', 's', 't', ' '};
nBytes = writer->write(arbitraryBytes);
nBytes = writer.write(arbitraryBytes);
EXPECT_EQ(nBytes, arbitraryBytes.size());

/*
Expand All @@ -56,18 +51,47 @@ TEST(LocalFileWriterTest, testWritingToFile) {
"writes to the above file\nWe assert that it's contents match this file\n";
auto buf2 = std::vector<char>(
remainingLine.c_str(), remainingLine.c_str() + remainingLine.size());
nBytes = writer->write(buf2);
nBytes = writer.write(buf2);
EXPECT_EQ(nBytes, remainingLine.size());

EXPECT_EQ(writer->close(), 0);
writer.close();

/*
Verify that file contents match the expected
*/
IOTestHelper::expectFileContentsMatch(
fileToWriteTo, baseDir + "data/expected_local_file_writer_test_file.txt");
IOTestHelper::expectFileContentsMatch(fileToWriteTo, expectedFile);

IOTestHelper::cleanup(fileToWriteTo);
}

TEST(LocalFileWriterTest, testWritingToFile) {
std::string baseDir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::random_device rd;
std::default_random_engine defEngine(rd());
std::uniform_int_distribution<int> intDistro(1, 25000);
auto randint = intDistro(defEngine);
std::string fileToWriteTo = baseDir + "data/local_file_writer_test_file" +
std::to_string(randint) + ".txt";
std::string expectedFile =
baseDir + "data/expected_local_file_writer_test_file.txt";

auto writer = fbpcf::io::LocalFileWriter(fileToWriteTo);
runBaseWriterTests(writer, fileToWriteTo, expectedFile);
}

TEST(LocalFileWriterTest, testLocalFileWriterThroughFileWriter) {
std::string baseDir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::random_device rd;
std::default_random_engine defEngine(rd());
std::uniform_int_distribution<int> intDistro(1, 25000);
auto randint = intDistro(defEngine);
std::string fileToWriteTo = baseDir + "data/local_file_writer_test_file" +
std::to_string(randint) + ".txt";
std::string expectedFile =
baseDir + "data/expected_local_file_writer_test_file.txt";

auto writer = fbpcf::io::FileWriter(fileToWriteTo);
runBaseWriterTests(writer, fileToWriteTo, expectedFile);
}

} // namespace fbpcf::io

0 comments on commit b47a72e

Please sign in to comment.