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