swap.pass.cpp 1.6 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677
  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. // <string_view>
  9. // void swap(basic_string_view& _other) noexcept
  10. #include <string_view>
  11. #include <cassert>
  12. #include "test_macros.h"
  13. template<typename CharT>
  14. void test ( const CharT *s, size_t len ) {
  15. typedef std::basic_string_view<CharT> SV;
  16. {
  17. SV sv1(s);
  18. SV sv2;
  19. assert ( sv1.size() == len );
  20. assert ( sv1.data() == s );
  21. assert ( sv2.size() == 0 );
  22. sv1.swap ( sv2 );
  23. assert ( sv1.size() == 0 );
  24. assert ( sv2.size() == len );
  25. assert ( sv2.data() == s );
  26. }
  27. }
  28. #if TEST_STD_VER > 11
  29. constexpr size_t test_ce ( size_t n, size_t k ) {
  30. typedef std::basic_string_view<char> SV;
  31. SV sv1{ "ABCDEFGHIJKL", n };
  32. SV sv2 { sv1.data(), k };
  33. sv1.swap ( sv2 );
  34. return sv1.size();
  35. }
  36. #endif
  37. int main(int, char**) {
  38. test ( "ABCDE", 5 );
  39. test ( "a", 1 );
  40. test ( "", 0 );
  41. test ( L"ABCDE", 5 );
  42. test ( L"a", 1 );
  43. test ( L"", 0 );
  44. #if TEST_STD_VER >= 11
  45. test ( u"ABCDE", 5 );
  46. test ( u"a", 1 );
  47. test ( u"", 0 );
  48. test ( U"ABCDE", 5 );
  49. test ( U"a", 1 );
  50. test ( U"", 0 );
  51. #endif
  52. #if TEST_STD_VER > 11
  53. {
  54. static_assert ( test_ce (2, 3) == 3, "" );
  55. static_assert ( test_ce (5, 3) == 3, "" );
  56. static_assert ( test_ce (0, 1) == 1, "" );
  57. }
  58. #endif
  59. return 0;
  60. }