1 #include "catch.hpp" 2 3 #include "utils.hpp" 4 5 #include <osmium/io/compression.hpp> 6 7 #include <string> 8 9 TEST_CASE("Invalid file descriptor of uncompressed file") { 10 osmium::io::NoDecompressor decomp{-1}; 11 REQUIRE_THROWS_AS(decomp.read(), std::system_error); 12 } 13 14 TEST_CASE("Non-open file descriptor of uncompressed file") { 15 // 12345 is just a random file descriptor that should not be open 16 osmium::io::NoDecompressor decomp{12345}; 17 REQUIRE_THROWS_AS(decomp.read(), std::system_error); 18 } 19 20 TEST_CASE("Empty uncompressed file") { 21 const int count = count_fds(); 22 23 const std::string input_file = with_data_dir("t/io/empty_file"); 24 const int fd = osmium::io::detail::open_for_reading(input_file); 25 REQUIRE(fd > 0); 26 27 osmium::io::NoDecompressor decomp{fd}; 28 REQUIRE(decomp.read().empty()); 29 decomp.close(); 30 31 REQUIRE(count == count_fds()); 32 } 33 34 TEST_CASE("Read uncompressed file") { 35 const int count = count_fds(); 36 37 const std::string input_file = with_data_dir("t/io/data.txt"); 38 const int fd = osmium::io::detail::open_for_reading(input_file); 39 REQUIRE(fd > 0); 40 41 size_t size = 0; 42 std::string all; 43 { 44 osmium::io::NoDecompressor decomp{fd}; 45 for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) { 46 size += data.size(); 47 all += data; 48 } 49 decomp.close(); 50 } 51 52 REQUIRE(size >= 9); 53 all.resize(8); 54 REQUIRE("TESTDATA" == all); 55 56 REQUIRE(count == count_fds()); 57 } 58 59 TEST_CASE("Read uncompressed file without explicit close") { 60 const int count = count_fds(); 61 62 const std::string input_file = with_data_dir("t/io/data.txt"); 63 const int fd = osmium::io::detail::open_for_reading(input_file); 64 REQUIRE(fd > 0); 65 66 size_t size = 0; 67 std::string all; 68 { 69 osmium::io::NoDecompressor decomp{fd}; 70 for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) { 71 size += data.size(); 72 all += data; 73 } 74 } 75 76 REQUIRE(size >= 9); 77 all.resize(8); 78 REQUIRE("TESTDATA" == all); 79 80 REQUIRE(count == count_fds()); 81 } 82 83 TEST_CASE("Compressor: Invalid file descriptor for uncompressed file") { 84 osmium::io::NoCompressor comp{-1, osmium::io::fsync::yes}; 85 REQUIRE_THROWS_AS(comp.write("foo"), std::system_error); 86 } 87 88 TEST_CASE("Compressor: Non-open file descriptor for uncompressed file") { 89 // 12345 is just a random file descriptor that should not be open 90 osmium::io::NoCompressor comp{12345, osmium::io::fsync::yes}; 91 REQUIRE_THROWS_AS(comp.write("foo"), std::system_error); 92 } 93 94 TEST_CASE("Write uncompressed file with explicit close") { 95 const int count = count_fds(); 96 97 const std::string output_file = "test_uncompressed_out.txt"; 98 const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow); 99 REQUIRE(fd > 0); 100 101 osmium::io::NoCompressor comp{fd, osmium::io::fsync::yes}; 102 comp.write("foo"); 103 comp.close(); 104 105 REQUIRE(count == count_fds()); 106 107 REQUIRE(osmium::file_size(output_file) == 3); 108 } 109 110 TEST_CASE("Write uncompressed file with implicit close") { 111 const int count = count_fds(); 112 113 const std::string output_file = "test_uncompressed_out.txt"; 114 const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow); 115 REQUIRE(fd > 0); 116 117 { 118 osmium::io::NoCompressor comp{fd, osmium::io::fsync::yes}; 119 comp.write("foo"); 120 } 121 REQUIRE(count == count_fds()); 122 123 REQUIRE(osmium::file_size(output_file) == 3); 124 } 125 126