X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=folly%2Ftest%2FFBVectorTest.cpp;h=33a164a004efe0379dd05ef1c78f570c7556c8c4;hb=86b83461fc47b591b9e873e36ceabe4efa03e11f;hp=aee0831c099218b7773f121bc29c70f3aa09df74;hpb=27494a20393fa45072e7d526d358835f3abe312a;p=folly.git diff --git a/folly/test/FBVectorTest.cpp b/folly/test/FBVectorTest.cpp index aee0831c..33a164a0 100644 --- a/folly/test/FBVectorTest.cpp +++ b/folly/test/FBVectorTest.cpp @@ -1,5 +1,5 @@ /* - * Copyright 2012 Facebook, Inc. + * Copyright 2016 Facebook, Inc. * * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. @@ -17,27 +17,26 @@ // // Author: andrei.alexandrescu@fb.com -#include "folly/Traits.h" -#include "folly/Random.h" -#include "folly/FBString.h" -#include "folly/FBVector.h" -#include "folly/Benchmark.h" - -#include +#include +#include +#include +#include +#include #include #include +#include #include #include using namespace std; using namespace folly; +namespace { + auto static const seed = randomNumberSeed(); typedef boost::mt19937 RandomT; static RandomT rng(seed); -static const size_t maxString = 100; -static const bool avoidAliasing = true; template Integral2 random(Integral1 low, Integral2 up) { @@ -55,32 +54,17 @@ void randomString(String* toFill, unsigned int maxSize = 1000) { } template -void Num2String(String& str, Integral n) { +void Num2String(String& str, Integral /* n */) { str.resize(10, '\0'); -// ultoa((unsigned long)n, &str[0], 10); sprintf(&str[0], "%ul", 10); str.resize(strlen(str.c_str())); } -std::list RandomList(unsigned int maxSize) { - std::list lst(random(0u, maxSize)); - std::list::iterator i = lst.begin(); - for (; i != lst.end(); ++i) { - *i = random('a', 'z'); - } - return lst; -} - template T randomObject(); template<> int randomObject() { return random(0, 1024); } - -template<> folly::fbstring randomObject() { - folly::fbstring result; - randomString(&result); - return result; } //////////////////////////////////////////////////////////////////////////////// @@ -225,35 +209,62 @@ TEST(FBVector, task858056) { EXPECT_EQ("Cycle detected: [baz] [bar] [foo] ", message); } -#define CONCAT(A, B) CONCAT_HELPER(A, B) -#define CONCAT_HELPER(A, B) A##B -#define BENCHFUN(F) CONCAT(CONCAT(BM_, F), CONCAT(_, VECTOR)) -#define TESTFUN(F) TEST(fbvector, CONCAT(F, VECTOR)) - -typedef vector IntVector; -typedef fbvector IntFBVector; -typedef vector FBStringVector; -typedef fbvector FBStringFBVector; - -#define VECTOR IntVector -#include "folly/test/FBVectorTestBenchmarks.cpp.h" -#undef VECTOR -#define VECTOR IntFBVector -#include "folly/test/FBVectorTestBenchmarks.cpp.h" -#undef VECTOR -#define VECTOR FBStringVector -#include "folly/test/FBVectorTestBenchmarks.cpp.h" -#undef VECTOR -#define VECTOR FBStringFBVector -#include "folly/test/FBVectorTestBenchmarks.cpp.h" -#undef VECTOR - -int main(int argc, char** argv) { - testing::InitGoogleTest(&argc, argv); - google::ParseCommandLineFlags(&argc, &argv, true); - auto ret = RUN_ALL_TESTS(); - if (!ret && FLAGS_benchmark) { - folly::runBenchmarks(); +TEST(FBVector, move_iterator) { + fbvector base = { 0, 1, 2 }; + + auto cp1 = base; + fbvector fbvi1(std::make_move_iterator(cp1.begin()), + std::make_move_iterator(cp1.end())); + EXPECT_EQ(fbvi1, base); + + auto cp2 = base; + fbvector fbvi2; + fbvi2.assign(std::make_move_iterator(cp2.begin()), + std::make_move_iterator(cp2.end())); + EXPECT_EQ(fbvi2, base); + + auto cp3 = base; + fbvector fbvi3; + fbvi3.insert(fbvi3.end(), + std::make_move_iterator(cp3.begin()), + std::make_move_iterator(cp3.end())); + EXPECT_EQ(fbvi3, base); +} + +TEST(FBVector, reserve_consistency) { + struct S { int64_t a, b, c, d; }; + + fbvector fb1; + for (size_t i = 0; i < 1000; ++i) { + fb1.reserve(1); + EXPECT_EQ(fb1.size(), 0); + fb1.shrink_to_fit(); } - return ret; +} + +TEST(FBVector, vector_of_maps) { + fbvector> v; + + v.push_back(std::map()); + v.push_back(std::map()); + + EXPECT_EQ(2, v.size()); + + v[1]["hello"] = "world"; + EXPECT_EQ(0, v[0].size()); + EXPECT_EQ(1, v[1].size()); + + v[0]["foo"] = "bar"; + EXPECT_EQ(1, v[0].size()); + EXPECT_EQ(1, v[1].size()); +} + +TEST(FBVector, shrink_to_fit_after_clear) { + fbvector fb1; + fb1.push_back(42); + fb1.push_back(1337); + fb1.clear(); + fb1.shrink_to_fit(); + EXPECT_EQ(fb1.size(), 0); + EXPECT_EQ(fb1.capacity(), 0); }