performance_codecvt_null.cpp 3.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  1. /////////1/////////2/////////3/////////4/////////5/////////6/////////7/////////8
  2. // test_codecvt_null.cpp
  3. // (C) Copyright 2002 Robert Ramey - http://www.rrsd.com .
  4. // Use, modification and distribution is subject to the Boost Software
  5. // License, Version 1.0. (See accompanying file LICENSE_1_0.txt or copy at
  6. // http://www.boost.org/LICENSE_1_0.txt)
  7. // should pass compilation and execution. Note: compilation with compilers
  8. // which use wchar_t as 2 byte objects will emit warnings. These should be
  9. // ignored.
  10. #include <algorithm>
  11. #include <fstream>
  12. #include <iostream>
  13. #include <iterator>
  14. #include <locale>
  15. #include <vector>
  16. #include <cstddef>
  17. #include <cstdio> // remove
  18. #include <boost/config.hpp>
  19. #if defined(BOOST_NO_STDC_NAMESPACE)
  20. namespace std{
  21. using ::remove;
  22. }
  23. #endif
  24. #include "test_tools.hpp"
  25. #include <boost/archive/add_facet.hpp>
  26. #include <boost/archive/codecvt_null.hpp>
  27. #include <boost/archive/iterators/ostream_iterator.hpp>
  28. #include <boost/archive/iterators/istream_iterator.hpp>
  29. template<std::size_t S>
  30. struct test_data
  31. {
  32. static wchar_t wchar_encoding[];
  33. };
  34. template<>
  35. wchar_t test_data<2>::wchar_encoding[] = {
  36. 0x0001,
  37. 0x007f,
  38. 0x0080,
  39. 0x07ff,
  40. 0x0800,
  41. 0x7fff
  42. };
  43. template<>
  44. wchar_t test_data<4>::wchar_encoding[] = {
  45. 0x00000001,
  46. 0x0000007f,
  47. 0x00000080,
  48. 0x000007ff,
  49. 0x00000800,
  50. 0x0000ffff,
  51. 0x00010000,
  52. 0x0010ffff,
  53. 0x001fffff,
  54. 0x00200000,
  55. 0x03ffffff,
  56. 0x04000000,
  57. 0x7fffffff
  58. };
  59. #include <iostream>
  60. int test_main( int /* argc */, char* /* argv */[] ) {
  61. const char * testfile = boost::archive::tmpnam(NULL);
  62. BOOST_REQUIRE(NULL != testfile);
  63. std::locale old_loc;
  64. std::locale * null_locale =
  65. boost::archive::add_facet(old_loc, new boost::archive::codecvt_null<wchar_t>);
  66. typedef test_data<sizeof(wchar_t)> td;
  67. {
  68. std::wofstream ofs;
  69. ofs.imbue(*null_locale);
  70. ofs.open(testfile, std::ios::binary);
  71. std::copy(
  72. td::wchar_encoding,
  73. #if ! defined(__BORLANDC__)
  74. // borland 5.60 complains about this
  75. td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
  76. #else
  77. // so use this instead
  78. td::wchar_encoding + 6,
  79. #endif
  80. boost::archive::iterators::ostream_iterator<wchar_t>(ofs)
  81. );
  82. }
  83. bool ok = false;
  84. {
  85. std::wifstream ifs;
  86. ifs.imbue(*null_locale);
  87. ifs.open(testfile, std::ios::binary);
  88. ok = std::equal(
  89. td::wchar_encoding,
  90. #if ! defined(__BORLANDC__)
  91. // borland 5.60 complains about this
  92. td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
  93. #else
  94. // so use this instead
  95. td::wchar_encoding + 6,
  96. #endif
  97. boost::archive::iterators::istream_iterator<wchar_t>(ifs)
  98. );
  99. }
  100. BOOST_CHECK(ok);
  101. {
  102. std::wofstream ofs("testfile2");
  103. ofs.imbue(*null_locale);
  104. int i = 10;
  105. ofs << i;
  106. ofs.close();
  107. std::wifstream ifs("testfile2");
  108. ifs.imbue(*null_locale);
  109. int i2;
  110. ifs >> i2;
  111. std::cout << "i=" << i << std::endl;
  112. std::cout << "i2=" << i2 << std::endl;
  113. BOOST_CHECK(i == i2);
  114. ifs.close();
  115. }
  116. delete null_locale;
  117. std::remove(testfile);
  118. return EXIT_SUCCESS;
  119. }