sapling/eden/fs/utils/test/MemoryTest.cpp
Adam Simpkins aa5e6c7295 update license headers in C++ files
Summary:
Update the copyright & license headers in C++ files to reflect the
relicensing to GPLv2

Reviewed By: wez

Differential Revision: D15487078

fbshipit-source-id: 19f24c933a64ecad0d3a692d0f8d2a38b4194b1d
2019-06-19 17:02:45 -07:00

48 lines
1.5 KiB
C++

/*
* Copyright (c) Facebook, Inc. and its affiliates.
*
* This software may be used and distributed according to the terms of the
* GNU General Public License version 2.
*/
#include "eden/fs/utils/Memory.h"
#include <folly/Exception.h>
#include <gtest/gtest.h>
using std::string;
using namespace facebook::eden;
TEST(Memory, StdStringMemoryUsage) {
std::string test;
for (int stringLength = 0; stringLength < 100; stringLength++) {
SCOPED_TRACE(folly::to<string>("string length = ", stringLength));
std::byte* data = reinterpret_cast<std::byte*>(test.data());
std::byte* stringloc = reinterpret_cast<std::byte*>(&test);
bool isSmall =
data >= stringloc && data < (stringloc + sizeof(std::string));
size_t allocated = isSmall ? 0 : test.capacity();
ASSERT_EQ(
folly::goodMallocSize(allocated), estimateIndirectMemoryUsage(test));
test += "p";
}
}
#if !FOLLY_LIBRARY_SANITIZE_ADDRESS
/*
* Test case disabled when Address Sanitation is on since SSO optimization is
* disabled when Address Sanitation is enabled
*/
TEST(Memory, FBStringMemoryUsage) {
folly::fbstring test("");
for (int i = 0; i < 100; i++) {
char* data = test.data();
char* stringloc = (char*)(&test);
bool isSmall =
data >= stringloc && data < (stringloc + sizeof(folly::fbstring));
size_t allocated = isSmall ? 0 : test.capacity();
ASSERT_EQ(
folly::goodMallocSize(allocated), estimateIndirectMemoryUsage(test));
test += "p";
}
}
#endif