unicode.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  1. // { dg-do run { target c++11 } }
  2. // { dg-require-iconv "ISO-8859-1" }
  3. // Copyright (C) 2006-2022 Free Software Foundation, Inc.
  4. //
  5. // This file is part of the GNU ISO C++ Library. This library is free
  6. // software; you can redistribute it and/or modify it under the
  7. // terms of the GNU General Public License as published by the
  8. // Free Software Foundation; either version 3, or (at your option)
  9. // any later version.
  10. // This library is distributed in the hope that it will be useful,
  11. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  12. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  13. // GNU General Public License for more details.
  14. // You should have received a copy of the GNU General Public License along
  15. // with this library; see the file COPYING3. If not see
  16. // <http://www.gnu.org/licenses/>.
  17. // 22.1.1.2 locale constructors and destructors [lib.locale.cons]
  18. #include <cwchar> // for mbstate_t
  19. #include <locale>
  20. #include <stdexcept>
  21. #include <typeinfo>
  22. #include <testsuite_hooks.h>
  23. #include <ext/codecvt_specializations.h>
  24. typedef std::codecvt<char, char, std::mbstate_t> c_codecvt;
  25. #ifdef _GLIBCXX_USE_WCHAR_T
  26. typedef std::codecvt<wchar_t, char, std::mbstate_t> w_codecvt;
  27. #endif
  28. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  29. typedef std::codecvt<char16_t, char, std::mbstate_t> u16_codecvt;
  30. typedef std::codecvt<char32_t, char, std::mbstate_t> u32_codecvt;
  31. #ifdef _GLIBCXX_USE_CHAR8_T
  32. typedef std::codecvt<char16_t, char8_t, std::mbstate_t> u16u8_codecvt;
  33. typedef std::codecvt<char32_t, char8_t, std::mbstate_t> u32u8_codecvt;
  34. #endif
  35. #endif
  36. class gnu_facet: public std::locale::facet
  37. {
  38. public:
  39. static std::locale::id id;
  40. };
  41. std::locale::id gnu_facet::id;
  42. void test01()
  43. {
  44. using namespace std;
  45. typedef unsigned short int_type;
  46. typedef char ext_type;
  47. typedef __gnu_cxx::encoding_state state_type;
  48. typedef codecvt<int_type, ext_type, state_type> unicode_codecvt;
  49. // unicode_codecvt
  50. locale loc01(locale::classic());
  51. locale loc13(locale::classic(), new unicode_codecvt);
  52. VERIFY( loc01 != loc13 );
  53. VERIFY( loc13.name() == "*" );
  54. try
  55. {
  56. VERIFY( has_facet<c_codecvt>(loc13) );
  57. #ifdef _GLIBCXX_USE_WCHAR_T
  58. VERIFY( has_facet<w_codecvt>(loc13) );
  59. #endif
  60. #ifdef _GLIBCXX_USE_C99_STDINT_TR1
  61. VERIFY( has_facet<u16_codecvt>(loc13) );
  62. VERIFY( has_facet<u32_codecvt>(loc13) );
  63. #ifdef _GLIBCXX_USE_CHAR8_T
  64. VERIFY( has_facet<u16u8_codecvt>(loc13) );
  65. VERIFY( has_facet<u32u8_codecvt>(loc13) );
  66. #endif
  67. #endif
  68. VERIFY( has_facet<unicode_codecvt>(loc13) );
  69. }
  70. catch(...)
  71. { VERIFY( false ); }
  72. try
  73. { use_facet<gnu_facet>(loc13); }
  74. catch(bad_cast& obj)
  75. { VERIFY( true ); }
  76. catch(...)
  77. { VERIFY( false ); }
  78. }
  79. int main()
  80. {
  81. test01();
  82. return 0;
  83. }