]> git.proxmox.com Git - ceph.git/blame - ceph/src/boost/libs/nowide/test/test_fstream_cxx11.cpp
buildsys: change download over to reef release
[ceph.git] / ceph / src / boost / libs / nowide / test / test_fstream_cxx11.cpp
CommitLineData
f67539c2
TL
1//
2// Copyright (c) 2019 Alexander Grund
3//
4// Distributed under the Boost Software License, Version 1.0. (See
20effc67 5// accompanying file LICENSE or copy at
f67539c2
TL
6// http://www.boost.org/LICENSE_1_0.txt)
7//
8
9#include <boost/nowide/config.hpp>
10
f67539c2
TL
11#include <boost/nowide/fstream.hpp>
12
13#include <boost/nowide/cstdio.hpp>
14#include <iterator>
15#include <utility>
16
17#include "test.hpp"
18
19namespace nw = boost::nowide;
20
21void create_file(const std::string& filename, const std::string& contents)
22{
23 nw::ofstream f(filename, std::ios::trunc);
24 TEST(f << contents);
25}
26
27template<typename T>
28std::string get_file_contents(T& stream)
29{
30 TEST(stream);
31 std::string s((std::istreambuf_iterator<char>(stream)), std::istreambuf_iterator<char>());
32 return s;
33}
34
35std::string get_file_contents(const std::string& filename)
36{
37 nw::ifstream f(filename);
38 return get_file_contents(f);
39}
40
41nw::ifstream make_ifstream(const std::string& filename)
42{
43 nw::ifstream f(filename);
44 TEST(f);
45 std::string s;
46 TEST(f >> s);
47 TEST(s == "Hello");
48 return f;
49}
50
51void test_ifstream(const std::string& filename)
52{
53 create_file(filename, "Hello\nWorld");
54 // Move construct
55 {
56 nw::ifstream f = make_ifstream(filename);
57 TEST(f);
58 std::string s;
59 TEST(f >> s);
60 TEST(s == "World");
61 }
62 // Move assign
63 {
64 nw::ifstream f;
65 {
66 nw::ifstream f2 = make_ifstream(filename);
67 f = std::move(f2);
68 }
69 TEST(f);
70 std::string s;
71 TEST(f >> s);
72 TEST(s == "World");
73 }
74 // Swap
75 {
76 nw::ifstream f;
77 {
78 nw::ifstream f2 = make_ifstream(filename);
79 swap(f, f2);
80 TEST(!f2.is_open());
81 }
82 TEST(f);
83 std::string s;
84 TEST(f >> s);
85 TEST(s == "World");
86 }
87 TEST(nw::remove(filename.c_str()) == 0);
88}
89
90nw::ofstream make_ofstream(const std::string& filename)
91{
92 nw::ofstream f(filename);
93 TEST(f);
94 TEST(f << "Hello");
95 return f;
96}
97
98void test_ofstream(const std::string& filename)
99{
100 // Move construct
101 {
102 nw::ofstream f = make_ofstream(filename);
103 TEST(f);
104 TEST(f << " world");
105 f.close();
106 TEST(get_file_contents(filename) == "Hello world");
107 }
108 // Move assign
109 {
110 nw::ofstream f;
111 {
112 nw::ofstream f2 = make_ofstream(filename);
113 f = std::move(f2);
114 }
115 TEST(f);
116 TEST(f << " world");
117 f.close();
118 TEST(get_file_contents(filename) == "Hello world");
119 }
120 // Swap
121 {
122 nw::ofstream f;
123 {
124 nw::ofstream f2 = make_ofstream(filename);
125 swap(f, f2);
126 TEST(!f2.is_open());
127 }
128 TEST(f);
129 TEST(f << " world");
130 f.close();
131 TEST(get_file_contents(filename) == "Hello world");
132 }
133 TEST(nw::remove(filename.c_str()) == 0);
134}
135
136nw::fstream make_fstream(const std::string& filename)
137{
138 create_file(filename, "");
139 nw::fstream f(filename);
140 TEST(f << "Hello");
141 return f;
142}
143
144void test_fstream(const std::string& filename)
145{
146 // Move construct
147 {
148 nw::fstream f = make_fstream(filename);
149 TEST(f);
150 TEST(f << " world");
151 f.seekg(0);
152 TEST(get_file_contents(f) == "Hello world");
153 }
154 // Move assign
155 {
156 nw::fstream f;
157 {
158 nw::fstream f2 = make_fstream(filename);
159 f = std::move(f2);
160 }
161 TEST(f);
162 TEST(f << " world");
163 f.seekg(0);
164 TEST(get_file_contents(f) == "Hello world");
165 }
166 // Swap
167 {
168 nw::fstream f;
169 {
170 nw::fstream f2 = make_fstream(filename);
171 swap(f, f2);
172 TEST(!f2.is_open());
173 }
174 TEST(f);
175 TEST(f << " world");
176 f.seekg(0);
177 TEST(get_file_contents(f) == "Hello world");
178 }
179 TEST(nw::remove(filename.c_str()) == 0);
180}
181
182void test_main(int, char** argv, char**)
183{
184 const std::string exampleFilename = std::string(argv[0]) + "-\xd7\xa9-\xd0\xbc-\xce\xbd.txt";
185 test_ifstream(exampleFilename);
186 test_ofstream(exampleFilename);
187 test_fstream(exampleFilename);
188}