File: test_gzip.cpp

package info (click to toggle)
libosmium 2.23.0-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 4,564 kB
  • sloc: cpp: 53,570; sh: 148; makefile: 19
file content (138 lines) | stat: -rw-r--r-- 4,014 bytes parent folder | download | duplicates (3)
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
#include "catch.hpp"

#include "utils.hpp"

#include <osmium/io/detail/read_write.hpp>
#include <osmium/io/gzip_compression.hpp>

#include <string>
#include <system_error>

TEST_CASE("Invalid file descriptor of gzip-compressed file") {
    REQUIRE_THROWS_AS(osmium::io::GzipDecompressor{-1}, osmium::gzip_error);
}

TEST_CASE("Non-open file descriptor of gzip-compressed file") {
    // 12345 is just a random file descriptor that should not be open
    osmium::io::GzipDecompressor decomp{12345};
    REQUIRE_THROWS_AS(decomp.read(), osmium::gzip_error);
}

TEST_CASE("Empty gzip-compressed file") {
    const int count = count_fds();

    const std::string input_file = with_data_dir("t/io/empty_file");
    const int fd = osmium::io::detail::open_for_reading(input_file);
    REQUIRE(fd > 0);

    osmium::io::GzipDecompressor decomp{fd};
    REQUIRE(decomp.read().empty());
    decomp.close();

    REQUIRE(count == count_fds());
}

TEST_CASE("Read gzip-compressed file") {
    const int count = count_fds();

    const std::string input_file = with_data_dir("t/io/data_gzip.txt.gz");
    const int fd = osmium::io::detail::open_for_reading(input_file);
    REQUIRE(fd > 0);

    size_t size = 0;
    std::string all;
    {
        osmium::io::GzipDecompressor decomp{fd};
        for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
            size += data.size();
            all += data;
        }
        decomp.close();
    }

    REQUIRE(size >= 9);
    all.resize(8);
    REQUIRE("TESTDATA" == all);

    REQUIRE(count == count_fds());
}

TEST_CASE("Read gzip-compressed file without explicit close") {
    const int count = count_fds();

    const std::string input_file = with_data_dir("t/io/data_gzip.txt.gz");
    const int fd = osmium::io::detail::open_for_reading(input_file);
    REQUIRE(fd > 0);

    size_t size = 0;
    std::string all;
    {
        osmium::io::GzipDecompressor decomp{fd};
        for (std::string data = decomp.read(); !data.empty(); data = decomp.read()) {
            size += data.size();
            all += data;
        }
    }

    REQUIRE(size >= 9);
    all.resize(8);
    REQUIRE("TESTDATA" == all);

    REQUIRE(count == count_fds());
}

TEST_CASE("Corrupted gzip-compressed file") {
    const int count = count_fds();

    const std::string input_file = with_data_dir("t/io/corrupt_data_gzip.txt.gz");
    const int fd = osmium::io::detail::open_for_reading(input_file);
    REQUIRE(fd > 0);

    osmium::io::GzipDecompressor decomp{fd};
    decomp.read();
    REQUIRE_THROWS_AS(decomp.close(), osmium::gzip_error);

    REQUIRE(count == count_fds());
}

TEST_CASE("Compressor: Invalid file descriptor for gzip-compressed file") {
    REQUIRE_THROWS_AS(osmium::io::GzipCompressor(999, osmium::io::fsync::yes), std::system_error);
}

TEST_CASE("Compressor: Non-open file descriptor for gzip-compressed file") {
    // 12345 is just a random file descriptor that should not be open
    REQUIRE_THROWS_AS(osmium::io::GzipCompressor(12345, osmium::io::fsync::yes), std::system_error);
}

TEST_CASE("Write gzip-compressed file with explicit close") {
    const int count = count_fds();

    const std::string output_file = "test_gzip_out.txt.gz";
    const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
    REQUIRE(fd > 0);

    osmium::io::GzipCompressor comp{fd, osmium::io::fsync::yes};
    comp.write("foo");
    comp.close();

    REQUIRE(count == count_fds());

    REQUIRE(osmium::file_size(output_file) > 10);
}

TEST_CASE("Write gzip-compressed file with implicit close") {
    const int count = count_fds();

    const std::string output_file = "test_gzip_out.txt.gz";
    const int fd = osmium::io::detail::open_for_writing(output_file, osmium::io::overwrite::allow);
    REQUIRE(fd > 0);

    {
        osmium::io::GzipCompressor comp{fd, osmium::io::fsync::yes};
        comp.write("foo");
    }
    REQUIRE(count == count_fds());

    REQUIRE(osmium::file_size(output_file) > 10);
}