Files
react-native/ReactCommon/cxxreact/tests/jsbigstring.cpp
T
Daniel Andersson 2387d7d255 Make it possible to run JSBigString tests
Summary: Add a target for JSBigString tests that can be run with a normal `buck test` invocation. Also fix an issue in the test when `getenv` returns null by defaulting to `/tmp`.

Reviewed By: ridiculousfish

Differential Revision: D14716270

fbshipit-source-id: f2eb6d3aab93c32a4b41f5786aedd04a70468d75
2019-04-02 16:42:56 -07:00

63 lines
1.5 KiB
C++

// Copyright (c) Facebook, Inc. and its affiliates.
// This source code is licensed under the MIT license found in the
// LICENSE file in the root directory of this source tree.
#include <sys/mman.h>
#include <fcntl.h>
#include <folly/File.h>
#include <gtest/gtest.h>
#include <cxxreact/JSBigString.h>
using namespace facebook;
using namespace facebook::react;
namespace {
int tempFileFromString(std::string contents)
{
const char *tmpDir = getenv("TMPDIR");
if (tmpDir == nullptr)
tmpDir = "/tmp";
std::string tmp {tmpDir};
tmp += "/temp.XXXXXX";
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]);
}
}