Repository: llvm/llvm-project
File: libcxx/test/std/containers/sequences/vector/vector.modifiers/append_range.pass.cpp (C++)

Code before:

  for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
    test_sequence_append_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
      LIBCPP_ASSERT(c.__invariants());
      LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
    });
  });
  test_sequence_append_range_move_only<std::vector>();
  test_sequence_append_range_emplace_constructible_and_move_insertable<std::vector>();

  {   // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
    { // Ensure reallocation happens.
      int in[]           = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
      std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
      v.shrink_to_fit();
      assert(v.capacity() < v.size() + std::ranges::size(in));

      v.append_range(in);
      assert(std::ranges::equal(v, std::array{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));
    }

    { // Ensure no reallocation happens.
      int in[]           = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
      std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
      v.reserve(v.size() + std::ranges::size(in));
      assert(v.capacity() >= v.size() + std::ranges::size(in));

      auto old_cap = v.capacity();
      v.append_range(in);
      assert(v.capacity() == old_cap);
      assert(std::ranges::equal(v, std::array{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));

      // vector is allowed to overallocate. Make sure it doesn't reallocate until the new capacity is reached.
      while (v.size() < old_cap) {
        int in2[] = {0};
        v.append_range(in2);
        assert(v.capacity() == old_cap);
      }
    }
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  test_append_range_exception_safety_throwing_copy<std::vector>();
  test_append_range_exception_safety_throwing_allocator<std::vector, int>();


Review comment (suggestion):
This assertion isn't really useful, and IMO it makes it less clear what we're actually trying to test. I'd remove it.

Code after:

  for_all_iterators_and_allocators<int, const int*>([]<class Iter, class Sent, class Alloc>() {
    test_sequence_append_range<std::vector<int, Alloc>, Iter, Sent>([]([[maybe_unused]] auto&& c) {
      LIBCPP_ASSERT(c.__invariants());
      LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
    });
  });
  test_sequence_append_range_move_only<std::vector>();
  test_sequence_append_range_emplace_constructible_and_move_insertable<std::vector>();

  {   // Vector may or may not need to reallocate because of the insertion -- make sure to test both cases.
    { // Ensure reallocation happens.
      int in[]           = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
      std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
      v.shrink_to_fit();
      assert(v.capacity() < v.size() + std::ranges::size(in));

      v.append_range(in);
      assert(std::ranges::equal(v, std::array{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));
    }

    { // Ensure no reallocation happens, and that we don't invalidate iterators-
      int in[]           = {-1, -2, -3, -4, -5, -6, -7, -8, -9, -10};
      std::vector<int> v = {1, 2, 3, 4, 5, 6, 7, 8};
      v.reserve(v.size() + std::ranges::size(in));

      auto ptr     = v.data();
      auto old_cap = v.capacity();

      v.append_range(in);
      assert(v.capacity() == old_cap);
      assert(std::ranges::equal(v, std::array{1, 2, 3, 4, 5, 6, 7, 8, -1, -2, -3, -4, -5, -6, -7, -8, -9, -10}));

      // vector is allowed to overallocate. Make sure it doesn't reallocate until the new capacity is reached.
      while (v.size() < old_cap) {
        int in2[] = {0};
        v.append_range(in2);
        assert(v.capacity() == old_cap);
      }
      assert(v.data() == ptr);
    }
  }

  return true;
}

int main(int, char**) {
  test();
  static_assert(test());

  test_append_range_exception_safety_throwing_copy<std::vector>();