remove_suffix.pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  1. //===----------------------------------------------------------------------===//
  2. //
  3. // The LLVM Compiler Infrastructure
  4. //
  5. // This file is dual licensed under the MIT and the University of Illinois Open
  6. // Source Licenses. See LICENSE.TXT for details.
  7. //
  8. //===----------------------------------------------------------------------===//
  9. // <string_view>
  10. // void remove_suffix(size_type _n)
  11. #include <string_view>
  12. #include <cassert>
  13. #include "test_macros.h"
  14. template<typename CharT>
  15. void test ( const CharT *s, size_t len ) {
  16. typedef std::basic_string_view<CharT> SV;
  17. {
  18. SV sv1 ( s );
  19. assert ( sv1.size() == len );
  20. assert ( sv1.data() == s );
  21. if ( len > 0 ) {
  22. sv1.remove_suffix ( 1 );
  23. assert ( sv1.size() == (len - 1));
  24. assert ( sv1.data() == s);
  25. sv1.remove_suffix ( len - 1 );
  26. }
  27. assert ( sv1.size() == 0 );
  28. sv1.remove_suffix ( 0 );
  29. assert ( sv1.size() == 0 );
  30. }
  31. }
  32. #if _LIBCPP_STD_VER > 11
  33. constexpr size_t test_ce ( size_t n, size_t k ) {
  34. typedef std::basic_string_view<char> SV;
  35. SV sv1{ "ABCDEFGHIJKL", n };
  36. sv1.remove_suffix ( k );
  37. return sv1.size();
  38. }
  39. #endif
  40. int main () {
  41. test ( "ABCDE", 5 );
  42. test ( "a", 1 );
  43. test ( "", 0 );
  44. test ( L"ABCDE", 5 );
  45. test ( L"a", 1 );
  46. test ( L"", 0 );
  47. #if TEST_STD_VER >= 11
  48. test ( u"ABCDE", 5 );
  49. test ( u"a", 1 );
  50. test ( u"", 0 );
  51. test ( U"ABCDE", 5 );
  52. test ( U"a", 1 );
  53. test ( U"", 0 );
  54. #endif
  55. #if _LIBCPP_STD_VER > 11
  56. {
  57. static_assert ( test_ce ( 5, 0 ) == 5, "" );
  58. static_assert ( test_ce ( 5, 1 ) == 4, "" );
  59. static_assert ( test_ce ( 5, 5 ) == 0, "" );
  60. static_assert ( test_ce ( 9, 3 ) == 6, "" );
  61. }
  62. #endif
  63. }