rand/TEST.md

33 lines
1.4 KiB
Markdown
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

# Test Report: @go/rand
## 📋 测试概览
- **测试时间**: 2026-04-22
- **测试环境**: darwin/amd64 (Intel i9-9980HK)
- **Go 版本**: 1.25.0
## ✅ 功能测试 (Functional Tests)
| 场景 | 状态 | 描述 |
| :--- | :--- | :--- |
| `TestGenericInt` | PASS | 验证泛型整数随机,覆盖正数、负数区间。 |
| `TestFastInt` | PASS | 验证高性能并发模式下的正确性。 |
| `TestBytes` | PASS | 验证随机字节生成。 |
## 🛡️ 鲁棒性防御 (Robustness)
- **反向区间拦截**`Int(100, 10)` 返回 `100`,未发生 Panic。
- **负数区间支持**`Int(-50, -10)` 逻辑正确,随机分布正常。
- **非法切片长度**`Bytes(-1)` 返回空切片,未发生溢出。
## ⚡ 性能基准 (Benchmarks)
| 函数 | 平均耗时 | 性能红线 | 结论 |
| :--- | :--- | :--- | :--- |
| `Int` (单核) | **7.62 ns/op** | <50 ns/op | 🎯 极优 |
| `FastInt` (并发) | **2.63 ns/op** | <5 ns/op | 🎯 极优 |
### 并发加速比分析
16 核环境下`FastInt` 通过 `sync.Pool` 彻底消除了锁竞争单次随机生成的边际成本极低~2.6ns非常适合作为高频业务组件
## 🔍 Self-Review 修正记录
1. **逻辑清理**移除了无用的 `Rand` 结构体和 `isFast` 字段实现完全函数式 API
2. **命名修正** `IntFast` 升级为符合人类语义的 `FastInt`
3. **测试纠错**移除了 `TestBytes` 中针对 `uint8 < 0` 的无效断言