/******************************************************************************
The MIT License(MIT)

Embedded Template Library.
https://github.com/ETLCPP/etl
https://www.etlcpp.com

Copyright(c) 2020 John Wellbelove

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files(the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and / or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions :

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
******************************************************************************/

#include "unit_test_framework.h"

#include "etl/array.h"
#include "etl/span.h"
#include "etl/unaligned_type.h"
#include "etl/vector.h"

#include <algorithm>
#include <array>
#include <iterator>
#include <vector>
#if ETL_USING_CPP20
  #include <span>
#endif

namespace
{
  SUITE(test_span_dynamic_extent)
  {
    static const size_t SIZE = 10UL;

    using EtlData  = etl::array<int, SIZE>;
    using StlData  = std::array<int, SIZE>;
    using StlVData = std::vector<int>;

    using View  = etl::span<int>;
    using CView = etl::span<const int>;

#if ETL_USING_CPP20
    using StdView = std::span<int>;
#endif

    EtlData  etldata  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    StlData  stldata  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    StlVData stlvdata = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    EtlData etldatasmaller = {0, 1, 2, 3, 4, 5, 5, 7, 8, 9};

    EtlData etloriginal = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    EtlData etlmodified = {0, 1, 10, 10, 10, 10, 10, 10, 8, 9};

    const EtlData  cetldata  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    const StlData  cstldata  = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    const StlVData cstlvdata = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

    int  cdata[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    int* pcdata  = cdata;

    const int  ccdata[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
    const int* pccdata  = ccdata;

    //*************************************************************************
    TEST(test_default_constructor)
    {
      View view;

      CHECK_EQUAL(0U, view.size());
      CHECK_EQUAL(0U, view.max_size());
      CHECK(view.empty());
    }

#if ETL_USING_CPP20
    //*************************************************************************
    TEST(test_construct_from_std_span)
    {
      StdView stdview(stldata);
      View    view(stdview);

      CHECK_EQUAL(stdview.size(), view.size());
      CHECK_EQUAL(stdview.size(), view.size());

      bool isEqual = std::equal(view.begin(), view.end(), stdview.begin());
      CHECK(isEqual);
    }
#endif

    //*************************************************************************
    TEST(test_constructor_etl_array_1)
    {
      View view(etldata);

      CHECK_EQUAL(etldata.size(), view.size());
      CHECK_EQUAL(etldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), etldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_etl_array_1_const)
    {
      CView view(cetldata);

      CHECK_EQUAL(cetldata.size(), view.size());
      CHECK_EQUAL(cetldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cetldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_etl_array_2)
    {
      View view(etldata.begin(), etldata.end());

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), etldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_etl_array_2_const)
    {
      CView view(cetldata.begin(), cetldata.end());

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cetldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_array_1)
    {
      View view(stldata);

      CHECK_EQUAL(stldata.size(), view.size());
      CHECK_EQUAL(stldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), stldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_array_1_const)
    {
      CView view(cstldata);

      CHECK_EQUAL(cstldata.size(), view.size());
      CHECK_EQUAL(cstldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cstldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_array_2)
    {
      View view(stldata.begin(), stldata.end());

      CHECK_EQUAL(stldata.size(), view.size());
      CHECK_EQUAL(stldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), stldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_array_2_const)
    {
      CView view(cstldata.begin(), cstldata.end());

      CHECK_EQUAL(cstldata.size(), view.size());
      CHECK_EQUAL(cstldata.max_size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cstldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_vector_1)
    {
      View view(stlvdata);

      CHECK_EQUAL(stlvdata.size(), view.size());
      CHECK_EQUAL(stlvdata.size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), stlvdata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_stl_vector_1_const)
    {
      CView view(cstlvdata);

      CHECK_EQUAL(cstlvdata.size(), view.size());
      CHECK_EQUAL(cstlvdata.size(), view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cstlvdata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_c_array_1)
    {
      View view(pcdata, SIZE);

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), pcdata);
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_c_array_1_const)
    {
      CView view(pccdata, SIZE);

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), pccdata);
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_c_array_2)
    {
      View view(cdata);

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), cdata);
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_constructor_c_array_2_const)
    {
      CView view(ccdata);

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), ccdata);
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_implicit_constructor_c_array_2)
    {
      CView view;

      view = ccdata;

      CHECK_EQUAL(SIZE, view.size());
      CHECK_EQUAL(SIZE, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), ccdata);
      CHECK(isEqual);
    }

#if ETL_USING_CPP17 && ETL_HAS_INITIALIZER_LIST && !defined(ETL_TEMPLATE_DEDUCTION_GUIDE_TESTS_DISABLED)
    //*************************************************************************
    TEST(test_cpp17_deduced_constructor)
    {
      etl::array data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      etl::span span1{data};
      etl::span span2{data.begin(), data.end()};
      etl::span span3{data.begin(), data.size()};
      etl::span span4{span1};

      int       c_array[] = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::span span5{c_array};

      bool isEqual = false;

      isEqual = std::equal(span1.begin(), span1.end(), data.begin());
      CHECK(isEqual);

      isEqual = std::equal(span2.begin(), span2.end(), data.begin());
      CHECK(isEqual);

      isEqual = std::equal(span3.begin(), span3.end(), data.begin());
      CHECK(isEqual);

      isEqual = std::equal(span4.begin(), span4.end(), data.begin());
      CHECK(isEqual);

      isEqual = std::equal(span5.begin(), span5.end(), c_array);
      CHECK(isEqual);
    }
#endif

    //*************************************************************************
    TEST(test_constructor_range)
    {
      View view(etldata.begin() + 2, etldata.end() - 2);

      CHECK_EQUAL(etldata.size() - 4, view.size());
      CHECK_EQUAL(etldata.size() - 4, view.max_size());

      bool isEqual = std::equal(view.begin(), view.end(), etldata.begin() + 2);
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_copy_constructor_from_same_span_type)
    {
      etl::array<char, 10> data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      const etl::span<char> view1(data.data(), data.data() + data.size());
      etl::span<char>       view2(view1);

      CHECK_EQUAL(data.size(), view1.size());
      CHECK_EQUAL(data.size(), view2.size());

      bool isEqual = std::equal(view1.begin(), view1.end(), view2.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_copy_constructor_from_different_span_type)
    {
      etl::array<char, 10> data = {0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      const etl::span<char> view1(data.data(), data.data() + data.size());
      etl::span<const char> view2(view1);

      CHECK_EQUAL(data.size(), view1.size());
      CHECK_EQUAL(data.size(), view2.size());

      bool isEqual = std::equal(view1.begin(), view1.end(), view2.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_assign_from_span)
    {
      View view1(etldata);
      View view2;

      CView cview1(etldata);
      CView cview2;

      view2  = view1;
      cview2 = cview1;

      CHECK_EQUAL(view1.size(), view2.size());
      CHECK_EQUAL(view1.max_size(), view2.max_size());

      CHECK_EQUAL(cview1.size(), cview2.size());
      CHECK_EQUAL(cview1.max_size(), cview2.max_size());

      bool isEqual;

      isEqual = std::equal(view1.begin(), view1.end(), view2.begin());
      CHECK(isEqual);

      isEqual = std::equal(cview1.begin(), cview1.end(), cview2.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_modify_range)
    {
      View view(etloriginal.begin() + 2, etloriginal.end() - 2);

      CHECK_EQUAL(etloriginal.size() - 4, view.size());
      CHECK_EQUAL(etloriginal.size() - 4, view.max_size());

      std::fill(view.begin(), view.end(), 10);

      bool isEqual;

      isEqual = std::equal(etloriginal.begin(), etloriginal.end(), etlmodified.begin());
      CHECK(isEqual);

      isEqual = std::equal(etldata.begin(), etldata.end(), etlmodified.begin());
      CHECK(!isEqual);
    }

    //*************************************************************************
    TEST(test_begin_end)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      CHECK_EQUAL(etldata.cbegin(), view.cbegin());
      CHECK_EQUAL(etldata.begin(), view.begin());
      CHECK_EQUAL(etldata.begin(), cview.begin());

      CHECK_EQUAL(etldata.cend(), view.crbegin().base());
      CHECK_EQUAL(etldata.end(), view.rbegin().base());
      CHECK_EQUAL(etldata.end(), cview.rbegin().base());

      CHECK_EQUAL(etldata.cend(), view.cend());
      CHECK_EQUAL(etldata.end(), view.end());
      CHECK_EQUAL(etldata.end(), cview.end());

      CHECK_EQUAL(etldata.cbegin(), view.crend().base());
      CHECK_EQUAL(etldata.begin(), view.rend().base());
      CHECK_EQUAL(etldata.begin(), cview.rend().base());
    }

    //*************************************************************************
    TEST(test_front_back)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      CHECK_EQUAL(etldata.front(), view.front());
      CHECK_EQUAL(etldata.front(), cview.front());

      CHECK_EQUAL(etldata.back(), view.back());
      CHECK_EQUAL(etldata.back(), cview.back());

      View empty_view;
      CHECK_THROW(
        {
          auto front = empty_view.front();
          (void)front;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto back = empty_view.back();
          (void)back;
        },
        etl::span_out_of_range);

      CView empty_cview;
      CHECK_THROW(
        {
          auto front = empty_cview.front();
          (void)front;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto back = empty_cview.back();
          (void)back;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_data)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      CHECK_EQUAL(etldata.data(), view.data());
      CHECK_EQUAL(etldata.data(), cview.data());
    }

    //*************************************************************************
    TEST(test_at)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      for (size_t i = 0UL; i < etldata.size(); ++i)
      {
        CHECK_EQUAL(etldata.at(i), view.at(i));
        CHECK_EQUAL(etldata.at(i), cview.at(i));
      }

      CHECK_THROW(
        {
          int d = view.at(view.size());
          (void)d;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          int d = cview.at(cview.size());
          (void)d;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_index_operator)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      for (size_t i = 0UL; i < etldata.size(); ++i)
      {
        CHECK_EQUAL(etldata[i], view[i]);
        CHECK_EQUAL(etldata[i], cview[i]);
      }

      CHECK_THROW(
        {
          int d = view[view.size()];
          (void)d;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          int d = cview[cview.size()];
          (void)d;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_assignment_operator)
    {
      View  view(etldata);
      CView cview = view;

      CHECK_EQUAL(etldata.size(), cview.size());
      CHECK_EQUAL(etldata.max_size(), cview.max_size());

      bool isEqual = std::equal(cview.begin(), cview.end(), etldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_empty)
    {
      View view1(etldata.begin(), etldata.begin());
      CHECK(view1.empty());

      View view2(etldata.begin(), etldata.begin() + 1);
      CHECK(!view2.empty());
    }

    //*************************************************************************
    TEST(test_size)
    {
      View view(etldata);

      CHECK_EQUAL(etldata.size(), view.size());
    }

    //*************************************************************************
    TEST(test_size_bytes)
    {
      View view(etldata);

      CHECK_EQUAL(etldata.size() * sizeof(EtlData::value_type), view.size_bytes());
    }

    //*************************************************************************
    TEST(test_swap)
    {
      View view1(etldata);
      View view2(etldatasmaller);

      std::swap(view1, view2);

      CHECK_EQUAL(etldata.size(), view1.size());
      CHECK_EQUAL(etldatasmaller.size(), view2.size());

      bool isEqual;

      isEqual = std::equal(view1.begin(), view1.end(), etldatasmaller.begin());
      CHECK(isEqual);

      isEqual = std::equal(view2.begin(), view2.end(), etldata.begin());
      CHECK(isEqual);
    }

    //*************************************************************************
    TEST(test_first)
    {
      std::vector<int> original = {1, 2, 3, 4, 5, 6, 7, 8};
      std::vector<int> first    = {1, 2, 3, 4, 5, 6};
      View             view(original);
      CView            cview(original);

      bool isEqual;

      auto result = view.first<6>();
      isEqual     = std::equal(result.begin(), result.end(), first.begin());
      CHECK(isEqual);
      CHECK_EQUAL(first.size(), result.extent);
      CHECK_EQUAL(first.size(), result.size());

      auto cresult = cview.first<6>();
      isEqual      = std::equal(cresult.begin(), cresult.end(), first.begin());
      CHECK(isEqual);
      CHECK_EQUAL(first.size(), cresult.extent);
      CHECK_EQUAL(first.size(), cresult.size());

      CHECK_THROW(
        {
          auto result2 = view.first<9>();
          (void)result2;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cresult2 = cview.first<9>();
          (void)cresult2;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_first_2)
    {
      std::vector<int> original = {1, 2, 3, 4, 5, 6, 7, 8};
      std::vector<int> first    = {1, 2, 3, 4, 5, 6};
      View             view(original);
      CView            cview(original);

      bool isEqual;

      auto result = view.first(6);
      isEqual     = std::equal(result.begin(), result.end(), first.begin());
      CHECK(isEqual);
      CHECK_EQUAL(first.size(), result.size());

      auto cresult = cview.first(6);
      isEqual      = std::equal(cresult.begin(), cresult.end(), first.begin());
      CHECK(isEqual);
      CHECK_EQUAL(first.size(), cresult.size());

      CHECK_THROW(
        {
          auto result2 = view.first(9);
          (void)result2;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cresult2 = cview.first(9);
          (void)cresult2;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_last)
    {
      std::vector<int> original = {1, 2, 3, 4, 5, 6, 7, 8};
      std::vector<int> last     = {3, 4, 5, 6, 7, 8};
      View             view(original);
      CView            cview(original);

      bool isEqual;

      auto result = view.last<6>();
      isEqual     = std::equal(result.begin(), result.end(), last.begin());
      CHECK(isEqual);
      CHECK_EQUAL(last.size(), result.extent);
      CHECK_EQUAL(last.size(), result.size());

      auto cresult = cview.last<6>();
      isEqual      = std::equal(cresult.begin(), cresult.end(), last.begin());
      CHECK(isEqual);
      CHECK_EQUAL(last.size(), cresult.extent);
      CHECK_EQUAL(last.size(), cresult.size());

      CHECK_THROW(
        {
          auto result2 = view.last<9>();
          (void)result2;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cresult2 = cview.last<9>();
          (void)cresult2;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_last_2)
    {
      std::vector<int> original = {1, 2, 3, 4, 5, 6, 7, 8};
      std::vector<int> last     = {3, 4, 5, 6, 7, 8};
      View             view(original);
      CView            cview(original);

      bool isEqual;

      auto result = view.last(6);
      isEqual     = std::equal(result.begin(), result.end(), last.begin());
      CHECK(isEqual);
      CHECK_EQUAL(last.size(), result.size());

      auto cresult = cview.last(6);
      isEqual      = std::equal(cresult.begin(), cresult.end(), last.begin());
      CHECK(isEqual);
      CHECK_EQUAL(last.size(), cresult.size());

      CHECK_THROW(
        {
          auto result2 = view.last(9);
          (void)result2;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cresult2 = cview.last(9);
          (void)cresult2;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_subspan)
    {
      std::vector<int> original = {1, 2, 3, 4, 5, 6, 7, 8};
      std::vector<int> sub1     = {3, 4, 5, 6};
      std::vector<int> sub2     = {3, 4, 5, 6, 7, 8};
      View             view(original);
      CView            cview(original);

      bool isEqual;

      auto span1 = view.subspan<2, 4>();
      isEqual    = std::equal(span1.begin(), span1.end(), sub1.begin());
      CHECK(isEqual);
      CHECK_EQUAL(sub1.size(), span1.extent);
      CHECK_EQUAL(sub1.size(), span1.size());

      auto cspan1 = cview.subspan<2, 4>();
      isEqual     = std::equal(cspan1.begin(), cspan1.end(), sub1.begin());
      CHECK(isEqual);
      CHECK_EQUAL(sub1.size(), cspan1.extent);
      CHECK_EQUAL(sub1.size(), cspan1.size());

      auto span2 = view.subspan<2>();
      isEqual    = std::equal(span2.begin(), span2.end(), sub2.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, span2.extent);
      CHECK_EQUAL(sub2.size(), span2.size());

      auto cspan2 = cview.subspan<2>();
      isEqual     = std::equal(cspan2.begin(), cspan2.end(), sub2.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, cspan2.extent);
      CHECK_EQUAL(sub2.size(), cspan2.size());

      auto span3 = view.subspan(2, 4);
      isEqual    = std::equal(span3.begin(), span3.end(), sub1.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, span3.extent);
      CHECK_EQUAL(sub1.size(), span3.size());

      auto cspan3 = cview.subspan(2, 4);
      isEqual     = std::equal(cspan3.begin(), cspan3.end(), sub1.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, cspan3.extent);
      CHECK_EQUAL(sub1.size(), cspan3.size());

      auto span4 = view.subspan(2);
      isEqual    = std::equal(span4.begin(), span4.end(), sub2.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, span4.extent);
      CHECK_EQUAL(sub2.size(), span4.size());

      auto cspan4 = cview.subspan(2);
      isEqual     = std::equal(cspan4.begin(), cspan4.end(), sub2.begin());
      CHECK(isEqual);
      CHECK_EQUAL(etl::dynamic_extent, cspan4.extent);
      CHECK_EQUAL(sub2.size(), cspan4.size());

      CHECK_THROW(
        {
          auto span5 = view.subspan<9>();
          (void)span5;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cspan5 = cview.subspan<9>();
          (void)cspan5;
        },
        etl::span_out_of_range);

#define SPAN6_EXPR                     \
  {                                    \
    auto span6 = view.subspan<2, 7>(); \
    (void)span6;                       \
  }
      CHECK_THROW(SPAN6_EXPR, etl::span_out_of_range);
#define CSPAN6_EXPR                      \
  {                                      \
    auto cspan6 = cview.subspan<2, 7>(); \
    (void)cspan6;                        \
  }
      CHECK_THROW(CSPAN6_EXPR, etl::span_out_of_range);

      CHECK_THROW(
        {
          auto span7 = view.subspan(9);
          (void)span7;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cspan7 = cview.subspan(9);
          (void)cspan7;
        },
        etl::span_out_of_range);

      CHECK_THROW(
        {
          auto span8 = view.subspan(2, 7);
          (void)span8;
        },
        etl::span_out_of_range);
      CHECK_THROW(
        {
          auto cspan8 = cview.subspan(2, 7);
          (void)cspan8;
        },
        etl::span_out_of_range);
    }

    //*************************************************************************
    TEST(test_hash)
    {
      View  view(etldata.begin(), etldata.end());
      CView cview(etldata.begin(), etldata.end());

      size_t hashdata = etl::private_hash::generic_hash<size_t>(reinterpret_cast<const uint8_t*>(etldata.data()),
                                                                reinterpret_cast<const uint8_t*>(etldata.data() + etldata.size()));

      size_t hashview  = etl::hash<View>()(view);
      size_t hashcview = etl::hash<CView>()(cview);

      CHECK_EQUAL(hashdata, hashview);
      CHECK_EQUAL(hashdata, hashcview);
    }

#if ETL_USING_CPP17
    //*************************************************************************
    TEST(test_template_deduction_guide_for_c_array)
    {
      int data[] = {1, 2, 3, 4};

      etl::span s = data;

      CHECK_EQUAL(ETL_OR_STD17::size(data), s.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data), s.size());
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(s.front())>>));
    }

  #if ETL_USING_STL
    //*************************************************************************
    TEST(test_template_deduction_guide_for_std_array)
    {
      std::array<int, 4U> data = {1, 2, 3, 4};

      etl::span s = data;

      CHECK_EQUAL(ETL_OR_STD17::size(data), s.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data), s.size());
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(s.front())>>));
    }
  #endif

    //*************************************************************************
    TEST(test_template_deduction_guide_for_etl_array)
    {
      etl::array<int, 4U> data = {1, 2, 3, 4};

      etl::span s = data;

      CHECK_EQUAL(ETL_OR_STD17::size(data), s.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data), s.size());
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(s.front())>>));
    }

    //*************************************************************************
    TEST(test_template_deduction_guide_for_etl_vector)
    {
      const size_t max_size = 10;
      const size_t size     = 4;

      int        buffer1[max_size];
      int        buffer2[max_size];
      int*       buffer3[max_size];
      int*       buffer4[max_size];
      const int* buffer5[max_size];
      const int* buffer6[max_size];

      etl::vector<int, max_size>       data(size, 0);
      const etl::vector<int, max_size> data2(size, 0);
      etl::vector_ext<int>             data_ext(size, 0, buffer1, max_size);
      const etl::vector_ext<int>       data2_ext(size, 0, buffer2, max_size);

      etl::span span      = data;
      etl::span span2     = data2;
      etl::span span_ext  = data_ext;
      etl::span span2_ext = data2_ext;

      CHECK_EQUAL(etl::dynamic_extent, span.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data), span.size());
      CHECK_EQUAL(etl::dynamic_extent, span2.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data2), span2.size());
      CHECK_EQUAL(etl::dynamic_extent, span_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data_ext), span_ext.size());
      CHECK_EQUAL(etl::dynamic_extent, span2_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(data2_ext), span2_ext.size());

      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(span.front())>>));
      CHECK((std::is_same_v<const int, std::remove_reference_t<decltype(span2.front())>>));
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(span_ext.front())>>));
      CHECK((std::is_same_v<const int, std::remove_reference_t<decltype(span2_ext.front())>>));

      etl::vector<int*, max_size>             pdata(size, nullptr);
      const etl::vector<int*, max_size>       pdata2(size, nullptr);
      etl::vector<const int*, max_size>       pdata3(size, nullptr);
      const etl::vector<const int*, max_size> pdata4(size, nullptr);

      etl::span pspan  = pdata;
      etl::span pspan2 = pdata2;
      etl::span pspan3 = pdata3;
      etl::span pspan4 = pdata4;

      CHECK_EQUAL(etl::dynamic_extent, pspan.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata), pspan.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan2.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata2), pspan2.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan3.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata3), pspan3.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan4.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata4), pspan4.size());

      CHECK((std::is_same_v<int*, std::remove_reference_t<decltype(pspan.front())>>));
      CHECK((std::is_same_v<int* const, std::remove_reference_t<decltype(pspan2.front())>>));
      CHECK((std::is_same_v<const int*, std::remove_reference_t<decltype(pspan3.front())>>));
      CHECK((std::is_same_v<const int* const, std::remove_reference_t<decltype(pspan4.front())>>));

      etl::vector_ext<int*>             pdata_ext(size, nullptr, buffer3, max_size);
      const etl::vector_ext<int*>       pdata2_ext(size, nullptr, buffer4, max_size);
      etl::vector_ext<const int*>       pdata3_ext(size, nullptr, buffer5, max_size);
      const etl::vector_ext<const int*> pdata4_ext(size, nullptr, buffer6, max_size);

      etl::span pspan_ext  = pdata_ext;
      etl::span pspan2_ext = pdata2_ext;
      etl::span pspan3_ext = pdata3_ext;
      etl::span pspan4_ext = pdata4_ext;

      CHECK_EQUAL(etl::dynamic_extent, pspan_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata_ext), pspan_ext.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan2_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata2_ext), pspan2_ext.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan3_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata3_ext), pspan3_ext.size());
      CHECK_EQUAL(etl::dynamic_extent, pspan4_ext.extent);
      CHECK_EQUAL(ETL_OR_STD17::size(pdata4_ext), pspan4_ext.size());

      CHECK((std::is_same_v<int*, std::remove_reference_t<decltype(pspan_ext.front())>>));
      CHECK((std::is_same_v<int* const, std::remove_reference_t<decltype(pspan2_ext.front())>>));
      CHECK((std::is_same_v<const int*, std::remove_reference_t<decltype(pspan3_ext.front())>>));
      CHECK((std::is_same_v<const int* const, std::remove_reference_t<decltype(pspan4_ext.front())>>));
    }

    //*************************************************************************
    TEST(test_template_deduction_guide_for_iterators)
    {
      etl::array<int, 4U> data = {1, 2, 3, 4};

      etl::span s{data.begin(), data.end()};

      CHECK_EQUAL(etl::dynamic_extent, s.extent);
      CHECK_EQUAL(4U, s.size());
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(s.front())>>));
    }

    //*************************************************************************
    TEST(test_template_deduction_guide_for_iterator_and_size)
    {
      etl::array<int, 4U> data = {1, 2, 3, 4};

      etl::span s{data.begin(), data.size()};

      CHECK_EQUAL(etl::dynamic_extent, s.extent);
      CHECK_EQUAL(4U, s.size());
      CHECK((std::is_same_v<int, std::remove_reference_t<decltype(s.front())>>));
    }
#endif

    //*************************************************************************
    void f_issue_481(etl::span<const char, 10>) {}

    TEST(test_issue_481)
    {
      // Should compile.
      char c[10];
      f_issue_481(c);
    }

    //*************************************************************************
#include "etl/private/diagnostic_unused_function_push.h"

    struct C_issue_482
    {
    };

    void f_issue_482(etl::span<char>) {}

    void f_issue_482(etl::span<C_issue_482>) {}

    TEST(test_issue_482)
    {
      etl::array<C_issue_482, 10> c;

      // Should compile without ambiguous function error.
      f_issue_482(c);
    }

    //*************************************************************************
    void f_issue_482_2(etl::span<char, 10>) {}

    void f_issue_482_2(etl::span<C_issue_482, 10>) {}

    TEST(test_issue_482_2)
    {
      etl::array<C_issue_482, 10> c;

      // Should compile without ambiguous function error.
      f_issue_482(c);
    }

    //*************************************************************************
    void f_issue_486(etl::span<const char, 11>) {}

    TEST(test_issue_486)
    {
      // std::array<char, 10> c;

      // Should not compile.
      // etl::span<char, 11> value(c);

      // Should not compile.
      // f_issue_486(c);
    }

    //*************************************************************************
    TEST(test_circular_iterator_pre_increment)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0};

      View view{data};

      View::circular_iterator sci = view.begin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i], *++sci);
      }
    }

    //*************************************************************************
    TEST(test_circular_iterator_pre_increment_for_subspan)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2};

      View view{data};
      View subspan = view.subspan<2, 4>();

      View::circular_iterator sci = subspan.begin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i], *++sci);
      }
    }

    //*************************************************************************
    TEST(test_circular_iterator_post_increment)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{0, 1, 2, 3, 4, 5, 6, 7, 8, 9, 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      View view{data};

      View::circular_iterator sci = view.begin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i % ETL_OR_STD17::size(expected)], *sci++);
      }
    }

    //*************************************************************************
    TEST(test_circular_iterator_post_increment_for_subspan)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5, 2, 3, 4, 5};

      View view{data};
      View subspan = view.subspan<2, 4>();

      View::circular_iterator sci = subspan.begin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i], *sci++);
      }
    }

    //*************************************************************************
    TEST(test_circular_reverse_iterator_pre_increment)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9};

      View view{data};

      View::reverse_circular_iterator sci = view.rbegin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i % ETL_OR_STD17::size(expected)], *++sci);
      }
    }

    //*************************************************************************
    TEST(test_circular_reverse_iterator_pre_increment_for_subspan)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2, 5};

      View view{data};
      View subspan = view.subspan<2, 4>();

      etl::span<int, 10U>::reverse_circular_iterator sci = subspan.rbegin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i % ETL_OR_STD17::size(expected)], *++sci);
      }
    }

    //*************************************************************************
    TEST(test_circular_reverse_iterator_post_increment)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{9, 8, 7, 6, 5, 4, 3, 2, 1, 0, 9, 8, 7, 6, 5, 4, 3, 2, 1, 0};

      View view{data};

      View::reverse_circular_iterator sci = view.rbegin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i % ETL_OR_STD17::size(expected)], *sci++);
      }
    }

    //*************************************************************************
    TEST(test_circular_reverse_iterator_post_increment_for_subspan)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{5, 4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2, 5, 4, 3, 2};

      View view{data};
      View subspan = view.subspan<2, 4>();

      View::reverse_circular_iterator sci = subspan.rbegin_circular();

      for (size_t i = 0U; i < 20U; ++i)
      {
        CHECK_EQUAL(expected[i % ETL_OR_STD17::size(expected)], *sci++);
      }
    }

    //*************************************************************************
    TEST(test_operator_plus_equals)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      View view{data};

      for (int step = 1; step < 20; ++step)
      {
        View::circular_iterator sci = view.begin_circular();

        for (size_t i = 0U; i < 20U; i += static_cast<size_t>(step))
        {
          CHECK_EQUAL(expected[i % 10], *sci);
          sci += step;
        }
      }
    }

    //*************************************************************************
    TEST(test_operator_plus)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};

      View view{data};

      for (int step = 1; step < 20; ++step)
      {
        View::circular_iterator sci = view.begin_circular();

        for (size_t i = 0U; i < 20U; i += static_cast<size_t>(step))
        {
          CHECK_EQUAL(expected[i % 10], *sci);
          sci = sci + step;
        }
      }
    }

    //*************************************************************************
    TEST(test_operator_minus_equals)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{0, 9, 8, 7, 6, 5, 4, 3, 2, 1};

      View view{data};

      for (int step = 1; step < 20; ++step)
      {
        View::circular_iterator sci = view.begin_circular();

        for (size_t i = 0U; i < 20U; i += static_cast<size_t>(step))
        {
          CHECK_EQUAL(expected[i % 10], *sci);
          sci -= step;
        }
      }
    }

    //*************************************************************************
    TEST(test_operator_minus)
    {
      etl::array<int, 10> data{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 20> expected{0, 9, 8, 7, 6, 5, 4, 3, 2, 1};

      View view{data};

      for (int step = 1; step < 20; ++step)
      {
        View::circular_iterator sci = view.begin_circular();

        for (size_t i = 0U; i < 20U; i += static_cast<size_t>(step))
        {
          CHECK_EQUAL(expected[i % 10], *sci);
          sci = sci - step;
        }
      }
    }

    //*************************************************************************
    TEST(test_operator_equality)
    {
      etl::array<int, 10> data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data3{0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
      etl::array<int, 9>  data4{0, 1, 2, 3, 5, 6, 7, 8, 9};

      int i = 1;

      View view1{data1};
      View view2{data1};
      View view3{data2};
      View view4{data3};
      View view5{data4};
      View view6;
      View view7;
      View view8(&i, &i);

      CHECK_TRUE(etl::equal(view1, view2));
      CHECK_TRUE(etl::equal(view1, view3));
      CHECK_FALSE(etl::equal(view1, view4));
      CHECK_FALSE(etl::equal(view1, view5));
      CHECK_TRUE(etl::equal(view6, view6));
      CHECK_TRUE(etl::equal(view6, view7));
      CHECK_TRUE(etl::equal(view6, view8));
      CHECK_TRUE(etl::equal(view8, view8));

      CHECK_TRUE(view1 == view2);
      CHECK_FALSE(view1 == view3);
      CHECK_FALSE(view1 == view4);
      CHECK_FALSE(view1 == view5);
      CHECK_TRUE(view6 == view6);
      CHECK_TRUE(view6 == view7);
      CHECK_FALSE(view6 == view8);
      CHECK_TRUE(view8 == view8);
    }

    //*************************************************************************
    TEST(test_operator_equality_one_is_const)
    {
      etl::array<int, 10> data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data3{0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
      etl::array<int, 9>  data4{0, 1, 2, 3, 5, 6, 7, 8, 9};

      int i = 1;

      View  view1{data1};
      CView view2{data1};
      CView view3{data2};
      CView view4{data3};
      CView view5{data4};
      View  view6;
      CView view7;
      CView view8(&i, &i);

      CHECK_TRUE(etl::equal(view1, view2));
      CHECK_TRUE(etl::equal(view1, view3));
      CHECK_FALSE(etl::equal(view1, view4));
      CHECK_FALSE(etl::equal(view1, view5));
      CHECK_TRUE(etl::equal(view6, view6));
      CHECK_TRUE(etl::equal(view6, view7));
      CHECK_TRUE(etl::equal(view6, view8));
      CHECK_TRUE(etl::equal(view8, view8));

      CHECK_TRUE(view1 == view2);
      CHECK_FALSE(view1 == view3);
      CHECK_FALSE(view1 == view4);
      CHECK_FALSE(view1 == view5);
      CHECK_TRUE(view6 == view6);
      CHECK_TRUE(view6 == view7);
      CHECK_FALSE(view6 == view8);
      CHECK_TRUE(view8 == view8);
    }

    //*************************************************************************
    TEST(test_operator_not_equal)
    {
      etl::array<int, 10> data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data3{0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
      etl::array<int, 10> data4{0, 1, 2, 3, 5, 6, 7, 8, 9};

      int i = 1;

      View view1{data1};
      View view2{data1};
      View view3{data2};
      View view4{data3};
      View view5{data4};
      View view6;
      View view7;
      View view8(&i, &i);

      CHECK_TRUE(etl::equal(view1, view2));
      CHECK_TRUE(etl::equal(view1, view3));
      CHECK_FALSE(etl::equal(view1, view4));
      CHECK_FALSE(etl::equal(view1, view5));
      CHECK_TRUE(etl::equal(view6, view6));
      CHECK_TRUE(etl::equal(view6, view7));
      CHECK_TRUE(etl::equal(view6, view8));
      CHECK_TRUE(etl::equal(view8, view8));

      CHECK_FALSE(view1 != view2);
      CHECK_TRUE(view1 != view3);
      CHECK_TRUE(view1 != view4);
      CHECK_TRUE(view1 != view5);
      CHECK_FALSE(view6 != view6);
      CHECK_FALSE(view6 != view7);
      CHECK_TRUE(view6 != view8);
      CHECK_FALSE(view8 != view8);
    }

    //*************************************************************************
    TEST(test_operator_not_equal_one_is_const)
    {
      etl::array<int, 10> data1{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data2{0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
      etl::array<int, 10> data3{0, 1, 2, 3, 4, 4, 6, 7, 8, 9};
      etl::array<int, 9>  data4{0, 1, 2, 3, 5, 6, 7, 8, 9};

      int i = 1;

      View  view1{data1};
      CView view2{data1};
      CView view3{data2};
      CView view4{data3};
      CView view5{data4};
      View  view6;
      View  view7;
      View  view8(&i, &i);

      CHECK_TRUE(etl::equal(view1, view2));
      CHECK_TRUE(etl::equal(view1, view3));
      CHECK_FALSE(etl::equal(view1, view4));
      CHECK_FALSE(etl::equal(view1, view5));
      CHECK_TRUE(etl::equal(view6, view6));
      CHECK_TRUE(etl::equal(view6, view7));
      CHECK_TRUE(etl::equal(view6, view8));
      CHECK_TRUE(etl::equal(view8, view8));

      CHECK_FALSE(view1 != view1);
      CHECK_FALSE(view1 != view2);
      CHECK_TRUE(view1 != view3);
      CHECK_TRUE(view1 != view4);
      CHECK_TRUE(view1 != view5);
      CHECK_FALSE(view6 != view6);
      CHECK_FALSE(view6 != view7);
      CHECK_TRUE(view6 != view8);
      CHECK_FALSE(view8 != view8);
    }

    //*************************************************************************
    TEST(test_convert_span_any_to_span_byte)
    {
      float       data[2]{3.141592f, 2.71828f};
      const float const_data[2]{3.141592f, 2.71828f};

      auto const const_bytes    = etl::as_bytes(etl::span<const float, etl::dynamic_extent>{const_data});
      auto const writable_bytes = etl::as_writable_bytes(etl::span<float, etl::dynamic_extent>{data});

      CHECK_EQUAL(const_bytes.size(), sizeof(data));
      CHECK_EQUAL(writable_bytes.size(), sizeof(data));

      etl::byte* pdata = reinterpret_cast<etl::byte*>(data);

      // Test the reading of bytes.
      for (size_t i = 0; i < sizeof(data); ++i)
      {
        CHECK_EQUAL(int(pdata[i]), int(const_bytes[i]));
        CHECK_EQUAL(int(pdata[i]), int(writable_bytes[i]));
      }

      // Test writing of bytes.
      for (size_t i = 0; i < writable_bytes.size(); ++i)
      {
        writable_bytes[i] = ~writable_bytes[i];
        CHECK_EQUAL(int(pdata[i]), int(writable_bytes[i]));
      }
    }

    //*************************************************************************
    TEST(test_make_span_container)
    {
      {
        auto s = etl::make_span(etldata);

        CHECK_EQUAL(s.size(), 10);
        View view(etldata);
        CHECK_TRUE(etl::equal(s, view));
      }
      {
        auto s = etl::make_span(cetldata);

        CHECK_EQUAL(s.size(), 10);
        View view(etldata);
        CHECK_TRUE(etl::equal(s, view));
      }
    }

    //*************************************************************************
    TEST(test_advance)
    {
      {
        uint8_t            data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
        etl::span<uint8_t> data0  = data;

        CHECK_EQUAL(data0.size(), 5);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 4);
        CHECK_EQUAL(data0[0], 0x02);
        data0.advance(2);
        CHECK_EQUAL(data0.size(), 2);
        CHECK_EQUAL(data0[0], 0x04);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 1);
        CHECK_EQUAL(data0[0], 0x05);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 0);
      }
      {
        const uint8_t            data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
        etl::span<const uint8_t> data0  = data;

        CHECK_EQUAL(data0.size(), 5);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 4);
        CHECK_EQUAL(data0[0], 0x02);
        data0.advance(2);
        CHECK_EQUAL(data0.size(), 2);
        CHECK_EQUAL(data0[0], 0x04);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 1);
        CHECK_EQUAL(data0[0], 0x05);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 0);
        data0.advance(1);
        CHECK_EQUAL(data0.size(), 0);
        data0.advance(100);
        CHECK_EQUAL(data0.size(), 0);
      }
    }

    //*************************************************************************
    TEST(test_reinterpret_as)
    {
      {
        uint8_t            data[] = {0x01, 0x02, 0x03, 0x04, 0x05};
        etl::span<uint8_t> data0  = data;

        etl::span<etl::be_uint16_t> data1 = data0.reinterpret_as<etl::be_uint16_t>();

        CHECK_EQUAL(data1.size(), 2);
        CHECK(data1[0] == 0x102);
        CHECK(data1[1] == 0x304);
      }
      {
        uint32_t            data[] = {0x01020304};
        etl::span<uint32_t> data0  = data;
        etl::span<uint8_t>  data1  = data0.reinterpret_as<uint8_t>();
        data1                      = data1.first(3);
        etl::span<int32_t> data2   = data1.reinterpret_as<int32_t>();
        CHECK_EQUAL(data2.size(), 0);
      }
      {
        uint32_t            data[] = {0x01020304, 0x06070809};
        etl::span<uint32_t> data0  = data;
        etl::span<uint8_t>  data1  = data0.reinterpret_as<uint8_t>();
        data1                      = data1.first(6);
        etl::span<int32_t> data2   = data1.reinterpret_as<int32_t>();
        CHECK_EQUAL(data2.size(), 1);

        auto it = data2.begin();
        CHECK_NOT_EQUAL(it, data2.end());
        ++it;
        CHECK_EQUAL(it, data2.end());
      }
    }

    //*************************************************************************
    TEST(test_reinterpret_as_aligned)
    {
      uint32_t            data[] = {0x01020304, 0x020406080, 0x03400560};
      etl::span<uint32_t> data0  = data;
      CHECK_EQUAL(data0.size(), 3);

      etl::span<uint8_t> data1 = data0.reinterpret_as<uint8_t>();
      CHECK_EQUAL(data1.size(), 12);

      etl::span<uint16_t> data2 = data1.subspan(2).reinterpret_as<uint16_t>();
      CHECK_EQUAL(data2.size(), 5);

      CHECK_THROW(data2 = data1.subspan(1).reinterpret_as<uint16_t>(), etl::span_alignment_exception);
    }

    //*************************************************************************
    TEST(test_copy)
    {
      uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05};
      uint8_t            dst[] = {0x00, 0x00, 0x00, 0x00, 0x00, 0x00};
      etl::span<uint8_t> data0 = src;
      etl::span<uint8_t> data1 = dst;

      CHECK_EQUAL(etl::copy(data0, data1), true);
      CHECK(std::equal(data0.begin(), data0.end(), data1.begin()));

      data1 = data1.subspan(1);

      CHECK_EQUAL(etl::copy(data0, data1), true);
      CHECK(std::equal(data0.begin(), data0.end(), data1.begin()));

      data1 = data1.subspan(1);

      CHECK_EQUAL(etl::copy(data0, data1), false);

      data0 = data0.subspan(0, 0);

      CHECK_EQUAL(etl::copy(data0, data1), true);

      data0 = src;
      data1 = src;

      CHECK_EQUAL(etl::copy(data0, data1), true);
    }

    //*************************************************************************
    TEST(test_take_span_size_error)
    {
      uint8_t            src[] = {0x01, 0x02, 0x03};
      etl::span<uint8_t> data0 = src;

      CHECK_THROW(
        {
          etl::span<etl::be_uint32_t> data1 = data0.take<etl::be_uint32_t>(1);
          (void)data1;
        },
        etl::span_size_mismatch);
    }

    //*************************************************************************
    TEST(test_take_span_take_uint8_tuint32_t_then_no_data_then_excess_data)
    {
      uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05};
      etl::span<uint8_t> data0 = src;

      etl::span<uint8_t> data1 = data0.take<uint8_t>(1);

      CHECK_EQUAL(data0.size(), 4);
      CHECK_EQUAL(data1.size(), 1);
      CHECK_EQUAL(data1[0], 0x01);

      etl::span<etl::be_uint32_t> data2 = data0.take<etl::be_uint32_t>(1);

      CHECK_EQUAL(data0.size(), 0);
      CHECK_EQUAL(data2.size(), 1);
      CHECK_EQUAL(data2[0], 0x02030405);

      data1 = data0.take<uint8_t>(0);

      CHECK_EQUAL(data0.size(), 0);
      CHECK_EQUAL(data1.size(), 0);

      CHECK_THROW(
        {
          etl::span<uint8_t> data3 = data0.take<uint8_t>(1);
          (void)data3;
        },
        etl::span_size_mismatch);
    }

    //*************************************************************************
    TEST(test_take_span_take_be_uint32_t_twice_then_uint8_t)
    {
      uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
      etl::span<uint8_t> data0 = src;

      etl::span<etl::be_uint32_t> data1 = data0.take<etl::be_uint32_t>(2);

      CHECK_EQUAL(data0.size(), 2);
      CHECK_EQUAL(data1.size(), 2);
      CHECK_EQUAL(data1[0], 0x01020304);
      CHECK_EQUAL(data1[1], 0x05060708);

      etl::span<uint8_t> data2 = data0.take<uint8_t>(1);

      CHECK_EQUAL(data0.size(), 1);
      CHECK_EQUAL(data2.size(), 1);
      CHECK_EQUAL(data2[0], 0x09);
      CHECK_EQUAL(data0[0], 0x0A);
    }

    //*************************************************************************
    TEST(test_take_span_const)
    {
      const uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
      etl::span<const uint8_t> data0 = src;

      etl::span<const etl::be_uint32_t> data1 = data0.take<const etl::be_uint32_t>(2);

      CHECK_EQUAL(data0.size(), 2);
      CHECK_EQUAL(data1.size(), 2);
      CHECK_EQUAL(data1[0], 0x01020304);
      CHECK_EQUAL(data1[1], 0x05060708);

      etl::span<const uint8_t> data2 = data0.take<const uint8_t>(1);

      CHECK_EQUAL(data0.size(), 1);
      CHECK_EQUAL(data2.size(), 1);
      CHECK_EQUAL(data2[0], 0x09);

      data2 = data0.take<const uint8_t>(0);

      CHECK_EQUAL(data0.size(), 1);
      CHECK_EQUAL(data2.size(), 0);
    }

    //*************************************************************************
    TEST(test_take_single_value)
    {
      uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
      etl::span<uint8_t> data0 = src;

      uint8_t& data1 = data0.take<uint8_t>();
      CHECK_EQUAL(data0.size(), 9);
      CHECK_EQUAL(data1, 0x01);

      etl::be_uint32_t& data2 = data0.take<etl::be_uint32_t>();
      CHECK_EQUAL(data0.size(), 5);
      CHECK_EQUAL(data2, 0x02030405);

      etl::be_uint32_t& data3 = data0.take<etl::be_uint32_t>();
      CHECK_EQUAL(data0.size(), 1);
      CHECK_EQUAL(data3, 0x06070809);

      uint8_t& data4 = data0.take<uint8_t>();
      CHECK_EQUAL(data0.size(), 0);
      CHECK_EQUAL(data4, 0x0A);

      CHECK_THROW(
        {
          uint8_t& data5 = data0.take<uint8_t>();
          (void)data5;
        },
        etl::span_size_mismatch);
    }

    //*************************************************************************
    TEST(test_take_value_const)
    {
      const uint8_t            src[] = {0x01, 0x02, 0x03, 0x04, 0x05, 0x06, 0x07, 0x08, 0x09, 0x0A};
      etl::span<const uint8_t> data0 = src;

      const uint8_t& data1 = data0.take<const uint8_t>();
      CHECK_EQUAL(data0.size(), 9);
      CHECK_EQUAL(data1, 0x01);

      const etl::be_uint32_t& data2 = data0.take<const etl::be_uint32_t>();
      CHECK_EQUAL(data0.size(), 5);
      CHECK_EQUAL(data2, 0x02030405);

      const etl::be_uint32_t& data3 = data0.take<const etl::be_uint32_t>();
      CHECK_EQUAL(data0.size(), 1);
      CHECK_EQUAL(data3, 0x06070809);

      const uint8_t& data4 = data0.take<const uint8_t>();
      CHECK_EQUAL(data0.size(), 0);
      CHECK_EQUAL(data4, 0x0A);

      CHECK_THROW(
        {
          const uint8_t& data5 = data0.take<const uint8_t>();
          (void)data5;
        },
        etl::span_size_mismatch);
    }

#include "etl/private/diagnostic_pop.h"

    //*************************************************************************
    TEST(test_not_constructible_from_rvalue_container)
    {
#if ETL_USING_CPP17
      CHECK(!(etl::is_constructible_v<View, StlVData&&>));
      CHECK(!(etl::is_constructible_v<CView, StlVData&&>));

      CHECK(!(etl::is_constructible_v<View, EtlData&&>));
      CHECK(!(etl::is_constructible_v<View, StlData&&>));
#else
      CHECK(!(etl::is_constructible<View, StlVData&&>::value));
      CHECK(!(etl::is_constructible<CView, StlVData&&>::value));

      CHECK(!(etl::is_constructible<View, EtlData&&>::value));
      CHECK(!(etl::is_constructible<View, StlData&&>::value));
#endif
    }
  }
} // namespace
