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 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286
|
// Copyright (c) 2015 Artyom Beilis (Tonkikh)
// Copyright (c) 2019-2021 Alexander Grund
//
// Distributed under the Boost Software License, Version 1.0.
// https://www.boost.org/LICENSE_1_0.txt
#include <boost/nowide/fstream.hpp>
#include <boost/nowide/convert.hpp>
#include <boost/nowide/cstdio.hpp>
#include "file_test_helpers.hpp"
#include "test.hpp"
#include <fstream>
#include <iostream>
#include <string>
namespace nw = boost::nowide;
using namespace boost::nowide::test;
void test_with_different_buffer_sizes(const char* filepath)
{
/* Important part of the standard for mixing input with output:
However, output shall not be directly followed by input without an intervening call to the fflush function
or to a file positioning function (fseek, fsetpos, or rewind),
and input shall not be directly followed by output without an intervening call to a file positioning function,
unless the input operation encounters end-of-file.
*/
for(int i = -1; i < 16; i++)
{
remove_file_at_exit _(filepath);
std::cout << "Buffer size = " << i << std::endl;
char buf[16];
nw::fstream f;
// Different conditions when setbuf might be called: Usually before opening a file is OK
if(i >= 0)
f.rdbuf()->pubsetbuf((i == 0) ? nullptr : buf, i);
f.open(filepath, std::ios::in | std::ios::out | std::ios::trunc | std::ios::binary);
TEST(f);
// Add 'abcdefg'
TEST(f.put('a'));
TEST(f.put('b'));
TEST(f.put('c'));
TEST(f.write("defg", 4));
// Read first char
TEST(f.seekg(0));
TEST_EQ(f.get(), 'a');
TEST_EQ(f.gcount(), std::streamsize(1));
// Skip next char
TEST(f.seekg(1, std::ios::cur));
TEST_EQ(f.get(), 'c');
TEST_EQ(f.gcount(), std::streamsize(1));
// Go back 1 char
TEST(f.seekg(-1, std::ios::cur));
TEST_EQ(f.get(), 'c');
TEST_EQ(f.gcount(), std::streamsize(1));
// Test switching between read->write->read
// case 1) overwrite, flush, read
TEST(f.seekg(1));
TEST(f.put('B'));
TEST(f.flush()); // Flush when changing out->in
TEST_EQ(f.get(), 'c');
TEST_EQ(f.gcount(), std::streamsize(1));
TEST(f.seekg(1));
TEST_EQ(f.get(), 'B');
TEST_EQ(f.gcount(), std::streamsize(1));
// case 2) overwrite, seek, read
TEST(f.seekg(2));
TEST(f.put('C'));
TEST(f.seekg(3)); // Seek when changing out->in
TEST_EQ(f.get(), 'd');
TEST_EQ(f.gcount(), std::streamsize(1));
// Check that sequence from start equals expected
TEST(f.seekg(0));
TEST_EQ(f.get(), 'a');
TEST_EQ(f.get(), 'B');
TEST_EQ(f.get(), 'C');
TEST_EQ(f.get(), 'd');
TEST_EQ(f.get(), 'e');
// Putback after flush is implementation defined
TEST(f << std::flush);
if(f.putback('e'))
{
if(f.putback('d'))
TEST_EQ(f.get(), 'd');
else
f.clear(); // LCOV_EXCL_LINE
TEST_EQ(f.get(), 'e');
} else
f.clear();
TEST(f << std::flush);
if(f.unget())
TEST_EQ(f.get(), 'e');
else
f.clear();
// Put back different char
TEST(f.seekg(-1, std::ios::cur));
TEST_EQ(f.get(), 'e');
TEST(f.putback('x'));
TEST_EQ(f.get(), 'x');
// Rest of sequence
TEST_EQ(f.get(), 'f');
TEST_EQ(f.get(), 'g');
TEST_EQ(f.get(), EOF);
// Put back until front of file is reached
f.clear();
TEST(f.seekg(1));
TEST_EQ(f.get(), 'B');
TEST(f.putback('B'));
// Putting back multiple chars is not possible on all implementations after a seek/flush
#if BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT
if(f.putback('a'))
{
// At beginning of file -> No putback possible
TEST(!f.putback('x')); // LCOV_EXCL_LINE
f.clear(); // LCOV_EXCL_LINE
// Get characters that were putback to avoid MSVC bug https://github.com/microsoft/STL/issues/342
TEST_EQ(f.get(), 'a'); // LCOV_EXCL_LINE
} else
f.clear();
#endif
TEST_EQ(f.get(), 'B');
f.close();
}
}
void test_switch_to_custom_buffer(const std::string& filename)
{
// Switching the buffer after file stream was used is not always defined. So only test custom stream
#if BOOST_NOWIDE_USE_FILEBUF_REPLACEMENT
nw::test::create_file(filename, "HelloWorld");
nw::ifstream f(filename, std::ios::binary);
std::string s(5, '\0');
TEST(f.read(&s.front(), s.size()));
TEST_EQ(s, "Hello");
// Switch buffer
std::string buffer(10, '\0');
TEST_EQ(f.sync(), 0);
TEST(f.rdbuf()->pubsetbuf(&buffer.front(), buffer.size()) == f.rdbuf());
TEST(f >> s);
TEST_EQ(s, "World");
TEST_EQ(s, buffer.c_str()); // same should be in buffer and some trailing NULL bytes
#else
(void)filename; // Suppress unused warning
#endif
}
// Reproducer for https://github.com/boostorg/nowide/issues/126
void test_getline_and_tellg(const char* filename)
{
{
nw::ofstream f(filename);
f << "Line 1" << std::endl;
f << "Line 2" << std::endl;
f << "Line 3" << std::endl;
}
remove_file_at_exit _(filename);
nw::fstream f;
// Open file in text mode, to read
f.open(filename, std::ios_base::in);
TEST(f);
std::string line1, line2, line3;
TEST(getline(f, line1));
TEST_EQ(line1, "Line 1");
const auto tg = f.tellg(); // This may cause issues
TEST(tg > 0u);
TEST(getline(f, line2));
TEST_EQ(line2, "Line 2");
TEST(getline(f, line3));
TEST_EQ(line3, "Line 3");
}
// Test that a sync after a peek does not swallow newlines
// This can happen because peek reads a char which needs to be "unread" on sync which may loose a converted newline
void test_peek_sync_get(const char* filename)
{
{
nw::ofstream f(filename);
f << "Line 1" << std::endl;
f << "Line 2" << std::endl;
}
remove_file_at_exit _(filename);
nw::ifstream f(filename);
TEST(f);
while(f)
{
const int curChar = f.peek();
if(curChar == std::char_traits<char>::eof())
break;
f.sync();
TEST_EQ(f.get(), char(curChar));
}
}
/// Test swapping at many possible positions within a stream to shake out missed state
void test_swap(const char* filename, const char* filename2)
{
remove_file_at_exit _(filename);
remove_file_at_exit _2(filename2);
{
nw::ofstream f(filename);
f << create_random_data(BUFSIZ * 2, data_type::text);
f.close();
f.open(filename2);
f << create_random_data(BUFSIZ * 3, data_type::text);
}
nw::ifstream f1(filename);
nw::ifstream f2(filename2);
TEST(f1);
TEST(f2);
unsigned ctr = 0;
while(f1 && f2)
{
const int curChar1 = f1.peek();
const int curChar2 = f2.peek();
TEST_CONTEXT("ctr " << ctr << ": c1=" << curChar1 << " c2=" << curChar2);
// Randomly do a no-op seek of either or both streams to flush internal buffer
if(ctr % 10 == 0)
TEST(f1.seekg(f1.tellg()));
else if(ctr % 15 == 0)
TEST(f2.seekg(f2.tellg()));
f1.swap(f2);
TEST_EQ(f1.peek(), curChar2);
TEST_EQ(f2.peek(), curChar1);
if(ctr % 10 == 4)
TEST(f1.seekg(f1.tellg()));
else if(ctr % 15 == 4)
TEST(f2.seekg(f2.tellg()));
TEST_EQ(f1.get(), curChar2);
f1.swap(f2);
TEST_EQ(f1.get(), curChar1);
++ctr;
}
}
void testPutback(const char* filename)
{
nw::test::create_file(filename, "abc");
// Does work for ifstreams
{
nw::ifstream f(filename);
const int c = f.get();
TEST(f.putback(static_cast<char>(c)));
TEST_EQ(f.get(), c);
}
// Does work for io fstreams
{
nw::fstream f(filename);
const int c = f.get();
TEST(f.putback(static_cast<char>(c)));
TEST_EQ(f.get(), c);
}
// Doesn't work for output fstreams
{
nw::fstream f(filename, std::ios::out);
TEST(!f.putback('x'));
}
}
// coverity[root_function]
void test_main(int, char** argv, char**)
{
const std::string exampleFilename = std::string(argv[0]) + "-\xd7\xa9-\xd0\xbc-\xce\xbd.txt";
const std::string exampleFilename2 = std::string(argv[0]) + "-\xd7\xa9-\xd0\xbc-\xce\xbd 2.txt";
std::cout << "Putback" << std::endl;
testPutback(exampleFilename.c_str());
std::cout << "Complex IO" << std::endl;
test_with_different_buffer_sizes(exampleFilename.c_str());
test_switch_to_custom_buffer(exampleFilename.c_str());
std::cout << "Regression tests" << std::endl;
test_getline_and_tellg(exampleFilename.c_str());
test_peek_sync_get(exampleFilename.c_str());
test_swap(exampleFilename.c_str(), exampleFilename2.c_str());
}
|