FBVectorTest.cpp 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260
  1. /*
  2. * Copyright 2011-present Facebook, Inc.
  3. *
  4. * Licensed under the Apache License, Version 2.0 (the "License");
  5. * you may not use this file except in compliance with the License.
  6. * You may obtain a copy of the License at
  7. *
  8. * http://www.apache.org/licenses/LICENSE-2.0
  9. *
  10. * Unless required by applicable law or agreed to in writing, software
  11. * distributed under the License is distributed on an "AS IS" BASIS,
  12. * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  13. * See the License for the specific language governing permissions and
  14. * limitations under the License.
  15. */
  16. //
  17. // Author: andrei.alexandrescu@fb.com
  18. #include <folly/FBVector.h>
  19. #include <list>
  20. #include <map>
  21. #include <memory>
  22. #include <boost/random.hpp>
  23. #include <folly/FBString.h>
  24. #include <folly/Random.h>
  25. #include <folly/Traits.h>
  26. #include <folly/container/Foreach.h>
  27. #include <folly/portability/GTest.h>
  28. #include <folly/test/FBVectorTestUtil.h>
  29. using namespace std;
  30. using namespace folly;
  31. using namespace folly::test::detail;
  32. using IntFBVector = fbvector<int>;
  33. using FBStringFBVector = fbvector<fbstring>;
  34. #define VECTOR IntFBVector
  35. #include <folly/test/FBVectorTests.cpp.h> // nolint
  36. #undef VECTOR
  37. #define VECTOR FBStringFBVector
  38. #include <folly/test/FBVectorTests.cpp.h> // nolint
  39. #undef VECTOR
  40. TEST(fbvector, clause_23_3_6_1_3_ambiguity) {
  41. fbvector<int> v(10, 20);
  42. EXPECT_EQ(v.size(), 10);
  43. FOR_EACH (i, v) { EXPECT_EQ(*i, 20); }
  44. }
  45. TEST(fbvector, clause_23_3_6_1_11_ambiguity) {
  46. fbvector<int> v;
  47. v.assign(10, 20);
  48. EXPECT_EQ(v.size(), 10);
  49. FOR_EACH (i, v) { EXPECT_EQ(*i, 20); }
  50. }
  51. TEST(fbvector, clause_23_3_6_2_6) {
  52. fbvector<int> v;
  53. auto const n = random(0U, 10000U);
  54. v.reserve(n);
  55. auto const n1 = random(0U, 10000U);
  56. auto const obj = randomObject<int>();
  57. v.assign(n1, obj);
  58. v.shrink_to_fit();
  59. // Nothing to verify except that the call made it through
  60. }
  61. TEST(fbvector, clause_23_3_6_4_ambiguity) {
  62. fbvector<int> v;
  63. fbvector<int>::const_iterator it = v.end();
  64. v.insert(it, 10, 20);
  65. EXPECT_EQ(v.size(), 10);
  66. for (auto i : v) {
  67. EXPECT_EQ(i, 20);
  68. }
  69. }
  70. TEST(fbvector, composition) {
  71. fbvector<fbvector<double>> matrix(100, fbvector<double>(100));
  72. }
  73. TEST(fbvector, works_with_std_string) {
  74. fbvector<std::string> v(10, "hello");
  75. EXPECT_EQ(v.size(), 10);
  76. v.push_back("world");
  77. }
  78. namespace {
  79. class UserDefinedType {
  80. int whatevs_;
  81. };
  82. } // namespace
  83. FOLLY_ASSUME_FBVECTOR_COMPATIBLE(UserDefinedType)
  84. TEST(fbvector, works_with_user_defined_type) {
  85. fbvector<UserDefinedType> v(10);
  86. EXPECT_EQ(v.size(), 10);
  87. v.push_back(UserDefinedType());
  88. }
  89. TEST(fbvector, move_construction) {
  90. fbvector<int> v1(100, 100);
  91. fbvector<int> v2;
  92. EXPECT_EQ(v1.size(), 100);
  93. EXPECT_EQ(v1.front(), 100);
  94. EXPECT_EQ(v2.size(), 0);
  95. v2 = std::move(v1);
  96. EXPECT_EQ(v1.size(), 0);
  97. EXPECT_EQ(v2.size(), 100);
  98. EXPECT_EQ(v2.front(), 100);
  99. v1.assign(100, 100);
  100. auto other = std::move(v1);
  101. EXPECT_EQ(v1.size(), 0);
  102. EXPECT_EQ(other.size(), 100);
  103. EXPECT_EQ(other.front(), 100);
  104. }
  105. TEST(fbvector, emplace) {
  106. fbvector<std::string> s(12, "asd");
  107. EXPECT_EQ(s.size(), 12);
  108. EXPECT_EQ(s.front(), "asd");
  109. s.emplace_back("funk");
  110. EXPECT_EQ(s.back(), "funk");
  111. }
  112. TEST(fbvector, initializer_lists) {
  113. fbvector<int> vec = {1, 2, 3};
  114. EXPECT_EQ(vec.size(), 3);
  115. EXPECT_EQ(vec[0], 1);
  116. EXPECT_EQ(vec[1], 2);
  117. EXPECT_EQ(vec[2], 3);
  118. vec = {0, 0, 12, 16};
  119. EXPECT_EQ(vec.size(), 4);
  120. EXPECT_EQ(vec[0], 0);
  121. EXPECT_EQ(vec[1], 0);
  122. EXPECT_EQ(vec[2], 12);
  123. EXPECT_EQ(vec[3], 16);
  124. vec.insert(vec.begin() + 1, {23, 23});
  125. EXPECT_EQ(vec.size(), 6);
  126. EXPECT_EQ(vec[0], 0);
  127. EXPECT_EQ(vec[1], 23);
  128. EXPECT_EQ(vec[2], 23);
  129. EXPECT_EQ(vec[3], 0);
  130. EXPECT_EQ(vec[4], 12);
  131. EXPECT_EQ(vec[5], 16);
  132. }
  133. TEST(fbvector, unique_ptr) {
  134. fbvector<std::unique_ptr<int>> v(12);
  135. std::unique_ptr<int> p(new int(12));
  136. v.push_back(std::move(p));
  137. EXPECT_EQ(*v.back(), 12);
  138. v[0] = std::move(p);
  139. EXPECT_FALSE(v[0].get());
  140. v[0] = std::make_unique<int>(32);
  141. std::unique_ptr<int> somePtr;
  142. v.insert(v.begin(), std::move(somePtr));
  143. EXPECT_EQ(*v[1], 32);
  144. }
  145. TEST(FBVector, task858056) {
  146. fbvector<fbstring> cycle;
  147. cycle.push_back("foo");
  148. cycle.push_back("bar");
  149. cycle.push_back("baz");
  150. fbstring message("Cycle detected: ");
  151. FOR_EACH_R (node_name, cycle) {
  152. message += "[";
  153. message += *node_name;
  154. message += "] ";
  155. }
  156. EXPECT_EQ("Cycle detected: [baz] [bar] [foo] ", message);
  157. }
  158. TEST(FBVector, move_iterator) {
  159. fbvector<int> base = {0, 1, 2};
  160. auto cp1 = base;
  161. fbvector<int> fbvi1(
  162. std::make_move_iterator(cp1.begin()), std::make_move_iterator(cp1.end()));
  163. EXPECT_EQ(fbvi1, base);
  164. auto cp2 = base;
  165. fbvector<int> fbvi2;
  166. fbvi2.assign(
  167. std::make_move_iterator(cp2.begin()), std::make_move_iterator(cp2.end()));
  168. EXPECT_EQ(fbvi2, base);
  169. auto cp3 = base;
  170. fbvector<int> fbvi3;
  171. fbvi3.insert(
  172. fbvi3.end(),
  173. std::make_move_iterator(cp3.begin()),
  174. std::make_move_iterator(cp3.end()));
  175. EXPECT_EQ(fbvi3, base);
  176. }
  177. TEST(FBVector, reserve_consistency) {
  178. struct S {
  179. int64_t a, b, c, d;
  180. };
  181. fbvector<S> fb1;
  182. for (size_t i = 0; i < 1000; ++i) {
  183. fb1.reserve(1);
  184. EXPECT_EQ(fb1.size(), 0);
  185. fb1.shrink_to_fit();
  186. }
  187. }
  188. TEST(FBVector, vector_of_maps) {
  189. fbvector<std::map<std::string, std::string>> v;
  190. v.push_back(std::map<std::string, std::string>());
  191. v.push_back(std::map<std::string, std::string>());
  192. EXPECT_EQ(2, v.size());
  193. v[1]["hello"] = "world";
  194. EXPECT_EQ(0, v[0].size());
  195. EXPECT_EQ(1, v[1].size());
  196. v[0]["foo"] = "bar";
  197. EXPECT_EQ(1, v[0].size());
  198. EXPECT_EQ(1, v[1].size());
  199. }
  200. TEST(FBVector, shrink_to_fit_after_clear) {
  201. fbvector<int> fb1;
  202. fb1.push_back(42);
  203. fb1.push_back(1337);
  204. fb1.clear();
  205. fb1.shrink_to_fit();
  206. EXPECT_EQ(fb1.size(), 0);
  207. EXPECT_EQ(fb1.capacity(), 0);
  208. }
  209. TEST(FBVector, zero_len) {
  210. fbvector<int> fb1(0);
  211. fbvector<int> fb2(0, 10);
  212. fbvector<int> fb3(std::move(fb1));
  213. fbvector<int> fb4;
  214. fb4 = std::move(fb2);
  215. fbvector<int> fb5 = fb3;
  216. fbvector<int> fb6;
  217. fb6 = fb4;
  218. std::initializer_list<int> il = {};
  219. fb6 = il;
  220. fbvector<int> fb7(fb6.begin(), fb6.end());
  221. }