string_ctor.pass.cpp 2.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103
  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. // test bitset(string, pos, n, zero, one);
  9. #include <bitset>
  10. #include <cassert>
  11. #include <algorithm> // for 'min' and 'max'
  12. #include <stdexcept> // for 'invalid_argument'
  13. #include "test_macros.h"
  14. #if defined(TEST_COMPILER_C1XX)
  15. #pragma warning(disable: 6294) // Ill-defined for-loop: initial condition does not satisfy test. Loop body not executed.
  16. #endif
  17. template <std::size_t N>
  18. void test_string_ctor()
  19. {
  20. #ifndef TEST_HAS_NO_EXCEPTIONS
  21. {
  22. try {
  23. std::string str("xxx1010101010xxxx");
  24. std::bitset<N> v(str, str.size()+1, 10);
  25. assert(false);
  26. }
  27. catch (std::out_of_range&)
  28. {
  29. }
  30. }
  31. {
  32. try {
  33. std::string str("xxx1010101010xxxx");
  34. std::bitset<N> v(str, 2, 10);
  35. assert(false);
  36. }
  37. catch (std::invalid_argument&)
  38. {
  39. }
  40. }
  41. {
  42. try {
  43. std::string str("xxxbababababaxxxx");
  44. std::bitset<N> v(str, 2, 10, 'a', 'b');
  45. assert(false);
  46. }
  47. catch (std::invalid_argument&)
  48. {
  49. }
  50. }
  51. #endif // TEST_HAS_NO_EXCEPTIONS
  52. {
  53. std::string str("xxx1010101010xxxx");
  54. std::bitset<N> v(str, 3, 10);
  55. std::size_t M = std::min<std::size_t>(N, 10);
  56. for (std::size_t i = 0; i < M; ++i)
  57. assert(v[i] == (str[3 + M - 1 - i] == '1'));
  58. for (std::size_t i = 10; i < N; ++i)
  59. assert(v[i] == false);
  60. }
  61. {
  62. std::string str("xxxbababababaxxxx");
  63. std::bitset<N> v(str, 3, 10, 'a', 'b');
  64. std::size_t M = std::min<std::size_t>(N, 10);
  65. for (std::size_t i = 0; i < M; ++i)
  66. assert(v[i] == (str[3 + M - 1 - i] == 'b'));
  67. for (std::size_t i = 10; i < N; ++i)
  68. assert(v[i] == false);
  69. }
  70. }
  71. struct Nonsense {
  72. virtual ~Nonsense() {}
  73. };
  74. void test_for_non_eager_instantiation() {
  75. // Ensure we don't accidentally instantiate `std::basic_string<Nonsense>`
  76. // since it may not be well formed and can cause an error in the
  77. // non-immediate context.
  78. static_assert(!std::is_constructible<std::bitset<3>, Nonsense*>::value, "");
  79. static_assert(!std::is_constructible<std::bitset<3>, Nonsense*, size_t, Nonsense&, Nonsense&>::value, "");
  80. }
  81. int main(int, char**)
  82. {
  83. test_string_ctor<0>();
  84. test_string_ctor<1>();
  85. test_string_ctor<31>();
  86. test_string_ctor<32>();
  87. test_string_ctor<33>();
  88. test_string_ctor<63>();
  89. test_string_ctor<64>();
  90. test_string_ctor<65>();
  91. test_string_ctor<1000>();
  92. test_for_non_eager_instantiation();
  93. return 0;
  94. }