diff mbox series

Add more testcases for class template argument deduction of maps

Message ID 20190102163113.GA1347@redhat.com
State New
Headers show
Series Add more testcases for class template argument deduction of maps | expand

Commit Message

Jonathan Wakely Jan. 2, 2019, 4:31 p.m. UTC
This adds additional tests for std::map and std::multimap CTAD. The
tests ensure that deduction works for braced-init-list of value_type
objects, and for pairs of input iterators (with both std::pair<Key, T>
and value_type as the iterator's value_type). This ensures deduction
from value_type still works, as well as the non-value_type cases in LWG
3025.

Similar tests for unordered maps do not work, apparently because the
constructor taking an initializer_list<value_type> is not usable for
deduction, and the deduction guide taking initializer_list<pair<Key, T>>
deduces key_type to be const. I am not addressing that.

	* testsuite/23_containers/map/cons/deduction.cc: Test deduction from
	initializer_list<value_type> and from input iterator ranges.
	* testsuite/23_containers/multimap/cons/deduction.cc: Likewise.

Tested x86_64-linux, committed to trunk.
commit d595c72ef9482a8597493f49a263258dbe634776
Author: Jonathan Wakely <jwakely@redhat.com>
Date:   Wed Jan 2 15:42:29 2019 +0000

    Add more testcases for class template argument deduction of maps
    
    This adds additional tests for std::map and std::multimap CTAD. The
    tests ensure that deduction works for braced-init-list of value_type
    objects, and for pairs of input iterators (with both std::pair<Key, T>
    and value_type as the iterator's value_type). This ensures deduction
    from value_type still works, as well as the non-value_type cases in LWG
    3025.
    
    Similar tests for unordered maps do not work, apparently because the
    constructor taking an initializer_list<value_type> is not usable for
    deduction, and the deduction guide taking initializer_list<pair<Key, T>>
    deduces key_type to be const. I am not addressing that.
    
            * testsuite/23_containers/map/cons/deduction.cc: Test deduction from
            initializer_list<value_type> and from input iterator ranges.
            * testsuite/23_containers/multimap/cons/deduction.cc: Likewise.
diff mbox series

Patch

diff --git a/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc
index 3880cd5e79d..f4195257e9c 100644
--- a/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc
+++ b/libstdc++-v3/testsuite/23_containers/map/cons/deduction.cc
@@ -3,32 +3,58 @@ 
 
 #include <map>
 #include <testsuite_allocator.h>
+#include <testsuite_iterators.h>
 
 using __gnu_test::SimpleAllocator;
+using value_type = std::map<int, double>::value_type;
+
+static_assert(std::is_same_v<
+	      decltype(std::map{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}),
+	      std::map<int, double>>);
 
 static_assert(std::is_same_v<
 	      decltype(std::map{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}),
 	      std::map<int, double>>);
 
+static_assert(std::is_same_v<
+	      decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
+	      std::map<int, double>>);
+
 static_assert(std::is_same_v<
 	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
 	      std::map<int, double>>);
 
 static_assert(std::is_same_v<
-	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
-		    std::less<int>{}, {}}),
+	      decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				std::less<int>{}, {}}),
 	      std::map<int, double>>);
 
 static_assert(std::is_same_v<
 	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
-		    {}}),
+				std::less<int>{}, {}}),
+	      std::map<int, double>>);
+
+/* This is not deducible, {} could be deduced as _Compare or _Allocator.
+static_assert(std::is_same_v<
+	      decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}, {}}),
+	      std::map<int, double>>);
+*/
+
+static_assert(std::is_same_v<
+	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}, {}}),
 	      std::map<int, double>>);
 
 static_assert(std::is_same_v<
-	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
-		    {}, SimpleAllocator<std::pair<const int, double>>{}}),
+	      decltype(std::map{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				{}, SimpleAllocator<value_type>{}}),
 	      std::map<int, double, std::less<int>,
-	      SimpleAllocator<std::pair<const int, double>>>>);
+		       SimpleAllocator<value_type>>>);
+
+static_assert(std::is_same_v<
+	      decltype(std::map{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				{}, SimpleAllocator<value_type>{}}),
+	      std::map<int, double, std::less<int>,
+		       SimpleAllocator<value_type>>>);
 
 void f()
 {
@@ -39,13 +65,13 @@  void f()
 
   static_assert(std::is_same_v<
 		decltype(std::map{x.begin(), x.end(),
-		      std::less<int>{},
-		      std::allocator<std::pair<const int, double>>{}}),
+				  std::less<int>{},
+				  std::allocator<value_type>{}}),
 		std::map<int, double>>);
-  
+
   static_assert(std::is_same_v<
 		decltype(std::map{x.begin(), x.end(),
-		      std::less<int>{}, {}}),
+				  std::less<int>{}, {}}),
 		std::map<int, double>>);
 
   static_assert(std::is_same_v<
@@ -55,14 +81,95 @@  void f()
 
   static_assert(std::is_same_v<
 		decltype(std::map{x.begin(), x.end(),
-		      {},
-		      std::allocator<std::pair<const int, double>>{}}),
+				  {},
+				  std::allocator<value_type>{}}),
 		std::map<int, double>>);
 
   static_assert(std::is_same_v<
 		decltype(std::map{x.begin(), x.end(),
-		      {},
-		      SimpleAllocator<std::pair<const int, double>>{}}),
+				  {},
+				  SimpleAllocator<value_type>{}}),
 		std::map<int, double, std::less<int>,
-		SimpleAllocator<std::pair<const int, double>>>>);
+			 SimpleAllocator<value_type>>>);
+}
+
+using __gnu_test::test_container;
+using __gnu_test::input_iterator_wrapper;
+
+void g()
+{
+  value_type array[1];
+  test_container<value_type, input_iterator_wrapper> x(array);
+
+  static_assert(std::is_same_v<
+		decltype(std::map(x.begin(), x.end())),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  std::less<int>{},
+				  std::allocator<value_type>{}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  std::less<int>{}, {}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map(x.begin(), x.end(),
+				  {})),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  {},
+				  std::allocator<value_type>{}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  {},
+				  SimpleAllocator<value_type>{}}),
+		std::map<int, double, std::less<int>,
+			 SimpleAllocator<value_type>>>);
+}
+
+void h()
+{
+  std::pair<int, double> array[1];
+  test_container<std::pair<int, double>, input_iterator_wrapper> x(array);
+
+  static_assert(std::is_same_v<
+		decltype(std::map(x.begin(), x.end())),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  std::less<int>{},
+				  std::allocator<value_type>{}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  std::less<int>{}, {}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map(x.begin(), x.end(),
+				  {})),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  {},
+				  std::allocator<value_type>{}}),
+		std::map<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::map{x.begin(), x.end(),
+				  {},
+				  SimpleAllocator<value_type>{}}),
+		std::map<int, double, std::less<int>,
+			 SimpleAllocator<value_type>>>);
 }
diff --git a/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc b/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc
index ee48bfda26b..2f9373a5bef 100644
--- a/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc
+++ b/libstdc++-v3/testsuite/23_containers/multimap/cons/deduction.cc
@@ -3,32 +3,60 @@ 
 
 #include <map>
 #include <testsuite_allocator.h>
+#include <testsuite_iterators.h>
 
 using __gnu_test::SimpleAllocator;
+using value_type = std::multimap<int, double>::value_type;
+
+static_assert(std::is_same_v<
+	      decltype(std::multimap{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}),
+	      std::multimap<int, double>>);
 
 static_assert(std::is_same_v<
 	      decltype(std::multimap{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}),
 	      std::multimap<int, double>>);
 
+static_assert(std::is_same_v<
+	      decltype(std::multimap{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
+	      std::multimap<int, double>>);
+
 static_assert(std::is_same_v<
 	      decltype(std::multimap{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}}}),
 	      std::multimap<int, double>>);
 
+static_assert(std::is_same_v<
+	      decltype(std::multimap{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				std::less<int>{}, {}}),
+	      std::multimap<int, double>>);
+
 static_assert(std::is_same_v<
 	      decltype(std::multimap{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
 		    std::less<int>{}, {}}),
 	      std::multimap<int, double>>);
 
+/* This is not deducible, {} could be deduced as _Compare or _Allocator.
+static_assert(std::is_same_v<
+	      decltype(std::multimap{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				     {}}),
+	      std::multimap<int, double>>);
+*/
+
 static_assert(std::is_same_v<
 	      decltype(std::multimap{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
 		    {}}),
 	      std::multimap<int, double>>);
 
 static_assert(std::is_same_v<
-	      decltype(std::multimap{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
-		    {}, SimpleAllocator<std::pair<const int, double>>{}}),
+	      decltype(std::multimap{{value_type{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				     {}, SimpleAllocator<value_type>{}}),
 	      std::multimap<int, double, std::less<int>,
-	      SimpleAllocator<std::pair<const int, double>>>>);
+		       SimpleAllocator<value_type>>>);
+
+static_assert(std::is_same_v<
+	      decltype(std::multimap{{std::pair{1, 2.0}, {2, 3.0}, {3, 4.0}},
+				     {}, SimpleAllocator<value_type>{}}),
+	      std::multimap<int, double, std::less<int>,
+			    SimpleAllocator<value_type>>>);
 
 void f()
 {
@@ -39,30 +67,111 @@  void f()
 
   static_assert(std::is_same_v<
 		decltype(std::multimap{x.begin(), x.end(),
-		      std::less<int>{},
-		      std::allocator<std::pair<const int, double>>{}}),
+				       std::less<int>{},
+				       std::allocator<value_type>{}}),
 		std::multimap<int, double>>);
   
   static_assert(std::is_same_v<
 		decltype(std::multimap{x.begin(), x.end(),
-		      std::less<int>{}, {}}),
+				       std::less<int>{}, {}}),
 		std::multimap<int, double>>);
 
   static_assert(std::is_same_v<
 		decltype(std::multimap(x.begin(), x.end(),
-				  {})),
+				       {})),
 		std::multimap<int, double>>);
 
   static_assert(std::is_same_v<
 		decltype(std::multimap{x.begin(), x.end(),
 		      {},
-		      std::allocator<std::pair<const int, double>>{}}),
+		      std::allocator<value_type>{}}),
 		std::multimap<int, double>>);
 
   static_assert(std::is_same_v<
 		decltype(std::multimap{x.begin(), x.end(),
-		      {},
-		      SimpleAllocator<std::pair<const int, double>>{}}),
+				       {},
+				       SimpleAllocator<value_type>{}}),
 		std::multimap<int, double, std::less<int>,
-		SimpleAllocator<std::pair<const int, double>>>>);
+			      SimpleAllocator<value_type>>>);
+}
+
+using __gnu_test::test_container;
+using __gnu_test::input_iterator_wrapper;
+
+void g()
+{
+  value_type array[1];
+  test_container<value_type, input_iterator_wrapper> x(array);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap(x.begin(), x.end())),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       std::less<int>{},
+				       std::allocator<value_type>{}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       std::less<int>{}, {}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap(x.begin(), x.end(),
+				       {})),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       {},
+				       std::allocator<value_type>{}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       {},
+				       SimpleAllocator<value_type>{}}),
+		std::multimap<int, double, std::less<int>,
+			 SimpleAllocator<value_type>>>);
+}
+
+void h()
+{
+  std::pair<int, double> array[1];
+  test_container<std::pair<int, double>, input_iterator_wrapper> x(array);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap(x.begin(), x.end())),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       std::less<int>{},
+				       std::allocator<value_type>{}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       std::less<int>{}, {}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap(x.begin(), x.end(),
+				       {})),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       {},
+				       std::allocator<value_type>{}}),
+		std::multimap<int, double>>);
+
+  static_assert(std::is_same_v<
+		decltype(std::multimap{x.begin(), x.end(),
+				       {},
+				       SimpleAllocator<value_type>{}}),
+		std::multimap<int, double, std::less<int>,
+			      SimpleAllocator<value_type>>>);
 }