2018-09-11 22:27:47 +00:00
|
|
|
// Copyright (c) Facebook, Inc. and its affiliates.
|
2018-05-31 22:31:03 +00:00
|
|
|
|
|
|
|
// This source code is licensed under the MIT license found in the
|
|
|
|
// LICENSE file in the root directory of this source tree.
|
2016-11-16 23:08:42 +00:00
|
|
|
#include <sys/mman.h>
|
|
|
|
#include <fcntl.h>
|
|
|
|
|
|
|
|
#include <folly/File.h>
|
|
|
|
#include <gtest/gtest.h>
|
2017-06-23 23:49:55 +00:00
|
|
|
#include <cxxreact/JSBigString.h>
|
2016-11-16 23:08:42 +00:00
|
|
|
|
|
|
|
using namespace facebook;
|
|
|
|
using namespace facebook::react;
|
|
|
|
|
|
|
|
namespace {
|
|
|
|
int tempFileFromString(std::string contents)
|
|
|
|
{
|
|
|
|
std::string tmp {getenv("TMPDIR")};
|
2017-02-28 22:14:51 +00:00
|
|
|
tmp += "/temp.XXXXXX";
|
2016-11-16 23:08:42 +00:00
|
|
|
|
|
|
|
std::vector<char> tmpBuf {tmp.begin(), tmp.end()};
|
|
|
|
tmpBuf.push_back('\0');
|
|
|
|
|
|
|
|
const int fd = mkstemp(tmpBuf.data());
|
|
|
|
write(fd, contents.c_str(), contents.size() + 1);
|
|
|
|
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
|
|
|
TEST(JSBigFileString, MapWholeFileTest) {
|
|
|
|
std::string data {"Hello, world"};
|
|
|
|
const auto size = data.length() + 1;
|
|
|
|
|
|
|
|
// Initialise Big String
|
|
|
|
int fd = tempFileFromString("Hello, world");
|
|
|
|
JSBigFileString bigStr {fd, size};
|
|
|
|
|
|
|
|
// Test
|
|
|
|
ASSERT_STREQ(data.c_str(), bigStr.c_str());
|
|
|
|
}
|
|
|
|
|
|
|
|
TEST(JSBigFileString, MapPartTest) {
|
|
|
|
std::string data {"Hello, world"};
|
|
|
|
|
|
|
|
// Sub-string to actually map
|
|
|
|
std::string needle {"or"};
|
|
|
|
off_t offset = data.find(needle);
|
|
|
|
|
|
|
|
// Initialise Big String
|
|
|
|
int fd = tempFileFromString(data);
|
|
|
|
JSBigFileString bigStr {fd, needle.size(), offset};
|
|
|
|
|
|
|
|
// Test
|
|
|
|
ASSERT_EQ(needle.length(), bigStr.size());
|
|
|
|
for (unsigned int i = 0; i < needle.length(); ++i) {
|
|
|
|
ASSERT_EQ(needle[i], bigStr.c_str()[i]);
|
|
|
|
}
|
|
|
|
}
|