as_writeable_bytes.pass.cpp 2.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. // -*- C++ -*-
  2. //===------------------------------ span ---------------------------------===//
  3. //
  4. // The LLVM Compiler Infrastructure
  5. //
  6. // This file is dual licensed under the MIT and the University of Illinois Open
  7. // Source Licenses. See LICENSE.TXT for details.
  8. //
  9. //===---------------------------------------------------------------------===//
  10. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  11. // <span>
  12. // template <class ElementType, ptrdiff_t Extent>
  13. // span<byte,
  14. // Extent == dynamic_extent
  15. // ? dynamic_extent
  16. // : static_cast<ptrdiff_t>(sizeof(ElementType)) * Extent>
  17. // as_writeable_bytes(span<ElementType, Extent> s) noexcept;
  18. #include <span>
  19. #include <cassert>
  20. #include <string>
  21. #include "test_macros.h"
  22. template<typename Span>
  23. void testRuntimeSpan(Span sp)
  24. {
  25. ASSERT_NOEXCEPT(std::as_writeable_bytes(sp));
  26. auto spBytes = std::as_writeable_bytes(sp);
  27. using SB = decltype(spBytes);
  28. ASSERT_SAME_TYPE(std::byte, typename SB::element_type);
  29. if (sp.extent == std::dynamic_extent)
  30. assert(spBytes.extent == std::dynamic_extent);
  31. else
  32. assert(spBytes.extent == static_cast<std::ptrdiff_t>(sizeof(typename Span::element_type)) * sp.extent);
  33. assert(static_cast<void*>(spBytes.data()) == static_cast<void*>(sp.data()));
  34. assert(spBytes.size() == sp.size_bytes());
  35. }
  36. struct A{};
  37. int iArr2[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  38. int main ()
  39. {
  40. testRuntimeSpan(std::span<int> ());
  41. testRuntimeSpan(std::span<long> ());
  42. testRuntimeSpan(std::span<double> ());
  43. testRuntimeSpan(std::span<A> ());
  44. testRuntimeSpan(std::span<std::string>());
  45. testRuntimeSpan(std::span<int, 0> ());
  46. testRuntimeSpan(std::span<long, 0> ());
  47. testRuntimeSpan(std::span<double, 0> ());
  48. testRuntimeSpan(std::span<A, 0> ());
  49. testRuntimeSpan(std::span<std::string, 0>());
  50. testRuntimeSpan(std::span<int>(iArr2, 1));
  51. testRuntimeSpan(std::span<int>(iArr2, 2));
  52. testRuntimeSpan(std::span<int>(iArr2, 3));
  53. testRuntimeSpan(std::span<int>(iArr2, 4));
  54. testRuntimeSpan(std::span<int>(iArr2, 5));
  55. testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1));
  56. testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2));
  57. testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3));
  58. testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4));
  59. testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5));
  60. std::string s;
  61. testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0));
  62. testRuntimeSpan(std::span<std::string>(&s, 1));
  63. }