JayBeams  0.1
Another project to have fun coding.
ut_fileio.cpp
Go to the documentation of this file.
1 #include <jb/fileio.hpp>
2 
3 #include <boost/filesystem.hpp>
4 #include <boost/test/unit_test.hpp>
5 
6 /**
7  * Implement the common part of the tests.
8  */
9 void check_read_write(boost::filesystem::path const& path) {
10  BOOST_TEST_MESSAGE("Using path=<" << path.string() << ">\n");
11 
12  std::vector<std::string> lines{
13  "This is a sample file", "with more than one line",
14  "yet entirely too short",
15  };
16 
17  {
18  boost::iostreams::filtering_ostream out;
19  jb::open_output_file(out, path.string());
20  for (auto const& line : lines) {
21  BOOST_TEST_MESSAGE("Writing: " << line);
22  out << line << "\n";
23  }
24  }
25 
26  {
27  boost::iostreams::filtering_istream in;
28  jb::open_input_file(in, path.string());
29  for (auto const& expected : lines) {
30  std::string got;
31  std::getline(in, got);
32  BOOST_CHECK_EQUAL(expected, got);
33  }
34  }
35 
36  boost::filesystem::remove(path);
37 }
38 
39 /**
40  * @test Verify we can read and write regular files...
41  */
42 BOOST_AUTO_TEST_CASE(fileio_basic) {
43 
44  boost::filesystem::path tmp = boost::filesystem::temp_directory_path();
45  tmp /= boost::filesystem::unique_path("%%%%-%%%%-%%%%.dat");
46 
47  check_read_write(tmp);
48 }
49 
50 /**
51  * @test Verify we can read and write regular files...
52  */
54 
55  boost::filesystem::path tmp = boost::filesystem::temp_directory_path();
56  tmp /= boost::filesystem::unique_path("%%%%-%%%%-%%%%.gz");
57 
58  check_read_write(tmp);
59 }
60 
61 /**
62  * @test Verify we can write to stdout.
63  */
64 BOOST_AUTO_TEST_CASE(fileio_stdout) {
65  boost::iostreams::filtering_ostream out;
66  BOOST_CHECK_NO_THROW(jb::open_output_file(out, "stdout"));
67  out << "test message, please ignore\n";
68 }
void check_read_write(boost::filesystem::path const &path)
Implement the common part of the tests.
Definition: ut_fileio.cpp:9
void open_output_file(boost::iostreams::filtering_ostream &out, std::string const &filename)
Open a file for writing.
Definition: fileio.cpp:12
void open_input_file(boost::iostreams::filtering_istream &in, std::string const &filename)
Open a file for reading.
Definition: fileio.cpp:27
BOOST_AUTO_TEST_CASE(fileio_basic)
Definition: ut_fileio.cpp:42