]>
Commit | Line | Data |
---|---|---|
b32b8144 FG |
1 | // |
2 | // Copyright (c) 2016-2017 Vinnie Falco (vinnie dot falco at gmail dot com) | |
3 | // | |
4 | // Distributed under the Boost Software License, Version 1.0. (See accompanying | |
5 | // file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt) | |
6 | // | |
7 | // Official repository: https://github.com/boostorg/beast | |
8 | // | |
9 | ||
10 | #ifndef BOOST_BEAST_TEST_CORE_FILE_TEST_HPP | |
11 | #define BOOST_BEAST_TEST_CORE_FILE_TEST_HPP | |
12 | ||
13 | #include <boost/beast/core/type_traits.hpp> | |
14 | #include <boost/beast/unit_test/suite.hpp> | |
15 | #include <boost/config.hpp> | |
16 | #include <boost/filesystem.hpp> | |
17 | #include <string> | |
18 | ||
19 | namespace boost { | |
20 | namespace beast { | |
21 | ||
22 | template<class File> | |
23 | void | |
24 | doTestFile(beast::unit_test::suite& test) | |
25 | { | |
26 | BOOST_STATIC_ASSERT(is_file<File>::value); | |
27 | ||
28 | error_code ec; | |
29 | auto const temp = boost::filesystem::unique_path(); | |
30 | ||
31 | { | |
32 | { | |
33 | File f1; | |
34 | test.BEAST_EXPECT(! f1.is_open()); | |
35 | f1.open(temp.string<std::string>().c_str(), file_mode::write, ec); | |
36 | test.BEAST_EXPECT(! ec); | |
37 | File f2{std::move(f1)}; | |
38 | test.BEAST_EXPECT(! f1.is_open()); | |
39 | test.BEAST_EXPECT(f2.is_open()); | |
40 | File f3; | |
41 | f3 = std::move(f2); | |
42 | test.BEAST_EXPECT(! f2.is_open()); | |
43 | test.BEAST_EXPECT(f3.is_open()); | |
44 | f1.close(ec); | |
45 | test.BEAST_EXPECT(! ec); | |
46 | auto const temp2 = boost::filesystem::unique_path(); | |
47 | f3.open(temp2.string<std::string>().c_str(), file_mode::read, ec); | |
48 | test.BEAST_EXPECT(ec); | |
49 | ec.assign(0, ec.category()); | |
50 | } | |
51 | boost::filesystem::remove(temp, ec); | |
52 | test.BEAST_EXPECT(! ec); | |
53 | } | |
54 | ||
55 | File f; | |
56 | ||
57 | test.BEAST_EXPECT(! f.is_open()); | |
58 | ||
59 | f.size(ec); | |
60 | test.BEAST_EXPECT(ec == errc::invalid_argument); | |
61 | ec.assign(0, ec.category()); | |
62 | ||
63 | f.pos(ec); | |
64 | test.BEAST_EXPECT(ec == errc::invalid_argument); | |
65 | ec.assign(0, ec.category()); | |
66 | ||
67 | f.seek(0, ec); | |
68 | test.BEAST_EXPECT(ec == errc::invalid_argument); | |
69 | ec.assign(0, ec.category()); | |
70 | ||
71 | f.read(nullptr, 0, ec); | |
72 | test.BEAST_EXPECT(ec == errc::invalid_argument); | |
73 | ec.assign(0, ec.category()); | |
74 | ||
75 | f.write(nullptr, 0, ec); | |
76 | test.BEAST_EXPECT(ec == errc::invalid_argument); | |
77 | ec.assign(0, ec.category()); | |
78 | ||
79 | f.open(temp.string<std::string>().c_str(), file_mode::write, ec); | |
80 | test.BEAST_EXPECT(! ec); | |
81 | ||
82 | std::string const s = "Hello, world!"; | |
83 | f.write(s.data(), s.size(), ec); | |
84 | test.BEAST_EXPECT(! ec); | |
85 | ||
86 | auto size = f.size(ec); | |
87 | test.BEAST_EXPECT(! ec); | |
88 | test.BEAST_EXPECT(size == s.size()); | |
89 | ||
90 | auto pos = f.pos(ec); | |
91 | test.BEAST_EXPECT(! ec); | |
92 | test.BEAST_EXPECT(pos == size); | |
93 | ||
94 | f.close(ec); | |
95 | test.BEAST_EXPECT(! ec); | |
96 | ||
97 | f.open(temp.string<std::string>().c_str(), file_mode::read, ec); | |
98 | test.BEAST_EXPECT(! ec); | |
99 | ||
100 | std::string buf; | |
101 | buf.resize(s.size()); | |
102 | f.read(&buf[0], buf.size(), ec); | |
103 | test.BEAST_EXPECT(! ec); | |
104 | test.BEAST_EXPECT(buf == s); | |
105 | ||
106 | f.seek(1, ec); | |
107 | test.BEAST_EXPECT(! ec); | |
108 | buf.resize(3); | |
109 | f.read(&buf[0], buf.size(), ec); | |
110 | test.BEAST_EXPECT(! ec); | |
111 | test.BEAST_EXPECT(buf == "ell"); | |
112 | ||
113 | pos = f.pos(ec); | |
114 | test.BEAST_EXPECT(! ec); | |
115 | test.BEAST_EXPECT(pos == 4); | |
116 | ||
117 | f.close(ec); | |
118 | test.BEAST_EXPECT(! ec); | |
119 | boost::filesystem::remove(temp, ec); | |
120 | test.BEAST_EXPECT(! ec); | |
121 | } | |
122 | ||
123 | } // beast | |
124 | } // boost | |
125 | ||
126 | #endif |