[libc++][NFC] Slight refactoring of some std::vector tests
authorLouis Dionne <ldionne.2@gmail.com>
Fri, 6 May 2022 14:49:14 +0000 (10:49 -0400)
committerLouis Dionne <ldionne.2@gmail.com>
Fri, 6 May 2022 14:56:34 +0000 (10:56 -0400)
libcxx/test/std/containers/sequences/vector/vector.cons/assign_iter_iter.pass.cpp
libcxx/test/std/containers/sequences/vector/vector.cons/construct_size.pass.cpp

index bbd461a..6a572fa 100644 (file)
@@ -23,7 +23,7 @@
 #endif
 
 
-void test_emplaceable_concept() {
+void test() {
 #if TEST_STD_VER >= 11
   int arr1[] = {42};
   int arr2[] = {1, 101, 42};
@@ -64,26 +64,22 @@ void test_emplaceable_concept() {
     }
   }
 #endif
-}
 
-// Test with a number of elements in the source range
-// that is greater than capacity
-void test_assign_bigger() {
-  typedef forward_iterator<int*> It;
+  // Test with a number of elements in the source range that is greater than capacity
+  {
+    typedef forward_iterator<int*> It;
 
-  std::vector<int> dst(10);
+    std::vector<int> dst(10);
 
-  size_t n = dst.capacity() * 2;
-  std::vector<int> src(n);
+    size_t n = dst.capacity() * 2;
+    std::vector<int> src(n);
 
-  dst.assign(It(src.data()), It(src.data() + src.size()));
-  assert(dst == src);
+    dst.assign(It(src.data()), It(src.data() + src.size()));
+    assert(dst == src);
+  }
 }
 
-int main(int, char**)
-{
-    test_emplaceable_concept();
-    test_assign_bigger();
-
+int main(int, char**) {
+  test();
   return 0;
 }
index cf85ea0..a06031c 100644 (file)
@@ -9,6 +9,7 @@
 // <vector>
 
 // explicit vector(size_type n);
+// explicit vector(size_type n, const Allocator& alloc = Allocator());
 
 #include <vector>
 #include <cassert>
 #include "asan_testing.h"
 
 template <class C>
-void
-test2(typename C::size_type n, typename C::allocator_type const& a = typename C::allocator_type ())
+void test(typename C::size_type n,
+          typename C::allocator_type const& a = typename C::allocator_type())
 {
-#if TEST_STD_VER >= 14
-    C c(n, a);
-    LIBCPP_ASSERT(c.__invariants());
-    assert(c.size() == n);
-    assert(c.get_allocator() == a);
-    LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
-    for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
-        assert(*i == typename C::value_type());
-#else
-    ((void)n);
-    ((void)a);
-#endif
-}
-
-template <class C>
-void
-test1(typename C::size_type n)
-{
-    C c(n);
-    LIBCPP_ASSERT(c.__invariants());
-    assert(c.size() == n);
-    assert(c.get_allocator() == typename C::allocator_type());
-    LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
+    (void)a;
+    // Test without a custom allocator
+    {
+        C c(n);
+        LIBCPP_ASSERT(c.__invariants());
+        assert(c.size() == n);
+        assert(c.get_allocator() == typename C::allocator_type());
+        LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
 #if TEST_STD_VER >= 11
-    for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
-        assert(*i == typename C::value_type());
+        for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
+            assert(*i == typename C::value_type());
 #endif
-}
+    }
 
-template <class C>
-void
-test(typename C::size_type n)
-{
-    test1<C> ( n );
-    test2<C> ( n );
+    // Test with a custom allocator
+#if TEST_STD_VER >= 14
+    {
+        C c(n, a);
+        LIBCPP_ASSERT(c.__invariants());
+        assert(c.size() == n);
+        assert(c.get_allocator() == a);
+        LIBCPP_ASSERT(is_contiguous_container_asan_correct(c));
+        for (typename C::const_iterator i = c.cbegin(), e = c.cend(); i != e; ++i)
+            assert(*i == typename C::value_type());
+    }
+#endif
 }
 
-int main(int, char**)
-{
+void tests() {
     test<std::vector<int> >(0);
     test<std::vector<int> >(50);
     test<std::vector<DefaultOnly> >(0);
     test<std::vector<DefaultOnly> >(500);
     assert(DefaultOnly::count == 0);
 #if TEST_STD_VER >= 11
-    test<std::vector<int, min_allocator<int>> >(0);
-    test<std::vector<int, min_allocator<int>> >(50);
-    test<std::vector<DefaultOnly, min_allocator<DefaultOnly>> >(0);
-    test<std::vector<DefaultOnly, min_allocator<DefaultOnly>> >(500);
-    test2<std::vector<DefaultOnly, test_allocator<DefaultOnly>> >( 0, test_allocator<DefaultOnly>(23));
-    test2<std::vector<DefaultOnly, test_allocator<DefaultOnly>> >( 100, test_allocator<DefaultOnly>(23));
+    test<std::vector<int, min_allocator<int>>>(0);
+    test<std::vector<int, min_allocator<int>>>(50);
+    test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(0);
+    test<std::vector<DefaultOnly, min_allocator<DefaultOnly>>>(500);
+    test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(0, test_allocator<DefaultOnly>(23));
+    test<std::vector<DefaultOnly, test_allocator<DefaultOnly>>>(100, test_allocator<DefaultOnly>(23));
     assert(DefaultOnly::count == 0);
 #endif
+}
 
-  return 0;
+int main(int, char**) {
+    tests();
+    return 0;
 }