Skip to content

Commit 205dddd

Browse files
authored
feat: add brotli example to test suite (#259)
- include tests/examples/brotli in the members list of mcpp.toml - enables testing for brotli library integration
1 parent aa8df60 commit 205dddd

4 files changed

Lines changed: 108 additions & 0 deletions

File tree

mcpp.toml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,7 @@ members = [
1717
"tests/examples/boost-throw-exception",
1818
"tests/examples/boost-type-traits",
1919
"tests/examples/boost-uuid",
20+
"tests/examples/brotli",
2021
"tests/examples/build-mcpp",
2122
"tests/examples/c-ares",
2223
"tests/examples/catch2",

pkgs/c/compat.brotli.lua

Lines changed: 56 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,56 @@
1+
-- Form B C-source compat descriptor for Brotli. The three source globs match
2+
-- upstream v1.2.0's BROTLI_COMMON_SOURCES, BROTLI_DEC_SOURCES, and
3+
-- BROTLI_ENC_SOURCES CMake sets; the CLI, tests, fuzzers, and the optional C++
4+
-- lazy static-initialization implementation are outside those sets.
5+
package = {
6+
spec = "1",
7+
namespace = "compat",
8+
name = "brotli",
9+
description = "Brotli compression library",
10+
licenses = {"MIT"},
11+
repo = "https://github.com/google/brotli",
12+
type = "package",
13+
14+
xpm = {
15+
linux = {
16+
["1.2.0"] = {
17+
url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz",
18+
sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec",
19+
},
20+
},
21+
macosx = {
22+
["1.2.0"] = {
23+
url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz",
24+
sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec",
25+
},
26+
},
27+
windows = {
28+
["1.2.0"] = {
29+
url = "https://github.com/google/brotli/archive/refs/tags/v1.2.0.tar.gz",
30+
sha256 = "816c96e8e8f193b40151dad7e8ff37b1221d019dbcb9c35cd3fadbfe6477dfec",
31+
},
32+
},
33+
},
34+
35+
mcpp = {
36+
language = "c++23",
37+
import_std = false,
38+
c_standard = "c11",
39+
include_dirs = { "*/c/include" },
40+
sources = {
41+
"*/c/common/*.c",
42+
"*/c/dec/*.c",
43+
"*/c/enc/*.c",
44+
},
45+
targets = { ["brotli"] = { kind = "lib" } },
46+
deps = {},
47+
48+
-- Upstream detects libm and propagates it for static builds.
49+
linux = { ldflags = { "-lm" } },
50+
macosx = { ldflags = { "-lm" } },
51+
windows = {
52+
-- This is upstream's only MSVC-specific library definition.
53+
cflags = { "-D_CRT_SECURE_NO_WARNINGS" },
54+
},
55+
},
56+
}

tests/examples/brotli/mcpp.toml

Lines changed: 8 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,8 @@
1+
# Brotli test project: consumes compat.brotli through the workspace-root
2+
# `compat` index redirect and exercises encoder, decoder, and common code.
3+
[package]
4+
name = "brotli-tests"
5+
version = "0.1.0"
6+
7+
[dependencies.compat]
8+
brotli = "1.2.0"
Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
#include <brotli/decode.h>
2+
#include <brotli/encode.h>
3+
4+
#include <cstddef>
5+
#include <cstring>
6+
#include <vector>
7+
8+
int main() {
9+
static constexpr unsigned char original[] =
10+
"Brotli round-trip payload. Brotli round-trip payload. "
11+
"Brotli round-trip payload. Brotli round-trip payload.";
12+
constexpr std::size_t original_size = sizeof(original) - 1;
13+
14+
const std::size_t encoded_capacity = BrotliEncoderMaxCompressedSize(original_size);
15+
if (original_size == 0 || encoded_capacity == 0) return 1;
16+
17+
std::vector<unsigned char> encoded(encoded_capacity);
18+
std::size_t encoded_size = encoded.size();
19+
const BROTLI_BOOL encoded_ok = BrotliEncoderCompress(
20+
BROTLI_DEFAULT_QUALITY,
21+
BROTLI_DEFAULT_WINDOW,
22+
BROTLI_MODE_GENERIC,
23+
original_size,
24+
original,
25+
&encoded_size,
26+
encoded.data());
27+
if (encoded_ok != BROTLI_TRUE || encoded_size == 0) return 2;
28+
29+
std::vector<unsigned char> decoded(original_size);
30+
std::size_t decoded_size = decoded.size();
31+
const BrotliDecoderResult decoded_result = BrotliDecoderDecompress(
32+
encoded_size,
33+
encoded.data(),
34+
&decoded_size,
35+
decoded.data());
36+
if (decoded_result != BROTLI_DECODER_RESULT_SUCCESS) return 3;
37+
if (decoded_size != original_size) return 4;
38+
39+
for (std::size_t i = 0; i < original_size; ++i) {
40+
if (decoded[i] != original[i]) return 5;
41+
}
42+
return std::memcmp(decoded.data(), original, original_size) == 0 ? 0 : 6;
43+
}

0 commit comments

Comments
 (0)