Skip to content

Commit

Permalink
Don't check in compressed files. Consume file in test.
Browse files Browse the repository at this point in the history
Change-Id: I87ead7b3324473713d543c661da17fd8fe274075
  • Loading branch information
garymm committed Nov 21, 2023
1 parent 7290908 commit 47b5055
Show file tree
Hide file tree
Showing 8 changed files with 62 additions and 44 deletions.
18 changes: 18 additions & 0 deletions src/test/BUILD.bazel
Original file line number Diff line number Diff line change
@@ -1,12 +1,30 @@
load("@rules_cc//cc:defs.bzl", "cc_test")
load("//tools:compressed_file.bzl", "compressed_file")

cc_test(
name = "decompress_test",
timeout = "short",
srcs = ["decompress_test.cpp"],
data = [
":starfleet.html.dynamic",
":starfleet.html.fixed",
],
deps = [
"//:boost_ut",
"//src:decompress",
"@bazel_tools//tools/cpp/runfiles",
"@boost_ut",
],
)

compressed_file(
name = "starfleet.html.dynamic",
src = "starfleet.html",
strategy = "dynamic",
)

compressed_file(
name = "starfleet.html.fixed",
src = "starfleet.html",
strategy = "fixed",
)
26 changes: 0 additions & 26 deletions src/test/data/BUILD.bazel

This file was deleted.

Binary file removed src/test/data/starfleet.html.dynamic.deflate
Binary file not shown.
Binary file removed src/test/data/starfleet.html.fixed.deflate
Binary file not shown.
45 changes: 40 additions & 5 deletions src/test/decompress_test.cpp
Original file line number Diff line number Diff line change
@@ -1,8 +1,12 @@
#include "huffman/src/utility.hpp"
#include "src/decompress.hpp"
#include "tools/cpp/runfiles/runfiles.h"

#include <boost/ut.hpp>

#include <fstream>
#include <iterator>
#include <memory>
#include <vector>

template <class... Ts>
Expand All @@ -11,7 +15,32 @@ constexpr auto byte_vector(Ts... values)
return std::vector<std::byte>{std::byte(values)...};
}

auto main() -> int
auto read_runfile(const char* argv0, const std::string& path)
-> std::vector<std::byte>
{
using ::bazel::tools::cpp::runfiles::Runfiles;
std::string error;
std::unique_ptr<Runfiles> runfiles(Runfiles::Create(argv0, &error));
::boost::ut::expect(::boost::ut::fatal(runfiles != nullptr)) << error;

const std::string abs_path{runfiles->Rlocation(path)};

std::ifstream file{abs_path, std::ios::binary};
::boost::ut::expect(::boost::ut::fatal(file.is_open()))
<< "failed to open " << path;
std::vector<char> chars(
(std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

std::vector<char> char_vector(
(std::istreambuf_iterator<char>(file)), std::istreambuf_iterator<char>());

// The standard library really doesn't want me to read bytes froma file, so
// reinterpret chars as bytes.
return {reinterpret_cast<std::byte*>(chars.data()),
reinterpret_cast<std::byte*>(chars.data() + chars.size())};
}

auto main(int, char* argv[]) -> int
{
using ::boost::ut::eq;
using ::boost::ut::expect;
Expand Down Expand Up @@ -70,10 +99,16 @@ auto main() -> int
expect(*actual == expected);
};

test("fixed huffman") = [] {
constexpr auto compressed = huffman::byte_array(0b101);
const auto actual = decompress(compressed);
expect(not actual.has_value());
test("fixed huffman") = [argv] {
const std::vector<std::byte> input_bytes =
read_runfile(argv[0], "src/test/data/starfleet.html.fixed");
huffman::bit_span input_bits(input_bytes);
// convert input_data to std::range

const auto header = detail::read_header(input_bits);
expect(header.has_value())
<< "got error: " << static_cast<int>(header.error());
expect(header->type == detail::BlockType::FixedHuffman);
};

test("dynamic huffman") = [] {
Expand Down
File renamed without changes.
11 changes: 3 additions & 8 deletions tools/compressed_file.bzl
Original file line number Diff line number Diff line change
Expand Up @@ -5,35 +5,30 @@ Compresses a DEFLATE compressed file with a fixed or dynamic Huffman tree.
def compressed_file(
name,
src,
out = None,
strategy = "fixed"):
"""
Compresses a file with the DEFLATE algorithms and a fixed or dynamic Huffman tree.
Compresses a file with the DEFLATE algorithm and a fixed or dynamic Huffman tree.
Args:
name: string
Name for compressed_file rule.
src: string_label
Decompressed file.
out: string
Name for generated compressed file. Defaults to `name` if not specified.
strategy: string
one of [`fixed`, `dynamic`].
Determines if compression should use a fixed Huffman tree or a dynamic
Huffman tree.
"""
out = out or name

if strategy not in ["fixed", "dynamic"]:
fail()

tools = ["//tools:gen_test_data"]

native.genrule(
name = "gen_" + out,
name = "gen_" + name,
srcs = [src],
outs = [out],
outs = [name],
tools = tools,
cmd = "$(execpath {tool}) --src $< {strat} > $@".format(
tool = tools[0],
Expand Down
6 changes: 1 addition & 5 deletions tools/gen_test_data.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,10 +5,7 @@
import zlib

def main(args):
if args.fixed:
strategy = zlib.Z_FIXED
else:
strategy = zlib.Z_DEFAULT_STRATEGY
strategy = zlib.Z_FIXED if args.fixed else zlib.Z_DEFAULT_STRATEGY
compression = zlib.compressobj(
# negative means no header or trailing checksum.
# basically raw DEFLATE instead of zlib format.
Expand All @@ -19,7 +16,6 @@ def main(args):
compressed = compression.compress(data)
compressed += compression.flush()

print(f"writing with {strategy} strategy", file = sys.stderr)
sys.stdout.buffer.write(compressed)

parser = argparse.ArgumentParser(
Expand Down

0 comments on commit 47b5055

Please sign in to comment.