diff --git a/mcpp.toml b/mcpp.toml index 38ca572f..e29f7d6b 100644 --- a/mcpp.toml +++ b/mcpp.toml @@ -17,6 +17,7 @@ members = [ "tests/examples/boost-throw-exception", "tests/examples/boost-type-traits", "tests/examples/boost-uuid", + "tests/examples/brotli", "tests/examples/build-mcpp", "tests/examples/c-ares", "tests/examples/catch2", diff --git a/pkgs/c/compat.brotli.lua b/pkgs/c/compat.brotli.lua new file mode 100644 index 00000000..0a6ad5d3 --- /dev/null +++ b/pkgs/c/compat.brotli.lua @@ -0,0 +1,56 @@ +-- Form B C-source compat descriptor for Brotli. The three source globs match +-- upstream v1.2.0's BROTLI_COMMON_SOURCES, BROTLI_DEC_SOURCES, and +-- BROTLI_ENC_SOURCES CMake sets; the CLI, tests, fuzzers, and the optional C++ +-- lazy static-initialization implementation are outside those sets. +package = { + spec = "1", + namespace = "compat", + name = "brotli", + description = "Brotli compression library", + licenses = {"MIT"}, + repo = "https://github.com/google/brotli", + type = "package", + + xpm = { + linux = { + ["1.2.0"] = { + url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz", + sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec", + }, + }, + macosx = { + ["1.2.0"] = { + url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz", + sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec", + }, + }, + windows = { + ["1.2.0"] = { + url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz", + sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec", + }, + }, + }, + + mcpp = { + language = "c++23", + import_std = false, + c_standard = "c11", + include_dirs = { "*/c/include" }, + sources = { + "*/c/common/*.c", + "*/c/dec/*.c", + "*/c/enc/*.c", + }, + targets = { ["brotli"] = { kind = "lib" } }, + deps = {}, + + -- Upstream detects libm and propagates it for static builds. + linux = { ldflags = { "-lm" } }, + macosx = { ldflags = { "-lm" } }, + windows = { + -- This is upstream's only MSVC-specific library definition. + cflags = { "-D_CRT_SECURE_NO_WARNINGS" }, + }, + }, +} diff --git a/tests/examples/brotli/mcpp.toml b/tests/examples/brotli/mcpp.toml new file mode 100644 index 00000000..792e872c --- /dev/null +++ b/tests/examples/brotli/mcpp.toml @@ -0,0 +1,8 @@ +# Brotli test project: consumes compat.brotli through the workspace-root +# `compat` index redirect and exercises encoder, decoder, and common code. +[package] +name = "brotli-tests" +version = "0.1.0" + +[dependencies.compat] +brotli = "1.2.0" diff --git a/tests/examples/brotli/tests/roundtrip.cpp b/tests/examples/brotli/tests/roundtrip.cpp new file mode 100644 index 00000000..33781e8b --- /dev/null +++ b/tests/examples/brotli/tests/roundtrip.cpp @@ -0,0 +1,43 @@ +#include +#include + +#include +#include +#include + +int main() { + static constexpr unsigned char original[] = + "Brotli round-trip payload. Brotli round-trip payload. " + "Brotli round-trip payload. Brotli round-trip payload."; + constexpr std::size_t original_size = sizeof(original) - 1; + + const std::size_t encoded_capacity = BrotliEncoderMaxCompressedSize(original_size); + if (original_size == 0 || encoded_capacity == 0) return 1; + + std::vector encoded(encoded_capacity); + std::size_t encoded_size = encoded.size(); + const BROTLI_BOOL encoded_ok = BrotliEncoderCompress( + BROTLI_DEFAULT_QUALITY, + BROTLI_DEFAULT_WINDOW, + BROTLI_MODE_GENERIC, + original_size, + original, + &encoded_size, + encoded.data()); + if (encoded_ok != BROTLI_TRUE || encoded_size == 0) return 2; + + std::vector decoded(original_size); + std::size_t decoded_size = decoded.size(); + const BrotliDecoderResult decoded_result = BrotliDecoderDecompress( + encoded_size, + encoded.data(), + &decoded_size, + decoded.data()); + if (decoded_result != BROTLI_DECODER_RESULT_SUCCESS) return 3; + if (decoded_size != original_size) return 4; + + for (std::size_t i = 0; i < original_size; ++i) { + if (decoded[i] != original[i]) return 5; + } + return std::memcmp(decoded.data(), original, original_size) == 0 ? 0 : 6; +}