[libcxx-commits] [PATCH] D117395: [libc++] Adds a test for std::fill_n.

Mark de Wever via Phabricator via libcxx-commits libcxx-commits at lists.llvm.org
Sat Jan 15 04:54:43 PST 2022


Mordante created this revision.
Mordante requested review of this revision.
Herald added a project: libc++.
Herald added a subscriber: libcxx-commits.
Herald added a reviewer: libc++.

The function `std::fill` requires a ForwardIterator, but `std::fill_n`
only requires an OutputIterator. Adds a test to validate `std::fill_n`
works with an OutputIterator.

Noticed this while working on LWG3539
format_to must not copy models of output_iterator<const charT&>


Repository:
  rG LLVM Github Monorepo

https://reviews.llvm.org/D117395

Files:
  libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp


Index: libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp
===================================================================
--- libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp
+++ libcxx/test/std/algorithms/alg.modifying.operations/alg.fill/fill_n.pass.cpp
@@ -35,13 +35,24 @@
 
 typedef UserDefinedIntegral<unsigned> UDI;
 
+template <class Iter, class Ptr>
+typename std::enable_if<!std::is_same<Iter, Ptr>::value, bool>::type equal(Iter& it, Ptr ptr) {
+  return it.base() == ptr;
+}
+
+template <class Ptr>
+bool equal(Ptr it, Ptr ptr) {
+  return it == ptr;
+}
+
 template <class Iter>
 void
 test_char()
 {
     const unsigned n = 4;
     char ca[n] = {0};
-    assert(std::fill_n(Iter(ca), UDI(n), char(1)) == std::next(Iter(ca), n));
+    Iter it = std::fill_n(Iter(ca), UDI(n), char(1));
+    assert(equal(it, ca + n));
     assert(ca[0] == 1);
     assert(ca[1] == 1);
     assert(ca[2] == 1);
@@ -54,7 +65,8 @@
 {
     const unsigned n = 4;
     int ia[n] = {0};
-    assert(std::fill_n(Iter(ia), UDI(n), 1) == std::next(Iter(ia), n));
+    Iter it = std::fill_n(Iter(ia), UDI(n), 1);
+    assert(equal(it, ia + n));
     assert(ia[0] == 1);
     assert(ia[1] == 1);
     assert(ia[2] == 1);
@@ -66,7 +78,7 @@
 {
     const unsigned n = 4;
     int ia[n] = {0};
-    assert(std::fill_n(ia, UDI(n), static_cast<char>(1)) == std::next(ia, n));
+    assert(std::fill_n(ia, UDI(n), static_cast<char>(1)) == ia + 4);
     assert(ia[0] == 1);
     assert(ia[1] == 1);
     assert(ia[2] == 1);
@@ -85,7 +97,7 @@
 {
     const unsigned n = 4;
     int ia[n] = {0};
-    assert(std::fill_n(ia, UDI(n), source()) == std::next(ia, n));
+    assert(std::fill_n(ia, UDI(n), source()) == ia + 4);
     assert(ia[0] == 0);
     assert(ia[1] == 1);
     assert(ia[2] == 2);
@@ -103,7 +115,7 @@
 {
     const unsigned n = 4;
     test1 test1a[n] = {0};
-    assert(std::fill_n(test1a, UDI(n), static_cast<char>(10)) == std::next(test1a, n));
+    assert(std::fill_n(test1a, UDI(n), static_cast<char>(10)) == test1a + n);
     assert(test1a[0].c == 11);
     assert(test1a[1].c == 11);
     assert(test1a[2].c == 11);
@@ -150,11 +162,13 @@
 
 int main(int, char**)
 {
+    test_char<output_iterator<char*> >();
     test_char<forward_iterator<char*> >();
     test_char<bidirectional_iterator<char*> >();
     test_char<random_access_iterator<char*> >();
     test_char<char*>();
 
+    test_int<output_iterator<int*> >();
     test_int<forward_iterator<int*> >();
     test_int<bidirectional_iterator<int*> >();
     test_int<random_access_iterator<int*> >();


-------------- next part --------------
A non-text attachment was scrubbed...
Name: D117395.400277.patch
Type: text/x-patch
Size: 2611 bytes
Desc: not available
URL: <http://lists.llvm.org/pipermail/libcxx-commits/attachments/20220115/4c2f1ace/attachment-0001.bin>


More information about the libcxx-commits mailing list