Skip to content

Commit

Permalink
Merge pull request google#1550 from antmicro:64294-fix-frame-header-t…
Browse files Browse the repository at this point in the history
…ests

PiperOrigin-RevId: 666198879
  • Loading branch information
copybara-github committed Aug 22, 2024
2 parents 789433a + 27aea1c commit f083652
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 11 deletions.
3 changes: 0 additions & 3 deletions xls/modules/zstd/BUILD
Original file line number Diff line number Diff line change
Expand Up @@ -154,7 +154,6 @@ cc_library(
],
deps = [
"@com_google_absl//absl/algorithm:container",
"@com_google_absl//absl/status",
"@com_google_absl//absl/status:statusor",
"@com_google_absl//absl/strings",
"@com_google_absl//absl/time",
Expand Down Expand Up @@ -210,7 +209,6 @@ cc_test(
":frame_header_test_dslx",
],
shard_count = 50,
tags = ["manual"],
deps = [
":data_generator",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down Expand Up @@ -999,7 +997,6 @@ cc_test(
":zstd_dec_test.ir",
],
shard_count = 50,
tags = ["manual"],
deps = [
":data_generator",
"@com_google_absl//absl/container:flat_hash_map",
Expand Down
33 changes: 25 additions & 8 deletions xls/modules/zstd/frame_header_test.cc
Original file line number Diff line number Diff line change
Expand Up @@ -57,10 +57,22 @@ enum FrameHeaderStatus : uint8_t {
UNSUPPORTED_WINDOW_SIZE
};

struct ZstdFrameHeader {
absl::Span<const uint8_t> kBuffer;
ZSTD_frameHeader kHeader;
size_t kResult;
class ZstdFrameHeader {
public:
absl::Span<const uint8_t> buffer() const {
return absl::MakeConstSpan(buffer_);
}

ZSTD_frameHeader header() const { return header_; }

size_t result() const { return result_; }

ZstdFrameHeader(absl::Span<const uint8_t> buffer, ZSTD_frameHeader h,
size_t r)
: header_(std::move(h)), result_(r) {
std::vector<uint8_t> v(buffer.begin(), buffer.end());
buffer_ = v;
}
// Parse a frame header from an arbitrary buffer with the ZSTD library.
static absl::StatusOr<ZstdFrameHeader> Parse(
absl::Span<const uint8_t> buffer) {
Expand All @@ -69,8 +81,13 @@ struct ZstdFrameHeader {
ZSTD_frameHeader zstd_fh;
size_t result = ZSTD_getFrameHeader_advanced(
&zstd_fh, buffer.data(), buffer.size(), ZSTD_f_zstd1_magicless);
return ZstdFrameHeader(buffer, zstd_fh, result);
return ZstdFrameHeader(buffer, std::move(zstd_fh), result);
}

private:
std::vector<uint8_t> buffer_;
ZSTD_frameHeader header_;
size_t result_;
};

class FrameHeaderTest : public xls::IrTestBase {
Expand Down Expand Up @@ -113,7 +130,7 @@ class FrameHeaderTest : public xls::IrTestBase {
// expected values.
void RunAndExpectFrameHeader(const ZstdFrameHeader& zstd_frame_header) {
// Extend buffer contents to 128 bits if necessary.
const absl::Span<const uint8_t>& buffer = zstd_frame_header.kBuffer;
const absl::Span<const uint8_t> buffer = zstd_frame_header.buffer();
std::vector<uint8_t> buffer_extended(kDslxBufferSizeBytes, 0);
absl::Span<const uint8_t> input_buffer;
if (buffer.size() < kDslxBufferSizeBytes) {
Expand All @@ -124,8 +141,8 @@ class FrameHeaderTest : public xls::IrTestBase {
}

// Decide on the expected status
ZSTD_frameHeader zstd_fh = zstd_frame_header.kHeader;
size_t result = zstd_frame_header.kResult;
ZSTD_frameHeader zstd_fh = zstd_frame_header.header();
size_t result = zstd_frame_header.result();
FrameHeaderStatus expected_status = FrameHeaderStatus::OK;
if (result != 0) {
if (ZSTD_isError(result)) {
Expand Down

0 comments on commit f083652

Please sign in to comment.