File: fstream_test.cpp

package info (click to toggle)
boost1.88 1.88.0-1
  • links: PTS, VCS
  • area: main
  • in suites: sid, trixie
  • size: 576,932 kB
  • sloc: cpp: 4,149,234; xml: 136,789; ansic: 35,092; python: 33,910; asm: 5,698; sh: 4,604; ada: 1,681; makefile: 1,633; pascal: 1,139; perl: 1,124; sql: 640; yacc: 478; ruby: 271; java: 77; lisp: 24; csh: 6
file content (199 lines) | stat: -rw-r--r-- 6,729 bytes parent folder | download
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
//  fstream_test.cpp  ------------------------------------------------------------------//

//  Copyright Beman Dawes 2002

//  Distributed under the Boost Software License, Version 1.0.
//  See http://www.boost.org/LICENSE_1_0.txt

//  Library home page: http://www.boost.org/libs/filesystem

#include <boost/config/warning_disable.hpp>

//  See deprecated_test for tests of deprecated features
#ifndef BOOST_FILESYSTEM_NO_DEPRECATED
#define BOOST_FILESYSTEM_NO_DEPRECATED
#endif
#ifndef BOOST_SYSTEM_NO_DEPRECATED
#define BOOST_SYSTEM_NO_DEPRECATED
#endif

#include <boost/filesystem/fstream.hpp>
#include <boost/filesystem/operations.hpp>
#include <string>
#include <iostream>
#include <cstdio> // for std::remove

#include <boost/filesystem/detail/utf8_codecvt_facet.hpp>

namespace fs = boost::filesystem;

#include <boost/config.hpp>
#ifdef BOOST_NO_STDC_NAMESPACE
namespace std {
using ::remove;
}
#endif

#if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
#include <type_traits>
#endif

#include <boost/core/lightweight_test.hpp>
#include <boost/detail/lightweight_main.hpp>

namespace {

bool cleanup = true;

void test(fs::path const& p)
{
    fs::remove(p);
    {
        std::cout << " in test 1\n";
        fs::filebuf fb1;
        fb1.open(p, std::ios_base::out);
        BOOST_TEST(fb1.is_open());
    }
    {
        std::cout << " in test 2\n";
        fs::filebuf fb2;
        fb2.open(p, std::ios_base::in);
        BOOST_TEST(fb2.is_open());
    }
    {
        std::cout << " in test 3\n";
        fs::ifstream tfs(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 4\n";
        fs::ifstream tfs(p / p.filename()); // should fail
        BOOST_TEST(!tfs.is_open());
    }
    {
        std::cout << " in test 5\n";
        fs::ifstream tfs(p, std::ios_base::in);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 6\n";
        fs::ifstream tfs;
        tfs.open(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 7\n";
        fs::ifstream tfs;
        tfs.open(p, std::ios_base::in);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 8\n";
        fs::ofstream tfs(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 9\n";
        fs::ofstream tfs(p, std::ios_base::out);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 10\n";
        fs::ofstream tfs;
        tfs.open(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 11\n";
        fs::ofstream tfs;
        tfs.open(p, std::ios_base::out);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 12\n";
        fs::fstream tfs(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 13\n";
        fs::fstream tfs(p, std::ios_base::in | std::ios_base::out);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 14\n";
        fs::fstream tfs;
        tfs.open(p);
        BOOST_TEST(tfs.is_open());
    }
    {
        std::cout << " in test 15\n";
        fs::fstream tfs;
        tfs.open(p, std::ios_base::in | std::ios_base::out);
        BOOST_TEST(tfs.is_open());
    }

    if (cleanup)
        fs::remove(p);
}

#if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
void test_movable()
{
    BOOST_TEST_EQ(std::is_move_constructible< fs::filebuf >::value, std::is_move_constructible< std::filebuf >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::filebuf >::value, std::is_move_assignable< std::filebuf >::value);
    BOOST_TEST_EQ(std::is_move_constructible< fs::wfilebuf >::value, std::is_move_constructible< std::wfilebuf >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::wfilebuf >::value, std::is_move_assignable< std::wfilebuf >::value);

    BOOST_TEST_EQ(std::is_move_constructible< fs::ifstream >::value, std::is_move_constructible< std::ifstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::ifstream >::value, std::is_move_assignable< std::ifstream >::value);
    BOOST_TEST_EQ(std::is_move_constructible< fs::wifstream >::value, std::is_move_constructible< std::wifstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::wifstream >::value, std::is_move_assignable< std::wifstream >::value);

    BOOST_TEST_EQ(std::is_move_constructible< fs::ofstream >::value, std::is_move_constructible< std::ofstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::ofstream >::value, std::is_move_assignable< std::ofstream >::value);
    BOOST_TEST_EQ(std::is_move_constructible< fs::wofstream >::value, std::is_move_constructible< std::wofstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::wofstream >::value, std::is_move_assignable< std::wofstream >::value);

    BOOST_TEST_EQ(std::is_move_constructible< fs::fstream >::value, std::is_move_constructible< std::fstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::fstream >::value, std::is_move_assignable< std::fstream >::value);
    BOOST_TEST_EQ(std::is_move_constructible< fs::wfstream >::value, std::is_move_constructible< std::wfstream >::value);
    BOOST_TEST_EQ(std::is_move_assignable< fs::wfstream >::value, std::is_move_assignable< std::wfstream >::value);
}
#endif // !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)

} // namespace

int cpp_main(int argc, char*[])
{
    if (argc > 1)
        cleanup = false;

    std::cout << "BOOST_FILESYSTEM_C_STR(p) defined as \""
              << BOOST_STRINGIZE(BOOST_FILESYSTEM_C_STR(p)) << "\"\n";

    // test narrow characters
    std::cout << "narrow character tests:\n";
    test(fs::unique_path("narrow_fstream_test-%%%%-%%%%.txt"));

    // So that tests are run with known encoding, use Boost UTF-8 codecvt
    std::locale global_loc = std::locale();
    std::locale loc(global_loc, new fs::detail::utf8_codecvt_facet());
    fs::path::imbue(loc);

    // test with some wide characters
    //  \u2780 is circled 1 against white background == e2 9e 80 in UTF-8
    //  \u2781 is circled 2 against white background == e2 9e 81 in UTF-8
    //  \u263A is a white smiling face
    std::cout << "\nwide character tests:\n";
    std::wstring ws(L"wide_fstream_test_");
    ws.push_back(static_cast< wchar_t >(0x2780));
    ws.push_back(static_cast< wchar_t >(0x263A));
    ws.append(L"-%%%%-%%%%.txt");
    test(fs::unique_path(ws));

#if !defined(BOOST_FILESYSTEM_DETAIL_NO_CXX11_MOVABLE_FSTREAMS) && !defined(BOOST_NO_CXX11_HDR_TYPE_TRAITS)
    test_movable();
#endif

    return ::boost::report_errors();
}