fstream_test.cpp 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169
  1. // fstream_test.cpp ------------------------------------------------------------------//
  2. // Copyright Beman Dawes 2002
  3. // Distributed under the Boost Software License, Version 1.0.
  4. // See http://www.boost.org/LICENSE_1_0.txt
  5. // Library home page: http://www.boost.org/libs/filesystem
  6. #include <boost/config/warning_disable.hpp>
  7. // See deprecated_test for tests of deprecated features
  8. #ifndef BOOST_FILESYSTEM_NO_DEPRECATED
  9. # define BOOST_FILESYSTEM_NO_DEPRECATED
  10. #endif
  11. #ifndef BOOST_SYSTEM_NO_DEPRECATED
  12. # define BOOST_SYSTEM_NO_DEPRECATED
  13. #endif
  14. #include <boost/filesystem/fstream.hpp>
  15. #include <boost/config.hpp>
  16. # if defined( BOOST_NO_STD_WSTRING )
  17. # error Configuration not supported: Boost.Filesystem V3 and later requires std::wstring support
  18. # endif
  19. #include <boost/filesystem/operations.hpp>
  20. #include <string>
  21. #include <iostream>
  22. #include <cstdio> // for std::remove
  23. #include <boost/filesystem/detail/utf8_codecvt_facet.hpp>
  24. namespace fs = boost::filesystem;
  25. #include <boost/config.hpp>
  26. #ifdef BOOST_NO_STDC_NAMESPACE
  27. namespace std { using ::remove; }
  28. #endif
  29. #include <boost/detail/lightweight_test.hpp>
  30. #include <boost/detail/lightweight_main.hpp>
  31. namespace
  32. {
  33. bool cleanup = true;
  34. void test(const fs::path & p)
  35. {
  36. fs::remove(p);
  37. {
  38. std::cout << " in test 1\n";
  39. fs::filebuf fb1;
  40. fb1.open(p, std::ios_base::out);
  41. BOOST_TEST(fb1.is_open());
  42. }
  43. {
  44. std::cout << " in test 2\n";
  45. fs::filebuf fb2;
  46. fb2.open(p, std::ios_base::in);
  47. BOOST_TEST(fb2.is_open());
  48. }
  49. {
  50. std::cout << " in test 3\n";
  51. fs::ifstream tfs(p);
  52. BOOST_TEST(tfs.is_open());
  53. }
  54. {
  55. std::cout << " in test 4\n";
  56. fs::ifstream tfs(p / p.filename()); // should fail
  57. BOOST_TEST(!tfs.is_open());
  58. }
  59. {
  60. std::cout << " in test 5\n";
  61. fs::ifstream tfs(p, std::ios_base::in);
  62. BOOST_TEST(tfs.is_open());
  63. }
  64. {
  65. std::cout << " in test 6\n";
  66. fs::ifstream tfs;
  67. tfs.open(p);
  68. BOOST_TEST(tfs.is_open());
  69. }
  70. {
  71. std::cout << " in test 7\n";
  72. fs::ifstream tfs;
  73. tfs.open(p, std::ios_base::in);
  74. BOOST_TEST(tfs.is_open());
  75. }
  76. {
  77. std::cout << " in test 8\n";
  78. fs::ofstream tfs(p);
  79. BOOST_TEST(tfs.is_open());
  80. }
  81. {
  82. std::cout << " in test 9\n";
  83. fs::ofstream tfs(p, std::ios_base::out);
  84. BOOST_TEST(tfs.is_open());
  85. }
  86. {
  87. std::cout << " in test 10\n";
  88. fs::ofstream tfs;
  89. tfs.open(p);
  90. BOOST_TEST(tfs.is_open());
  91. }
  92. {
  93. std::cout << " in test 11\n";
  94. fs::ofstream tfs;
  95. tfs.open(p, std::ios_base::out);
  96. BOOST_TEST(tfs.is_open());
  97. }
  98. {
  99. std::cout << " in test 12\n";
  100. fs::fstream tfs(p);
  101. BOOST_TEST(tfs.is_open());
  102. }
  103. {
  104. std::cout << " in test 13\n";
  105. fs::fstream tfs(p, std::ios_base::in|std::ios_base::out);
  106. BOOST_TEST(tfs.is_open());
  107. }
  108. {
  109. std::cout << " in test 14\n";
  110. fs::fstream tfs;
  111. tfs.open(p);
  112. BOOST_TEST(tfs.is_open());
  113. }
  114. {
  115. std::cout << " in test 15\n";
  116. fs::fstream tfs;
  117. tfs.open(p, std::ios_base::in|std::ios_base::out);
  118. BOOST_TEST(tfs.is_open());
  119. }
  120. if (cleanup)
  121. fs::remove(p);
  122. } // test
  123. } // unnamed namespace
  124. int cpp_main(int argc, char*[])
  125. {
  126. if (argc > 1) cleanup = false;
  127. std::cout << "BOOST_FILESYSTEM_C_STR defined as \""
  128. << BOOST_STRINGIZE(BOOST_FILESYSTEM_C_STR) << "\"\n";
  129. // test narrow characters
  130. std::cout << "narrow character tests:\n";
  131. test("narrow_fstream_test");
  132. // So that tests are run with known encoding, use Boost UTF-8 codecvt
  133. std::locale global_loc = std::locale();
  134. std::locale loc(global_loc, new fs::detail::utf8_codecvt_facet);
  135. fs::path::imbue(loc);
  136. // test with some wide characters
  137. // \u2780 is circled 1 against white background == e2 9e 80 in UTF-8
  138. // \u2781 is circled 2 against white background == e2 9e 81 in UTF-8
  139. // \u263A is a white smiling face
  140. std::cout << "\nwide character tests:\n";
  141. std::wstring ws(L"wide_fstream_test_");
  142. ws += 0x2780;
  143. ws += 0x263A;
  144. test(ws);
  145. return ::boost::report_errors();
  146. }