mirror of
https://github.com/zhigang1992/react-native.git
synced 2026-05-10 01:15:00 +08:00
Introducing JSBigFileString
Reviewed By: michalgr Differential Revision: D4189896 fbshipit-source-id: 0f3cebcd7e76d5c763c29ebc1119ae24b643e5ad
This commit is contained in:
committed by
Facebook Github Bot
parent
79fa6d41a1
commit
2de1c3507a
61
ReactCommon/cxxreact/tests/jsbigstring.cpp
Normal file
61
ReactCommon/cxxreact/tests/jsbigstring.cpp
Normal file
@@ -0,0 +1,61 @@
|
||||
|
||||
// Copyright 2004-present Facebook. All Rights Reserved.
|
||||
#include <sys/mman.h>
|
||||
#include <fcntl.h>
|
||||
|
||||
#include <folly/File.h>
|
||||
#include <gtest/gtest.h>
|
||||
#include <cxxreact/Executor.h>
|
||||
#include <cxxreact/MessageQueueThread.h>
|
||||
#include <cxxreact/MethodCall.h>
|
||||
|
||||
using namespace facebook;
|
||||
using namespace facebook::react;
|
||||
|
||||
namespace {
|
||||
int tempFileFromString(std::string contents)
|
||||
{
|
||||
std::string tmp {getenv("TMPDIR")};
|
||||
tmp += "/temp.XXXXX";
|
||||
|
||||
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_EQ(fd, bigStr.fd());
|
||||
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(fd, bigStr.fd());
|
||||
ASSERT_EQ(needle.length(), bigStr.size());
|
||||
for (unsigned int i = 0; i < needle.length(); ++i) {
|
||||
ASSERT_EQ(needle[i], bigStr.c_str()[i]);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user