+TEST_F(FileSystemTest, FileMapping) {
+ // Create a temp file.
+ int FileDescriptor;
+ SmallString<64> TempPath;
+ ASSERT_NO_ERROR(
+ fs::createTemporaryFile("prefix", "temp", FileDescriptor, TempPath));
+ // Map in temp file and add some content
+ error_code EC;
+ StringRef Val("hello there");
+ {
+ fs::mapped_file_region mfr(FileDescriptor,
+ true,
+ fs::mapped_file_region::readwrite,
+ 4096,
+ 0,
+ EC);
+ ASSERT_NO_ERROR(EC);
+ std::copy(Val.begin(), Val.end(), mfr.data());
+ // Explicitly add a 0.
+ mfr.data()[Val.size()] = 0;
+ // Unmap temp file
+ }
+
+ // Map it back in read-only
+ fs::mapped_file_region mfr(Twine(TempPath),
+ fs::mapped_file_region::readonly,
+ 0,
+ 0,
+ EC);
+ ASSERT_NO_ERROR(EC);
+
+ // Verify content
+ EXPECT_EQ(StringRef(mfr.const_data()), Val);
+
+ // Unmap temp file
+
+#if LLVM_HAS_RVALUE_REFERENCES
+ fs::mapped_file_region m(Twine(TempPath),
+ fs::mapped_file_region::readonly,
+ 0,
+ 0,
+ EC);
+ ASSERT_NO_ERROR(EC);
+ const char *Data = m.const_data();
+ fs::mapped_file_region mfrrv(llvm_move(m));
+ EXPECT_EQ(mfrrv.const_data(), Data);
+#endif
+}