Skip to content

Commit

Permalink
Implement LocalFileWriter with tests (#91)
Browse files Browse the repository at this point in the history
Summary:
Pull Request resolved: #91

See title, implementation is fairly simple

Reviewed By: elliottlawrence

Differential Revision: D34885840

fbshipit-source-id: 6f118dc96bd392f8aaf563520c2ed7e7ae776b7a
  • Loading branch information
adshastri authored and facebook-github-bot committed Mar 21, 2022
1 parent f46b5d2 commit f8863c8
Show file tree
Hide file tree
Showing 5 changed files with 117 additions and 4 deletions.
21 changes: 17 additions & 4 deletions fbpcf/io/api/LocalFileWriter.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -7,17 +7,30 @@

#include "fbpcf/io/api/LocalFileWriter.h"
#include <cstddef>
#include <stdexcept>
#include <string>
#include <vector>

namespace fbpcf::io {
LocalFileWriter::LocalFileWriter(std::string /* filePath */) {}
LocalFileWriter::LocalFileWriter(std::string filePath) {
outputStream_ = std::make_unique<std::ofstream>(filePath);
}

int LocalFileWriter::close() {
return 0;
outputStream_->close();

return outputStream_->fail() ? -1 : 0;
}
size_t LocalFileWriter::write(std::vector<char>& /* buf */) {
return 0;

size_t LocalFileWriter::write(std::vector<char>& buf) {
outputStream_->write(buf.data(), buf.size());

if (outputStream_->fail()) {
throw std::runtime_error(
"Internal error when writing to local file. Stream integrity may have been affected.");
}

return buf.size();
}

LocalFileWriter::~LocalFileWriter() {
Expand Down
5 changes: 5 additions & 0 deletions fbpcf/io/api/LocalFileWriter.h
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,8 @@

#pragma once
#include <cstddef>
#include <fstream>
#include <memory>
#include <string>
#include <vector>

Expand All @@ -26,6 +28,9 @@ class LocalFileWriter : public IWriterCloser {
int close() override;
size_t write(std::vector<char>& buf) override;
~LocalFileWriter() override;

private:
std::unique_ptr<std::ofstream> outputStream_;
};

} // namespace fbpcf::io
71 changes: 71 additions & 0 deletions fbpcf/io/api/test/LocalFileWriterTest.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,71 @@
/*
* Copyright (c) Meta Platforms, Inc. and affiliates.
*
* This source code is licensed under the MIT license found in the
* LICENSE file in the root directory of this source tree.
*/

#include <gtest/gtest.h>
#include <stdio.h>
#include <filesystem>
#include <string>
#include "folly/logging/xlog.h"

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

namespace fbpcf::io {

inline void cleanup(std::string fileToDelete) {
remove(fileToDelete.c_str());
}

TEST(LocalFileWriterTest, testWritingToFile) {
std::string baseDir = IOTestHelper::getBaseDirFromPath(__FILE__);
std::string fileToWriteTo = baseDir + "data/local_file_writer_test_file.txt";
auto writer = std::make_unique<fbpcf::io::LocalFileWriter>(fileToWriteTo);

/*
CASE 1
Write simple string to file
*/
std::string toWrite =
"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);
EXPECT_EQ(nBytes, toWrite.size());

/*
CASE 2
Write arbitrary bytes to file
*/
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);
EXPECT_EQ(nBytes, arbitraryBytes.size());

/*
CASE 3
Write larger buffer
*/
std::string remainingLine =
"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);
EXPECT_EQ(nBytes, remainingLine.size());

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

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

cleanup(fileToWriteTo);
}

} // namespace fbpcf::io
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
this file contains the expected text in local_file_writer_test_file.text

LocalFileWriterTest writes to the above file
We assert that it's contents match this file
20 changes: 20 additions & 0 deletions fbpcf/io/api/test/utils/IOTestHelper.h
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,9 @@
#pragma once

#include <gtest/gtest.h>
#include <fstream>
#include <memory>

#include <string>

namespace fbpcf::io {
Expand All @@ -26,6 +29,23 @@ class IOTestHelper {
static std::string getBaseDirFromPath(const std::string& filePath) {
return filePath.substr(0, filePath.rfind("/") + 1);
}

static void expectFileContentsMatch(
std::string testFilePath,
std::string expectedFilePath) {
auto testFile = std::make_unique<std::ifstream>(testFilePath);
auto expectedFile = std::make_unique<std::ifstream>(expectedFilePath);

while (!expectedFile->eof()) {
if (testFile->eof()) {
FAIL();
}

auto expected = expectedFile->get();
auto test = testFile->get();
EXPECT_EQ(test, expected);
}
}
};

} // namespace fbpcf::io

0 comments on commit f8863c8

Please sign in to comment.