| 1 | // fstream_test.cpp --------------------------------------------------------// |
|---|
| 2 | |
|---|
| 3 | // Copyright Beman Dawes 2002. |
|---|
| 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 | |
|---|
| 8 | // See library home page at http://www.boost.org/libs/filesystem |
|---|
| 9 | |
|---|
| 10 | // VC++ 8.0 warns on various less-than-safe practices. |
|---|
| 11 | // See http://msdn.microsoft.com/msdnmag/issues/05/05/SafeCandC/default.aspx |
|---|
| 12 | // But at least in VC++ 8.0 betas, their own libraries use the problem |
|---|
| 13 | // practices. So turn off the warnings. |
|---|
| 14 | #define _CRT_SECURE_NO_DEPRECATE |
|---|
| 15 | #define _SCL_SECURE_NO_DEPRECATE |
|---|
| 16 | |
|---|
| 17 | #include <boost/filesystem/fstream.hpp> |
|---|
| 18 | #include <boost/filesystem/operations.hpp> |
|---|
| 19 | #include <string> |
|---|
| 20 | #include <iostream> |
|---|
| 21 | #include <cstdio> // for std::remove |
|---|
| 22 | |
|---|
| 23 | #include "../src/utf8_codecvt_facet.hpp" |
|---|
| 24 | |
|---|
| 25 | #ifndef BOOST_FILESYSTEM_NARROW_ONLY |
|---|
| 26 | # include "lpath.hpp" |
|---|
| 27 | #endif |
|---|
| 28 | |
|---|
| 29 | namespace fs = boost::filesystem; |
|---|
| 30 | |
|---|
| 31 | #include <boost/config.hpp> |
|---|
| 32 | #ifdef BOOST_NO_STDC_NAMESPACE |
|---|
| 33 | namespace std { using ::remove; } |
|---|
| 34 | #endif |
|---|
| 35 | |
|---|
| 36 | #include <boost/test/minimal.hpp> |
|---|
| 37 | |
|---|
| 38 | namespace |
|---|
| 39 | { |
|---|
| 40 | template< class Path > |
|---|
| 41 | void test( const Path & p ) |
|---|
| 42 | { |
|---|
| 43 | # if !BOOST_WORKAROUND( BOOST_MSVC, <= 1200 ) // VC++ 6.0 can't handle open |
|---|
| 44 | { |
|---|
| 45 | std::cout << " in test 1\n"; |
|---|
| 46 | fs::filebuf fb; |
|---|
| 47 | fb.open( p, std::ios_base::in ); |
|---|
| 48 | BOOST_CHECK( fb.is_open() == fs::exists( p ) ); |
|---|
| 49 | } |
|---|
| 50 | { |
|---|
| 51 | std::cout << " in test 2\n"; |
|---|
| 52 | fs::filebuf fb1; |
|---|
| 53 | fb1.open( p, std::ios_base::out ); |
|---|
| 54 | BOOST_CHECK( fb1.is_open() ); |
|---|
| 55 | } |
|---|
| 56 | { |
|---|
| 57 | std::cout << " in test 3\n"; |
|---|
| 58 | fs::filebuf fb2; |
|---|
| 59 | fb2.open( p, std::ios_base::in ); |
|---|
| 60 | BOOST_CHECK( fb2.is_open() ); |
|---|
| 61 | } |
|---|
| 62 | # else |
|---|
| 63 | std::cout << "<note>\n"; |
|---|
| 64 | std::cout << |
|---|
| 65 | "VC++6.0 does not support boost::filesystem open()\n"; |
|---|
| 66 | # endif |
|---|
| 67 | { |
|---|
| 68 | std::cout << " in test 4\n"; |
|---|
| 69 | fs::ifstream tfs( p ); |
|---|
| 70 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 71 | } |
|---|
| 72 | { |
|---|
| 73 | std::cout << " in test 4.1\n"; |
|---|
| 74 | fs::ifstream tfs( p / p.leaf() ); // should fail |
|---|
| 75 | BOOST_CHECK( !tfs.is_open() ); |
|---|
| 76 | } |
|---|
| 77 | { |
|---|
| 78 | std::cout << " in test 5\n"; |
|---|
| 79 | fs::ifstream tfs( p, std::ios_base::in ); |
|---|
| 80 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 81 | } |
|---|
| 82 | # if !BOOST_WORKAROUND( BOOST_MSVC, <= 1200 ) // VC++ 6.0 can't handle open |
|---|
| 83 | { |
|---|
| 84 | std::cout << " in test 6\n"; |
|---|
| 85 | fs::ifstream tfs; |
|---|
| 86 | tfs.open( p ); |
|---|
| 87 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 88 | } |
|---|
| 89 | { |
|---|
| 90 | std::cout << " in test 7\n"; |
|---|
| 91 | fs::ifstream tfs; |
|---|
| 92 | tfs.open( p, std::ios_base::in ); |
|---|
| 93 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 94 | } |
|---|
| 95 | # endif |
|---|
| 96 | { |
|---|
| 97 | std::cout << " in test 8\n"; |
|---|
| 98 | fs::ofstream tfs( p ); |
|---|
| 99 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 100 | } |
|---|
| 101 | { |
|---|
| 102 | std::cout << " in test 9\n"; |
|---|
| 103 | fs::ofstream tfs( p, std::ios_base::out ); |
|---|
| 104 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 105 | } |
|---|
| 106 | # if !BOOST_WORKAROUND( BOOST_MSVC, <= 1200 ) // VC++ 6.0 can't handle open |
|---|
| 107 | { |
|---|
| 108 | std::cout << " in test 10\n"; |
|---|
| 109 | fs::ofstream tfs; |
|---|
| 110 | tfs.open( p ); |
|---|
| 111 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 112 | } |
|---|
| 113 | { |
|---|
| 114 | std::cout << " in test 11\n"; |
|---|
| 115 | fs::ofstream tfs; |
|---|
| 116 | tfs.open( p, std::ios_base::out ); |
|---|
| 117 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 118 | } |
|---|
| 119 | # endif |
|---|
| 120 | { |
|---|
| 121 | std::cout << " in test 12\n"; |
|---|
| 122 | fs::fstream tfs( p ); |
|---|
| 123 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 124 | } |
|---|
| 125 | { |
|---|
| 126 | std::cout << " in test 13\n"; |
|---|
| 127 | fs::fstream tfs( p, std::ios_base::in|std::ios_base::out ); |
|---|
| 128 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 129 | } |
|---|
| 130 | # if !BOOST_WORKAROUND( BOOST_MSVC, <= 1200 ) // VC++ 6.0 can't handle open |
|---|
| 131 | { |
|---|
| 132 | std::cout << " in test 14\n"; |
|---|
| 133 | fs::fstream tfs; |
|---|
| 134 | tfs.open( p ); |
|---|
| 135 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 136 | } |
|---|
| 137 | { |
|---|
| 138 | std::cout << " in test 15\n"; |
|---|
| 139 | fs::fstream tfs; |
|---|
| 140 | tfs.open( p, std::ios_base::in|std::ios_base::out ); |
|---|
| 141 | BOOST_CHECK( tfs.is_open() ); |
|---|
| 142 | } |
|---|
| 143 | # endif |
|---|
| 144 | } // test |
|---|
| 145 | } // unnamed namespace |
|---|
| 146 | |
|---|
| 147 | int test_main( int, char*[] ) |
|---|
| 148 | { |
|---|
| 149 | |
|---|
| 150 | // test fs::path |
|---|
| 151 | std::cout << "path tests:\n"; |
|---|
| 152 | test( fs::path( "fstream_test_foo" ) ); |
|---|
| 153 | |
|---|
| 154 | #ifndef BOOST_FILESYSTEM_NARROW_ONLY |
|---|
| 155 | |
|---|
| 156 | // So that tests are run with known encoding, use Boost UTF-8 codecvt |
|---|
| 157 | std::locale global_loc = std::locale(); |
|---|
| 158 | std::locale loc( global_loc, new fs::detail::utf8_codecvt_facet ); |
|---|
| 159 | fs::wpath_traits::imbue( loc ); |
|---|
| 160 | |
|---|
| 161 | // test fs::wpath |
|---|
| 162 | // x2780 is circled 1 against white background == e2 9e 80 in UTF-8 |
|---|
| 163 | // x2781 is circled 2 against white background == e2 9e 81 in UTF-8 |
|---|
| 164 | std::cout << "\nwpath tests:\n"; |
|---|
| 165 | test( fs::wpath( L"fstream_test_\x2780" ) ); |
|---|
| 166 | |
|---|
| 167 | // test user supplied basic_path |
|---|
| 168 | const long lname[] = { 'f', 's', 'r', 'e', 'a', 'm', '_', 't', 'e', 's', |
|---|
| 169 | 't', '_', 'l', 'p', 'a', 't', 'h', 0 }; |
|---|
| 170 | std::cout << "\nlpath tests:\n"; |
|---|
| 171 | test( user::lpath( lname ) ); |
|---|
| 172 | |
|---|
| 173 | #endif |
|---|
| 174 | |
|---|
| 175 | return 0; |
|---|
| 176 | } |
|---|