size_bytes.pass.cpp 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. // constexpr index_type size_bytes() const noexcept;
  13. //
  14. // Effects: Equivalent to: return size() * sizeof(element_type);
  15. #include <span>
  16. #include <cassert>
  17. #include <string>
  18. #include "test_macros.h"
  19. template <typename Span>
  20. constexpr bool testConstexprSpan(Span sp, ptrdiff_t sz)
  21. {
  22. ASSERT_NOEXCEPT(sp.size_bytes());
  23. return (size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type);
  24. }
  25. template <typename Span>
  26. void testRuntimeSpan(Span sp, ptrdiff_t sz)
  27. {
  28. ASSERT_NOEXCEPT(sp.size_bytes());
  29. assert((size_t) sp.size_bytes() == sz * sizeof(typename Span::element_type));
  30. }
  31. struct A{};
  32. constexpr int iArr1[] = { 0, 1, 2, 3, 4, 5, 6, 7, 8, 9};
  33. int iArr2[] = {10, 11, 12, 13, 14, 15, 16, 17, 18, 19};
  34. int main ()
  35. {
  36. static_assert(testConstexprSpan(std::span<int>(), 0), "");
  37. static_assert(testConstexprSpan(std::span<long>(), 0), "");
  38. static_assert(testConstexprSpan(std::span<double>(), 0), "");
  39. static_assert(testConstexprSpan(std::span<A>(), 0), "");
  40. static_assert(testConstexprSpan(std::span<std::string>(), 0), "");
  41. static_assert(testConstexprSpan(std::span<int, 0>(), 0), "");
  42. static_assert(testConstexprSpan(std::span<long, 0>(), 0), "");
  43. static_assert(testConstexprSpan(std::span<double, 0>(), 0), "");
  44. static_assert(testConstexprSpan(std::span<A, 0>(), 0), "");
  45. static_assert(testConstexprSpan(std::span<std::string, 0>(), 0), "");
  46. static_assert(testConstexprSpan(std::span<const int>(iArr1, 1), 1), "");
  47. static_assert(testConstexprSpan(std::span<const int>(iArr1, 2), 2), "");
  48. static_assert(testConstexprSpan(std::span<const int>(iArr1, 3), 3), "");
  49. static_assert(testConstexprSpan(std::span<const int>(iArr1, 4), 4), "");
  50. static_assert(testConstexprSpan(std::span<const int>(iArr1, 5), 5), "");
  51. testRuntimeSpan(std::span<int> (), 0);
  52. testRuntimeSpan(std::span<long> (), 0);
  53. testRuntimeSpan(std::span<double> (), 0);
  54. testRuntimeSpan(std::span<A> (), 0);
  55. testRuntimeSpan(std::span<std::string>(), 0);
  56. testRuntimeSpan(std::span<int, 0> (), 0);
  57. testRuntimeSpan(std::span<long, 0> (), 0);
  58. testRuntimeSpan(std::span<double, 0> (), 0);
  59. testRuntimeSpan(std::span<A, 0> (), 0);
  60. testRuntimeSpan(std::span<std::string, 0>(), 0);
  61. testRuntimeSpan(std::span<int>(iArr2, 1), 1);
  62. testRuntimeSpan(std::span<int>(iArr2, 2), 2);
  63. testRuntimeSpan(std::span<int>(iArr2, 3), 3);
  64. testRuntimeSpan(std::span<int>(iArr2, 4), 4);
  65. testRuntimeSpan(std::span<int>(iArr2, 5), 5);
  66. testRuntimeSpan(std::span<int, 1>(iArr2 + 5, 1), 1);
  67. testRuntimeSpan(std::span<int, 2>(iArr2 + 4, 2), 2);
  68. testRuntimeSpan(std::span<int, 3>(iArr2 + 3, 3), 3);
  69. testRuntimeSpan(std::span<int, 4>(iArr2 + 2, 4), 4);
  70. testRuntimeSpan(std::span<int, 5>(iArr2 + 1, 5), 5);
  71. std::string s;
  72. testRuntimeSpan(std::span<std::string>(&s, (std::ptrdiff_t) 0), 0);
  73. testRuntimeSpan(std::span<std::string>(&s, 1), 1);
  74. }