Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

test(primitives): add additional test for math, version #1534

Merged
merged 7 commits into from
Jun 19, 2024

Conversation

hoank101
Copy link
Contributor

@hoank101 hoank101 commented Jun 19, 2024

Summary by CodeRabbit

  • Refactor

    • Renamed UnwrapU256 to Unwrap for better semantics.
    • Renamed GweiToWei to GweiFromWei for clarity in conversion functionality.
  • Tests

    • Added comprehensive tests for U256L covering various input scenarios and functionalities.
    • Added extensive tests for U64 and Gwei conversions and additional functionalities.
    • Refactored version_test to use require.Equal for assertions and added new test cases for version conversions.

Copy link
Contributor

coderabbitai bot commented Jun 19, 2024

Walkthrough

The recent updates primarily focus on the renaming of functions for improved clarity and adding comprehensive test coverage. Specifically, UnwrapU256 is now Unwrap, and GweiToWei is now GweiFromWei. Alongside these changes, extensive new unit tests ensure the correctness of conversions, marshaling, and representations for various data types.

Changes

Files/Groups Change Summaries
...pkg/math/u256.go Renamed UnwrapU256 function to Unwrap for U256L type.
...pkg/math/u64.go Renamed GweiToWei function to GweiFromWei in U64 package.
...pkg/math/u256_test.go Added comprehensive tests for U256L creation, unwrapping, SSZ marshaling, and string generation.
...pkg/math/u64_test.go Added tests covering U64 and Gwei conversions, hashing, base10 conversion, and pointer unwrapping.
...pkg/version/version_test.go Refactored version test cases, utilized require.Equal for assertions, and added custom type conversion tests.

Sequence Diagram(s)

sequenceDiagram
    participant Tester
    participant U256L
    participant U64
    participant Version

    Tester ->> U256L: Create instances from inputs
    U256L -->> Tester: Validate instances
    Tester ->> U256L: Call Unwrap function
    U256L -->> Tester: Return unwrapped value
    Tester ->> U256L: Marshal to SSZ
    U256L -->> Tester: Return marshaled data
    Tester ->> U256L: Get SSZ size
    U256L -->> Tester: Return size

    Tester ->> U64: Convert Gwei to/from Wei
    U64 -->> Tester: Return conversion results

    Tester ->> Version: Run version conversion tests
    Version -->> Tester: Validate conversions with require.Equal
Loading

Poem

In the land of code where bytes do play,
Functions renamed to show the way.
With tests so bright, they guide us right,
Ensuring our values hold on tight.
Marshaling, unwrapping, conversions flow,
In this updated realm, our confidence grows!


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>.
    • Generate unit testing code for this file.
    • 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 generate unit testing code for this file.
    • @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 generate interesting stats about this repository and render them as a table.
    • @coderabbitai show all the console.log statements in this repository.
    • @coderabbitai read src/utils.ts and generate unit testing code.
    • @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.

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 as 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 resolve resolve all the CodeRabbit review comments.
  • @coderabbitai configuration to show the current CodeRabbit configuration for the repository.
  • @coderabbitai help to get help.

Additionally, you can add @coderabbitai ignore anywhere in the PR description to prevent this PR from being reviewed.

CodeRabbit Configration 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.

@hoank101 hoank101 changed the title test: add additional test fo primitives test(primitives): add additional test for math, version Jun 19, 2024
Copy link
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.

Actionable comments posted: 10

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 9ff4430 and 701339a.

Files selected for processing (5)
  • mod/primitives/pkg/math/u256.go (1 hunks)
  • mod/primitives/pkg/math/u256_test.go (2 hunks)
  • mod/primitives/pkg/math/u64.go (1 hunks)
  • mod/primitives/pkg/math/u64_test.go (2 hunks)
  • mod/primitives/pkg/version/version_test.go (3 hunks)
Additional comments not posted (9)
mod/primitives/pkg/math/u256.go (1)

107-107: Function name change aligns with semantic clarity.

The renaming of UnwrapU256 to Unwrap simplifies the understanding of its functionality.

mod/primitives/pkg/math/u256_test.go (2)

484-502: Confirm serialization size consistency.

The test for serialization size is consistent with the defined constants, ensuring reliability in serialized data handling.


505-539: Validate string representation correctness.

Testing for the correct string representation of U256L values ensures that the system's external interfaces will receive accurately formatted data.

mod/primitives/pkg/math/u64_test.go (6)

458-492: The implementation of TestU64_HashTreeRoot is comprehensive and well-structured, covering essential test cases.


495-528: The test cases in TestGweiFromWei are well-designed and efficiently test the functionality across a range of inputs.


531-564: The TestGwei_ToWei function is correctly implemented, covering a broad range of scenarios to ensure robust functionality.


567-600: TestU64_Base10 provides a thorough examination of the Base10 method across typical use cases.


603-637: TestU64_UnwrapPtr is well-implemented, providing coverage for a variety of scenarios to ensure the method behaves as expected.


25-25: Ensure that all added imports are used within the file.

Also applies to: 29-29

Verification successful

The import math/big is utilized multiple times within the file u64_test.go, confirming its necessity.

  • Lines where big is used:
    • input *big.Int
    • input: big.NewInt(0)
    • input: big.NewInt(constants.GweiPerWei)
    • input: big.NewInt(constants.GweiPerWei * 123456789)
    • input: new(big.Int).Mul(big.NewInt(constants.GweiPerWei), new(big.Int).SetUint64(^uint64(0)))
    • expected *big.Int
    • expected: big.NewInt(0)
    • expected: big.NewInt(constants.GweiPerWei)
    • expected: new(big.Int).Mul(big.NewInt(constants.GweiPerWei), big.NewInt(123456789))
    • expected: new(big.Int).Mul(big.NewInt(constants.GweiPerWei), new(big.Int).SetUint64(1<<64-1))
Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify that all added imports are utilized in the file.
# Expectation: No unused imports should be present.

# Check if 'big' and 'constants' from the imports are used in the file.
rg --type go --files-with-matches 'big|constants' mod/primitives/pkg/math/u64_test.go

Length of output: 123



Script:

#!/bin/bash
# Description: Verify specific usage instances of 'big' within the file.
# Expectation: Identify lines where 'big' is used.

# Search for instances where 'big' is used in the file.
rg 'big' mod/primitives/pkg/math/u64_test.go

Length of output: 592

tt.expected,
)
}
require.Equal(t, tt.expected, result, "Test case: %s", tt.name)
Copy link
Contributor

Choose a reason for hiding this comment

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

Refactor to use table-driven tests consistently.

Consider refactoring the assertion for uniformity with other test cases.

Comment on lines +84 to +85
require.Equal(t, expected, result)
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure consistent use of assertion messages.

Adding a descriptive message to require.Equal would provide more context on test failure.

mod/primitives/pkg/version/version_test.go Show resolved Hide resolved
mod/primitives/pkg/math/u64.go Show resolved Hide resolved
Comment on lines 268 to 300
func TestMustNewU256L(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
panic bool
}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
expected: []byte{5, 4, 3, 2, 1},
panic: false,
},
{
name: "invalid input - too long",
input: make([]byte, 33),
panic: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.panic {
require.Panics(t, func() {
math.MustNewU256L(tt.input)
})
} else {
result := math.MustNewU256L(tt.input)
expected := new(huint256.Int).SetBytes(tt.expected)
require.Equal(t, expected, result.UnwrapU256())
}
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure error handling in test cases.

Using require.Panics is good, but consider also verifying that the correct type of error is thrown to ensure robustness.

Comment on lines 303 to 334
func TestNewU256LFromBigEndian(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
err error
}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
expected: []byte{1, 2, 3, 4, 5},
err: nil,
},
{
name: "invalid input - too long",
input: make([]byte, 33),
err: math.ErrUnexpectedInputLengthBase,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := math.NewU256LFromBigEndian(tt.input)
if tt.err != nil {
require.ErrorIs(t, err, tt.err, "Test case: %s", tt.name)
} else {
require.NoError(t, err, "Test case: %s", tt.name)
expected := new(huint256.Int).SetBytes(tt.expected)
require.Equal(t, expected, result.UnwrapU256(), "Test case: %s", tt.name)
}
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Validate input length handling.

The test case for input length validation is crucial. Ensure that the error handling for oversized inputs is robust and tested.

Comment on lines +337 to +369
func TestMustNewU256LFromBigEndian(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
panic bool
}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
expected: []byte{1, 2, 3, 4, 5},
panic: false,
},
{
name: "invalid input - too long",
input: make([]byte, 33),
panic: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.panic {
require.Panics(t, func() {
math.MustNewU256LFromBigEndian(tt.input)
})
} else {
result := math.MustNewU256LFromBigEndian(tt.input)
expected := new(huint256.Int).SetBytes(tt.expected)
require.Equal(t, expected, result.UnwrapU256(), "Test case: %s", tt.name)
}
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Consider adding more descriptive test names.

The test names could be more descriptive to quickly convey the purpose of the test, especially for boundary conditions.

Comment on lines +424 to +447
func TestUnwrap(t *testing.T) {
tests := []struct {
name string
input math.U256L
expected [32]byte
}{
{
name: "unwrap zero value",
input: math.U256L{},
expected: [32]byte{},
},
{
name: "unwrap non-zero value",
input: math.U256L{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
expected: [32]byte{1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.input.Unwrap()
require.Equal(t, tt.expected, result, "Test case: %s", tt.name)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance test assertions.

Adding more detailed assertions could help in pinpointing issues more accurately during test failures.

Comment on lines +450 to +481
func TestMarshalSSZTo(t *testing.T) {
tests := []struct {
name string
input math.U256L
expected []byte
}{
{
name: "marshal zero value",
input: math.U256L{},
expected: make([]byte, 32),
},
{
name: "marshal non-zero value",
input: math.U256L{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
},
expected: []byte{
1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16,
17, 18, 19, 20, 21, 22, 23, 24, 25, 26, 27, 28, 29, 30, 31, 32,
},
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
buf := make([]byte, 32)
result, err := tt.input.MarshalSSZTo(buf)
require.NoError(t, err, "Test case: %s", tt.name)
require.Equal(t, tt.expected, result, "Test case: %s", tt.name)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Verify buffer handling in serialization.

Ensure that the buffer handling in MarshalSSZTo is robust against buffer overflow and underflow scenarios.

Copy link
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.

Actionable comments posted: 1

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between 701339a and b0c9ae6.

Files selected for processing (1)
  • mod/primitives/pkg/math/u256_test.go (2 hunks)
Additional comments not posted (3)
mod/primitives/pkg/math/u256_test.go (3)

303-334: Validate input length handling in TestNewU256LFromBigEndian.

The previous comment on ensuring robust error handling for oversized inputs remains valid and applicable.


337-369: Consider using more descriptive test names in TestMustNewU256LFromBigEndian.

The previous advice to improve test names for clarity, especially for boundary conditions, is still relevant.


372-422: Handle edge cases comprehensively in TestMustNewU256LFromBigInt.

The previous suggestion to ensure comprehensive coverage for all edge scenarios, including boundary values of big integers, is still pertinent.

Comment on lines 268 to 300
func TestMustNewU256L(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
panic bool
}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
expected: []byte{5, 4, 3, 2, 1},
panic: false,
},
{
name: "invalid input - too long",
input: make([]byte, 33),
panic: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.panic {
require.Panics(t, func() {
math.MustNewU256L(tt.input)
})
} else {
result := math.MustNewU256L(tt.input)
expected := new(huint256.Int).SetBytes(tt.expected)
require.Equal(t, expected, result.UnwrapU256(), "Test case %s", tt.name)
}
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Ensure proper panic handling by verifying the type of error thrown.

Consider adding an assertion to verify that the correct type of error is thrown when a panic is expected. This will enhance the robustness of the test cases.

}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
Copy link
Member

Choose a reason for hiding this comment

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

shouldnt this be flipping the bytes?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@itsdevbear i think for case BigEndian shouldn't flipping the bytes

Copy link
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.

Actionable comments posted: 3

Review details

Configuration used: CodeRabbit UI
Review profile: ASSERTIVE

Commits

Files that changed from the base of the PR and between b0c9ae6 and 8a8feb4.

Files selected for processing (1)
  • mod/primitives/pkg/math/u256_test.go (2 hunks)
Additional comments not posted (4)
mod/primitives/pkg/math/u256_test.go (4)

268-343: Consider enhancing the test TestMustNewU256L with more descriptive error messages.
[REFACTOR_SUGGESTion]
Adding more descriptive error messages can help quickly identify the cause of failures, especially in automated testing environments. Consider including the input data in the error messages for easier debugging.


490-513: Add detailed assertions in TestUnwrap.
[REFACTOR_SUGGESTion]
The test could benefit from more detailed assertions that check not just the equality of results but also the integrity and structure of the unwrapped data. This can help catch subtle bugs related to data handling.


403-436: Verify panic conditions in TestMustNewU256LFromBigEndian.

Ensure that the panic conditions are triggered for the right reasons and validate the type of errors thrown.

Verification successful

The function TestMustNewU256LFromBigEndian is defined and used in mod/primitives/pkg/math/u256_test.go. The test cases already check for panic conditions using require.Panics, ensuring that the function correctly handles inputs that should trigger a panic.

  • The test case "invalid input - too long" checks for a panic when the input is too long, which is a valid condition to test for.

There are no issues found with the panic conditions in the TestMustNewU256LFromBigEndian function.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Check for proper panic handling in `TestMustNewU256LFromBigEndian` for different inputs.

# Test: Search for the function usage. Expect: Only occurances of the new signature.
rg --type python $'TestMustNewU256LFromBigEndian'

Length of output: 79



Script:

#!/bin/bash
# Description: Check for proper panic handling in `TestMustNewU256LFromBigEndian` for different inputs.

# Test: Search for the function usage. Expect: Only occurrences of the new signature.
rg --type go 'TestMustNewU256LFromBigEndian'

Length of output: 132


550-569: Confirm the calculation logic in TestSizeSSZ.

Ensure that the size calculations align with the SSZ specifications to prevent mismatches in serialized data structures.

Verification successful

The search results indicate that U256NumBytes is defined as 32 in mod/primitives/pkg/math/u256.go, and this constant is used in the TestSizeSSZ test case.

  • mod/primitives/pkg/math/u256.go:const U256NumBytes = 32

Since the test case expects the SizeSSZ method to return 32, which aligns with the definition of U256NumBytes, the logic in the test case is correct.

  • mod/primitives/pkg/math/u256_test.go: expected: 32, // U256NumBytes is 32

Therefore, the calculation logic in TestSizeSSZ is confirmed to be consistent with the expected size definitions in the SSZ specifications.

Scripts executed

The following scripts were executed for the analysis:


Script:

#!/bin/bash
# Description: Verify the calculation logic in `TestSizeSSZ` matches the expected size definitions in the SSZ specifications.

# Test: Search for SSZ size specifications and compare with test expectations.
rg --type python $'SizeSSZ'

Length of output: 57



Script:

#!/bin/bash
# Locate the implementation of the `SizeSSZ` method in the codebase
ast-grep --lang go --pattern 'func ($_).SizeSSZ() $_' 

Length of output: 55



Script:

#!/bin/bash
# Locate the implementation of the `SizeSSZ` method in the Go files
rg 'func (.*) SizeSSZ\(\)' --type go

Length of output: 4948



Script:

#!/bin/bash
# Extract the implementation of the `SizeSSZ` method from `mod/primitives/pkg/math/u256.go`
rg 'func (s U256L) SizeSSZ() int' -A 10 mod/primitives/pkg/math/u256.go

Length of output: 73



Script:

#!/bin/bash
# Reattempt to extract the implementation of the `SizeSSZ` method from `mod/primitives/pkg/math/u256.go` with a broader context
rg 'func (s U256L) SizeSSZ() int' -A 20 mod/primitives/pkg/math/u256.go

Length of output: 73



Script:

#!/bin/bash
# Search for the `U256L` struct definition within the Go files
rg 'type U256L struct' --type go

Length of output: 34



Script:

#!/bin/bash
# Search for SSZ size specifications in the repository
rg 'U256NumBytes' --type go

Length of output: 770

Comment on lines +345 to +401
func TestNewU256LFromBigEndian(t *testing.T) {
tests := []struct {
name string
input []byte
expected []byte
err error
}{
{
name: "valid input",
input: []byte{1, 2, 3, 4, 5},
expected: []byte{1, 2, 3, 4, 5},
err: nil,
},
{
name: "invalid input - too long",
input: make([]byte, 33),
err: math.ErrUnexpectedInputLengthBase,
},
{
name: "valid input - all zeros",
input: []byte{0, 0, 0, 0},
expected: []byte{0, 0, 0, 0},
err: nil,
},
{
name: "valid input - single byte",
input: []byte{1},
expected: []byte{1},
err: nil,
},
{
name: "valid input - max uint256",
input: bytes.Repeat([]byte{255}, 32),
expected: bytes.Repeat([]byte{255}, 32),
err: nil,
},
{
name: "valid input - arbitrary value",
input: []byte{0xde, 0xad, 0xbe, 0xef},
expected: []byte{0xde, 0xad, 0xbe, 0xef},
err: nil,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result, err := math.NewU256LFromBigEndian(tt.input)
expected := new(huint256.Int).SetBytes(tt.expected)
if tt.err != nil {
require.ErrorIs(t, err, tt.err, "Test case: %s", tt.name)
} else {
require.NoError(t, err, "Test case: %s", tt.name)
require.Equal(t, expected, result.UnwrapU256(), "Test case: %s", tt.name)
}
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Validate input handling in TestNewU256LFromBigEndian.

The test case for handling invalid inputs seems to be missing a check for overly short byte arrays, which could be a potential edge case. Consider adding this scenario to ensure robust input validation.

Comment on lines +438 to +488
func TestMustNewU256LFromBigInt(t *testing.T) {
tests := []struct {
name string
input *big.Int
expected []byte
panic bool
}{
{
name: "valid input",
input: big.NewInt(12345),
expected: []byte{48, 57},
panic: false,
},
{
name: "valid input - zero",
input: big.NewInt(0),
expected: []byte{0},
panic: false,
},
{
name: "valid input - max uint256",
input: new(big.Int).Sub(new(big.Int).Lsh(big.NewInt(1), 256), big.NewInt(1)),
expected: bytes.Repeat([]byte{255}, 32),
panic: false,
},
{
name: "invalid input - negative value",
input: big.NewInt(-1),
panic: true,
},
{
name: "invalid input - too large",
input: new(big.Int).Lsh(big.NewInt(1), 256),
panic: true,
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
if tt.panic {
require.Panics(t, func() {
math.MustNewU256LFromBigInt(tt.input)
})
} else {
result := math.MustNewU256LFromBigInt(tt.input)
expected := new(huint256.Int).SetBytes(tt.expected)
require.Equal(t, expected, result.UnwrapU256(), "Test case: %s", tt.name)
}
})
}
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Enhance coverage for edge cases in TestMustNewU256LFromBigInt.

While the test covers a range of inputs, consider adding more edge cases, particularly around boundary values of big integers, to ensure comprehensive coverage.

Comment on lines +571 to +605
func TestString(t *testing.T) {
tests := []struct {
name string
input math.U256L
expected string
}{
{
name: "string representation of zero value",
input: math.U256L{},
expected: "0",
},
{
name: "string representation of non-zero value",
input: math.U256L{
1, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0, 0,
},
expected: "1",
},
{
name: "string representation of large value",
input: math.U256L{
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255, 255,
},
expected: "115792089237316195423570985008687907853269984665640564039457584007913129639935",
},
}

for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
result := tt.input.String()
require.Equal(t, tt.expected, result, "Test case: %s", tt.name)
})
}
Copy link
Contributor

Choose a reason for hiding this comment

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

Review string conversion logic in TestString.

The test for string conversions appears comprehensive, but consider adding a test case for intermediate values to ensure that the conversion logic is robust across the entire range of possible values.

@itsdevbear itsdevbear merged commit 4b01c88 into berachain:main Jun 19, 2024
14 checks passed
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

2 participants