test_codecvt_null.cpp 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124
  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> // std::copy
  11. #include <fstream>
  12. #include <iostream>
  13. #include <iterator>
  14. #include <locale>
  15. #include <vector>
  16. #include <cstdio> // remove
  17. #include <cstddef> // NULL, size_t
  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/codecvt_null.hpp>
  26. #include <boost/archive/iterators/ostream_iterator.hpp>
  27. #include <boost/archive/iterators/istream_iterator.hpp>
  28. template<std::size_t S>
  29. struct test_data
  30. {
  31. static wchar_t wchar_encoding[];
  32. };
  33. template<>
  34. wchar_t test_data<2>::wchar_encoding[] = {
  35. (wchar_t) 0x0001,
  36. (wchar_t) 0x007f,
  37. (wchar_t) 0x0080,
  38. (wchar_t) 0x07ff,
  39. (wchar_t) 0x0800,
  40. (wchar_t) 0x7fff
  41. };
  42. template<>
  43. wchar_t test_data<4>::wchar_encoding[] = {
  44. (wchar_t) 0x00000001,
  45. (wchar_t) 0x0000007f,
  46. (wchar_t) 0x00000080,
  47. (wchar_t) 0x000007ff,
  48. (wchar_t) 0x00000800,
  49. (wchar_t) 0x0000ffff,
  50. (wchar_t) 0x00010000,
  51. (wchar_t) 0x0010ffff,
  52. (wchar_t) 0x001fffff,
  53. (wchar_t) 0x00200000,
  54. (wchar_t) 0x03ffffff,
  55. (wchar_t) 0x04000000,
  56. (wchar_t) 0x7fffffff
  57. };
  58. #include <iostream>
  59. int test_main( int /* argc */, char* /* argv */[] ) {
  60. const char * testfile = boost::archive::tmpnam(NULL);
  61. BOOST_REQUIRE(NULL != testfile);
  62. std::locale old_loc;
  63. std::locale null_locale = std::locale(
  64. old_loc,
  65. new boost::archive::codecvt_null<wchar_t>
  66. );
  67. typedef test_data<sizeof(wchar_t)> td;
  68. {
  69. std::wofstream ofs;
  70. ofs.imbue(null_locale);
  71. ofs.open(testfile, std::ios::binary);
  72. std::copy(
  73. td::wchar_encoding,
  74. td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
  75. boost::archive::iterators::ostream_iterator<wchar_t>(ofs)
  76. );
  77. }
  78. bool ok = false;
  79. {
  80. std::wifstream ifs;
  81. ifs.imbue(null_locale);
  82. ifs.open(testfile, std::ios::binary);
  83. ok = std::equal(
  84. td::wchar_encoding,
  85. td::wchar_encoding + sizeof(td::wchar_encoding)/sizeof(wchar_t),
  86. boost::archive::iterators::istream_iterator<wchar_t>(ifs)
  87. );
  88. }
  89. BOOST_CHECK(ok);
  90. {
  91. std::wofstream ofs("testfile2");
  92. ofs.imbue(null_locale);
  93. int i = 10;
  94. ofs << i;
  95. ofs.close();
  96. std::wifstream ifs("testfile2");
  97. ifs.imbue(null_locale);
  98. int i2;
  99. ifs >> i2;
  100. std::cout << "i=" << i << std::endl;
  101. std::cout << "i2=" << i2 << std::endl;
  102. BOOST_CHECK(i == i2);
  103. ifs.close();
  104. }
  105. std::remove(testfile);
  106. return EXIT_SUCCESS;
  107. }