Skip to content

Add large 1mb buffer test for crc32 hashing.#1913

Merged
Dead2 merged 1 commit intozlib-ng:developfrom
nmoinvaz:ci/crc32-1mb-buf-test
May 27, 2025
Merged

Add large 1mb buffer test for crc32 hashing.#1913
Dead2 merged 1 commit intozlib-ng:developfrom
nmoinvaz:ci/crc32-1mb-buf-test

Conversation

@nmoinvaz
Copy link
Copy Markdown
Member

@nmoinvaz nmoinvaz commented May 12, 2025

Summary by CodeRabbit

  • Tests
    • Added new tests to verify CRC32 implementations on large 512KB buffers for improved coverage.
    • Extended existing test macros to include large buffer test cases for each CRC32 variant.

@coderabbitai
Copy link
Copy Markdown
Contributor

coderabbitai bot commented May 12, 2025

"""

Walkthrough

A new test fixture class, crc32_large_buf, is introduced to enable CRC32 hashing tests on a large 512KB buffer. The macro TEST_CRC32 is updated to generate an additional test case for each CRC32 variant, utilizing this new fixture and buffer. Existing test cases remain unchanged.

Changes

File(s) Change Summary
test/test_crc32.cc Added crc32_large_buf fixture class for large 512KB buffer CRC32 tests; updated TEST_CRC32 macro to include new test using this fixture.

Sequence Diagram(s)

sequenceDiagram
    participant TestRunner
    participant crc32_large_buf
    participant CRC32Function

    TestRunner->>crc32_large_buf: SetUpTestSuite()
    crc32_large_buf->>crc32_large_buf: Allocate 512KB buffer, fill with 0x55
    TestRunner->>crc32_large_buf: Run hash(CRC32Function)
    crc32_large_buf->>CRC32Function: Compute CRC32 on buffer
    CRC32Function-->>crc32_large_buf: Return CRC32 hash
    crc32_large_buf->>TestRunner: Assert hash == expected
    TestRunner->>crc32_large_buf: TearDownTestSuite()
    crc32_large_buf->>crc32_large_buf: Free buffer
Loading

"""

Note

⚡️ AI Code Reviews for VS Code, Cursor, Windsurf

CodeRabbit now has a plugin for VS Code, Cursor and Windsurf. This brings AI code reviews directly in the code editor. Each commit is reviewed immediately, finding bugs before the PR is raised. Seamless context handoff to your AI code agent ensures that you can easily incorporate review feedback.
Learn more here.


Note

⚡️ Faster reviews with caching

CodeRabbit now supports caching for code and dependencies, helping speed up reviews. This means quicker feedback, reduced wait times, and a smoother review experience overall. Cached data is encrypted and stored securely. This feature will be automatically enabled for all accounts on May 16th. To opt out, configure Review - Disable Cache at either the organization or repository level. If you prefer to disable all data retention across your organization, simply turn off the Data Retention setting under your Organization Settings.
Enjoy the performance boost—your workflow just got faster.


📜 Recent review details

Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
Cache: Disabled due to data retention organization setting
Knowledge Base: Disabled due to data retention organization setting

📥 Commits

Reviewing files that changed from the base of the PR and between 77a79fe and f542646.

📒 Files selected for processing (1)
  • test/test_crc32.cc (2 hunks)
🚧 Files skipped from review as they are similar to previous changes (1)
  • test/test_crc32.cc
✨ Finishing Touches
  • 📝 Generate Docstrings

Thanks for using CodeRabbit! It's free for OSS, and your support helps us grow. If you like it, consider giving us a shout-out.

❤️ Share
🪧 Tips

Chat

There are 3 ways to chat with CodeRabbit:

  • Review comments: Directly reply to a review comment made by CodeRabbit. Example:
    • I pushed a fix in commit <commit_id>, please review it.
    • Explain this complex logic.
    • Open a follow-up GitHub issue for this discussion.
  • Files and specific lines of code (under the "Files changed" tab): Tag @coderabbitai in a new review comment at the desired location with your query. Examples:
    • @coderabbitai explain this code block.
    • @coderabbitai modularize this function.
  • PR comments: Tag @coderabbitai in a new PR comment to ask questions about the PR branch. For the best results, please provide a very specific query, as very limited context is provided in this mode. Examples:
    • @coderabbitai gather interesting stats about this repository and render them as a table. Additionally, render a pie chart showing the language distribution in the codebase.
    • @coderabbitai read src/utils.ts and explain its main purpose.
    • @coderabbitai read the files in the src/scheduler package and generate a class diagram using mermaid and a README in the markdown format.
    • @coderabbitai help me debug CodeRabbit configuration file.

Support

Need help? Create a ticket on our support page for assistance with any issues or questions.

Note: Be mindful of the bot's finite context window. It's strongly recommended to break down tasks such as reading entire modules into smaller chunks. For a focused discussion, use review comments to chat about specific files and their changes, instead of using the PR comments.

CodeRabbit Commands (Invoked using PR comments)

  • @coderabbitai pause to pause the reviews on a PR.
  • @coderabbitai resume to resume the paused reviews.
  • @coderabbitai review to trigger an incremental review. This is useful when automatic reviews are disabled for the repository.
  • @coderabbitai full review to do a full review from scratch and review all the files again.
  • @coderabbitai summary to regenerate the summary of the PR.
  • @coderabbitai generate docstrings to generate docstrings for this PR.
  • @coderabbitai generate sequence diagram to generate a sequence diagram of the changes in this PR.
  • @coderabbitai resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Other keywords and placeholders

  • Add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.
  • Add @coderabbitai summary to generate the high-level summary at a specific location in the PR description.
  • Add @coderabbitai anywhere in the PR title to generate the title automatically.

CodeRabbit Configuration File (.coderabbit.yaml)

  • You can programmatically configure CodeRabbit by adding a .coderabbit.yaml file to the root of your repository.
  • Please see the configuration documentation for more information.
  • If your editor has YAML language server enabled, you can add the path at the top of this file to enable auto-completion and validation: # yaml-language-server: $schema=https://coderabbit.ai/integrations/schema.v2.json

Documentation and Community

  • Visit our Documentation for detailed information on how to use CodeRabbit.
  • Join our Discord Community to get help, request features, and share feedback.
  • Follow us on X/Twitter for updates and announcements.

@nmoinvaz nmoinvaz force-pushed the ci/crc32-1mb-buf-test branch from 4362d1a to 9b3452b Compare May 12, 2025 17:22
@codecov
Copy link
Copy Markdown

codecov bot commented May 12, 2025

Codecov Report

All modified and coverable lines are covered by tests ✅

Project coverage is 39.40%. Comparing base (e4a31e0) to head (f542646).
Report is 13 commits behind head on develop.

Current head f542646 differs from pull request most recent head 77a79fe

Please upload reports for the commit 77a79fe to get more accurate results.

❗ There is a different number of reports uploaded between BASE (e4a31e0) and HEAD (f542646). Click for more details.

HEAD has 1 upload less than BASE
Flag BASE (e4a31e0) HEAD (f542646)
2 1
Additional details and impacted files
@@             Coverage Diff              @@
##           develop    #1913       +/-   ##
============================================
- Coverage    83.42%   39.40%   -44.03%     
============================================
  Files          144       74       -70     
  Lines        12948     7883     -5065     
  Branches      2857     1303     -1554     
============================================
- Hits         10802     3106     -7696     
- Misses        1202     4538     +3336     
+ Partials       944      239      -705     

☔ View full report in Codecov by Sentry.
📢 Have feedback on the report? Share it here.

🚀 New features to boost your workflow:
  • ❄️ Test Analytics: Detect flaky tests, report on failures, and find test suite problems.

Copy link
Copy Markdown
Contributor

@coderabbitai coderabbitai bot left a comment

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Caution

Inline review comments failed to post. This is likely due to GitHub's limits when posting large numbers of comments. If you are seeing this consistently it is likely a permissions issue. Please check "Moderation" -> "Code review limits" under your organization settings.

Actionable comments posted: 1

🧹 Nitpick comments (1)
test/test_crc32.cc (1)

237-244: Consider adding allocation failure handling to the test setup.

While the memory allocation and filling are correctly implemented, there's no check for allocation failure in SetUpTestSuite().

Add error handling to prevent segmentation faults if allocation fails:

static void SetUpTestSuite() {
    buffer = (uint8_t*)zng_alloc(buffer_size);
+   ASSERT_NE(buffer, nullptr) << "Failed to allocate large buffer for CRC32 tests";
    memset(buffer, 0x55, buffer_size);
}
🛑 Comments failed to post (1)
test/test_crc32.cc (1)

230-251: ⚠️ Potential issue

Good implementation of the test fixture class for large buffer testing.

The crc32_large_buf test fixture is well structured with static setup and teardown methods that properly allocate and free memory only once for all test cases. The buffer size of 1MB is appropriate for testing CRC32 performance with large inputs.

One minor issue: the static constant expected_hash needs an external definition to prevent linker errors.

The pipeline failure indicates an undefined reference to crc32_large_buf::expected_hash. Static const members with in-class initializers still need definitions outside the class. Add this definition:

uint8_t *crc32_large_buf::buffer = nullptr;
+const uint32_t crc32_large_buf::expected_hash;

Or alternatively, make it a constexpr:

-    static const uint32_t expected_hash = 0x0026D5FB;
+    static constexpr uint32_t expected_hash = 0x0026D5FB;
📝 Committable suggestion

‼️ IMPORTANT
Carefully review the code before committing. Ensure that it accurately replaces the highlighted code, contains no missing lines, and has no issues with indentation. Thoroughly test & benchmark the code to ensure it meets the requirements.

/* Test large 1MB buffer with known CRC32 */
class crc32_large_buf : public ::testing::Test {
protected:
    static uint8_t *buffer;
    static const size_t buffer_size = 1024 * 1024;
-    static const uint32_t expected_hash = 0x0026D5FB;
+    static constexpr uint32_t expected_hash = 0x0026D5FB;

    static void SetUpTestSuite() {
        buffer = (uint8_t*)zng_alloc(buffer_size);
        memset(buffer, 0x55, buffer_size);
    }

    static void TearDownTestSuite() {
        zng_free(buffer);
    }

public:
    void hash(crc32_func crc32) {
        const uint32_t actual_hash = crc32(0, buffer, buffer_size);
        EXPECT_EQ(actual_hash, expected_hash);
    }
};
🤖 Prompt for AI Agents (early access)
In test/test_crc32.cc around lines 230 to 251, the static const member
expected_hash is initialized inside the class but lacks an external definition,
causing linker errors. Fix this by adding an out-of-class definition for
expected_hash after the class definition or change its declaration to constexpr
to allow inline initialization without needing an external definition.

@nmoinvaz nmoinvaz force-pushed the ci/crc32-1mb-buf-test branch 2 times, most recently from 4c664b3 to 0803b07 Compare May 12, 2025 17:31
Comment on lines +253 to +251
const uint32_t crc32_large_buf::expected_hash;
uint8_t *crc32_large_buf::buffer = nullptr;
Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I don't see why these shouldn't be declared and initialized inside the class like buffer_size is. Also, crc32_large_buf::expected_hash is forward declaration that happens after the real declaration, which makes it redundant. The way buffer is initialized here can result in memory leak if SetupTestSuite is run more than once.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not really a C++ programmer so..

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The way buffer is initialized here can result in memory leak if SetupTestSuite is run more than once.

That would be a bug in Google test framework if it happened.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Regarding forward declaring, the linker just complained and I did what it said so idk. 🤷‍♂️

Copy link
Copy Markdown
Member Author

@nmoinvaz nmoinvaz May 12, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe I need constexpr but I didn't want to be the first to introduce it.

Copy link
Copy Markdown
Collaborator

@mtl1979 mtl1979 May 13, 2025

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I’m not really a C++ programmer so..

Neither am I... I started with Z80 machine code and moved on to Pascal... I'm just saying that if I can't understand the logic, then most likely new contributors can't either... Without knowing the exact error messages, it will be hard to know if there is better solution... It's my personal opinion, but I really don't think we should introduce code that gets reverted or replaced right after it is committed... My professional opinion is that since this fails Emscripten WASM32 CI in a way that is unexpected, it should be rewritten...

According to clang 19, the construct is valid for C++17, but we set C++11 as minimum...

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

diff --git a/test/test_crc32.cc b/test/test_crc32.cc
index 00abd5f6..dc8c497e 100644
--- a/test/test_crc32.cc
+++ b/test/test_crc32.cc
@@ -232,7 +232,7 @@ class crc32_large_buf : public ::testing::Test {
 protected:
     static uint8_t *buffer;
     static const size_t buffer_size = 1024 * 1024;
-    static const uint32_t expected_hash = 0x0026D5FB;
+    static const uint32_t expected_hash;

     static void SetUpTestSuite() {
         buffer = (uint8_t*)zng_alloc(buffer_size);
@@ -250,7 +250,8 @@ public:
     }
 };

-const uint32_t crc32_large_buf::expected_hash;
+// inline static declaration requires C++17
+const uint32_t crc32_large_buf::expected_hash = 0x0026D5FB;
 uint8_t *crc32_large_buf::buffer = nullptr;

 INSTANTIATE_TEST_SUITE_P(crc32, crc32_variant, testing::ValuesIn(tests));

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Still unsure why buffer_size doesn't trigger warning or error...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Grok told me it may be that EXPECT_EQ macro is referencing the expected_hash variable in such a way that it takes the address of it. So that is possibly why it wants it for expected_hash but not buffer_size. I don't care enough to find out for sure, but it seems plausable.

Copy link
Copy Markdown
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not really familiar with static const class members as I haven't really used C++17 features much... I did briefly read the relevant section of the standard, but that's quite confusing...

In my own code, I usually just use global non-member variables when static class members need to access same variables as non-static class members and there is no instance of the class that could be passed to the static member function.

@nmoinvaz nmoinvaz force-pushed the ci/crc32-1mb-buf-test branch from 0803b07 to ee6d71a Compare May 15, 2025 02:48
@nmoinvaz nmoinvaz force-pushed the ci/crc32-1mb-buf-test branch 2 times, most recently from f542646 to 77a79fe Compare May 15, 2025 16:23
@nmoinvaz
Copy link
Copy Markdown
Member Author

@Dead2 this is ready to merge.

@Dead2 Dead2 merged commit 4b7037c into zlib-ng:develop May 27, 2025
227 of 287 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

3 participants