remove_prefix.pass.cpp 1.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778
  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 remove_prefix(size_type _n)
  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. assert ( sv1.size() == len );
  19. assert ( sv1.data() == s );
  20. if ( len > 0 ) {
  21. sv1.remove_prefix ( 1 );
  22. assert ( sv1.size() == (len - 1));
  23. assert ( sv1.data() == (s + 1));
  24. sv1.remove_prefix ( len - 1 );
  25. }
  26. assert ( sv1.size() == 0 );
  27. sv1.remove_prefix ( 0 );
  28. assert ( sv1.size() == 0 );
  29. }
  30. }
  31. #if TEST_STD_VER > 11
  32. constexpr size_t test_ce ( size_t n, size_t k ) {
  33. typedef std::basic_string_view<char> SV;
  34. SV sv1{ "ABCDEFGHIJKL", n };
  35. sv1.remove_prefix ( k );
  36. return sv1.size();
  37. }
  38. #endif
  39. int main(int, char**) {
  40. test ( "ABCDE", 5 );
  41. test ( "a", 1 );
  42. test ( "", 0 );
  43. test ( L"ABCDE", 5 );
  44. test ( L"a", 1 );
  45. test ( L"", 0 );
  46. #if TEST_STD_VER >= 11
  47. test ( u"ABCDE", 5 );
  48. test ( u"a", 1 );
  49. test ( u"", 0 );
  50. test ( U"ABCDE", 5 );
  51. test ( U"a", 1 );
  52. test ( U"", 0 );
  53. #endif
  54. #if TEST_STD_VER > 11
  55. {
  56. static_assert ( test_ce ( 5, 0 ) == 5, "" );
  57. static_assert ( test_ce ( 5, 1 ) == 4, "" );
  58. static_assert ( test_ce ( 5, 5 ) == 0, "" );
  59. static_assert ( test_ce ( 9, 3 ) == 6, "" );
  60. }
  61. #endif
  62. return 0;
  63. }