Use std::thread rather than pthreads in AtomicHashMapTest
[folly.git] / folly / test / FileUtilTest.cpp
index 23a49f96faf1d4762bee91dc54f92a376f4c6bbf..6a63b33cedee020a6bc8dee868acdf1779d4da87 100644 (file)
@@ -23,6 +23,7 @@
 #include <glog/logging.h>
 #include <gtest/gtest.h>
 
+#include <folly/File.h>
 #include <folly/Range.h>
 #include <folly/String.h>
 
@@ -159,7 +160,7 @@ TEST_F(FileUtilTest, read) {
   for (auto& p : readers_) {
     std::string out(in_.size(), '\0');
     EXPECT_EQ(p.first, wrapFull(p.second, 0, &out[0], out.size()));
-    if (p.first != (typeof(p.first))(-1)) {
+    if (p.first != (decltype(p.first))(-1)) {
       EXPECT_EQ(in_.substr(0, p.first), out.substr(0, p.first));
     }
   }
@@ -169,7 +170,7 @@ TEST_F(FileUtilTest, pread) {
   for (auto& p : readers_) {
     std::string out(in_.size(), '\0');
     EXPECT_EQ(p.first, wrapFull(p.second, 0, &out[0], out.size(), off_t(42)));
-    if (p.first != (typeof(p.first))(-1)) {
+    if (p.first != (decltype(p.first))(-1)) {
       EXPECT_EQ(in_.substr(0, p.first), out.substr(0, p.first));
     }
   }
@@ -230,7 +231,7 @@ TEST_F(FileUtilTest, readv) {
 
     auto iov = buf.iov();
     EXPECT_EQ(p.first, wrapvFull(p.second, 0, iov.data(), iov.size()));
-    if (p.first != (typeof(p.first))(-1)) {
+    if (p.first != (decltype(p.first))(-1)) {
       EXPECT_EQ(in_.substr(0, p.first), buf.join().substr(0, p.first));
     }
   }
@@ -258,23 +259,16 @@ TEST_F(FileUtilTest, preadv) {
     auto iov = buf.iov();
     EXPECT_EQ(p.first,
               wrapvFull(p.second, 0, iov.data(), iov.size(), off_t(42)));
-    if (p.first != (typeof(p.first))(-1)) {
+    if (p.first != (decltype(p.first))(-1)) {
       EXPECT_EQ(in_.substr(0, p.first), buf.join().substr(0, p.first));
     }
   }
 }
 
 TEST(String, readFile) {
-  srand(time(nullptr));
-  const string tmpPrefix = to<string>("/tmp/folly-file-util-test-",
-                                      getpid(), "-", rand(), "-");
-  const string afile = tmpPrefix + "myfile";
-  const string emptyFile = tmpPrefix + "myfile2";
-
-  SCOPE_EXIT {
-    unlink(afile.c_str());
-    unlink(emptyFile.c_str());
-  };
+  const TemporaryFile afileTemp, emptyFileTemp;
+  auto afile = afileTemp.path().string();
+  auto emptyFile = emptyFileTemp.path().string();
 
   EXPECT_TRUE(writeFile(string(), emptyFile.c_str()));
   EXPECT_TRUE(writeFile(StringPiece("bar"), afile.c_str()));
@@ -303,4 +297,45 @@ TEST(String, readFile) {
   }
 }
 
+class ReadFileFd : public ::testing::Test {
+ protected:
+  void SetUp() override {
+    ASSERT_TRUE(writeFile(StringPiece("bar"), aFile.path().string().c_str()));
+  }
+
+  TemporaryFile aFile;
+};
+
+TEST_F(ReadFileFd, ReadZeroBytes) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents, 0));
+  EXPECT_EQ("", contents);
+}
+
+TEST_F(ReadFileFd, ReadPartial) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents, 2));
+  EXPECT_EQ("ba", contents);
+}
+
+TEST_F(ReadFileFd, ReadFull) {
+  std::string contents;
+  EXPECT_TRUE(readFile(aFile.fd(), contents));
+  EXPECT_EQ("bar", contents);
+}
+
+TEST_F(ReadFileFd, WriteOnlyFd) {
+  File f(aFile.path().string(), O_WRONLY);
+  std::string contents;
+  EXPECT_FALSE(readFile(f.fd(), contents));
+  PLOG(INFO);
+}
+
+TEST_F(ReadFileFd, InvalidFd) {
+  File f(aFile.path().string());
+  f.close();
+  std::string contents;
+  EXPECT_FALSE(readFile(f.fd(), contents));
+  PLOG(INFO);
+}
 }}  // namespaces