xref: /llvm-project/libcxx/test/std/utilities/intseq/intseq.general/integer_seq.pass.cpp (revision 2df59c50688c122bbcae7467d3eaf862c3ea3088)
1 //===----------------------------------------------------------------------===//
2 //
3 // Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4 // See https://llvm.org/LICENSE.txt for license information.
5 // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6 //
7 //===----------------------------------------------------------------------===//
8 
9 // UNSUPPORTED: c++98, c++03, c++11
10 // <utility>
11 
12 // class make_integer_sequence
13 
14 #include <tuple>
15 #include <utility>
16 #include <type_traits>
17 #include <cassert>
18 
19 template <typename AtContainer, typename T, T... I>
20 auto extract ( const AtContainer &t, const std::integer_sequence<T, I...> )
21 -> decltype ( std::make_tuple ( std::get<I>(t)... ))
22 {     return  std::make_tuple ( std::get<I>(t)... ); }
23 
24 int main(int, char**)
25 {
26 //  Make a couple of sequences
27     using int3    = std::make_integer_sequence<int, 3>;     // generates int:    0,1,2
28     using size7   = std::make_integer_sequence<size_t, 7>;  // generates size_t: 0,1,2,3,4,5,6
29     using size4   = std::make_index_sequence<4>;            // generates size_t: 0,1,2,3
30     using size2   = std::index_sequence_for<int, size_t>;   // generates size_t: 0,1
31     using intmix  = std::integer_sequence<int, 9, 8, 7, 2>; // generates int:    9,8,7,2
32     using sizemix = std::index_sequence<1, 1, 2, 3, 5>;     // generates size_t: 1,1,2,3,5
33 
34 //  Make sure they're what we expect
35     static_assert ( std::is_same<int3::value_type, int>::value, "int3 type wrong" );
36     static_assert ( int3::size () == 3, "int3 size wrong" );
37 
38     static_assert ( std::is_same<size7::value_type, size_t>::value, "size7 type wrong" );
39     static_assert ( size7::size () == 7, "size7 size wrong" );
40 
41     static_assert ( std::is_same<size4::value_type, size_t>::value, "size4 type wrong" );
42     static_assert ( size4::size () == 4, "size4 size wrong" );
43 
44     static_assert ( std::is_same<size2::value_type, size_t>::value, "size2 type wrong" );
45     static_assert ( size2::size () == 2, "size2 size wrong" );
46 
47     static_assert ( std::is_same<intmix::value_type, int>::value, "intmix type wrong" );
48     static_assert ( intmix::size () == 4, "intmix size wrong" );
49 
50     static_assert ( std::is_same<sizemix::value_type, size_t>::value, "sizemix type wrong" );
51     static_assert ( sizemix::size () == 5, "sizemix size wrong" );
52 
53     auto tup = std::make_tuple ( 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20 );
54 
55 //  Use them
56     auto t3 = extract ( tup, int3() );
57     static_assert ( std::tuple_size<decltype(t3)>::value == int3::size (), "t3 size wrong");
58     assert ( t3 == std::make_tuple ( 10, 11, 12 ));
59 
60     auto t7 = extract ( tup, size7 ());
61     static_assert ( std::tuple_size<decltype(t7)>::value == size7::size (), "t7 size wrong");
62     assert ( t7 == std::make_tuple ( 10, 11, 12, 13, 14, 15, 16 ));
63 
64     auto t4 = extract ( tup, size4 ());
65     static_assert ( std::tuple_size<decltype(t4)>::value == size4::size (), "t4 size wrong");
66     assert ( t4 == std::make_tuple ( 10, 11, 12, 13 ));
67 
68     auto t2 = extract ( tup, size2 ());
69     static_assert ( std::tuple_size<decltype(t2)>::value == size2::size (), "t2 size wrong");
70     assert ( t2 == std::make_tuple ( 10, 11 ));
71 
72     auto tintmix = extract ( tup, intmix ());
73     static_assert ( std::tuple_size<decltype(tintmix)>::value == intmix::size (), "tintmix size wrong");
74     assert ( tintmix == std::make_tuple ( 19, 18, 17, 12 ));
75 
76     auto tsizemix = extract ( tup, sizemix ());
77     static_assert ( std::tuple_size<decltype(tsizemix)>::value == sizemix::size (), "tsizemix size wrong");
78     assert ( tsizemix == std::make_tuple ( 11, 11, 12, 13, 15 ));
79 
80   return 0;
81 }
82