ssize.pass.cpp 4.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120
  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. // UNSUPPORTED: c++98, c++03, c++11, c++14, c++17
  9. // <iterator>
  10. // template <class C> constexpr auto ssize(const C& c)
  11. // -> common_type_t<ptrdiff_t, make_signed_t<decltype(c.size())>>; // C++20
  12. // template <class T, ptrdiff_t> constexpr ptrdiff_t ssize(const T (&array)[N]) noexcept; // C++20
  13. #include <iterator>
  14. #include <cassert>
  15. #include <vector>
  16. #include <array>
  17. #include <list>
  18. #include <initializer_list>
  19. #include <string_view>
  20. #include "test_macros.h"
  21. struct short_container {
  22. uint16_t size() const { return 60000; } // not noexcept
  23. };
  24. template<typename C>
  25. void test_container(C& c)
  26. {
  27. // Can't say noexcept here because the container might not be
  28. static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
  29. assert ( std::ssize(c) == static_cast<decltype(std::ssize(c))>(c.size()));
  30. }
  31. template<typename C>
  32. void test_const_container(const C& c)
  33. {
  34. // Can't say noexcept here because the container might not be
  35. static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
  36. assert ( std::ssize(c) == static_cast<decltype(std::ssize(c))>(c.size()));
  37. }
  38. template<typename T>
  39. void test_const_container(const std::initializer_list<T>& c)
  40. {
  41. LIBCPP_ASSERT_NOEXCEPT(std::ssize(c)); // our std::ssize is conditionally noexcept
  42. static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
  43. assert ( std::ssize(c) == static_cast<decltype(std::ssize(c))>(c.size()));
  44. }
  45. template<typename T>
  46. void test_container(std::initializer_list<T>& c)
  47. {
  48. LIBCPP_ASSERT_NOEXCEPT(std::ssize(c)); // our std::ssize is conditionally noexcept
  49. static_assert( std::is_signed_v<decltype(std::ssize(c))>, "");
  50. assert ( std::ssize(c) == static_cast<decltype(std::ssize(c))>(c.size()));
  51. }
  52. template<typename T, size_t Sz>
  53. void test_const_array(const T (&array)[Sz])
  54. {
  55. ASSERT_NOEXCEPT(std::ssize(array));
  56. static_assert( std::is_signed_v<decltype(std::ssize(array))>, "");
  57. assert ( std::ssize(array) == Sz );
  58. }
  59. int main(int, char**)
  60. {
  61. std::vector<int> v; v.push_back(1);
  62. std::list<int> l; l.push_back(2);
  63. std::array<int, 1> a; a[0] = 3;
  64. std::initializer_list<int> il = { 4 };
  65. test_container ( v );
  66. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(v)));
  67. test_container ( l );
  68. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(l)));
  69. test_container ( a );
  70. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(a)));
  71. test_container ( il );
  72. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(il)));
  73. test_const_container ( v );
  74. test_const_container ( l );
  75. test_const_container ( a );
  76. test_const_container ( il );
  77. std::string_view sv{"ABC"};
  78. test_container ( sv );
  79. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(sv)));
  80. test_const_container ( sv );
  81. static constexpr int arrA [] { 1, 2, 3 };
  82. ASSERT_SAME_TYPE(ptrdiff_t, decltype(std::ssize(arrA)));
  83. static_assert( std::is_signed_v<decltype(std::ssize(arrA))>, "");
  84. test_const_array ( arrA );
  85. // From P1227R2:
  86. // Note that the code does not just return the std::make_signed variant of
  87. // the container's size() method, because it's conceivable that a container
  88. // might choose to represent its size as a uint16_t, supporting up to
  89. // 65,535 elements, and it would be a disaster for std::ssize() to turn a
  90. // size of 60,000 into a size of -5,536.
  91. short_container sc;
  92. // is the return type signed? Is it big enough to hold 60K?
  93. // is the "signed version" of sc.size() too small?
  94. static_assert( std::is_signed_v< decltype(std::ssize(sc))>, "");
  95. static_assert( std::numeric_limits< decltype(std::ssize(sc))>::max() > 60000, "");
  96. static_assert( std::numeric_limits<std::make_signed_t<decltype(std:: size(sc))>>::max() < 60000, "");
  97. assert (std::ssize(sc) == 60000);
  98. LIBCPP_ASSERT_NOT_NOEXCEPT(std::ssize(sc));
  99. return 0;
  100. }