Skip to content

Commit

Permalink
test data for fixed and dynamic huffman trees
Browse files Browse the repository at this point in the history
and a script to generate it

I guess the nice way to do this would be to bring in rules_python , or
pull in zlib and have our test generate the compressed data at run-time.
We can add that later if we find issues.

Change-Id: Iaba4043956a2eb6fd0f51aef40414ae948a7311e
  • Loading branch information
garymm committed Dec 12, 2023
1 parent 0f44c15 commit 28b83dc
Show file tree
Hide file tree
Showing 6 changed files with 1,309 additions and 10 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",
)
61 changes: 52 additions & 9 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,36 @@ 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>());

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

// The standard library really doesn't want me to read std::byte from a file,
// so reinterpret chars as bytes.
// NOLINTBEGIN(cppcoreguidelines-pro-type-reinterpret-cast)
return {
reinterpret_cast<std::byte*>(chars.data()),
// NOLINTNEXTLINE(cppcoreguidelines-pro-bounds-pointer-arithmetic)
reinterpret_cast<std::byte*>(chars.data() + chars.size())};
// NOLINTEND(cppcoreguidelines-pro-type-reinterpret-cast)
}

auto main(int, char* argv[]) -> int
{
using ::boost::ut::eq;
using ::boost::ut::expect;
Expand Down Expand Up @@ -70,15 +103,25 @@ 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, "starflate/src/test/starfleet.html.fixed");
huffman::bit_span input_bits(input_bytes);

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") = [] {
constexpr auto compressed = huffman::byte_array(0b011);
const auto actual = decompress(compressed);
expect(not actual.has_value());
test("dynamic huffman") = [argv] {
const std::vector<std::byte> input_bytes =
read_runfile(*argv, "starflate/src/test/starfleet.html.dynamic");
huffman::bit_span input_bits(input_bytes);

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

0 comments on commit 28b83dc

Please sign in to comment.