Commit 9f9e6d96 authored by Lucian Grijincu's avatar Lucian Grijincu Committed by facebook-github-bot-4

folly: small_vector: emplace_back for iterator constructor (compat with std::vector)

Summary: This works:
  std::vector<T*> v;
  std::vector<std::unique_ptr<T>> uv(v.begin(), v.end())

Make it work for small_vector as well.

Reviewed By: @ot, @Gownta

Differential Revision: D2426919
parent 3e6ccd5c
......@@ -883,7 +883,7 @@ private:
// With iterators that only allow a single pass, we can't really
// do anything sane here.
while (first != last) {
push_back(*first++);
emplace_back(*first++);
}
return;
}
......
......@@ -16,13 +16,16 @@
#include <folly/small_vector.h>
#include <gtest/gtest.h>
#include <string>
#include <memory>
#include <iostream>
#include <iterator>
#include <limits>
#include <memory>
#include <sstream>
#include <string>
#include <vector>
#include <boost/algorithm/string.hpp>
#include <gtest/gtest.h>
#include <folly/Conv.h>
......@@ -802,3 +805,32 @@ TEST(small_vector, RVPushValueInsideVector) {
ASSERT_EQ(1, v.back().value);
}
}
TEST(small_vector, EmplaceIterCtor) {
std::vector<int*> v{new int(1), new int(2)};
std::vector<std::unique_ptr<int>> uv(v.begin(), v.end());
std::vector<int*> w{new int(1), new int(2)};
small_vector<std::unique_ptr<int>> uw(v.begin(), v.end());
}
TEST(small_vector, InputIterator) {
std::vector<int> expected{125, 320, 512, 750, 333};
std::string values = "125 320 512 750 333";
std::istringstream is1(values);
std::istringstream is2(values);
std::vector<int> stdV{std::istream_iterator<int>(is1),
std::istream_iterator<int>()};
ASSERT_EQ(stdV.size(), expected.size());
for (size_t i = 0; i < expected.size(); i++) {
ASSERT_EQ(stdV[i], expected[i]);
}
small_vector<int> smallV{std::istream_iterator<int>(is2),
std::istream_iterator<int>()};
ASSERT_EQ(smallV.size(), expected.size());
for (size_t i = 0; i < expected.size(); i++) {
ASSERT_EQ(smallV[i], expected[i]);
}
}
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment