aboutsummaryrefslogtreecommitdiff
path: root/libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp')
-rw-r--r--libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp61
1 files changed, 60 insertions, 1 deletions
diff --git a/libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp b/libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp
index 3f9ed7ee8951..be325b65ec29 100644
--- a/libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp
+++ b/libcxx/test/std/numerics/numeric.ops/inner.product/inner_product_comp.pass.cpp
@@ -21,11 +21,65 @@
#include <numeric>
#include <functional>
+#include <string>
#include <cassert>
#include "test_macros.h"
#include "test_iterators.h"
+#if TEST_STD_VER > 17
+struct do_nothing_op
+{
+ template<class T>
+ T operator()(T a, T)
+ { return a; }
+};
+
+struct rvalue_addable
+{
+ bool correctOperatorUsed = false;
+
+ rvalue_addable operator*(rvalue_addable const&) { return *this; }
+
+ // make sure the predicate is passed an rvalue and an lvalue (so check that the first argument was moved)
+ rvalue_addable operator()(rvalue_addable&& r, rvalue_addable const&) {
+ r.correctOperatorUsed = true;
+ return std::move(r);
+ }
+};
+
+rvalue_addable operator+(rvalue_addable& lhs, rvalue_addable const&)
+{
+ lhs.correctOperatorUsed = false;
+ return lhs;
+}
+
+rvalue_addable operator+(rvalue_addable&& lhs, rvalue_addable const&)
+{
+ lhs.correctOperatorUsed = true;
+ return std::move(lhs);
+}
+
+void
+test_use_move()
+{
+ rvalue_addable arr[100];
+ auto res1 = std::inner_product(arr, arr + 100, arr, rvalue_addable());
+ auto res2 = std::inner_product(arr, arr + 100, arr, rvalue_addable(), /*predicate=*/rvalue_addable(), do_nothing_op());
+
+ assert(res1.correctOperatorUsed);
+ assert(res2.correctOperatorUsed);
+}
+#endif // TEST_STD_VER > 17
+
+void
+test_string()
+{
+ std::string sa[] = {"a", "b", "c"};
+ assert(std::accumulate(sa, sa + 3, std::string()) == "abc");
+ assert(std::accumulate(sa, sa + 3, std::string(), std::plus<std::string>()) == "abc");
+}
+
template <class Iter1, class Iter2, class T>
void
test(Iter1 first1, Iter1 last1, Iter2 first2, T init, T x)
@@ -83,5 +137,10 @@ int main(int, char**)
test<const int*, random_access_iterator<const int*> >();
test<const int*, const int*>();
- return 0;
+#if TEST_STD_VER > 17
+ test_use_move();
+#endif // TEST_STD_VER > 17
+ test_string();
+
+ return 0;
}