-
-
Notifications
You must be signed in to change notification settings - Fork 2.3k
fix(utils): 修正 bytes2size 阈值并补充单元测试 #2056
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
base: main
Are you sure you want to change the base?
Conversation
WalkthroughThe Bytes2Size function's unit conversion boundaries are adjusted from strict greater-than comparisons to greater-than-or-equal comparisons, changing when byte values transition to larger units (GB, MB, KB). A new test file is added with table-driven tests verifying the function's behavior across multiple size ranges. Changes
Estimated code review effort🎯 2 (Simple) | ⏱️ ~12 minutes Poem
Pre-merge checks and finishing touches❌ Failed checks (1 warning)
✅ Passed checks (2 passed)
✨ Finishing touches
🧪 Generate unit tests (beta)
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. Comment |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Actionable comments posted: 0
🧹 Nitpick comments (1)
common/utils_test.go (1)
5-25: LGTM! Consider additional test cases for comprehensive coverage.The table-driven test structure is well-implemented and validates the boundary fix. The test cases confirm:
- Bytes display correctly (500 B)
- Exact boundaries display in larger units (1 KB, 1 MB, 1.00 GB)
- Fractional KB values truncate to integers (1536 → "1 KB")
For more comprehensive coverage, consider adding test cases for:
- Edge values: 0 bytes, 1 byte
- Fractional MB: e.g., 1572864 (1.5 MB) → "1 MB"
- Fractional GB: e.g., 1610612736 (1.5 GB) → "1.50 GB"
- Large intermediate values: e.g., 1023 KB, 1023 MB
- One byte below boundaries: 1023, 1048575, 1073741823
Apply this diff to add edge case tests:
{name: "gigabyte", input: 1024 * 1024 * 1024, want: "1.00 GB"}, + {name: "zero", input: 0, want: "0 B"}, + {name: "oneByte", input: 1, want: "1 B"}, + {name: "justBelowKB", input: 1023, want: "1023 B"}, + {name: "justBelowMB", input: 1048575, want: "1023 KB"}, + {name: "justBelowGB", input: 1073741823, want: "1023 MB"}, + {name: "fractionalMB", input: 1572864, want: "1 MB"}, // 1.5 MB truncated + {name: "fractionalGB", input: 1610612736, want: "1.50 GB"}, // 1.5 GB }
📜 Review details
Configuration used: CodeRabbit UI
Review profile: CHILL
Plan: Pro
📒 Files selected for processing (2)
common/utils.go(1 hunks)common/utils_test.go(1 hunks)
🧰 Additional context used
🧬 Code graph analysis (1)
common/utils_test.go (1)
common/utils.go (1)
Bytes2Size(147-163)
🔇 Additional comments (1)
common/utils.go (1)
150-156: LGTM! Boundary fix is correct.The change from
>to>=properly handles exact unit boundaries. Now:
- 1024 bytes displays as "1 KB" instead of "1024 B"
- 1048576 bytes displays as "1 MB" instead of "1024 KB"
- 1073741824 bytes displays as "1.00 GB" instead of "1024 MB"
This aligns with standard unit conversion behavior.
修复:更正 bytes2size 的阈值判断,修复边界数值显示错误问题(见 common/utils.go)
测试:新增 common/utils_test.go 覆盖典型与边界场景
Summary by CodeRabbit
Bug Fixes
Tests