proxygen
FileTest.cpp File Reference
#include <string>
#include <vector>
#include <folly/File.h>
#include <folly/Range.h>
#include <folly/container/Array.h>
#include <folly/experimental/TestUtil.h>
#include <folly/gen/Base.h>
#include <folly/gen/File.h>
#include <folly/portability/GTest.h>

Go to the source code of this file.

Classes

class  FileGenBufferedTest
 

Functions

 TEST (FileGen, ByLine)
 
 TEST (FileGen, ByLineFull)
 
 TEST_P (FileGenBufferedTest, FileWriter)
 
 TEST (FileGenBufferedTest, FileWriterSimple)
 
 INSTANTIATE_TEST_CASE_P (DifferentBufferSizes, FileGenBufferedTest,::testing::Values(0, 1, 2, 4, 8, 64, 4096))
 

Function Documentation

INSTANTIATE_TEST_CASE_P ( DifferentBufferSizes  ,
FileGenBufferedTest  ,
::testing::Values(0, 1, 2, 4, 8, 64, 4096)   
)

Referenced by TEST().

TEST ( FileGen  ,
ByLine   
)

Definition at line 32 of file FileTest.cpp.

References folly::gen::byLine(), folly::collect(), EXPECT_EQ, folly::test::TemporaryFile::fd(), folly::gen::from(), folly::gen::lines(), folly::test::TemporaryFile::path(), folly::gen::resplit(), string, and fizz::detail::write().

32  {
33  auto collect = eachTo<std::string>() | as<vector>();
34  const std::string cases[] = {
35  "Hello world\n"
36  "This is the second line\n"
37  "\n"
38  "\n"
39  "a few empty lines above\n"
40  "incomplete last line",
41 
42  "complete last line\n",
43 
44  "\n",
45 
46  "",
47  };
48 
49  for (auto& lines : cases) {
50  test::TemporaryFile file("ByLine");
51  EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
52 
53  auto expected = from({lines}) | resplit('\n') | collect;
54  auto found = byLine(file.path().string().c_str()) | collect;
55 
56  EXPECT_EQ(expected, found) << "For Input: '" << lines << "'";
57  }
58 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
static auto collect
Definition: StringTest.cpp:37
S lines(StringPiece source)
Definition: String.h:80
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
auto byLine(File file, char delim= '\n')
Definition: File-inl.h:148
const char * string
Definition: Conv.cpp:212
TEST ( FileGen  ,
ByLineFull   
)

Definition at line 60 of file FileTest.cpp.

References folly::gen::byLineFull(), EXPECT_EQ, folly::test::TemporaryFile::fd(), folly::gen::lines(), folly::test::TemporaryFile::path(), folly::stripLeftMargin(), and fizz::detail::write().

60  {
61  auto cases = std::vector<std::string>{
62  stripLeftMargin(R"(
63  Hello world
64  This is the second line
65 
66 
67  a few empty lines above
68  incomplete last line)"),
69 
70  "complete last line\n",
71 
72  "\n",
73 
74  "",
75  };
76 
77  for (auto& lines : cases) {
78  test::TemporaryFile file("ByLineFull");
79  EXPECT_EQ(lines.size(), write(file.fd(), lines.data(), lines.size()));
80 
81  auto found =
82  byLineFull(file.path().string().c_str()) | unsplit<std::string>("");
83 
84  EXPECT_EQ(lines, found);
85  }
86 }
void write(const T &in, folly::io::Appender &appender)
Definition: Types-inl.h:112
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
auto byLineFull(File file, char delim= '\n')
Definition: File-inl.h:136
std::string stripLeftMargin(std::string s)
Definition: String.cpp:704
S lines(StringPiece source)
Definition: String.h:80
TEST ( FileGenBufferedTest  ,
FileWriterSimple   
)

Definition at line 111 of file FileTest.cpp.

References folly::gen::byLine(), EXPECT_EQ, folly::test::TemporaryFile::fd(), INSTANTIATE_TEST_CASE_P(), folly::gen::map(), folly::test::TemporaryFile::path(), folly::gen::seq(), folly::gen::sum, folly::gen::toFile(), v, and x.

111  {
112  test::TemporaryFile file("FileWriter");
113  auto toLine = [](int v) { return to<std::string>(v, '\n'); };
114 
115  auto squares = seq(1, 100) | map([](int x) { return x * x; });
116  squares | map(toLine) | eachAs<StringPiece>() | toFile(File(file.fd()));
117  EXPECT_EQ(
118  squares | sum,
119  byLine(File(file.path().string().c_str())) | eachTo<int>() | sum);
120 }
Definition: InvokeTest.cpp:58
std::atomic< int64_t > sum(0)
auto v
#define EXPECT_EQ(val1, val2)
Definition: gtest.h:1922
const int x
Gen seq(Value first, Value last)
Definition: Base.h:484
static Map map(mapCap)
auto byLine(File file, char delim= '\n')
Definition: File-inl.h:148
S toFile(File file, size_t bufferSize=4096)
Definition: File.h:55
TEST_P ( FileGenBufferedTest  ,
FileWriter   
)

Definition at line 90 of file FileTest.cpp.

References folly::gen::byLine(), folly::collect(), EXPECT_TRUE, folly::test::TemporaryFile::fd(), folly::gen::from(), folly::gen::lines(), folly::test::TemporaryFile::path(), folly::gen::resplit(), string, and folly::gen::toFile().

90  {
91  size_t bufferSize = GetParam();
92  test::TemporaryFile file("FileWriter");
93 
94  static const std::string lines(
95  "Hello world\n"
96  "This is the second line\n"
97  "\n"
98  "\n"
99  "a few empty lines above\n");
100 
101  auto src = from({lines, lines, lines, lines, lines, lines, lines, lines});
102  auto collect = eachTo<std::string>() | as<vector>();
103  auto expected = src | resplit('\n') | collect;
104 
105  src | eachAs<StringPiece>() | toFile(File(file.fd()), bufferSize);
106  auto found = byLine(file.path().string().c_str()) | collect;
107 
108  EXPECT_TRUE(expected == found);
109 }
S resplit(char delimiter, bool keepDelimiter=false)
Definition: String.h:56
static auto collect
Definition: StringTest.cpp:37
S lines(StringPiece source)
Definition: String.h:80
PUSHMI_INLINE_VAR constexpr struct folly::pushmi::operators::from_fn from
#define EXPECT_TRUE(condition)
Definition: gtest.h:1859
auto byLine(File file, char delim= '\n')
Definition: File-inl.h:148
const char * string
Definition: Conv.cpp:212
S toFile(File file, size_t bufferSize=4096)
Definition: File.h:55