1.cc 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132
  1. // { dg-options "-std=gnu++17" }
  2. // Copyright (C) 2013-2022 Free Software Foundation, Inc.
  3. //
  4. // This file is part of the GNU ISO C++ Library. This library is free
  5. // software; you can redistribute it and/or modify it under the
  6. // terms of the GNU General Public License as published by the
  7. // Free Software Foundation; either version 3, or (at your option)
  8. // any later version.
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  12. // GNU General Public License for more details.
  13. // You should have received a copy of the GNU General Public License along
  14. // with this library; see the file COPYING3. If not see
  15. // <http://www.gnu.org/licenses/>.
  16. // basic_string_view::compare
  17. // int compare(const basic_string_view& str) const;
  18. // int compare(size_type pos1, size_type n1, const basic_string_view& str) const;
  19. // int compare(size_type pos1, size_type n1, const basic_string_view& str,
  20. // size_type pos2, size_type n2) const;
  21. // int compare(const charT* s) const;
  22. // int compare(size_type pos1, size_type n1,
  23. // const charT* s, size_type n2 = npos) const;
  24. // NB compare should be thought of as a lexographical compare, ie how
  25. // things would be sorted in a dictionary.
  26. namespace operations_compare_1 {
  27. enum want_value {lt=0, z=1, gt=2};
  28. int
  29. test_value(int result, want_value expected);
  30. int
  31. test_value(int result, want_value expected)
  32. {
  33. bool pass = false;
  34. switch (expected) {
  35. case lt:
  36. if (result < 0)
  37. pass = true;
  38. break;
  39. case z:
  40. if (!result)
  41. pass = true;
  42. break;
  43. case gt:
  44. if (result > 0)
  45. pass = true;
  46. break;
  47. default:
  48. pass = false; //should not get here
  49. }
  50. VERIFY(pass);
  51. return 0;
  52. }
  53. static int
  54. test01 ()
  55. {
  56. using gdb::string_view;
  57. string_view str_0("costa rica");
  58. string_view str_1("costa marbella");
  59. string_view str_2;
  60. //sanity check
  61. test_value(strcmp("costa marbella", "costa rica"), lt);
  62. test_value(strcmp("costa rica", "costa rica"), z);
  63. test_value(strcmp(str_1.data(), str_0.data()), lt);
  64. test_value(strcmp(str_0.data(), str_1.data()), gt);
  65. test_value(strncmp(str_1.data(), str_0.data(), 6), z);
  66. test_value(strncmp(str_1.data(), str_0.data(), 14), lt);
  67. test_value(memcmp(str_1.data(), str_0.data(), 6), z);
  68. test_value(memcmp(str_1.data(), str_0.data(), 10), lt);
  69. test_value(memcmp("costa marbella", "costa rica", 10), lt);
  70. // int compare(const basic_string_view& str) const;
  71. test_value(str_0.compare(str_1), gt); //because r>m
  72. test_value(str_1.compare(str_0), lt); //because m<r
  73. str_2 = str_0;
  74. test_value(str_2.compare(str_0), z);
  75. str_2 = "cost";
  76. test_value(str_2.compare(str_0), lt);
  77. str_2 = "costa ricans";
  78. test_value(str_2.compare(str_0), gt);
  79. // int compare(size_type pos1, size_type n1, const basic_string_view& str) const;
  80. test_value(str_1.compare(0, 6, str_0), lt);
  81. str_2 = "cost";
  82. test_value(str_1.compare(0, 4, str_2), z);
  83. test_value(str_1.compare(0, 5, str_2), gt);
  84. // int compare(size_type pos1, size_type n1, const basic_string_view& str,
  85. // size_type pos2, size_type n2) const;
  86. test_value(str_1.compare(0, 6, str_0, 0, 6), z);
  87. test_value(str_1.compare(0, 7, str_0, 0, 7), lt);
  88. test_value(str_0.compare(0, 7, str_1, 0, 7), gt);
  89. // int compare(const charT* s) const;
  90. test_value(str_0.compare("costa marbella"), gt);
  91. test_value(str_1.compare("costa rica"), lt);
  92. str_2 = str_0;
  93. test_value(str_2.compare("costa rica"), z);
  94. test_value(str_2.compare("cost"), gt);
  95. test_value(str_2.compare("costa ricans"), lt);
  96. // int compare(size_type pos, size_type n1, const charT* str,
  97. // size_type n2 = npos) const;
  98. test_value(str_1.compare(0, 6, "costa rica", 0, 6), z);
  99. test_value(str_1.compare(0, 7, "costa rica", 0, 7), lt);
  100. test_value(str_0.compare(0, 7, "costa marbella", 0, 7), gt);
  101. return 0;
  102. }
  103. static int
  104. main ()
  105. {
  106. test01();
  107. return 0;
  108. }
  109. } // namespace operations_compare_1