LCOV - code coverage report
Current view: top level - estd/examples - index_sequence.cpp (source / functions) Hit Total Coverage
Test: coverage.info Lines: 6 6 100.0 %
Date: 2025-01-20 13:53:09 Functions: 2 3 66.7 %

          Line data    Source code
       1             : // Copyright 2022 Accenture.
       2             : 
       3             : #include <estd/index_sequence.h>
       4             : 
       5             : #include <gmock/gmock.h>
       6             : #include <gtest/gtest.h>
       7             : 
       8             : using namespace ::testing;
       9             : 
      10             : // [EXAMPLE_INDEX_SEQUENCE_STRUCT_START]
      11             : template<typename T>
      12             : struct UnpackSequence;
      13             : 
      14             : // Defining a structure 'UnpackSequence'.
      15             : template<size_t... Index>
      16           1 : struct UnpackSequence<estd::index_sequence<Index...>>
      17             : {
      18             :     size_t values[sizeof...(Index)]{Index...};
      19             : };
      20             : 
      21             : // [EXAMPLE_INDEX_SEQUENCE_STRUCT_END]
      22             : 
      23           3 : TEST(IndexSequenceExample, index_sequence)
      24             : {
      25             :     // [EXAMPLE_INDEX_SEQUENCE_START]
      26             :     // make_index_sequence<1> = 0.
      27           1 :     EXPECT_THAT(UnpackSequence<estd::make_index_sequence<1>>().values, ElementsAre(0));
      28             :     // make_index_sequence<3> = 0,1,2.
      29           3 :     EXPECT_THAT(UnpackSequence<estd::make_index_sequence<3>>().values, ElementsAre(0, 1, 2));
      30             :     // make_index_sequence<5> = 0,1,2,3,4.
      31           5 :     EXPECT_THAT(UnpackSequence<estd::make_index_sequence<5>>().values, ElementsAre(0, 1, 2, 3, 4));
      32             :     // [EXAMPLE_INDEX_SEQUENCE_END]
      33           1 : }
      34             : 
      35             : template<::size_t i, ::size_t j, class Tuple>
      36             : class tuple_element_swap
      37             : {
      38             :     // [EXAMPLE_INDEX_SEQUENCE_IN_TUPLE_START]
      39             :     template<class index_sequence>
      40             :     struct tuple_element_swap_impl;
      41             : 
      42             :     // Defining a structure 'tuple_element_swap_impl', which used to swap the elements in the tuple
      43             :     // using selection sort.
      44             :     template<::size_t... indices>
      45             :     // Passing '::estd::index_sequence<indices...>' as parameter indicates that this
      46             :     // implementation is tailored to work with a specific sequence of indices.
      47             :     struct tuple_element_swap_impl<::estd::index_sequence<indices...>>
      48             :     {
      49             :         // Swaps the elements of tuple.
      50             :         using type = std::tuple<typename std::tuple_element<
      51             :             (indices != i) && (indices != j) ? indices
      52             :             : (indices == i)                 ? j
      53             :                                              : i,
      54             :             Tuple>::type...>;
      55             :     };
      56             : 
      57             :     // [EXAMPLE_INDEX_SEQUENCE_IN_TUPLE_END]
      58             : public:
      59             :     using type = typename tuple_element_swap_impl<
      60             :         ::estd::make_index_sequence<std::tuple_size<Tuple>::value>>::type;
      61             : };

Generated by: LCOV version 1.14