//
// Author: andrei.alexandrescu@fb.com
-#include "folly/FBString.h"
+#include <folly/FBString.h>
#include <cstdlib>
#include <gflags/gflags.h>
-#include "folly/Foreach.h"
-#include "folly/Portability.h"
-#include "folly/Random.h"
-#include "folly/Conv.h"
+#include <folly/Foreach.h>
+#include <folly/Portability.h>
+#include <folly/Random.h>
+#include <folly/Conv.h>
using namespace std;
using namespace folly;
std::wstring wr;
folly::fbstring c;
folly::basic_fbstring<wchar_t> wc;
-#define TEST_CLAUSE(x) \
- do { \
- if (1) {} else EXPECT_TRUE(1) << "Testing clause " << #x; \
- randomString(&r); \
- c = r; \
- EXPECT_EQ(c, r); \
- wr = std::wstring(r.begin(), r.end()); \
- wc = folly::basic_fbstring<wchar_t>(wr.c_str()); \
- auto localSeed = seed + count; \
- rng = RandomT(localSeed); \
- clause11_##x(r); \
- rng = RandomT(localSeed); \
- clause11_##x(c); \
- EXPECT_EQ(r, c) \
- << "Lengths: " << r.size() << " vs. " << c.size() \
- << "\nReference: '" << r << "'" \
- << "\nActual: '" << c.data()[0] << "'"; \
- rng = RandomT(localSeed); \
- clause11_##x(wc); \
- int wret = wcslen(wc.c_str()); \
- char mb[wret+1]; \
- int ret = wcstombs(mb, wc.c_str(), sizeof(mb)); \
- if (ret == wret) mb[wret] = '\0'; \
- const char *mc = c.c_str(); \
- std::string one(mb); \
- std::string two(mc); \
- EXPECT_EQ(one, two); \
- } while (++count % 100 != 0)
-
int count = 0;
+
+ auto l = [&](const char * const clause,
+ void(*f_string)(std::string&),
+ void(*f_fbstring)(folly::fbstring&),
+ void(*f_wfbstring)(folly::basic_fbstring<wchar_t>&)) {
+ do {
+ if (1) {} else EXPECT_TRUE(1) << "Testing clause " << clause;
+ randomString(&r);
+ c = r;
+ EXPECT_EQ(c, r);
+ wr = std::wstring(r.begin(), r.end());
+ wc = folly::basic_fbstring<wchar_t>(wr.c_str());
+ auto localSeed = seed + count;
+ rng = RandomT(localSeed);
+ f_string(r);
+ rng = RandomT(localSeed);
+ f_fbstring(c);
+ EXPECT_EQ(r, c)
+ << "Lengths: " << r.size() << " vs. " << c.size()
+ << "\nReference: '" << r << "'"
+ << "\nActual: '" << c.data()[0] << "'";
+ rng = RandomT(localSeed);
+ f_wfbstring(wc);
+ int wret = wcslen(wc.c_str());
+ char mb[wret+1];
+ int ret = wcstombs(mb, wc.c_str(), sizeof(mb));
+ if (ret == wret) mb[wret] = '\0';
+ const char *mc = c.c_str();
+ std::string one(mb);
+ std::string two(mc);
+ EXPECT_EQ(one, two);
+ } while (++count % 100 != 0);
+ };
+
+#define TEST_CLAUSE(x) \
+ l(#x, \
+ clause11_##x<std::string>, \
+ clause11_##x<folly::fbstring>, \
+ clause11_##x<folly::basic_fbstring<wchar_t>>);
+
TEST_CLAUSE(21_4_2_a);
TEST_CLAUSE(21_4_2_b);
TEST_CLAUSE(21_4_2_c);
cp.c_str();
EXPECT_EQ(str.front(), 'f');
}
- { // D481173, --extra-cxxflags=-DFBSTRING_CONSERVATIVE
+ { // D481173
fbstring str(1337, 'f');
for (int i = 0; i < 2; ++i) {
fbstring cp = str;
ss.str("");
}
+TEST(FBString, rvalueIterators) {
+ // you cannot take &* of a move-iterator, so use that for testing
+ fbstring s = "base";
+ fbstring r = "hello";
+ r.replace(r.begin(), r.end(),
+ make_move_iterator(s.begin()), make_move_iterator(s.end()));
+ EXPECT_EQ("base", r);
+
+ // The following test is probably not required by the standard.
+ // i.e. this could be in the realm of undefined behavior.
+ fbstring b = "123abcXYZ";
+ auto ait = b.begin() + 3;
+ auto Xit = b.begin() + 6;
+ b.replace(ait, b.end(), b.begin(), Xit);
+ EXPECT_EQ("123123abc", b); // if things go wrong, you'd get "123123123"
+}
+
+TEST(FBString, moveTerminator) {
+ // The source of a move must remain in a valid state
+ fbstring s(100, 'x'); // too big to be in-situ
+ fbstring k;
+ k = std::move(s);
+
+ EXPECT_EQ(0, s.size());
+ EXPECT_EQ('\0', *s.c_str());
+}
+
int main(int argc, char** argv) {
testing::InitGoogleTest(&argc, argv);
google::ParseCommandLineFlags(&argc, &argv, true);